Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Global Reduction (towards #2381) #2476

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
97 changes: 97 additions & 0 deletions src/psyclone/domain/lfric/lfric_global_reduction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# -----------------------------------------------------------------------------
# BSD 3-Clause License
#
# Copyright (c) 2017-2024, Science and Technology Facilities Council.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------
# Authors R. W. Ford, A. R. Porter and S. Siso, STFC Daresbury Lab
# Modified I. Kavcic, A. Coughtrie, L. Turner and O. Brunt, Met Office
# Modified J. Henrichs, Bureau of Meteorology
# Modified A. B. G. Chalk and N. Nobre, STFC Daresbury Lab

''' This module contains the LFRic-specific Global Reduction
node implementation.'''

from psyclone.core import AccessType
from psyclone.f2pygen import CommentGen
from psyclone.psyir.nodes.node import Node
from psyclone.psyir.nodes.statement import Statement

class LFRicGlobalReduction(GlobalReduction):
'''
LFRic-specific global reduction class which can be added to and
manipulated in a schedule.

:param scalar: the kernel argument for which to perform a global reduction.
:type scalar: :py:class:`psyclone.dynamo0p3.DynKernelArgument`
:param parent: the parent node of this node in the PSyIR.
:type parent: :py:class:`psyclone.psyir.nodes.Node`

:raises GenerationError: if distributed memory is not enabled.
:raises GenerationError: if the scalar is not of "real" intrinsic type.

'''
def __init__(self, scalar, parent=None):
# Check that distributed memory is enabled
if not Config.get().distributed_memory:
raise GenerationError(
"It makes no sense to create an LFRicGlobalReduction "
"object when distributed memory is not enabled (dm=False).")
# Check scalar intrinsic types that this class supports (only
# "real" for now)
if scalar.intrinsic_type != "real":
raise GenerationError(
f"LFRicGlobalReduction currently only supports real scalars, "
f"but argument '{scalar.name}' in Kernel '{scalar.call.name}'"
f" has '{scalar.intrinsic_type}' intrinsic type.")
# Initialise the parent class
super().__init__(scalar, parent=parent)

def gen_code(self, parent):
'''
LFRic-specific code generation for this class.

:param parent: f2pygen node to which to add AST nodes.
:type parent: :py:class:`psyclone.f2pygen.SubroutineGen`

'''
name = self._scalar.name
# Use InvokeSchedule SymbolTable to share the same symbol for all
# GlobalSums in the Invoke.
sum_name = self.ancestor(InvokeSchedule).symbol_table.\
find_or_create_tag("global_sum").name
sum_type = self._scalar.data_type
sum_mod = self._scalar.module_name
parent.add(UseGen(parent, name=sum_mod, only=True,
funcnames=[sum_type]))
parent.add(TypeDeclGen(parent, datatype=sum_type,
entity_decls=[sum_name]))
parent.add(AssignGen(parent, lhs=sum_name+"%value", rhs=name))
parent.add(AssignGen(parent, lhs=name, rhs=sum_name+"%get_sum()"))
82 changes: 82 additions & 0 deletions src/psyclone/domain/lfric/lfric_global_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# -----------------------------------------------------------------------------
# BSD 3-Clause License
#
# Copyright (c) 2017-2024, Science and Technology Facilities Council.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------
# Authors R. W. Ford, A. R. Porter and S. Siso, STFC Daresbury Lab
# Modified I. Kavcic, A. Coughtrie, L. Turner and O. Brunt, Met Office
# Modified J. Henrichs, Bureau of Meteorology
# Modified A. B. G. Chalk and N. Nobre, STFC Daresbury Lab

''' This module contains the LFRic-specific Global Reduction
node implementation.'''

from psyclone.core import AccessType
from psyclone.f2pygen import CommentGen
from psyclone.psyir.nodes.node import Node
from psyclone.psyir.nodes.statement import Statement

class LFRicGlobalSum(LFRicGlobalReduction):
'''
LFRic-specific global sum class which can be added to and
manipulated in a schedule.

:param scalar: the kernel argument for which to perform a global sum.
:type scalar: :py:class:`psyclone.dynamo0p3.DynKernelArgument`
:param parent: the parent node of this node in the PSyIR.
:type parent: :py:class:`psyclone.psyir.nodes.Node`

'''
def __init__(self, scalar, parent=None):
# Initialise the parent class
super().__init__(scalar, parent=parent)

def gen_code(self, parent):
'''
LFRic-specific code generation for this class.

:param parent: f2pygen node to which to add AST nodes.
:type parent: :py:class:`psyclone.f2pygen.SubroutineGen`

'''
name = self._scalar.name
# Use InvokeSchedule SymbolTable to share the same symbol for all
# GlobalSums in the Invoke.
sum_name = self.ancestor(InvokeSchedule).symbol_table.\
find_or_create_tag("global_sum").name
sum_type = self._scalar.data_type
sum_mod = self._scalar.module_name
parent.add(UseGen(parent, name=sum_mod, only=True,
funcnames=[sum_type]))
parent.add(TypeDeclGen(parent, datatype=sum_type,
entity_decls=[sum_name]))
parent.add(AssignGen(parent, lhs=sum_name+"%value", rhs=name))
parent.add(AssignGen(parent, lhs=name, rhs=sum_name+"%get_sum()"))
65 changes: 3 additions & 62 deletions src/psyclone/dynamo0p3.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@
from psyclone.parse.kernel import getkerneldescriptors
from psyclone.parse.utils import ParseError
from psyclone.psyGen import (PSy, InvokeSchedule, Arguments,
KernelArgument, HaloExchange, GlobalSum,
DataAccess)
KernelArgument, HaloExchange, DataAccess)
from psyclone.psyir.frontend.fortran import FortranReader
from psyclone.psyir.nodes import Reference, ACCEnterDataDirective, ScopingNode
from psyclone.psyir.nodes import (ACCEnterDataDirective, GlobalReduction,
Reference, ScopingNode)
from psyclone.psyir.symbols import (INTEGER_TYPE, DataSymbol, ScalarType,
UnresolvedType, DataTypeSymbol,
ContainerSymbol, ImportInterface,
Expand Down Expand Up @@ -4505,65 +4505,6 @@ def node_str(self, colour=True):
"', dm=" + str(Config.get().distributed_memory)+"]")


