Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 5 additions & 51 deletions examples/nemo/eg7/openmp_cpu_nowait_trans.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,67 +42,21 @@
OMPLoopTrans,
OMPMinimiseSyncTrans,
TransformationError,
OMPParallelTrans
MaximalOMPParallelRegionTrans
)
from psyclone.psyir.nodes import (
Assignment,
Directive,
IfBlock,
Loop,
OMPBarrierDirective,
OMPDoDirective,
Routine,
)


def add_parallel_region_to_contiguous_directives(schedule):
'''Adds OMPParallelDirective nodes around areas of the schedule with
contiguous OpenMP directives.

:param schedule: The Schedule to add OpenMPParallelDirectives to.
:type schedule: :py:class:`psyclone.nodes.Schedule`
'''
par_trans = OMPParallelTrans()
start = -1
end = -1
sets = []
# Loop through the children, if its an OpenMP directive add it
# to the current set
for child in schedule:
if isinstance(child, (OMPDoDirective, OMPBarrierDirective)):
if start < 0:
start = child.position
end = child.position + 1
else:
# If we have a non OMPDodirective/OMPBarrierDirective then add
# an OMPParallelDirective if needed.
if start >= 0:
sets.append((start, end))
start = -1
end = -1
# Recurse appropriately to sub schedules:
if isinstance(child, Loop):
add_parallel_region_to_contiguous_directives(child.loop_body)
elif isinstance(child, IfBlock):
add_parallel_region_to_contiguous_directives(child.if_body)
if child.else_body:
add_parallel_region_to_contiguous_directives(
child.else_body
)
# If we get to the end and need to enclose some nodes in a parallel
# directive we do it now
if start >= 0:
sets.append((start, end))

for subset in sets[::-1]:
par_trans.apply(schedule[subset[0]:subset[1]])


def trans(psyir):
''' Adds OpenMP Loop directives with nowait to Nemo loops over levels.
This is followed by applying OpenMP parallel directives as required,
before removing barriers where possible.

This is followed by applying OpenMP parallel directives as required
with the OMPMaximalParallelRegionTrans, before removing barriers where
possible.

:param psyir: the PSyIR of the provided file.
:type psyir: :py:class:`psyclone.psyir.nodes.FileContainer`
Expand Down Expand Up @@ -130,5 +84,5 @@ def trans(psyir):
# Apply the largest possible parallel regions and remove any barriers that
# can be removed.
for routine in psyir.walk(Routine):
add_parallel_region_to_contiguous_directives(routine)
MaximalOMPParallelRegionTrans().apply(routine)
minsync_trans.apply(routine)
64 changes: 9 additions & 55 deletions examples/nemo/scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@

from psyclone.domain.common.transformations import KernelModuleInlineTrans
from psyclone.psyir.nodes import (
Assignment, Loop, Directive, Node, Reference, CodeBlock, Call, Return,
IfBlock, Routine, Schedule, IntrinsicCall, StructureReference)
Assignment, Loop, Directive, Node, Reference, CodeBlock, Call,
Routine, Schedule, IntrinsicCall, StructureReference)
from psyclone.psyir.symbols import DataSymbol
from psyclone.psyir.transformations import (
ArrayAssignment2LoopsTrans, HoistLoopBoundExprTrans, HoistLocalArraysTrans,
HoistTrans, InlineTrans, Maxval2LoopTrans, ProfileTrans,
OMPMinimiseSyncTrans, Reference2ArrayRangeTrans,
ScalarisationTrans, IncreaseRankLoopArraysTrans)
ScalarisationTrans, IncreaseRankLoopArraysTrans, MaximalRegionTrans)
from psyclone.transformations import TransformationError

# USE statements to chase to gather additional symbol information.
Expand Down Expand Up @@ -474,6 +474,11 @@ def add_profiling(children: Union[List[Node], Schedule]):
attempt to add profiling regions.

'''
class MaximalProfilingTrans(MaximalRegionTrans):
'''Applies Profiling to the largest possible region.'''
_allowed_nodes = [Assignment, Call, CodeBlock]
_transformation = ProfileTrans

if children and isinstance(children, Schedule):
# If we are given a Schedule, we look at its children.
children = children.children
Expand All @@ -487,55 +492,4 @@ def add_profiling(children: Union[List[Node], Schedule]):
if parent_routine and parent_routine.return_symbol:
return

node_list = []
for child in children[:]:
# Do we want this node to be included in a profiling region?
if child.walk((Directive, Return)):
# It contains a directive or return statement so we put what we
# have so far inside a profiling region.
add_profile_region(node_list)
# A node that is not included in a profiling region marks the
# end of the current candidate region so reset the list.
node_list = []
# Now we go down a level and try again without attempting to put
# profiling below directives or within Assignments
if isinstance(child, IfBlock):
add_profiling(child.if_body)
add_profiling(child.else_body)
elif not isinstance(child, (Assignment, Directive)):
add_profiling(child.children)
else:
# We can add this node to our list for the current region
node_list.append(child)
add_profile_region(node_list)


