def test_create_structuretype(): ''' Test the create() method of StructureType. ''' # One member will have its type defined by a DataTypeSymbol tsymbol = DataTypeSymbol("my_type", DeferredType()) stype = StructureType.create([ ("fred", INTEGER_TYPE, Symbol.Visibility.PUBLIC), ("george", REAL_TYPE, Symbol.Visibility.PRIVATE), ("barry", tsymbol, Symbol.Visibility.PUBLIC)]) assert len(stype.components) == 3 george = stype.lookup("george") assert isinstance(george, StructureType.ComponentType) assert george.name == "george" assert george.datatype == REAL_TYPE assert george.visibility == Symbol.Visibility.PRIVATE barry = stype.lookup("barry") assert isinstance(barry, StructureType.ComponentType) assert barry.datatype is tsymbol assert barry.visibility == Symbol.Visibility.PUBLIC with pytest.raises(TypeError) as err: StructureType.create([ ("fred", INTEGER_TYPE, Symbol.Visibility.PUBLIC), ("george", Symbol.Visibility.PRIVATE)]) assert ("Each component must be specified using a 3-tuple of (name, " "type, visibility) but found a tuple with 2 members: (" "'george', " in str(err.value))
def test_kernelfunctor_str(): '''Check the str method of the KernelFunctor class.''' symbol = DataTypeSymbol("hello", StructureType()) arg = Reference(Symbol("dummy")) klr = KernelFunctor.create(symbol, [arg]) assert klr.__str__() == "KernelFunctor[name='hello']"
def test_kernelfunctor_node_str(): '''Check the node_str method of the KernelFunctor class.''' symbol = DataTypeSymbol("hello", StructureType()) arg = Reference(Symbol("dummy")) klr = KernelFunctor.create(symbol, [arg]) coloredtext = colored("KernelFunctor", KernelFunctor._colour) assert klr.node_str() == coloredtext + "[name='hello']"
def test_lfrickernelfunctor(): '''test that an instance of LFRicKernelFunctor class can be created. ''' routine = DataTypeSymbol("hello", StructureType()) lbc = LFRicKernelFunctor(routine) assert isinstance(lbc, LFRicKernelFunctor) assert lbc._text_name == "LFRicKernelFunctor"
def test_is_not_array_range(): ''' Test that is_array_range correctly rejects things that aren't an assignment to an array range. ''' int_one = Literal("1", INTEGER_SINGLE_TYPE) one = Literal("1.0", REAL_TYPE) var = DataSymbol("x", REAL_TYPE) reference = Reference(var) # lhs is not an array assignment = Assignment.create(reference, one) assert assignment.is_array_range is False # lhs is an array reference but has no range array_type = ArrayType(REAL_TYPE, [10, 10]) symbol = DataSymbol("y", array_type) array_ref = Reference(symbol) assignment = Assignment.create(array_ref, one.copy()) assert assignment.is_array_range is False # lhs is an array reference but the single index value is obtained # using an array range, y(1, SUM(map(:), 1)) = 1.0 int_array_type = ArrayType(INTEGER_SINGLE_TYPE, [10]) map_sym = DataSymbol("map", int_array_type) start = BinaryOperation.create(BinaryOperation.Operator.LBOUND, Reference(map_sym), int_one.copy()) stop = BinaryOperation.create(BinaryOperation.Operator.UBOUND, Reference(map_sym), int_one.copy()) my_range = Range.create(start, stop) sum_op = BinaryOperation.create(BinaryOperation.Operator.SUM, ArrayReference.create(map_sym, [my_range]), int_one.copy()) assignment = Assignment.create( ArrayReference.create(symbol, [int_one.copy(), sum_op]), one.copy()) assert assignment.is_array_range is False # When the slice has two operator ancestors, one of which is a reduction # e.g y(1, SUM(ABS(map(:)), 1)) = 1.0 abs_op = UnaryOperation.create( UnaryOperation.Operator.ABS, ArrayReference.create(map_sym, [my_range.copy()])) sum_op2 = BinaryOperation.create(BinaryOperation.Operator.SUM, abs_op, int_one.copy()) assignment = Assignment.create( ArrayReference.create(symbol, [int_one.copy(), sum_op2]), one.copy()) assert assignment.is_array_range is False # lhs is a scalar member of a structure grid_type = StructureType.create([ ("dx", REAL_SINGLE_TYPE, Symbol.Visibility.PUBLIC), ("dy", REAL_SINGLE_TYPE, Symbol.Visibility.PUBLIC) ]) grid_type_symbol = DataTypeSymbol("grid_type", grid_type) grid_sym = DataSymbol("grid", grid_type_symbol) assignment = Assignment.create(StructureReference.create(grid_sym, ["dx"]), one.copy()) assert assignment.is_array_range is False
def test_kernelfunctor_parent(): '''Check that the optional parent argument to a KernelFunctor class constructor is stored correctly. ''' parent = Node() symbol = DataTypeSymbol("hello", StructureType()) klr = KernelFunctor(symbol, parent=parent) assert klr.parent == parent
def test_kernelfunctor_create_invalid_args1(): '''Check that the create method of KernelFunctor raises the expected exception if the provided 'arguments' argument is not a list. ''' symbol = DataTypeSymbol("hello", StructureType()) with pytest.raises(GenerationError) as info: _ = KernelFunctor.create(symbol, "Not a list") assert ("KernelFunctor create() arguments argument should be a list " "but found 'str'." in str(info.value))
def test_arraytype_datatypesymbol_only(): ''' Test that we currently refuse to make an ArrayType with an intrinsic type of StructureType. (This limitation is the subject of #1031.) ''' with pytest.raises(NotImplementedError) as err: _ = ArrayType(StructureType.create( [("nx", INTEGER_TYPE, Symbol.Visibility.PUBLIC)]), [5]) assert ("When creating an array of structures, the type of those " "structures must be supplied as a DataTypeSymbol but got a " "StructureType instead." in str(err.value))
def test_kernelfunctor(): '''Check that an instance of KernelFunctor class can be created. Also check that the symbol method works as expected. ''' symbol = DataTypeSymbol("hello", StructureType()) klr = KernelFunctor(symbol) assert klr._symbol == symbol assert klr.symbol == symbol assert klr._colour == "yellow" assert klr._text_name == "KernelFunctor" assert klr.parent is None
def test_kernelfunctor_invalid_args2(): '''Check that the create method of KernelFunctor raises the expected exception if its supplied list of children are not the expected type (tests _validate_child method and _children_valid_format variable) ''' symbol = DataTypeSymbol("hello", StructureType()) with pytest.raises(GenerationError) as info: _ = KernelFunctor.create(symbol, ["hello"]) assert ("Item 'str' can't be child 0 of 'KernelFunctor'. The valid " "format is: '[DataNode]*'." in str(info.value))
def _specialise_symbol(symbol): '''If the symbol argument is a Symbol then change it into a TypeSymbol. :param symbol: a symbol that will be modified to a TypeSymbol \ if it is a Symbol. :type symbol: :py:class:`psyclone.psyir.symbols.Symbol` ''' # pylint: disable=unidiomatic-typecheck if type(symbol) is Symbol: symbol.specialise(TypeSymbol) symbol.datatype = StructureType()
def test_lfricalgorithminvokecall_create(cls): '''Check that the LFRicAlgorithmInvokeCall create method creates the expected object. ''' routine = RoutineSymbol("hello") klc = LFRicKernelFunctor.create(DataTypeSymbol("arg", StructureType()), []) call = cls.create(routine, [klc], 0, description="describing an invoke") assert call._description == "describing an invoke" assert call.routine is routine # pylint: disable=unidiomatic-typecheck assert type(call) is cls assert len(call.children) == 1 assert call.children[0] == klc
def test_kernelfunctor_create(cls): '''Check that the create method of KernelFunctor works as expected. ''' symbol = DataTypeSymbol("hello", StructureType()) klr = cls.create(symbol, []) # pylint: disable=unidiomatic-typecheck assert type(klr) is cls assert klr._symbol == symbol assert len(klr.children) == 0 arg = Reference(Symbol("dummy")) klr = KernelFunctor.create(symbol, [arg]) assert len(klr.children) == 1 assert klr.children[0] == arg assert arg.parent == klr
def _get_symbol(call, fp2_node): '''Return the name of a Structure Constructor stored as a CodeBlock containing an fparser2 ast. :param code_block: the CodeBlock containing a StructureConstructor. :type code_block: :py:class:`psyclone.psyir.nodes.CodeBlock` :param fp2_node: the fparser2 Structure Constructor node. :type fp2_node: \ :py:class:`fparser.two.Fortran2003.Structure_Constructor` :returns: the symbol capturing the name and type of the \ StructureConstructor. :rtype: :py:class:`psyclone.psyir.symbols.Symbol` ''' name = fp2_node.children[0].string symbol_table = call.scope.symbol_table try: type_symbol = symbol_table.lookup(name) except KeyError: type_symbol = TypeSymbol(name, StructureType()) symbol_table.add(type_symbol) return type_symbol
DeferredType, Symbol from psyclone.psyir.backend.fortran import FortranWriter # Symbol table for container (container itself created after kernel) CONTAINER_SYMBOL_TABLE = SymbolTable() REAL_KIND = CONTAINER_SYMBOL_TABLE.new_symbol( root_name="RKIND", symbol_type=DataSymbol, datatype=INTEGER_TYPE, constant_value=8) # Shorthand for a scalar type with REAL_KIND precision SCALAR_TYPE = ScalarType(ScalarType.Intrinsic.REAL, REAL_KIND) # Derived-type definition in container GRID_TYPE = StructureType.create([ ("dx", SCALAR_TYPE, Symbol.Visibility.PUBLIC), ("dy", SCALAR_TYPE, Symbol.Visibility.PUBLIC)]) GRID_TYPE_SYMBOL = TypeSymbol("grid_type", GRID_TYPE) CONTAINER_SYMBOL_TABLE.add(GRID_TYPE_SYMBOL) # Kernel symbol table, symbols and scalar datatypes SYMBOL_TABLE = SymbolTable() CONT = ContainerSymbol("kernel_mod") SYMBOL_TABLE.add(CONT) DTYPE_SYMBOL = TypeSymbol("other_type", DeferredType(), interface=GlobalInterface(CONT)) SYMBOL_TABLE.add(DTYPE_SYMBOL) # Create the definition of the 'field_type'
def test_is_array_range(): '''test that the is_array_range method behaves as expected, returning true if the LHS of the assignment is an array range access. ''' one = Literal("1.0", REAL_TYPE) int_one = Literal("1", INTEGER_TYPE) int_ten = Literal("10", INTEGER_TYPE) # lhs is an array reference with a range array_type = ArrayType(REAL_TYPE, [10, 10]) symbol = DataSymbol("x", array_type) x_range = Range.create(int_one, int_ten.copy(), int_one.copy()) array_ref = ArrayReference.create(symbol, [x_range, int_one.copy()]) assignment = Assignment.create(array_ref, one.copy()) assert assignment.is_array_range is True # Check when lhs consists of various forms of structure access grid_type = StructureType.create([ ("dx", REAL_SINGLE_TYPE, Symbol.Visibility.PUBLIC), ("dy", REAL_SINGLE_TYPE, Symbol.Visibility.PUBLIC) ]) grid_type_symbol = DataTypeSymbol("grid_type", grid_type) # Create the definition of the 'field_type', contains array of grid_types field_type_def = StructureType.create([ ("data", ArrayType(REAL_SINGLE_TYPE, [10]), Symbol.Visibility.PUBLIC), ("sub_meshes", ArrayType(grid_type_symbol, [3]), Symbol.Visibility.PUBLIC) ]) field_type_symbol = DataTypeSymbol("field_type", field_type_def) field_symbol = DataSymbol("wind", field_type_symbol) # Array reference to component of derived type using a range lbound = BinaryOperation.create( BinaryOperation.Operator.LBOUND, StructureReference.create(field_symbol, ["data"]), int_one.copy()) ubound = BinaryOperation.create( BinaryOperation.Operator.UBOUND, StructureReference.create(field_symbol, ["data"]), int_one.copy()) my_range = Range.create(lbound, ubound) data_ref = StructureReference.create(field_symbol, [("data", [my_range])]) assign = Assignment.create(data_ref, one.copy()) assert assign.is_array_range is True # Access to slice of 'sub_meshes': wind%sub_meshes(1:3)%dx = 1.0 sub_range = Range.create(int_one.copy(), Literal("3", INTEGER_TYPE)) dx_ref = StructureReference.create(field_symbol, [("sub_meshes", [sub_range]), "dx"]) sub_assign = Assignment.create(dx_ref, one.copy()) assert sub_assign.is_array_range is True # Create an array of these derived types and assign to a slice: # chi(1:10)%data(1) = 1.0 field_bundle_symbol = DataSymbol("chi", ArrayType(field_type_symbol, [3])) fld_range = Range.create(int_one.copy(), Literal("10", INTEGER_TYPE)) fld_ref = ArrayOfStructuresReference.create(field_bundle_symbol, [fld_range], [("data", [int_one.copy()])]) fld_assign = Assignment.create(fld_ref, one.copy()) assert fld_assign.is_array_range is True # When the slice has two operator ancestors, none of which are a reduction # e.g y(1, INT(ABS(map(:, 1)))) = 1.0 int_array_type = ArrayType(INTEGER_SINGLE_TYPE, [10, 10]) map_sym = DataSymbol("map", int_array_type) lbound1 = BinaryOperation.create(BinaryOperation.Operator.LBOUND, Reference(map_sym), int_one.copy()) ubound1 = BinaryOperation.create(BinaryOperation.Operator.UBOUND, Reference(map_sym), int_one.copy()) my_range1 = Range.create(lbound1, ubound1) abs_op = UnaryOperation.create( UnaryOperation.Operator.ABS, ArrayReference.create(map_sym, [my_range1, int_one.copy()])) int_op = UnaryOperation.create(UnaryOperation.Operator.INT, abs_op) assignment = Assignment.create( ArrayReference.create(symbol, [int_one.copy(), int_op]), one.copy()) assert assignment.is_array_range is True
def test_structure_type(): ''' Check the StructureType constructor and that we can add components. ''' stype = StructureType() assert str(stype) == "StructureType<>" assert not stype.components stype.add("flag", INTEGER_TYPE, Symbol.Visibility.PUBLIC) flag = stype.lookup("flag") assert isinstance(flag, StructureType.ComponentType) with pytest.raises(TypeError) as err: stype.add(1, "hello", "hello") assert ("name of a component of a StructureType must be a 'str' but got " "'int'" in str(err.value)) with pytest.raises(TypeError) as err: stype.add("hello", "hello", "hello") assert ("type of a component of a StructureType must be a 'DataType' " "or 'DataTypeSymbol' but got 'str'" in str(err.value)) with pytest.raises(TypeError) as err: stype.add("hello", INTEGER_TYPE, "hello") assert ("visibility of a component of a StructureType must be an instance " "of 'Symbol.Visibility' but got 'str'" in str(err.value)) with pytest.raises(KeyError): stype.lookup("missing") # Cannot have a recursive type definition with pytest.raises(TypeError) as err: stype.add("hello", stype, Symbol.Visibility.PUBLIC) assert ("attempting to add component 'hello' - a StructureType definition " "cannot be recursive" in str(err.value))