def test_transform_apply_insert(tmpdir): '''Check that the PSyIR is transformed as expected when there are multiple statements in the PSyIR. The resultant Fortran code is used to confirm the transformation has worked correctly. ''' trans = ArrayRange2LoopTrans() symbol_table = SymbolTable() symbol = DataSymbol("n", INTEGER_TYPE) symbol_table.add(symbol) # Create the first assignment. In Fortran notation: x(:) = y(n,:) lhs = create_array_x(symbol_table) rhs = create_array_y(symbol_table) assignment1 = Assignment.create(lhs, rhs) # Create the second assignment. In Fortran notation: y2(:,:) = z(:,n,:) lhs = create_array_y_2d_slice(symbol_table) rhs = create_array_z(symbol_table) assignment2 = Assignment.create(lhs, rhs) routine = KernelSchedule.create("work", symbol_table, [assignment1, assignment2]) trans.apply(assignment1) trans.apply(assignment2) writer = FortranWriter() expected = (" do idx = LBOUND(x, 1), UBOUND(x, 1), 1\n" " x(idx)=y(n,idx)\n" " enddo\n" " do idx_1 = LBOUND(y2, 2), UBOUND(y2, 2), 1\n" " y2(:,idx_1)=z(:,n,idx_1)\n" " enddo\n") result = writer(routine) assert expected in result assert Compile(tmpdir).string_compiles(result)
def example_psyir(create_expression): '''Utility function that creates a PSyIR tree containing an ABS intrinsic operator and returns the operator. :param function create_expresssion: function used to create the \ content of the ABS operator. :returns: PSyIR ABS operator instance. :rtype: :py:class:`psyclone.psyGen.UnaryOperation` ''' symbol_table = SymbolTable() name1 = symbol_table.new_symbol_name("arg") arg1 = DataSymbol(name1, REAL_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READWRITE)) symbol_table.add(arg1) name2 = symbol_table.new_symbol_name() local = DataSymbol(name2, REAL_TYPE) symbol_table.add(local) symbol_table.specify_argument_list([arg1]) var1 = Reference(arg1) var2 = Reference(local) oper = UnaryOperation.Operator.ABS operation = UnaryOperation.create(oper, create_expression(var1)) assign = Assignment.create(var2, operation) _ = KernelSchedule.create("abs_example", symbol_table, [assign]) return operation
def test_routine_create_invalid(): '''Test that the create method in the Routine class raises the expected exceptions if the provided input is invalid. ''' symbol_table = SymbolTable() symbol = DataSymbol("x", REAL_TYPE) symbol_table.add(symbol) children = [Assignment.create(Reference(symbol), Literal("1", REAL_TYPE))] # name is not a string. with pytest.raises(TypeError) as excinfo: _ = Routine.create(1, symbol_table, children) assert ("name argument in create method of Routine class " "should be a string but found 'int'.") in str(excinfo.value) # symbol_table not a SymbolTable. with pytest.raises(TypeError) as excinfo: _ = Routine.create("mod_name", "invalid", children) assert ("symbol_table argument in create method of Routine class " "should be a SymbolTable but found 'str'.") in str(excinfo.value) # children not a list. with pytest.raises(TypeError) as excinfo: _ = Routine.create("mod_name", symbol_table, "invalid") assert ("children argument in create method of Routine class " "should be a list but found 'str'." in str(excinfo.value)) # contents of children list are not Node. with pytest.raises(TypeError) as excinfo: _ = Routine.create("mod_name", symbol_table, ["invalid"]) assert ( "child of children argument in create method of Routine class " "should be a PSyIR Node but found 'str'." in str(excinfo.value))
def test_transform_multi_apply(tmpdir): '''Check that the ArrayRange2Loop transformation can be used to create nested loops by calling it multiple times when an array has multiple dimensions that use a range. ''' trans = ArrayRange2LoopTrans() symbol_table = SymbolTable() symbol = DataSymbol("n", INTEGER_TYPE) symbol_table.add(symbol) lhs = create_array_y_2d_slice(symbol_table) rhs = create_array_z(symbol_table) assignment = Assignment.create(lhs, rhs) routine = KernelSchedule.create("work", symbol_table, [assignment]) trans.apply(assignment) trans.apply(assignment) expected = (" do idx = LBOUND(y2, 2), UBOUND(y2, 2), 1\n" " do idx_1 = LBOUND(y2, 1), UBOUND(y2, 1), 1\n" " y2(idx_1,idx)=z(idx_1,n,idx)\n" " enddo\n" " enddo\n") writer = FortranWriter() result = writer(routine) assert expected in result assert Compile(tmpdir).string_compiles(result)
def test_container_create_invalid(): '''Test that the create method in a Container class raises the expected exception if the provided input is invalid. ''' symbol_table = SymbolTable() symbol_table.add(DataSymbol("x", REAL_SINGLE_TYPE)) children = [KernelSchedule.create("mod_1", SymbolTable(), [])] # name is not a string. with pytest.raises(GenerationError) as excinfo: _ = Container.create(1, symbol_table, children) assert ("name argument in create method of Container class " "should be a string but found 'int'.") in str(excinfo.value) # symbol_table not a SymbolTable. with pytest.raises(GenerationError) as excinfo: _ = Container.create("container", "invalid", children) assert ("symbol_table argument in create method of Container class " "should be a SymbolTable but found 'str'.") in str(excinfo.value) # children not a list. with pytest.raises(GenerationError) as excinfo: _ = Container.create("mod_name", symbol_table, "invalid") assert ("children argument in create method of Container class should " "be a list but found 'str'." in str(excinfo.value)) # contents of children list are not Container or KernelSchedule. with pytest.raises(GenerationError) as excinfo: _ = Container.create("mod_name", symbol_table, ["invalid"]) assert ("Item 'str' can't be child 0 of 'Container'. The valid format is:" " '[Container | KernelSchedule | InvokeSchedule]*'." in str(excinfo.value))
def test_scope(): '''Test that the scope method in a Node instance returns the closest ancestor Schedule or Container Node (including itself) or raises an exception if one does not exist. ''' kernel_symbol_table = SymbolTable() symbol = DataSymbol("tmp", REAL_TYPE) kernel_symbol_table.add(symbol) ref = Reference(symbol) assign = Assignment.create(ref, Literal("0.0", REAL_TYPE)) kernel_schedule = KernelSchedule.create("my_kernel", kernel_symbol_table, [assign]) container = Container.create("my_container", SymbolTable(), [kernel_schedule]) assert ref.scope is kernel_schedule assert assign.scope is kernel_schedule assert kernel_schedule.scope is kernel_schedule assert container.scope is container anode = Literal("x", INTEGER_TYPE) with pytest.raises(SymbolError) as excinfo: _ = anode.scope assert ("Unable to find the scope of node " "'Literal[value:'x', Scalar<INTEGER, UNDEFINED>]' as " "none of its ancestors are Container or Schedule nodes." in str(excinfo.value))
def test_find_or_create_imported_symbol_2(): ''' Check that the _find_or_create_imported_symbol() method creates new symbols when appropriate. ''' # Create some suitable PSyIR from scratch symbol_table = SymbolTable() symbol_table.add(DataSymbol("tmp", REAL_TYPE)) kernel1 = KernelSchedule.create("mod_1", SymbolTable(), []) container = Container.create("container_name", symbol_table, [kernel1]) xvar = DataSymbol("x", REAL_TYPE) xref = Reference(xvar) assign = Assignment.create(xref, Literal("1.0", REAL_TYPE)) kernel1.addchild(assign) # We have no wildcard imports so there can be no symbol named 'undefined' with pytest.raises(SymbolError) as err: _ = _find_or_create_imported_symbol(assign, "undefined") assert "No Symbol found for name 'undefined'" in str(err.value) # We should be able to find the 'tmp' symbol in the parent Container sym = _find_or_create_imported_symbol(assign, "tmp") assert sym.datatype.intrinsic == ScalarType.Intrinsic.REAL # Add a wildcard import to the SymbolTable of the KernelSchedule new_container = ContainerSymbol("some_mod") new_container.wildcard_import = True kernel1.symbol_table.add(new_container) # Symbol not in any container but we do have wildcard imports so we # get a new symbol back new_symbol = _find_or_create_imported_symbol(assign, "undefined") assert new_symbol.name == "undefined" assert isinstance(new_symbol.interface, UnresolvedInterface) # pylint: disable=unidiomatic-typecheck assert type(new_symbol) == Symbol assert "undefined" not in container.symbol_table assert kernel1.symbol_table.lookup("undefined") is new_symbol
def test_routine_create(): '''Test that the create method correctly creates a Routine instance. ''' symbol_table = SymbolTable() symbol = DataSymbol("tmp", REAL_TYPE) symbol_table.add(symbol) assignment = Assignment.create(Reference(symbol), Literal("0.0", REAL_TYPE)) kschedule = Routine.create("mod_name", symbol_table, [assignment], is_program=True, return_type=INTEGER_TYPE) assert isinstance(kschedule, Routine) check_links(kschedule, [assignment]) assert kschedule.symbol_table is symbol_table assert kschedule.is_program assert kschedule.return_type == INTEGER_TYPE
def test_transform_apply(lhs_create, rhs_create, expected, tmpdir): '''Check that the PSyIR is transformed as expected for various types of ranges in an array. The resultant Fortran code is used to confirm the transformation has worked correctly. ''' trans = ArrayRange2LoopTrans() symbol_table = SymbolTable() symbol = DataSymbol("n", INTEGER_TYPE) symbol_table.add(symbol) lhs = lhs_create(symbol_table) rhs = rhs_create(symbol_table) assignment = Assignment.create(lhs, rhs) routine = KernelSchedule.create("work", symbol_table, [assignment]) trans.apply(assignment) writer = FortranWriter() result = writer(routine) assert expected in result assert Compile(tmpdir).string_compiles(result)
def test_file_container_create(): '''Test that the create method in the Container class correctly creates a FileContainer instance. ''' symbol_table = SymbolTable() symbol_table.add(DataSymbol("tmp", REAL_SINGLE_TYPE)) module = Container.create("mod_1", symbol_table, []) program = Routine.create("prog_1", SymbolTable(), [], is_program=True) file_container = FileContainer.create("container_name", SymbolTable(), [module, program]) assert isinstance(file_container, FileContainer) result = FortranWriter().filecontainer_node(file_container) assert result == ("module mod_1\n" " implicit none\n" " real :: tmp\n\n" " contains\n\n" "end module mod_1\n" "program prog_1\n\n\n" "end program prog_1\n")
def test_oclw_gen_array_length_variables(): '''Check the OpenCLWriter class gen_array_length_variables method produces the expected declarations. ''' oclwriter = OpenCLWriter() # A scalar should not return any LEN variables symbol1 = DataSymbol("dummy2LEN1", INTEGER_TYPE) result = oclwriter.gen_array_length_variables(symbol1) assert result == "" # Array with 1 dimension generates 1 length variable array_type = ArrayType(INTEGER_TYPE, [2]) symbol2 = DataSymbol("dummy1", array_type) result = oclwriter.gen_array_length_variables(symbol2) assert result == "int dummy1LEN1 = get_global_size(0);\n" # Array with multiple dimension generates one variable per dimension array_type = ArrayType(INTEGER_TYPE, [2, ArrayType.Extent.ATTRIBUTE, 2]) symbol3 = DataSymbol("dummy2", array_type) result = oclwriter.gen_array_length_variables(symbol3) assert result == "int dummy2LEN1 = get_global_size(0);\n" \ "int dummy2LEN2 = get_global_size(1);\n" \ "int dummy2LEN3 = get_global_size(2);\n" # Create a symbol table symtab = SymbolTable() symtab.add(symbol1) symtab.add(symbol2) symtab.add(symbol3) # If there are no name clashes, generate array length variables. result = oclwriter.gen_array_length_variables(symbol2, symtab) assert result == "int dummy1LEN1 = get_global_size(0);\n" with pytest.raises(VisitorError) as excinfo: _ = oclwriter.gen_array_length_variables(symbol3, symtab) assert "Unable to declare the variable 'dummy2LEN1' to store the length " \ "of 'dummy2' because the Symbol Table already contains a symbol with" \ " the same name." in str(excinfo.value)
def test_kernelschedule_create(): '''Test that the create method in the KernelSchedule class correctly creates a KernelSchedule instance. ''' symbol_table = SymbolTable() symbol = DataSymbol("tmp", REAL_TYPE) symbol_table.add(symbol) assignment = Assignment.create(Reference(symbol), Literal("0.0", REAL_TYPE)) kschedule = KernelSchedule.create("mod_name", symbol_table, [assignment]) assert isinstance(kschedule, KernelSchedule) # A KernelSchedule is not a main program and has no return type. assert not kschedule.is_program assert kschedule.return_type is None check_links(kschedule, [assignment]) assert kschedule.symbol_table is symbol_table result = FortranWriter().routine_node(kschedule) assert result == ("subroutine mod_name()\n" " real :: tmp\n\n" " tmp = 0.0\n\n" "end subroutine mod_name\n")
def test_container_create(): '''Test that the create method in the Container class correctly creates a Container instance. ''' symbol_table = SymbolTable() symbol_table.add(DataSymbol("tmp", REAL_SINGLE_TYPE)) kernel1 = KernelSchedule.create("mod_1", SymbolTable(), []) kernel2 = KernelSchedule.create("mod_2", SymbolTable(), []) container = Container.create("container_name", symbol_table, [kernel1, kernel2]) check_links(container, [kernel1, kernel2]) assert container.symbol_table is symbol_table result = FortranWriter().container_node(container) assert result == ("module container_name\n" " real :: tmp\n\n" " contains\n" " subroutine mod_1()\n\n\n" " end subroutine mod_1\n" " subroutine mod_2()\n\n\n" " end subroutine mod_2\n\n" "end module container_name\n")
def test_find_symbol_table(): ''' Test the find_symbol_table() method. ''' sym = Symbol("a_var") with pytest.raises(TypeError) as err: sym.find_symbol_table("3") assert ("expected to be passed an instance of psyir.nodes.Node but got " "'str'" in str(err.value)) # Search for a SymbolTable with only one level of hierarchy sched = KernelSchedule("dummy") table = sched.symbol_table table.add(sym) assert sym.find_symbol_table(sched) is table # Create a Container so that we have two levels of hierarchy ctable = SymbolTable() sym2 = Symbol("b_var") ctable.add(sym2) _ = Container.create("test", ctable, [sched]) assert sym2.find_symbol_table(sched) is ctable # A Symbol that isn't in any table sym3 = Symbol("missing") assert sym3.find_symbol_table(sched) is None # When there is no SymbolTable associated with the PSyIR node orphan = Literal("1", INTEGER_SINGLE_TYPE) assert sym3.find_symbol_table(orphan) is None
def create_matmul(): '''Utility function that creates a valid matmul node for use with subsequent tests. ''' symbol_table = SymbolTable() one = Literal("1", INTEGER_TYPE) two = Literal("2", INTEGER_TYPE) index = DataSymbol("idx", INTEGER_TYPE, constant_value=3) symbol_table.add(index) array_type = ArrayType(REAL_TYPE, [5, 10, 15]) mat_symbol = DataSymbol("x", array_type) symbol_table.add(mat_symbol) lbound1 = BinaryOperation.create(BinaryOperation.Operator.LBOUND, Reference(mat_symbol), one.copy()) ubound1 = BinaryOperation.create(BinaryOperation.Operator.UBOUND, Reference(mat_symbol), one.copy()) my_mat_range1 = Range.create(lbound1, ubound1, one.copy()) lbound2 = BinaryOperation.create(BinaryOperation.Operator.LBOUND, Reference(mat_symbol), two.copy()) ubound2 = BinaryOperation.create(BinaryOperation.Operator.UBOUND, Reference(mat_symbol), two.copy()) my_mat_range2 = Range.create(lbound2, ubound2, one.copy()) matrix = ArrayReference.create( mat_symbol, [my_mat_range1, my_mat_range2, Reference(index)]) array_type = ArrayType(REAL_TYPE, [10, 20]) vec_symbol = DataSymbol("y", array_type) symbol_table.add(vec_symbol) lbound = BinaryOperation.create(BinaryOperation.Operator.LBOUND, Reference(vec_symbol), one.copy()) ubound = BinaryOperation.create(BinaryOperation.Operator.UBOUND, Reference(vec_symbol), one.copy()) my_vec_range = Range.create(lbound, ubound, one.copy()) vector = ArrayReference.create( vec_symbol, [my_vec_range, Reference(index)]) matmul = BinaryOperation.create(BinaryOperation.Operator.MATMUL, matrix, vector) lhs_type = ArrayType(REAL_TYPE, [10]) lhs_symbol = DataSymbol("result", lhs_type) symbol_table.add(lhs_symbol) lhs = Reference(lhs_symbol) assign = Assignment.create(lhs, matmul) KernelSchedule.create("my_kern", symbol_table, [assign]) return matmul
def example_psyir_nary(): '''Utility function that creates a PSyIR tree containing a nary MIN intrinsic operator and returns the operator. :returns: PSyIR MIN operator instance. :rtype: :py:class:`psyclone.psyGen.NaryOperation` ''' symbol_table = SymbolTable() name1 = symbol_table.new_symbol_name("arg") arg1 = DataSymbol(name1, REAL_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READWRITE)) symbol_table.add(arg1) name2 = symbol_table.new_symbol_name("arg") arg2 = DataSymbol(name2, REAL_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READWRITE)) symbol_table.add(arg2) name3 = symbol_table.new_symbol_name("arg") arg3 = DataSymbol(name3, REAL_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READWRITE)) symbol_table.add(arg3) name4 = symbol_table.new_symbol_name() arg4 = DataSymbol(name4, REAL_TYPE) symbol_table.add(arg4) symbol_table.specify_argument_list([arg1, arg2, arg3]) var1 = Reference(arg1) var2 = Reference(arg2) var3 = Reference(arg3) var4 = Reference(arg4) oper = NaryOperation.Operator.MIN operation = NaryOperation.create(oper, [var1, var2, var3]) assign = Assignment.create(var4, operation) _ = KernelSchedule.create("min_example", symbol_table, [assign]) return operation
def test_get_external_symbol(monkeypatch): ''' Test the get_external_symbol() method. ''' asym = Symbol("a") with pytest.raises(NotImplementedError) as err: asym.get_external_symbol() assert ("trying to resolve symbol 'a' properties, the lazy evaluation " "of 'Local' interfaces is not supported" in str(err.value)) other_container = ContainerSymbol("some_mod") ctable = SymbolTable() ctable.add(other_container) # Create a Symbol that is imported from the "some_mod" Container bsym = Symbol("b", interface=GlobalInterface(other_container)) ctable.add(bsym) _ = Container.create("test", ctable, [KernelSchedule("dummy")]) # Monkeypatch the container's FortranModuleInterface so that it always # appears to be unable to find the "some_mod" module def fake_import(name): raise SymbolError("Oh dear") monkeypatch.setattr(other_container._interface, "import_container", fake_import) with pytest.raises(SymbolError) as err: bsym.get_external_symbol() assert ("trying to resolve the properties of symbol 'b' in module " "'some_mod': PSyclone SymbolTable error: Oh dear" in str(err.value)) # Now create a Container for the 'some_mod' module and attach this to # the ContainerSymbol ctable2 = SymbolTable() some_mod = Container.create("some_mod", ctable2, [KernelSchedule("dummy2")]) other_container._reference = some_mod # Currently the Container does not contain an entry for 'b' with pytest.raises(SymbolError) as err: bsym.get_external_symbol() assert ("trying to resolve the properties of symbol 'b'. The interface " "points to module 'some_mod' but could not find the definition" in str(err.value)) # Add an entry for 'b' to the Container's symbol table ctable2.add(DataSymbol("b", INTEGER_SINGLE_TYPE)) new_sym = bsym.resolve_deferred() assert isinstance(new_sym, DataSymbol) assert new_sym.datatype == INTEGER_SINGLE_TYPE
def example_psyir_binary(create_expression): '''Utility function that creates a PSyIR tree containing a binary MIN intrinsic operator and returns the operator. :param function create_expresssion: function used to create the \ content of the first argument of the MIN operator. :returns: PSyIR MIN operator instance. :rtype: :py:class:`psyclone.psyGen.BinaryOperation` ''' symbol_table = SymbolTable() name1 = symbol_table.new_symbol_name("arg") arg1 = DataSymbol(name1, REAL_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READWRITE)) symbol_table.add(arg1) name2 = symbol_table.new_symbol_name("arg") arg2 = DataSymbol(name2, REAL_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READWRITE)) symbol_table.add(arg2) name3 = symbol_table.new_symbol_name() arg3 = DataSymbol(name3, REAL_TYPE) symbol_table.add(arg3) symbol_table.specify_argument_list([arg1, arg2]) var1 = Reference(arg1) var2 = Reference(arg2) var3 = Reference(arg3) oper = BinaryOperation.Operator.MIN operation = BinaryOperation.create(oper, create_expression(var1), var2) assign = Assignment.create(var3, operation) _ = KernelSchedule.create("min_example", symbol_table, [assign]) return operation
from __future__ import print_function from psyclone.psyir.nodes import Call, Reference, Container, KernelSchedule from psyclone.psyir.symbols import RoutineSymbol, SymbolTable, \ ArgumentInterface from psyclone.domain.lfric import psyir as lfric_psyir from psyclone.psyir.backend.fortran import FortranWriter READ_ARG = ArgumentInterface(ArgumentInterface.Access.READ) # Add LFRic precision symbols and the module in which they are # contained to the symbol table SYMBOL_TABLE = SymbolTable() for symbol in [ lfric_psyir.I_DEF, lfric_psyir.R_DEF, lfric_psyir.CONSTANTS_MOD ]: SYMBOL_TABLE.add(symbol) # Create LFRic ndf and undf symbols and add them to the symbol table NDF_W3 = lfric_psyir.NumberOfDofsDataSymbol("ndf_w3", "w3", interface=READ_ARG) UNDF_W3 = lfric_psyir.NumberOfUniqueDofsDataSymbol("undf_w3", "w3", interface=READ_ARG) for symbol in [NDF_W3, UNDF_W3]: SYMBOL_TABLE.add(symbol) # Create LFRic field data symbols and add them to the symbol table FIELD1 = lfric_psyir.RealFieldDataDataSymbol("field1", [Reference(UNDF_W3)], "w3") FIELD2 = lfric_psyir.RealFieldDataDataSymbol( "field2", [Reference(UNDF_W3)], "w3",
# 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' FIELD_TYPE_DEF = StructureType.create( [("data", ArrayType(SCALAR_TYPE, [10]), Symbol.Visibility.PUBLIC), ("grid", GRID_TYPE_SYMBOL, Symbol.Visibility.PUBLIC),
Container, Range, Array, Call, KernelSchedule from psyclone.psyir.symbols import DataSymbol, RoutineSymbol, SymbolTable, \ ContainerSymbol, ArgumentInterface, ScalarType, ArrayType, \ GlobalInterface, REAL_TYPE, REAL4_TYPE, REAL_DOUBLE_TYPE, INTEGER_TYPE, \ INTEGER_SINGLE_TYPE, INTEGER4_TYPE, INTEGER8_TYPE from psyclone.psyir.backend.fortran import FortranWriter from psyclone.psyir.backend.c import CWriter # Symbol table, symbols and scalar datatypes SYMBOL_TABLE = SymbolTable() TMP_NAME1 = SYMBOL_TABLE.new_symbol_name() ARG1 = DataSymbol(TMP_NAME1, REAL_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READWRITE)) SYMBOL_TABLE.add(ARG1) TMP_NAME2 = SYMBOL_TABLE.new_symbol_name() TMP_SYMBOL = DataSymbol(TMP_NAME2, REAL_DOUBLE_TYPE) SYMBOL_TABLE.add(TMP_SYMBOL) INDEX_NAME = SYMBOL_TABLE.new_symbol_name(root_name="i") INDEX_SYMBOL = DataSymbol(INDEX_NAME, INTEGER4_TYPE) SYMBOL_TABLE.add(INDEX_SYMBOL) SYMBOL_TABLE.specify_argument_list([ARG1]) REAL_KIND_NAME = SYMBOL_TABLE.new_symbol_name(root_name="RKIND") REAL_KIND = DataSymbol(REAL_KIND_NAME, INTEGER_TYPE, constant_value=8) SYMBOL_TABLE.add(REAL_KIND) ROUTINE_SYMBOL = RoutineSymbol("my_sub") # Array using precision defined by another symbol ARRAY_NAME = SYMBOL_TABLE.new_symbol_name(root_name="a") SCALAR_TYPE = ScalarType(ScalarType.Intrinsic.REAL, REAL_KIND)
def create_psyir_tree(): ''' Create an example PSyIR Tree. :returns: an example PSyIR tree. :rtype: :py:class:`psyclone.psyir.nodes.Container` ''' # Symbol table, symbols and scalar datatypes symbol_table = SymbolTable() arg1 = symbol_table.new_symbol(symbol_type=DataSymbol, datatype=REAL_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READWRITE)) symbol_table.specify_argument_list([arg1]) tmp_symbol = symbol_table.new_symbol(symbol_type=DataSymbol, datatype=REAL_DOUBLE_TYPE) index_symbol = symbol_table.new_symbol(root_name="i", symbol_type=DataSymbol, datatype=INTEGER4_TYPE) real_kind = symbol_table.new_symbol(root_name="RKIND", symbol_type=DataSymbol, datatype=INTEGER_TYPE, constant_value=8) routine_symbol = RoutineSymbol("my_sub") # Array using precision defined by another symbol scalar_type = ScalarType(ScalarType.Intrinsic.REAL, real_kind) array = symbol_table.new_symbol(root_name="a", symbol_type=DataSymbol, datatype=ArrayType(scalar_type, [10])) # Make generators for nodes which do not have other Nodes as children, # with some predefined scalar datatypes def zero(): return Literal("0.0", REAL_TYPE) def one(): return Literal("1.0", REAL4_TYPE) def two(): return Literal("2.0", scalar_type) def int_zero(): return Literal("0", INTEGER_SINGLE_TYPE) def int_one(): return Literal("1", INTEGER8_TYPE) def tmp1(): return Reference(arg1) def tmp2(): return Reference(tmp_symbol) # Unary Operation oper = UnaryOperation.Operator.SIN unaryoperation = UnaryOperation.create(oper, tmp2()) # Binary Operation oper = BinaryOperation.Operator.ADD binaryoperation = BinaryOperation.create(oper, one(), unaryoperation) # Nary Operation oper = NaryOperation.Operator.MAX naryoperation = NaryOperation.create(oper, [tmp1(), tmp2(), one()]) # Array reference using a range lbound = BinaryOperation.create(BinaryOperation.Operator.LBOUND, Reference(array), int_one()) ubound = BinaryOperation.create(BinaryOperation.Operator.UBOUND, Reference(array), int_one()) my_range = Range.create(lbound, ubound) tmparray = ArrayReference.create(array, [my_range]) # Assignments assign1 = Assignment.create(tmp1(), zero()) assign2 = Assignment.create(tmp2(), zero()) assign3 = Assignment.create(tmp2(), binaryoperation) assign4 = Assignment.create(tmp1(), tmp2()) assign5 = Assignment.create(tmp1(), naryoperation) assign6 = Assignment.create(tmparray, two()) # Call call = Call.create(routine_symbol, [tmp1(), binaryoperation.copy()]) # If statement if_condition = BinaryOperation.create(BinaryOperation.Operator.GT, tmp1(), zero()) ifblock = IfBlock.create(if_condition, [assign3, assign4]) # Loop loop = Loop.create(index_symbol, int_zero(), int_one(), int_one(), [ifblock]) # KernelSchedule kernel_schedule = KernelSchedule.create( "work", symbol_table, [assign1, call, assign2, loop, assign5, assign6]) # Container container_symbol_table = SymbolTable() container = Container.create("CONTAINER", container_symbol_table, [kernel_schedule]) # Import data from another container external_container = ContainerSymbol("some_mod") container_symbol_table.add(external_container) external_var = DataSymbol("some_var", INTEGER_TYPE, interface=GlobalInterface(external_container)) container_symbol_table.add(external_var) routine_symbol.interface = GlobalInterface(external_container) container_symbol_table.add(routine_symbol) return container
def create_psyir_tree(): ''' Create an example PSyIR Tree. :returns: an example PSyIR tree. :rtype: :py:class:`psyclone.psyir.nodes.Container` ''' # Symbol table, symbols and scalar datatypes symbol_table = SymbolTable() arg1 = symbol_table.new_symbol(symbol_type=DataSymbol, datatype=REAL_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READWRITE)) symbol_table.specify_argument_list([arg1]) tmp_symbol = symbol_table.new_symbol(symbol_type=DataSymbol, datatype=REAL_DOUBLE_TYPE) index_symbol = symbol_table.new_symbol(root_name="i", symbol_type=DataSymbol, datatype=INTEGER4_TYPE) real_kind = symbol_table.new_symbol(root_name="RKIND", symbol_type=DataSymbol, datatype=INTEGER_TYPE, constant_value=8) routine_symbol = RoutineSymbol("my_sub") # Array using precision defined by another symbol scalar_type = ScalarType(ScalarType.Intrinsic.REAL, real_kind) array = symbol_table.new_symbol(root_name="a", symbol_type=DataSymbol, datatype=ArrayType(scalar_type, [10])) # Nodes which do not have Nodes as children and (some) predefined # scalar datatypes # TODO: Issue #1136 looks at how to avoid all of the _x versions zero_1 = Literal("0.0", REAL_TYPE) zero_2 = Literal("0.0", REAL_TYPE) zero_3 = Literal("0.0", REAL_TYPE) one_1 = Literal("1.0", REAL4_TYPE) one_2 = Literal("1.0", REAL4_TYPE) one_3 = Literal("1.0", REAL4_TYPE) two = Literal("2.0", scalar_type) int_zero = Literal("0", INTEGER_SINGLE_TYPE) int_one_1 = Literal("1", INTEGER8_TYPE) int_one_2 = Literal("1", INTEGER8_TYPE) int_one_3 = Literal("1", INTEGER8_TYPE) int_one_4 = Literal("1", INTEGER8_TYPE) tmp1_1 = Reference(arg1) tmp1_2 = Reference(arg1) tmp1_3 = Reference(arg1) tmp1_4 = Reference(arg1) tmp1_5 = Reference(arg1) tmp1_6 = Reference(arg1) tmp2_1 = Reference(tmp_symbol) tmp2_2 = Reference(tmp_symbol) tmp2_3 = Reference(tmp_symbol) tmp2_4 = Reference(tmp_symbol) tmp2_5 = Reference(tmp_symbol) tmp2_6 = Reference(tmp_symbol) # Unary Operation oper = UnaryOperation.Operator.SIN unaryoperation_1 = UnaryOperation.create(oper, tmp2_1) unaryoperation_2 = UnaryOperation.create(oper, tmp2_2) # Binary Operation oper = BinaryOperation.Operator.ADD binaryoperation_1 = BinaryOperation.create(oper, one_1, unaryoperation_1) binaryoperation_2 = BinaryOperation.create(oper, one_2, unaryoperation_2) # Nary Operation oper = NaryOperation.Operator.MAX naryoperation = NaryOperation.create(oper, [tmp1_1, tmp2_3, one_3]) # Array reference using a range lbound = BinaryOperation.create(BinaryOperation.Operator.LBOUND, Reference(array), int_one_1) ubound = BinaryOperation.create(BinaryOperation.Operator.UBOUND, Reference(array), int_one_2) my_range = Range.create(lbound, ubound) tmparray = ArrayReference.create(array, [my_range]) # Assignments assign1 = Assignment.create(tmp1_2, zero_1) assign2 = Assignment.create(tmp2_4, zero_2) assign3 = Assignment.create(tmp2_5, binaryoperation_1) assign4 = Assignment.create(tmp1_3, tmp2_6) assign5 = Assignment.create(tmp1_4, naryoperation) assign6 = Assignment.create(tmparray, two) # Call call = Call.create(routine_symbol, [tmp1_5, binaryoperation_2]) # If statement if_condition = BinaryOperation.create(BinaryOperation.Operator.GT, tmp1_6, zero_3) ifblock = IfBlock.create(if_condition, [assign3, assign4]) # Loop loop = Loop.create(index_symbol, int_zero, int_one_3, int_one_4, [ifblock]) # KernelSchedule kernel_schedule = KernelSchedule.create( "work", symbol_table, [assign1, call, assign2, loop, assign5, assign6]) # Container container_symbol_table = SymbolTable() container = Container.create("CONTAINER", container_symbol_table, [kernel_schedule]) # Import data from another container external_container = ContainerSymbol("some_mod") container_symbol_table.add(external_container) external_var = DataSymbol("some_var", INTEGER_TYPE, interface=GlobalInterface(external_container)) container_symbol_table.add(external_var) routine_symbol.interface = GlobalInterface(external_container) container_symbol_table.add(routine_symbol) return container
def __init__(self, ast=None, children=None, parent=None, options=None): if not options: options = {} # This string stores a prefix to be used with all external PSyData # symbols (i.e. data types and module name), used in the # method 'add_psydata_class_prefix'. self._class_string = options.get("prefix", "") if self._class_string: self._class_string = self._class_string + "_" # Root of the name to use for variables associated with # PSyData regions self._psy_data_symbol_with_prefix = \ self.add_psydata_class_prefix("psy_data") # The use statement that will be inserted. Any use of a module # of the same name that doesn't match this will result in a # NotImplementedError at code-generation time. self.use_stmt = "use {0}, only: "\ .format(self.add_psydata_class_prefix("psy_data_mod")) + \ ", ".join(self.add_psydata_class_prefix(symbol) for symbol in PSyDataNode.symbols) if children: # We need to store the position of the original children, # i.e. before they are added to a schedule node_position = children[0].position # A PSyData node always contains a Schedule sched = self._insert_schedule(children) super(PSyDataNode, self).__init__(ast=ast, children=[sched], parent=parent) # Get or create a symbol table so we can avoid name clashes # when creating variables if parent and hasattr(self.root, 'symbol_table'): symtab = self.root.symbol_table else: # FIXME: This may not be a good solution symtab = SymbolTable() # Store the name of the PSyData variable that is used for this # PSyDataNode. This allows the variable name to be shown in str # (and also, calling create_name in gen() would result in the name # being changed every time gen() is called). self._var_name = symtab.new_symbol_name( self._psy_data_symbol_with_prefix) symtab.add(Symbol(self._var_name)) if children and parent: # Correct the parent's list of children. Use a slice of the list # of nodes so that we're looping over a local copy of the list. # Otherwise things get confused when we remove children from # the list. for child in children[:]: # Remove child from the parent's list of children parent.children.remove(child) # Add this node as a child of the parent # of the nodes being enclosed and at the original location # of the first of these nodes parent.addchild(self, index=node_position) elif parent: parent.addchild(self) # Name of the region. In general at constructor time we might # not have a parent subroutine or any child nodes, so # the name is left empty, unless explicitly provided by the # user. If names are not provided here then the region and # module names are set the first time gen() is called (and # then remain unchanged). self._module_name = None self._region_name = None # The region identifier caches the computed module- and region-name # as a tuple of strings. This is required so that a derived class can # query the actual name of region (e.g. during generation of a driver # for an extract node). If the user does not define a name, i.e. # module_name and region_name are empty, a unique name will be # computed in gen_code(). If this name would then be stored in # module_name and region_name, and gen() is called again, the # names would not be computed again, since the code detects already # defined module and region names. This can then result in duplicated # region names: The test 'test_region' in profile_test triggers this. # gen()) is called first after one profile region is applied, then # another profile region is added, and gen() is called again. The # second profile region would compute a new name, which then happens # to be the same as the name computed for the first region in the # first gen_code call (which indeed implies that the name of the # first profile region is different the second time it is computed). # So in order to guarantee that the computed module and region names # are unique when gen_code is called more than once, we # cannot store a computed name in module_name and region_name. self._region_identifier = ("", "") name = options.get("region_name", None) if name: # pylint: disable=too-many-boolean-expressions if not isinstance(name, tuple) or not len(name) == 2 or \ not name[0] or not isinstance(name[0], str) or \ not name[1] or not isinstance(name[1], str): raise InternalError("Error in PSyDataNode. The name must be a " "tuple containing two non-empty strings.") # pylint: enable=too-many-boolean-expressions # Valid PSyData names have been provided by the user. self._module_name = name[0] self._region_name = name[1] self.set_region_identifier(self._module_name, self._region_name)
class KernelInterface(ArgOrdering): '''Create the kernel arguments for the supplied kernel as specified by the associated kernel metadata and the kernel ordering rules encoded in the ArgOrdering base class as method callbacks. A PSyIR symbol table is created containing appropriate LFRic PSyIR symbols to specify the arguments. If an argument is an array with one or more dimension sizes specified by another argument, then the associated array symbol is created so that it references the appropriate symbol. Related arguments - e.g. a field has an associated dofmap - are not directly connected, they must be inferred from the function space names. It is not yet clear whether this would be useful or not. TBD: This class should replace the current kernel stub generation code when all of its methods are implemented, see issue #928. :param kern: the kernel for which to create arguments. :type kern: :py:class:`psyclone.dynamo0p3.DynKern` ''' #: Mapping from a generic PSyIR datatype to the equivalent #: LFRic-specific field datasymbol. field_mapping = { "integer": lfric_psyir.IntegerFieldDataDataSymbol, "real": lfric_psyir.RealFieldDataDataSymbol, "logical": lfric_psyir.LogicalFieldDataDataSymbol } #: Mapping from a generic PSyIR datatype to the equivalent #: LFRic-specific vector field datasymbol. vector_field_mapping = { "integer": lfric_psyir.IntegerVectorFieldDataDataSymbol, "real": lfric_psyir.RealVectorFieldDataDataSymbol, "logical": lfric_psyir.LogicalVectorFieldDataDataSymbol } #: Mapping from the LFRic metadata description of quadrature to the #: associated LFRic-specific basis function datasymbol. basis_mapping = { "gh_quadrature_xyoz": lfric_psyir.BasisFunctionQrXyozDataSymbol, "gh_quadrature_face": lfric_psyir.BasisFunctionQrFaceDataSymbol, "gh_quadrature_edge": lfric_psyir.BasisFunctionQrEdgeDataSymbol } #: Mapping from the LFRic metadata description of quadrature to the #: associated LFRic-specific differential basis function datasymbol. diff_basis_mapping = { "gh_quadrature_xyoz": lfric_psyir.DiffBasisFunctionQrXyozDataSymbol, "gh_quadrature_face": lfric_psyir.DiffBasisFunctionQrFaceDataSymbol, "gh_quadrature_edge": lfric_psyir.DiffBasisFunctionQrEdgeDataSymbol } _read_access = ArgumentInterface(ArgumentInterface.Access.READ) def __init__(self, kern): super(KernelInterface, self).__init__(kern) self._symbol_table = SymbolTable() self._arglist = [] def generate(self, var_accesses=None): '''Call the generate base class then add the argument list as it can't be appended as we go along. :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` ''' super(KernelInterface, self).generate(var_accesses=var_accesses) # Set the argument list for the symbol table. This is done at # the end after incrementally adding symbols to the _args # list, as it is not possible to incrementally add symbols to # the symbol table argument list. self._symbol_table.specify_argument_list(self._arglist) # While data dependence analysis does not use the symbol # table, see #845, we have to provide variable information # separately. This is done by using the base class append() # method. However, this method also adds the variable names to the # internal _arglist list which we do not want as we have # already added our symbols there. Therefore we need to remove # them afterwards. # Map from symbol table accesses to dependence analysis accesses. mapping = { ArgumentInterface.Access.READ: AccessType.READ, ArgumentInterface.Access.READWRITE: AccessType.READWRITE, ArgumentInterface.Access.WRITE: AccessType.WRITE } len_arglist = len(self._arglist) for symbol in self._symbol_table.symbols: self.append(symbol.name, var_accesses, mode=mapping[symbol.interface.access]) self._arglist = self._arglist[:len_arglist] def cell_position(self, var_accesses=None): '''Create an LFRic cell-position object and add it to the symbol table and argument list. :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` ''' symbol = self._symbol_table.symbol_from_tag( "cell", symbol_type=lfric_psyir.CellPositionDataSymbol, interface=self._read_access) self._arglist.append(symbol) def mesh_height(self, var_accesses=None): '''Create an LFRic mesh height object and add it to the symbol table and argument list. :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` ''' symbol = self._symbol_table.symbol_from_tag( "nlayers", symbol_type=lfric_psyir.MeshHeightDataSymbol, interface=self._read_access) self._arglist.append(symbol) def mesh_ncell2d(self, var_accesses=None): '''Not implemented. :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises NotImplementedError: as this method is not implemented. ''' raise NotImplementedError("mesh_ncell2d not implemented") def cell_map(self, var_accesses=None): '''Not implemented. :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises NotImplementedError: as this method is not implemented. ''' raise NotImplementedError("cell_map not implemented") def field_vector(self, argvect, var_accesses=None): '''Create LFRic field vector arguments and add them to the symbol table and argument list. Also declare the associated "undf" symbol if it has not already been declared so that it can be used to dimension the field vector arguments. :param argvect: the field vector to add. :type argvect: :py:class:`psyclone.dynamo0p3.DynKernelArgument` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises NotImplementedError: if the datatype of the vector \ field is not supported. ''' fs_name = argvect.function_space.orig_name undf_symbol = self._symbol_table.symbol_from_tag( "undf_{0}".format(fs_name), fs=fs_name, symbol_type=lfric_psyir.NumberOfUniqueDofsDataSymbol, interface=self._read_access) interface = ArgumentInterface(INTENT_MAPPING[argvect.intent]) try: field_class = self.vector_field_mapping[argvect.intrinsic_type] except KeyError as info: message = ("kernel interface does not support a vector field of " "type '{0}'.".format(argvect.intrinsic_type)) six.raise_from(NotImplementedError(message), info) for idx in range(argvect.vector_size): tag = "{0}_v{1}".format(argvect.name, idx) field_data_symbol = self._symbol_table.symbol_from_tag( tag, symbol_type=field_class, dims=[Reference(undf_symbol)], fs=fs_name, interface=interface) self._arglist.append(field_data_symbol) def field(self, arg, var_accesses=None): '''Create an LFRic field argument and add it to the symbol table and argument list. Also declare the associated "undf" symbol if it has not already been declared so that it can be used to dimension the field argument. :param arg: the field to add. :type arg: :py:class:`psyclone.dynamo0p3.DynKernelArgument` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises NotImplementedError: if the datatype of the field is \ not supported. ''' fs_name = arg.function_space.orig_name undf_symbol = self._symbol_table.symbol_from_tag( "undf_{0}".format(fs_name), symbol_type=lfric_psyir.NumberOfUniqueDofsDataSymbol, fs=fs_name, interface=self._read_access) try: field_class = self.field_mapping[arg.intrinsic_type] except KeyError as info: message = ("kernel interface does not support a field of type " "'{0}'.".format(arg.intrinsic_type)) six.raise_from(NotImplementedError(message), info) field_data_symbol = self._symbol_table.symbol_from_tag( arg.name, interface=ArgumentInterface(INTENT_MAPPING[arg.intent]), symbol_type=field_class, dims=[Reference(undf_symbol)], fs=fs_name) self._arglist.append(field_data_symbol) def stencil_unknown_extent(self, arg, var_accesses=None): '''Not implemented. :param arg: the kernel argument with which the stencil is associated. :type arg: :py:class:`psyclone.dynamo0p3.DynKernelArgument` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises NotImplementedError: as this method is not implemented. ''' raise NotImplementedError("stencil_unknown_extent not implemented") def stencil_unknown_direction(self, arg, var_accesses=None): '''Not implemented. :param arg: the kernel argument with which the stencil is associated. :type arg: :py:class:`psyclone.dynamo0p3.DynKernelArgument` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises NotImplementedError: as this method is not implemented. ''' raise NotImplementedError("stencil_unknown_direction not implemented") def stencil(self, arg, var_accesses=None): '''Not implemented. :param arg: the kernel argument with which the stencil is associated. :type arg: :py:class:`psyclone.dynamo0p3.DynKernelArgument` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises NotImplementedError: as this method is not implemented. ''' raise NotImplementedError("stencil not implemented") def operator(self, arg, var_accesses=None): '''Create an LFRic operator argument and an ncells argument and add them to the symbol table and argument list. Also declare the associated 'fs_from', 'fs_to' symbols if they have not already been declared so that they can be used to dimension the operator symbol (as well as ncells). :param arg: the operator to add. :type arg: :py:class:`psyclone.dynamo0p3.DynKernelArgument` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises NotImplementedError: if the datatype of the field is \ not supported. ''' fs_from_name = arg.function_space_from.orig_name ndf_symbol_from = self._symbol_table.symbol_from_tag( "ndf_{0}".format(fs_from_name), fs=fs_from_name, symbol_type=lfric_psyir.NumberOfDofsDataSymbol, interface=self._read_access) fs_to_name = arg.function_space_to.orig_name ndf_symbol_to = self._symbol_table.symbol_from_tag( "ndf_{0}".format(fs_to_name), fs=fs_to_name, symbol_type=lfric_psyir.NumberOfDofsDataSymbol, interface=self._read_access) ncells = lfric_psyir.NumberOfCellsDataSymbol( "ncell_3d", interface=self._read_access) self._symbol_table.add(ncells) self._arglist.append(ncells) op_arg_symbol = self._symbol_table.symbol_from_tag( arg.name, symbol_type=lfric_psyir.OperatorDataSymbol, dims=[ Reference(ndf_symbol_from), Reference(ndf_symbol_to), Reference(ncells) ], fs_from=fs_from_name, fs_to=fs_to_name, interface=ArgumentInterface(INTENT_MAPPING[arg.intent])) self._arglist.append(op_arg_symbol) def cma_operator(self, arg, var_accesses=None): '''Not implemented. :param arg: the CMA operator argument. :type arg: :py:class:`psyclone.dynamo0p3.DynKernelArgument` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises NotImplementedError: as this method is not implemented. ''' raise NotImplementedError("cma_operator not implemented") def scalar(self, scalar_arg, var_accesses=None): '''Create an LFRic scalar argument and add it to the symbol table and argument list. :param scalar_arg: the scalar to add. :type scalar_arg: :py:class:`psyclone.dynamo0p3.DynKernelArgument` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises NotImplementedError: if the datatype of the scalar is \ not supported. ''' mapping = { "integer": lfric_psyir.LfricIntegerScalarDataSymbol, "real": lfric_psyir.LfricRealScalarDataSymbol, "logical": lfric_psyir.LfricLogicalScalarDataSymbol } try: symbol = self._symbol_table.symbol_from_tag( scalar_arg.name, symbol_type=mapping[scalar_arg.intrinsic_type], interface=ArgumentInterface(INTENT_MAPPING[scalar_arg.intent])) except KeyError as info: message = ( "scalar of type '{0}' not implemented in KernelInterface " "class.".format(scalar_arg.intrinsic_type)) six.raise_from(NotImplementedError(message), info) self._arglist.append(symbol) def fs_common(self, function_space, var_accesses=None): '''Create any arguments that are common to a particular function space. At this time the only common argument is the number of degrees of freedom. Create the associated LFRic symbol, and add it to the symbol table and argument list. :param function_space: the function space for any common arguments. :type function_space: :py:class:`psyclone.domain.lfric.FunctionSpace` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` ''' fs_name = function_space.orig_name ndf_symbol = self._symbol_table.symbol_from_tag( "ndf_{0}".format(fs_name), fs=fs_name, symbol_type=lfric_psyir.NumberOfDofsDataSymbol, interface=self._read_access) self._arglist.append(ndf_symbol) def fs_intergrid(self, function_space, var_accesses=None): '''Not implemented. :param arg: the CMA operator argument. :type arg: :py:class:`psyclone.dynamo0p3.DynKernelArgument` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises NotImplementedError: as this method is not implemented. ''' raise NotImplementedError("fs_intergrid not implemented") def fs_compulsory_field(self, function_space, var_accesses=None): '''Create any arguments that are compulsory for a field on a particular function space. At this time the compulsory arguments are the unique number of degrees of freedom and the dofmap. Create the associated LFRic symbol, and add it to the symbol table and argument list. Also declare the number of degrees of freedom and add to the symbol table if one has not yet been added. :param function_space: the function space for any compulsory arguments. :type function_space: :py:class:`psyclone.domain.lfric.FunctionSpace` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` ''' fs_name = function_space.orig_name undf_symbol = self._symbol_table.symbol_from_tag( "undf_{0}".format(fs_name), fs=fs_name, symbol_type=lfric_psyir.NumberOfUniqueDofsDataSymbol, interface=self._read_access) self._arglist.append(undf_symbol) fs_name = function_space.orig_name ndf_symbol = self._symbol_table.symbol_from_tag( "ndf_{0}".format(fs_name), fs=fs_name, symbol_type=lfric_psyir.NumberOfDofsDataSymbol, interface=self._read_access) dofmap_symbol = self._symbol_table.symbol_from_tag( "dofmap_{0}".format(fs_name), fs=fs_name, symbol_type=lfric_psyir.DofMapDataSymbol, dims=[Reference(ndf_symbol)], interface=self._read_access) self._arglist.append(dofmap_symbol) def banded_dofmap(self, function_space, var_accesses=None): '''Not implemented. :param function_space: the function space for this dofmap. :type function_space: :py:class:`psyclone.domain.lfric.FunctionSpace` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises NotImplementedError: as this method is not implemented. ''' raise NotImplementedError("banded_dofmap not implemented") def indirection_dofmap(self, function_space, operator=None, var_accesses=None): '''Not implemented. :param function_space: the function space for this dofmap. :type function_space: :py:class:`psyclone.domain.lfric.FunctionSpace` :param operator: the CMA operator. :type operator: :py:class:`psyclone.dynamo0p3.DynKernelArgument` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises NotImplementedError: as this method is not implemented. ''' raise NotImplementedError("indirection_dofmap not implemented") def basis(self, function_space, var_accesses=None): '''Create an LFRic basis function argument and add it to the symbol table and argument list. :param function_space: the function space for this basis function. :type function_space: :py:class:`psyclone.domain.lfric.FunctionSpace` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` ''' basis_name_func = function_space.get_basis_name # This import must be placed here to avoid circular dependencies # pylint: disable=import-outside-toplevel from psyclone.dynamo0p3 import DynBasisFunctions first_dim_value_func = DynBasisFunctions.basis_first_dim_value self._create_basis(function_space, self.basis_mapping, basis_name_func, first_dim_value_func) def diff_basis(self, function_space, var_accesses=None): '''Create an LFRic differential basis function argument and add it to the symbol table and argument list. :param function_space: the function space for this \ differential basis function. :type function_space: :py:class:`psyclone.domain.lfric.FunctionSpace` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` ''' basis_name_func = function_space.get_diff_basis_name # This import must be placed here to avoid circular dependencies # pylint: disable=import-outside-toplevel from psyclone.dynamo0p3 import DynBasisFunctions first_dim_value_func = DynBasisFunctions.diff_basis_first_dim_value self._create_basis(function_space, self.diff_basis_mapping, basis_name_func, first_dim_value_func) def field_bcs_kernel(self, function_space, var_accesses=None): '''Not implemented. :param function_space: the function space for this boundary condition. :type function_space: :py:class:`psyclone.domain.lfric.FunctionSpace` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises NotImplementedError: as this method is not implemented. ''' raise NotImplementedError("field_bcs_kernel not implemented") def operator_bcs_kernel(self, function_space, var_accesses=None): '''Not implemented. :param function_space: the function space for this bcs kernel :type function_space: :py:class:`psyclone.domain.lfric.FunctionSpace` :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises NotImplementedError: as this method is not implemented. ''' raise NotImplementedError("operator_bcs_kernel not implemented") def ref_element_properties(self, var_accesses=None): ''' Properties associated with the reference element :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` ''' # This callback does not contribute any kernel arguments def mesh_properties(self, var_accesses=None): ''' Properties associated with the mesh :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` ''' # This callback does not contribute any kernel arguments def quad_rule(self, var_accesses=None): '''Create LFRic arguments associated with the required quadrature, if they do not already exist, and add them to the symbol table and argument list. The arguments depend on the type of quadrature requested. :param var_accesses: an unused optional argument that stores \ information about variable accesses. :type var_accesses: :\ py:class:`psyclone.core.access_info.VariablesAccessInfo` :raises InternalError: if an unsupported quadrature shape is \ found. ''' # The kernel captures all the required quadrature shapes for shape in self._kern.qr_rules: if shape == "gh_quadrature_xyoz": nqp_xy = self._symbol_table.symbol_from_tag( "nqp_xy", symbol_type=lfric_psyir.NumberOfQrPointsInXyDataSymbol, interface=self._read_access) nqp_z = self._symbol_table.symbol_from_tag( "nqp_z", symbol_type=lfric_psyir.NumberOfQrPointsInZDataSymbol, interface=self._read_access) weights_xy = self._symbol_table.symbol_from_tag( "weights_xy", symbol_type=lfric_psyir.QrWeightsInXyDataSymbol, dims=[Reference(nqp_xy)], interface=self._read_access) weights_z = self._symbol_table.symbol_from_tag( "weights_z", symbol_type=lfric_psyir.QrWeightsInZDataSymbol, dims=[Reference(nqp_z)], interface=self._read_access) self._arglist.extend([nqp_xy, nqp_z, weights_xy, weights_z]) elif shape == "gh_quadrature_face": nfaces = self._symbol_table.symbol_from_tag( "nfaces", symbol_type=lfric_psyir.NumberOfFacesDataSymbol, interface=self._read_access) nqp = self._symbol_table.symbol_from_tag( "nqp_faces", symbol_type=lfric_psyir.NumberOfQrPointsInFacesDataSymbol, interface=self._read_access) weights = self._symbol_table.symbol_from_tag( "weights_faces", symbol_type=lfric_psyir.QrWeightsInFacesDataSymbol, dims=[Reference(nqp)], interface=self._read_access) self._arglist.extend([nfaces, nqp, weights]) elif shape == "gh_quadrature_edge": nedges = self._symbol_table.symbol_from_tag( "nedges", symbol_type=lfric_psyir.NumberOfEdgesDataSymbol, interface=self._read_access) nqp = self._symbol_table.symbol_from_tag( "nqp_edges", symbol_type=lfric_psyir.NumberOfQrPointsInEdgesDataSymbol, interface=self._read_access) weights = self._symbol_table.symbol_from_tag( "weights_edges", symbol_type=lfric_psyir.QrWeightsInEdgesDataSymbol, dims=[Reference(nqp)], interface=self._read_access) self._arglist.extend([nedges, nqp, weights]) else: raise InternalError("Unsupported quadrature shape '{0}' " "found in kernel_interface.".format(shape)) def _create_basis(self, function_space, mapping, basis_name_func, first_dim_value_func): '''Internal utility to create an LFRic basis or differential basis function argument specific to the particular quadrature that is being used and add it to the symbol table and argument list. Also declare the associated "ndf" symbol and any quadrature-specific symbols if they have not already been declared so that they can be used to dimension the basis or differential basis symbol. This utility function is used to avoid code replication as the structure of a basis function is very similar to the structure of a differential basis function. :param function_space: the function space that this basis or \ differential basis function is on. :type function_space: :py:class:`psyclone.domain.lfric.FunctionSpace` :param dict mapping: a mapping from quadrature type to basis \ or differential basis class name. :param method basis_name_func: a method that returns the name \ of the basis or differential basis function for the \ current function space. :param function first_dim_value_func: a function that returns \ the size of the first dimension of the basis or \ differential basis function for the current function \ space. :raises NotImplementedError: if an evaluator shape is found \ that is not a quadrature shape (currently just \ 'gh_evaluator'). :raises InternalError: if the supplied evaluator shape is not \ recognised. ''' # pylint: disable=too-many-locals # This import must be placed here to avoid circular dependencies # pylint: disable=import-outside-toplevel from psyclone.dynamo0p3 import VALID_EVALUATOR_SHAPES for shape in self._kern.eval_shapes: fs_name = function_space.orig_name ndf_symbol = self._symbol_table.symbol_from_tag( "ndf_{0}".format(fs_name), symbol_type=lfric_psyir.NumberOfDofsDataSymbol, fs=fs_name, interface=self._read_access) # Create the qr tag by appending the last part of the shape # name to "qr_". quad_name = shape.split("_")[-1] basis_tag = basis_name_func(qr_var="qr_" + quad_name) if shape == "gh_quadrature_xyoz": nqp_xy = self._symbol_table.symbol_from_tag( "nqp_xy", symbol_type=lfric_psyir.NumberOfQrPointsInXyDataSymbol, interface=self._read_access) nqp_z = self._symbol_table.symbol_from_tag( "nqp_z", symbol_type=lfric_psyir.NumberOfQrPointsInZDataSymbol, interface=self._read_access) arg = mapping["gh_quadrature_xyoz"]( basis_tag, [ int(first_dim_value_func(function_space)), Reference(ndf_symbol), Reference(nqp_xy), Reference(nqp_z) ], fs_name, interface=self._read_access) elif shape == "gh_quadrature_face": nfaces = self._symbol_table.symbol_from_tag( "nfaces", symbol_type=lfric_psyir.NumberOfFacesDataSymbol, interface=self._read_access) nqp = self._symbol_table.symbol_from_tag( "nqp_faces", symbol_type=lfric_psyir.NumberOfQrPointsInFacesDataSymbol, interface=self._read_access) arg = mapping["gh_quadrature_face"]( basis_tag, [ int(first_dim_value_func(function_space)), Reference(ndf_symbol), Reference(nqp), Reference(nfaces) ], fs_name, interface=self._read_access) elif shape == "gh_quadrature_edge": nedges = self._symbol_table.symbol_from_tag( "nedges", symbol_type=lfric_psyir.NumberOfEdgesDataSymbol, interface=self._read_access) nqp = self._symbol_table.symbol_from_tag( "nqp_edges", symbol_type=lfric_psyir.NumberOfQrPointsInEdgesDataSymbol, interface=self._read_access) arg = mapping["gh_quadrature_edge"]( basis_tag, [ int(first_dim_value_func(function_space)), Reference(ndf_symbol), Reference(nqp), Reference(nedges) ], fs_name, interface=self._read_access) elif shape in VALID_EVALUATOR_SHAPES: # Need a (diff) basis array for each target space upon # which the basis functions have been # evaluated. _kern.eval_targets is a dict where the # values are 2-tuples of (FunctionSpace, argument). for _, _ in self._kern.eval_targets.items(): raise NotImplementedError( "Evaluator shapes not implemented in kernel_interface " "class.") else: raise InternalError( "Unrecognised quadrature or evaluator shape '{0}'. " "Expected one of: {1}.".format(shape, VALID_EVALUATOR_SHAPES)) self._symbol_table.add(arg) self._arglist.append(arg)