Reference Guide  2.5.0
lfric_collection.py
1 # -----------------------------------------------------------------------------
2 # BSD 3-Clause License
3 #
4 # Copyright (c) 2017-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 # Authors R. W. Ford, A. R. Porter and S. Siso, STFC Daresbury Lab
35 # Modified I. Kavcic, A. Coughtrie and L. Turner, Met Office
36 # Modified J. Henrichs, Bureau of Meteorology
37 # Modified A. B. G. Chalk and N. Nobre, STFC Daresbury Lab
38 
39 ''' This module implements PSyclone LFRic (Dynamo 0.3) API by specialising the
40  base class for managing the declaration and initialisation of a group of
41  related entities within an Invoke or Kernel stub.'''
42 
43 # Imports
44 import abc
45 from psyclone.domain.lfric import (LFRicSymbolTable, LFRicInvoke,
46  LFRicKern)
47 from psyclone.errors import InternalError
48 
49 
51  '''
52  Base class for managing the declaration and initialisation of a
53  group of related entities within an Invoke or Kernel stub
54 
55  :param node: the Kernel or Invoke for which to manage variable \
56  declarations and initialisation.
57  :type node: :py:class:`psyclone.domain.lfric.LFRicInvoke` or \
58  :py:class:`psyclone.domain.lfric.LFRicKern`
59 
60  :raises InternalError: if the supplied node is not an LFRicInvoke or an \
61  LFRicKern.
62 
63  '''
64  def __init__(self, node):
65  if isinstance(node, LFRicInvoke):
66  # We are handling declarations/initialisations for an Invoke
67  self._invoke_invoke = node
68  self._kernel_kernel = None
69  self._symbol_table_symbol_table = self._invoke_invoke.schedule.symbol_table
70  # The list of Kernel calls we are responsible for
71  self._calls_calls = node.schedule.kernels()
72  elif isinstance(node, LFRicKern):
73  # We are handling declarations for a Kernel stub
74  self._invoke_invoke = None
75  self._kernel_kernel = node
76  # TODO #719 The symbol table is not connected to other parts of
77  # the Stub generation.
78  self._symbol_table_symbol_table = LFRicSymbolTable()
79  # We only have a single Kernel call in this case
80  self._calls_calls = [node]
81  else:
82  raise InternalError(f"LFRicCollection takes only an LFRicInvoke "
83  f"or an LFRicKern but got: {type(node)}")
84 
85  # Whether or not the associated Invoke contains only Kernels that
86  # operate on DoFs.
87  if self._invoke_invoke:
88  self._dofs_only_dofs_only = self._invoke_invoke.operates_on_dofs_only
89  else:
90  self._dofs_only_dofs_only = False
91 
92  def declarations(self, parent):
93  '''
94  Insert declarations for all necessary variables into the AST of
95  the generated code. Simply calls either '_invoke_declarations()' or
96  '_stub_declarations()' depending on whether we're handling an Invoke
97  or a Kernel stub.
98 
99  :param parent: the node in the f2pygen AST representing the routine \
100  in which to insert the declarations.
101  :type parent: :py:class:`psyclone.f2pygen.SubroutineGen`
102 
103  :raises InternalError: if neither 'self._invoke' nor 'self._kernel' \
104  are set.
105 
106  '''
107  if self._invoke_invoke:
108  self._invoke_declarations_invoke_declarations(parent)
109  elif self._kernel_kernel:
110  self._stub_declarations_stub_declarations(parent)
111  else:
112  raise InternalError("LFRicCollection has neither a Kernel "
113  "nor an Invoke - should be impossible.")
114 
115  def initialise(self, parent):
116  '''
117  Add code to initialise the entities being managed by this class.
118  We do nothing by default - it is up to the sub-class to override
119  this method if initialisation is required.
120 
121  :param parent: the node in the f2pygen AST to which to add \
122  initialisation code.
123  :type parent: :py:class:`psyclone.f2pygen.SubroutineGen`
124 
125  '''
126 
127  @abc.abstractmethod
128  def _invoke_declarations(self, parent):
129  '''
130  Add all necessary declarations for an Invoke.
131 
132  :param parent: node in the f2pygen AST representing the Invoke to \
133  which to add declarations.
134  :type parent: :py:class:`psyclone.f2pygen.SubroutineGen`
135 
136  '''
137 
138  def _stub_declarations(self, parent):
139  '''
140  Add all necessary declarations for a Kernel stub. Not abstract because
141  not all entities need representing within a Kernel.
142 
143  :param parent: node in the f2pygen AST representing the Kernel stub \
144  to which to add declarations.
145  :type parent: :py:class:`psyclone.f2pygen.SubroutineGen`
146 
147  '''
148 
149 
150 # ---------- Documentation utils -------------------------------------------- #
151 # The list of module members that we wish AutoAPI to generate
152 # documentation for (see https://psyclone-ref.readthedocs.io).
153 __all__ = ['LFRicCollection']