How to use PSyclone - 10 common examples

To help you get started, we’ve selected a few PSyclone examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stfc / PSyclone / src / psyclone / gocean1p0.py View on Github external
else:
                stop = schedule.jloop_stop

            # This strange line splitting was the only way I could find
            # to avoid pep8 warnings: using [..._space]\ keeps on
            # complaining about a white space
            bounds = GOLoop._bounds_lookup[index_offset][self.field_space][
                self._iteration_space][self._loop_type]
            stop = bounds["stop"].format(start='2', stop=stop)
            # Remove all white spaces
            stop = "".join(stop.split())
            # This common case is a bit of compile-time computation
            # but it helps to fix all of the test cases.
            if stop == "2-1":
                stop = "1"
            return Literal(stop, INTEGER_TYPE, self)

        if self.field_space == "go_every":
            # Bounds are independent of the grid-offset convention in use

            # We look-up the upper bounds by enquiring about the SIZE of
            # the array itself
            stop = BinaryOperation(BinaryOperation.Operator.SIZE,
                                   self)
            # TODO 363 - needs to be updated once the PSyIR has support for
            # Fortran derived types.
            api_config = Config.get().api_conf("gocean1.0")
            # Use the data property to access the member of the field that
            # contains the actual grid points. The property value is a
            # string with a placeholder ({0}) where the name of the field
            # must go.
            data = api_config.grid_properties["go_grid_data"].fortran \
github stfc / PSyclone / src / psyclone / gocean1p0.py View on Github external
# to avoid pep8 warnings: using [..._space]\ keeps on
            # complaining about a white space
            bounds = GOLoop._bounds_lookup[index_offset][self.field_space][
                self._iteration_space][self._loop_type]
            start = bounds["start"].format(start='2', stop=stop)
            # Remove all white spaces
            start = "".join(start.split())
            # This common case is a bit of compile-time computation
            # but it helps with fixing all of the test cases.
            if start == "2-1":
                start = "1"
            return Literal(start, INTEGER_TYPE, self)

        if self.field_space == "go_every":
            # Bounds are independent of the grid-offset convention in use
            return Literal("1", INTEGER_TYPE, self)

        # Loop bounds are pulled from the field object which is more
        # straightforward for us but provides the Fortran compiler
        # with less information.
        if self._iteration_space.lower() == "go_internal_pts":
            key = "internal"
        elif self._iteration_space.lower() == "go_all_pts":
            key = "whole"
        else:
            raise GenerationError("Unrecognised iteration space, '{0}'. "
                                  "Cannot generate loop bounds.".
                                  format(self._iteration_space))
        api_config = Config.get().api_conf("gocean1.0")
        props = api_config.grid_properties
        # key is 'internal' or 'whole', and _loop_type is either
        # 'inner' or 'outer'. The four possible combinations are
github stfc / PSyclone / src / psyclone / psyir / backend / sir.py View on Github external
:returns: the SIR Python code.
        :rtype: str

        :raises VisitorError: if there is no mapping from the PSyIR \
        operator to SIR.

        '''
        binary_operators = {
            BinaryOperation.Operator.ADD: '+',
            BinaryOperation.Operator.SUB: '-',
            BinaryOperation.Operator.MUL: '*',
            BinaryOperation.Operator.DIV: '/',
            BinaryOperation.Operator.POW: '**',
            BinaryOperation.Operator.EQ: '==',
            BinaryOperation.Operator.NE: '!=',
            BinaryOperation.Operator.LE: '<=',
            BinaryOperation.Operator.LT: '<',
            BinaryOperation.Operator.GE: '>=',
            BinaryOperation.Operator.GT: '>',
            BinaryOperation.Operator.AND: '&&',
            BinaryOperation.Operator.OR: '||'}

        self._depth += 1
        lhs = self._visit(node.children[0])
        try:
            oper = binary_operators[node.operator]
        except KeyError:
            raise VisitorError(
                "Method binaryoperation_node in class SIRWriter, unsupported "
                "operator '{0}' found.".format(str(node.operator)))
        rhs = self._visit(node.children[1])
github stfc / PSyclone / src / psyclone / psyir / backend / sir.py View on Github external
:param node: a BinaryOperation PSyIR node.
        :type node: :py:class:`psyclone.psyir.nodes.BinaryOperation`

        :returns: the SIR Python code.
        :rtype: str

        :raises VisitorError: if there is no mapping from the PSyIR \
        operator to SIR.

        '''
        binary_operators = {
            BinaryOperation.Operator.ADD: '+',
            BinaryOperation.Operator.SUB: '-',
            BinaryOperation.Operator.MUL: '*',
            BinaryOperation.Operator.DIV: '/',
            BinaryOperation.Operator.POW: '**',
            BinaryOperation.Operator.EQ: '==',
            BinaryOperation.Operator.NE: '!=',
            BinaryOperation.Operator.LE: '<=',
            BinaryOperation.Operator.LT: '<',
            BinaryOperation.Operator.GE: '>=',
            BinaryOperation.Operator.GT: '>',
            BinaryOperation.Operator.AND: '&&',
            BinaryOperation.Operator.OR: '||'}

        self._depth += 1
        lhs = self._visit(node.children[0])
        try:
            oper = binary_operators[node.operator]
        except KeyError:
            raise VisitorError(
github stfc / PSyclone / src / psyclone / gocean1p0.py View on Github external
def _check_init(self):
        '''Internal method which checks that the stencil information has been
        loaded.

        :raises GenerationError: if the GOStencil object has not been
        initialised i.e. the load() method has not been called

        '''
        if not self._initialised:
            raise GenerationError(
                "Error in class GOStencil: the object has not yet been "
                "initialised. Please ensure the load() method is called.")
github stfc / PSyclone / src / psyclone / gocean1p0.py View on Github external
self._iteration_space][self._loop_type]
            stop = bounds["stop"].format(start='2', stop=stop)
            # Remove all white spaces
            stop = "".join(stop.split())
            # This common case is a bit of compile-time computation
            # but it helps to fix all of the test cases.
            if stop == "2-1":
                stop = "1"
            return Literal(stop, INTEGER_TYPE, self)

        if self.field_space == "go_every":
            # Bounds are independent of the grid-offset convention in use

            # We look-up the upper bounds by enquiring about the SIZE of
            # the array itself
            stop = BinaryOperation(BinaryOperation.Operator.SIZE,
                                   self)
            # TODO 363 - needs to be updated once the PSyIR has support for
            # Fortran derived types.
            api_config = Config.get().api_conf("gocean1.0")
            # Use the data property to access the member of the field that
            # contains the actual grid points. The property value is a
            # string with a placeholder ({0}) where the name of the field
            # must go.
            data = api_config.grid_properties["go_grid_data"].fortran \
                .format(self.field_name)
            stop.addchild(Literal(data, INTEGER_TYPE, parent=stop))
            if self._loop_type == "inner":
                stop.addchild(Literal("1", INTEGER_TYPE, parent=stop))
            elif self._loop_type == "outer":
                stop.addchild(Literal("2", INTEGER_TYPE, parent=stop))
            return stop
