Reference Guide  2.5.0
create_nemo_psy_trans.py
1 # -----------------------------------------------------------------------------
2 # BSD 3-Clause License
3 #
4 # Copyright (c) 2021-2024, Science and Technology Facilities Council.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 #
10 # * Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
12 #
13 # * Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 #
17 # * Neither the name of the copyright holder nor the names of its
18 # contributors may be used to endorse or promote products derived from
19 # this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 # -----------------------------------------------------------------------------
34 # Author: A. R. Porter, STFC Daresbury Lab
35 # Modified: R. W. Ford, S. Siso and N. Nobre, STFC Daresbury Lab
36 
37 
38 '''Module providing a transformation from a generic PSyIR representation of
39  a PSy layer into a NEMO-specific one.
40 
41 '''
42 
43 from psyclone.transformations import Transformation, TransformationError
44 from psyclone.psyir.nodes import Routine, Loop, Node
46  CreateNemoInvokeScheduleTrans, \
47  CreateNemoLoopTrans
48 
49 
50 class CreateNemoPSyTrans(Transformation):
51  """
52  Transform generic (language-level) PSyIR representation into a PSyclone
53  version with specialised, NEMO-specific nodes. For example:
54 
55  >>> from psyclone.psyir.frontend.fortran import FortranReader
56  >>> from psyclone.psyir.nodes import Loop
57  >>> from psyclone.domain.nemo.transformations import CreateNemoPSyTrans
58  >>> code = '''
59  ... subroutine sub()
60  ... integer :: ji, tmp(10)
61  ... do ji=1, 10
62  ... tmp(ji) = 2*ji
63  ... end do
64  ... end subroutine sub'''
65  >>> psyir = FortranReader().psyir_from_source(code)
66  >>> loop = psyir.walk(Loop)[0]
67  >>> trans = CreateNemoPSyTrans()
68  >>> trans.apply(psyir)
69  >>> print(psyir.view(colour=False, indent=" "))
70  FileContainer[]
71  NemoInvokeSchedule[invoke='sub']
72  0: Loop[type='lon', field_space='None', it_space='None']
73  Literal[value:'1', Scalar<INTEGER, UNDEFINED>]
74  Literal[value:'10', Scalar<INTEGER, UNDEFINED>]
75  Literal[value:'1', Scalar<INTEGER, UNDEFINED>]
76  Schedule[]
77  0: Assignment[]
78  ArrayReference[name:'tmp']
79  Reference[name:'ji']
80  BinaryOperation[operator:'MUL']
81  Literal[value:'2', Scalar<INTEGER, UNDEFINED>]
82  Reference[name:'ji']
83  <BLANKLINE>
84 
85  The result of this transformation is that the root `Routine` has
86  been converted into a `NemoInvokeSchedule`, the `Loop` is now a
87  `NemoLoop` (with type 'lon' [for longitude]) and the body of the loop
88  is now an `InlinedKern`.
89 
90  """
91  @property
92  def name(self):
93  '''
94  :returns: the name of the transformation.
95  :rtype: str
96 
97  TODO #1214 remove this method.
98 
99  '''
100  return type(self).__name__
101 
102  def validate(self, node, options=None):
103  '''
104  Check that the supplied node is a valid target for this transformation.
105 
106  :param node: the root of the PSyIR tree to be transformed.
107  :type node: :py:class:`psyclone.psyir.nodes.Node`
108  :param options: a dictionary with options for \
109  transformations. No options are used in this \
110  transformation. This is an optional argument that defaults \
111  to None.
112  :type options: Optional[Dict[str, Any]]
113 
114  :raises TransformationError: if the supplied node is not a PSyIR node.
115 
116  '''
117  if not isinstance(node, Node):
118  raise TransformationError(
119  f"Error in CreateNemoPSyTrans transformation. The supplied "
120  f"node should be a PSyIR Node but found "
121  f"'{type(node).__name__}'")
122 
123  super(CreateNemoPSyTrans, self).validate(node, options=options)
124 
125  def apply(self, psyir, options=None):
126  '''
127  Takes generic PSyIR and replaces recognised structures with
128  NEMO-specific PSyIR (in-place). Note that this may mean replacing
129  the top-level node itself and therefore this routine returns the
130  root of the modified tree.
131 
132  :param psyir: the root node of the PSyIR tree to process.
133  :type psyir: :py:class:`psyclone.psyir.nodes.Node`
134  :param options: a dictionary with options for \
135  transformations. No options are used in this \
136  transformation. This is an optional argument that defaults \
137  to None.
138  :type options: Optional[Dict[str, Any]]
139 
140  '''
141  self.validatevalidate(psyir, options=options)
142 
143  invoke_trans = CreateNemoInvokeScheduleTrans()
144  loop_trans = CreateNemoLoopTrans()
145 
146  # Since the transformations replace nodes in the tree, we apply
147  # them 'depth first':
148 
149  loops = psyir.walk(Loop)
150  # Reverse the list so that we transform the deepest loop bodies first
151  # so as to try to reduce repeated walking of the tree.
152  loops.reverse()
153 
154  # First, transform generic Loops into NemoLoops
155  for loop in loops:
156  loop_trans.apply(loop)
157 
158  # Second, transform any Routines into NemoInvokeSchedules. Have to
159  # allow for the supplied top-level node being a Routine and therefore
160  # being replaced.
161  for routine in psyir.walk(Routine):
162  invoke_trans.apply(routine)
163 
164 
165 # For AutoAPI documentation generation
166 __all__ = ['CreateNemoPSyTrans']