class DynGlobalSum(GlobalSum):
'''
Dynamo specific global sum class which can be added to and
manipulated in a schedule.

:param scalar: the kernel argument for which to perform a global sum.
:type scalar: :py:class:`psyclone.dynamo0p3.DynKernelArgument`
:param parent: the parent node of this node in the PSyIR.
:type parent: :py:class:`psyclone.psyir.nodes.Node`

:raises GenerationError: if distributed memory is not enabled.
:raises InternalError: if the supplied argument is not a scalar.
:raises GenerationError: if the scalar is not of "real" intrinsic type.

'''
def __init__(self, scalar, parent=None):
# Check that distributed memory is enabled
if not Config.get().distributed_memory:
raise GenerationError(
"It makes no sense to create a DynGlobalSum object when "
"distributed memory is not enabled (dm=False).")
# Check that the global sum argument is indeed a scalar
if not scalar.is_scalar:
raise InternalError(
f"DynGlobalSum.init(): A global sum argument should be a "
f"scalar but found argument of type '{scalar.argument_type}'.")
# Check scalar intrinsic types that this class supports (only
# "real" for now)
if scalar.intrinsic_type != "real":
raise GenerationError(
f"DynGlobalSum currently only supports real scalars, but "
f"argument '{scalar.name}' in Kernel '{scalar.call.name}' has "
f"'{scalar.intrinsic_type}' intrinsic type.")
# Initialise the parent class
super().__init__(scalar, parent=parent)

def gen_code(self, parent):
'''
Dynamo-specific code generation for this class.

:param parent: f2pygen node to which to add AST nodes.
:type parent: :py:class:`psyclone.f2pygen.SubroutineGen`

'''
name = self._scalar.name
# Use InvokeSchedule SymbolTable to share the same symbol for all
# GlobalSums in the Invoke.
sum_name = self.ancestor(InvokeSchedule).symbol_table.\
find_or_create_tag("global_sum").name
sum_type = self._scalar.data_type
sum_mod = self._scalar.module_name
parent.add(UseGen(parent, name=sum_mod, only=True,
funcnames=[sum_type]))
parent.add(TypeDeclGen(parent, datatype=sum_type,
entity_decls=[sum_name]))
parent.add(AssignGen(parent, lhs=sum_name+"%value", rhs=name))
parent.add(AssignGen(parent, lhs=name, rhs=sum_name+"%get_sum()"))


def _create_depth_list(halo_info_list, sym_table):
'''Halo exchanges may have more than one dependency. This method
simplifies multiple dependencies to remove duplicates and any
Expand Down
Loading