github stfc / PSyclone / src / psyclone / psyir / backend / fortran.py View on Github external
my_range = node.parent
            array = my_range.parent
            array_index = array.children.index(my_range) + 1
            # pylint: disable=too-many-boolean-expressions
            if isinstance(node, BinaryOperation) and \
               node.operator == operator and \
               isinstance(node.children[0], Reference) and \
               node.children[0].name == array.name and \
               isinstance(node.children[1], Literal) and \
               node.children[1].datatype.intrinsic == \
               ScalarType.Intrinsic.INTEGER and \
               node.children[1].value == str(array_index):
                return True
            return False

        if _full_extent(node.start, BinaryOperation.Operator.LBOUND):
            # The range starts for the first element in this
            # dimension. This is the default in Fortran so no need to
            # output anything.
            start = ""
        else:
            start = self._visit(node.start)

        if _full_extent(node.stop, BinaryOperation.Operator.UBOUND):
            # The range ends with the last element in this
            # dimension. This is the default in Fortran so no need to
            # output anything.
            stop = ""
        else:
            stop = self._visit(node.stop)
        result = "{0}:{1}".format(start, stop)
github stfc / PSyclone / src / psyclone / psyir / backend / fortran.py View on Github external
:returns: the Fortran code as a string.
        :rtype: str

        :raises VisitorError: if an unexpected Unary op is encountered.

        '''
        content = self._visit(node.children[0])
        try:
            fort_oper = get_fortran_operator(node.operator)
            if is_fortran_intrinsic(fort_oper):
                # This is a unary intrinsic function.
                return "{0}({1})".format(fort_oper, content)
            return "{0}{1}".format(fort_oper, content)
        except KeyError:
            raise VisitorError("Unexpected unary op '{0}'.".format(
                node.operator))
github stfc / PSyclone / src / psyclone / psyir / backend / fortran.py View on Github external
:type node: :py:class:`psyclone.psyir.nodes.NaryOperation`

        :returns: the Fortran code as a string.
        :rtype: str

        :raises VisitorError: if an unexpected N-ary operator is found.

        '''
        arg_list = []
        for child in node.children:
            arg_list.append(self._visit(child))
        try:
            fort_oper = get_fortran_operator(node.operator)
            return "{0}({1})".format(fort_oper, ", ".join(arg_list))
        except KeyError:
            raise VisitorError("Unexpected N-ary op '{0}'".
                               format(node.operator))
github stfc / PSyclone / src / psyclone / psyir / frontend / fparser2.py View on Github external
:raises InternalError: if the arguments are of the wrong type.
    :raises InternalError: if there's no symbol table associated with \
                           `psyir_literal_parent` or one of its ancestors.

    '''
    if not isinstance(fparser2_node,
                      (Fortran2003.Real_Literal_Constant,
                       Fortran2003.Logical_Literal_Constant,
                       Fortran2003.Char_Literal_Constant,
                       Fortran2003.Int_Literal_Constant)):
        raise InternalError(
            "Unsupported literal type '{0}' found in get_literal_precision."
            "".format(type(fparser2_node).__name__))
    if not isinstance(psyir_literal_parent, Node):
        raise InternalError(
            "Expecting argument psyir_literal_parent to be a PSyIR Node but "
            "found '{0}' in get_literal_precision."
            "".format(type(psyir_literal_parent).__name__))
    precision_name = fparser2_node.items[1]
    if not precision_name:
        # Precision may still be specified by the exponent in a real literal
        if isinstance(fparser2_node, Fortran2003.Real_Literal_Constant):
            precision_value = fparser2_node.items[0]
            if "d" in precision_value.lower():
                return ScalarType.Precision.DOUBLE
            if "e" in precision_value.lower():
                return ScalarType.Precision.SINGLE
        # Return the default precision
        try:
            data_name = CONSTANT_TYPE_MAP[type(fparser2_node)]
        except KeyError: