Reference Guide  2.5.0
adjoint_trans.py
1 # BSD 3-Clause License
2 #
3 # Copyright (c) 2021-2024, Science and Technology Facilities Council.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # * Redistributions of source code must retain the above copyright notice, this
10 # list of conditions and the following disclaimer.
11 #
12 # * Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation
14 # and/or other materials provided with the distribution.
15 #
16 # * Neither the name of the copyright holder nor the names of its
17 # contributors may be used to endorse or promote products derived from
18 # this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 # -----------------------------------------------------------------------------
33 # Authors: R. W. Ford, A. R. Porter, N. Nobre and S. Siso, STFC Daresbury Lab
34 
35 '''This module contains an abstract parent class for adjoint
36 transformations.
37 
38 '''
39 from psyclone.psyGen import Transformation
40 from psyclone.psyir.symbols import DataSymbol
41 
42 # AdjointTransformation is purposefully abstract. It does not
43 # implement the validate or apply methods and instead provides
44 # initialisation that all subclasses can benefit from. Therefore we
45 # disable the pylint warning here.
46 # pylint: disable=abstract-method
47 
48 # We make use of the form of super that works with both Python2 and
49 # Python3 here so disable the pylint warning about using a Python3
50 # specific version of super.
51 # pylint: disable=super-with-arguments
52 
53 
55  '''An abstract class for Adjoint transformations. Requires a list of
56  active variables to be passed when creating an instance of the
57  class. Also supports an optional writer argument.
58 
59  :param active_variables: a list of names of the active variables.
60  :type active_variables: list of \
61  :py:class:`psyclone.psyir.symbols.DataSymbol`
62 
63  :raises TypeError: if the active_variables are of the wrong type.
64 
65  '''
66  def __init__(self, active_variables):
67  super(AdjointTransformation, self).__init__()
68 
69  if not isinstance(active_variables, list):
70  raise TypeError(
71  f"The active variables argument should be a list, but found "
72  f"'{type(active_variables).__name__}'.")
73 
74  if not active_variables:
75  raise TypeError("There should be at least one active variable.")
76 
77  for active_variable in active_variables:
78  if not isinstance(active_variable, DataSymbol):
79  raise TypeError(
80  f"Active variables should be of type DataSymbol, but "
81  f"found '{type(active_variable).__name__}'.")
82 
83  # A list of active variables.
84  self._active_variables_active_variables = active_variables
85 
86 
87 # =============================================================================
88 # Documentation utils: The list of module members that we wish AutoAPI to
89 # generate documentation for (see https://psyclone-ref.readthedocs.io).
90 __all__ = ["AdjointTransformation"]