def add_profile_region(nodes):
'''
Attempt to put the supplied list of nodes within a profiling region.

:param nodes: list of sibling PSyIR nodes to enclose.
:type nodes: list of :py:class:`psyclone.psyir.nodes.Node`

'''
if nodes:
# Check whether we should be adding profiling inside this routine
routine_name = nodes[0].ancestor(Routine).name.lower()
if any(ignore in routine_name for ignore in PROFILING_IGNORE):
return
if len(nodes) == 1:
if isinstance(nodes[0], CodeBlock) and \
len(nodes[0].get_ast_nodes) == 1:
# Don't create profiling regions for CodeBlocks consisting
# of a single statement
return
if isinstance(nodes[0], IfBlock) and \
"was_single_stmt" in nodes[0].annotations and \
isinstance(nodes[0].if_body[0], CodeBlock):
# We also don't put single statements consisting of
# 'IF(condition) CALL blah()' inside profiling regions
return
try:
ProfileTrans().apply(nodes)
except TransformationError:
pass
MaximalProfilingTrans.apply(children)
7 changes: 7 additions & 0 deletions src/psyclone/psyir/transformations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
from psyclone.psyir.transformations.loop_trans import LoopTrans
from psyclone.psyir.transformations.value_range_check_trans import (
ValueRangeCheckTrans)
from psyclone.psyir.transformations.maximal_region_trans import (
MaximalRegionTrans)
from psyclone.psyir.transformations.omp_critical_trans import (
OMPCriticalTrans)
from psyclone.psyir.transformations.omp_loop_trans import OMPLoopTrans
Expand Down Expand Up @@ -126,6 +128,9 @@
OMPTaskloopTrans
from psyclone.psyir.transformations.omp_declare_target_trans import \
OMPDeclareTargetTrans
from psyclone.psyir.transformations.maximal_omp_parallel_region_trans import (
MaximalOMPParallelRegionTrans
)
from psyclone.psyir.transformations.omp_parallel_trans import (
OMPParallelTrans,
)
Expand Down Expand Up @@ -179,6 +184,8 @@
"ParallelRegionTrans",
"OMPTaskloopTrans",
"OMPDeclareTargetTrans",
"MaximalRegionTrans",
"OMPCriticalTrans",
"MaximalOMPParallelRegionTrans",
"OMPParallelTrans",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# -----------------------------------------------------------------------------
# BSD 3-Clause License
#
# Copyright (c) 2017-2025, 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 A. B. G. Chalk, STFC Daresbury Lab

'''This module contains the MaximalOMPParallelRegionTrans.'''

from typing import Union

from psyclone.psyir.nodes import (
OMPTaskwaitDirective,
OMPBarrierDirective,
OMPSerialDirective,
OMPTaskloopDirective,
OMPDoDirective,
OMPLoopDirective,
OMPTaskDirective,
DynamicOMPTaskDirective,
Node,
Schedule
)
from psyclone.psyir.transformations.maximal_region_trans import (
MaximalRegionTrans)
from psyclone.psyir.transformations.omp_parallel_trans import OMPParallelTrans
from psyclone.utils import transformation_documentation_wrapper


@transformation_documentation_wrapper
class MaximalOMPParallelRegionTrans(MaximalRegionTrans):
'''Applies OpenMP Parallel directives around the largest possible sections
of the input.

At current, this will never place OpenMP parallel sections around
Assignments that are outside of another OpenMP directive. See #3157 and
the discussion on #3205 for more detail.'''
# The type of parallel transformation to be applied to the input region.
_transformation = OMPParallelTrans
# Tuple of statement nodes allowed inside the _transformation
_allowed_nodes = (
OMPTaskwaitDirective,
OMPBarrierDirective,
OMPSerialDirective,
OMPTaskloopDirective,
OMPDoDirective,
OMPLoopDirective,
OMPTaskDirective,
DynamicOMPTaskDirective,
)
_required_nodes = (
OMPSerialDirective,
OMPTaskloopDirective,
OMPDoDirective,
OMPLoopDirective,
OMPTaskDirective,
DynamicOMPTaskDirective,
)

def apply(self, nodes: Union[Node, Schedule, list[Node]], **kwargs):
'''Applies the transformation to the nodes provided.

:param nodes: can be a single node, a schedule or a list of nodes.
'''
super().apply(nodes, **kwargs)
Loading
Loading