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() arg1 = symbol_table.new_symbol("arg", symbol_type=DataSymbol, datatype=REAL_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READWRITE)) arg2 = symbol_table.new_symbol("arg", symbol_type=DataSymbol, datatype=REAL_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READWRITE)) arg3 = symbol_table.new_symbol("arg", symbol_type=DataSymbol, datatype=REAL_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READWRITE)) arg4 = symbol_table.new_symbol(symbol_type=DataSymbol, datatype=REAL_TYPE) 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 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() arg1 = symbol_table.new_symbol("arg", symbol_type=DataSymbol, datatype=REAL_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READWRITE)) arg2 = symbol_table.new_symbol("arg", symbol_type=DataSymbol, datatype=REAL_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READWRITE)) arg3 = symbol_table.new_symbol(symbol_type=DataSymbol, datatype=REAL_TYPE) 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
def test_find_or_create_change_symbol_type(): ''' Check that the _find_or_create_imported_symbol routine correctly updates the class of the located symbol if it is not an instance of the requested symbol type. ''' # pylint: disable=unidiomatic-typecheck # Create some suitable PSyIR from scratch symbol_table = SymbolTable() tmp_sym = symbol_table.new_symbol("tmp") sub_sym = symbol_table.new_symbol("my_sub") kernel1 = KernelSchedule.create("mod_1", SymbolTable(), []) _ = Container.create("container_name", symbol_table, [kernel1]) assign = Assignment.create(Reference(tmp_sym), Literal("1.0", REAL_TYPE)) kernel1.addchild(assign) # Search for the 'tmp' symbol sym = _find_or_create_imported_symbol(assign, "tmp") assert sym is tmp_sym assert type(sym) == Symbol # Repeat but this time specify that we're expecting a DataSymbol sym = _find_or_create_imported_symbol(assign, "tmp", symbol_type=DataSymbol, datatype=REAL_TYPE) assert sym is tmp_sym assert type(sym) == DataSymbol assert sym.datatype == REAL_TYPE # Search for 'my_sub' and specify that it should be a RoutineSymbol sym2 = _find_or_create_imported_symbol(assign, "my_sub", symbol_type=RoutineSymbol) assert sym2 is sub_sym assert type(sym2) == RoutineSymbol
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() arg1 = symbol_table.new_symbol("arg", symbol_type=DataSymbol, datatype=REAL_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READWRITE)) local = symbol_table.new_symbol(symbol_type=DataSymbol, datatype=REAL_TYPE) 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_lower_to_lang_level_single_node(): ''' Test the lower_to_language_level() method when a Schedule contains a single ProfileNode. ''' Profiler.set_options([Profiler.INVOKES]) symbol_table = SymbolTable() arg1 = symbol_table.new_symbol(symbol_type=DataSymbol, datatype=REAL_TYPE) zero = Literal("0.0", REAL_TYPE) one = Literal("1.0", REAL_TYPE) assign1 = Assignment.create(Reference(arg1), zero) assign2 = Assignment.create(Reference(arg1), one) kschedule = KernelSchedule.create( "work1", symbol_table, [assign1, assign2, Return()]) Profiler.add_profile_nodes(kschedule, Loop) assert isinstance(kschedule.children[0], ProfileNode) assert isinstance(kschedule.children[-1], Return) kschedule.lower_to_language_level() # The ProfileNode should have been replaced by two CodeBlocks with its # children inserted between them. assert isinstance(kschedule[0], CodeBlock) # The first CodeBlock should have the "profile-start" annotation. assert kschedule[0].annotations == ["profile-start"] ptree = kschedule[0].get_ast_nodes assert len(ptree) == 1 assert isinstance(ptree[0], Fortran2003.Call_Stmt) assert kschedule[1] is assign1 assert kschedule[2] is assign2 assert isinstance(kschedule[-2], CodeBlock) assert kschedule[-2].annotations == [] ptree = kschedule[-2].get_ast_nodes assert len(ptree) == 1 assert isinstance(ptree[0], Fortran2003.Call_Stmt) assert isinstance(kschedule[-1], Return)
def test_auto_invoke_return_last_stmt(parser): ''' Check that using the auto-invoke profiling option avoids including a return statement within the profiling region if it is the last statement in the routine. ''' symbol_table = SymbolTable() arg1 = symbol_table.new_symbol(symbol_type=DataSymbol, datatype=REAL_TYPE) zero = Literal("0.0", REAL_TYPE) assign1 = Assignment.create(Reference(arg1), zero) kschedule = KernelSchedule.create("work", symbol_table, [assign1, Return()]) # Double-check that the tree is as we expect assert isinstance(kschedule[-1], Return) Profiler.set_options([Profiler.INVOKES]) Profiler.add_profile_nodes(kschedule, Loop) # The Return should be a sibling of the ProfileNode rather than a child assert isinstance(kschedule[0], ProfileNode) assert isinstance(kschedule[0].children[0].children[0], Assignment) assert isinstance(kschedule[1], Return)
def test_auto_invoke_no_return(capsys): ''' Check that using the auto-invoke profiling option does not add any profiling if the invoke contains a Return anywhere other than as the last statement. ''' Profiler.set_options([Profiler.INVOKES]) symbol_table = SymbolTable() arg1 = symbol_table.new_symbol( symbol_type=DataSymbol, datatype=REAL_TYPE) zero = Literal("0.0", REAL_TYPE) assign1 = Assignment.create(Reference(arg1), zero) assign2 = Assignment.create(Reference(arg1), zero.copy()) # Create Schedule with Return at the start. kschedule = KernelSchedule.create( "work1", symbol_table, [Return(), assign1, assign2]) Profiler.add_profile_nodes(kschedule, Loop) # No profiling should have been added assert not kschedule.walk(ProfileNode) _, err = capsys.readouterr() assert ("Not adding profiling to routine 'work1' because it contains one " "or more Return statements" in err) # Create Schedule with Return in the middle. kschedule = KernelSchedule.create( "work2", symbol_table, [assign1.copy(), Return(), assign2.copy()]) Profiler.add_profile_nodes(kschedule, Loop) # No profiling should have been added assert not kschedule.walk(ProfileNode) _, err = capsys.readouterr() assert ("Not adding profiling to routine 'work2' because it contains one " "or more Return statements" in err) # Create Schedule with a Return at the end as well as in the middle. kschedule = KernelSchedule.create( "work3", symbol_table, [assign1.copy(), Return(), assign2.copy(), Return()]) Profiler.add_profile_nodes(kschedule, Loop) # No profiling should have been added assert not kschedule.walk(ProfileNode) _, err = capsys.readouterr() assert ("Not adding profiling to routine 'work3' because it contains one " "or more Return statements" in err)
def test_lower_named_profile_node(): ''' Test that the lower_to_language_level method behaves as expected when a ProfileNode has pre-set names for the module and region. ''' Profiler.set_options([Profiler.INVOKES]) symbol_table = SymbolTable() arg1 = symbol_table.new_symbol(symbol_type=DataSymbol, datatype=REAL_TYPE) assign1 = Assignment.create(Reference(arg1), Literal("0.0", REAL_TYPE)) kschedule = KernelSchedule.create("work1", symbol_table, [assign1, Return()]) Profiler.add_profile_nodes(kschedule, Loop) pnode = kschedule.walk(ProfileNode)[0] # Manually set the module and region names (to save using a transformation) pnode._module_name = "my_mod" pnode._region_name = "first" kschedule.lower_to_language_level() cblocks = kschedule.walk(CodeBlock) assert ("PreStart('my_mod', 'first', 0, 0)" in str(cblocks[0].get_ast_nodes[0]))
''' from __future__ import print_function from psyclone.psyir.nodes import Literal, KernelSchedule, Container, \ StructureReference, ArrayOfStructuresReference, Assignment, \ BinaryOperation, Range from psyclone.psyir.symbols import DataSymbol, SymbolTable, StructureType, \ ContainerSymbol, ArgumentInterface, ScalarType, ArrayType, TypeSymbol, \ GlobalInterface, INTEGER_TYPE, INTEGER4_TYPE, INTEGER8_TYPE, \ 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()
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( self._psy_data_symbol_with_prefix).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)