Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
172c205
Initial implementation of maximal parallel region trans. Tests TODO
LonelyCat124 Oct 31, 2025
8b19ef0
Tests for base MaximalParallelRegionTrans class
LonelyCat124 Oct 31, 2025
38ef3e6
Missed __init__ updates
LonelyCat124 Oct 31, 2025
586c51a
Merge branch 'master' into 3157_parallel_routine_trans
LonelyCat124 Nov 21, 2025
1b86da5
Changes to fix unnecessary parallel additions and to recurse correctl…
LonelyCat124 Nov 24, 2025
f3fa2a3
Fixed some of the behaviour to improve barrier removal
LonelyCat124 Nov 26, 2025
cbdde6d
linting
LonelyCat124 Nov 26, 2025
0617319
Update the test to handle the new required node structure
LonelyCat124 Nov 26, 2025
d76e988
Merge branch 'master' into 3157_parallel_routine_trans
LonelyCat124 Dec 10, 2025
bdc9f12
Added tests
LonelyCat124 Jan 12, 2026
1db7f8d
Merged master
LonelyCat124 Jan 12, 2026
67ae4c0
Fix test overlaps
LonelyCat124 Jan 12, 2026
a275f48
Update the example
LonelyCat124 Jan 12, 2026
f711ae9
Missing init update
LonelyCat124 Jan 12, 2026
fef6b0b
merged master and updated as needed
LonelyCat124 Jan 23, 2026
c15d552
First changes towards review
LonelyCat124 Jan 27, 2026
321893c
Changes for review
LonelyCat124 Jan 29, 2026
44c7a94
linting
LonelyCat124 Jan 29, 2026
e521436
linting
LonelyCat124 Jan 29, 2026
3002bd6
Changes for coverage
LonelyCat124 Jan 30, 2026
5395733
Merge branch 'master' into 3157_parallel_routine_trans
LonelyCat124 Jan 30, 2026
67bac6f
changes for review
LonelyCat124 Feb 12, 2026
8c517b6
Merge branch 'master' into 3157_parallel_routine_trans
LonelyCat124 Feb 12, 2026
f780562
linting error
LonelyCat124 Feb 12, 2026
e5db5eb
Missing coverage
LonelyCat124 Feb 13, 2026
4591a99
Merge branch 'master' into 3157_parallel_routine_trans
LonelyCat124 Feb 13, 2026
eba4099
Updates for review
LonelyCat124 Feb 19, 2026
18af34f
linting
LonelyCat124 Feb 19, 2026
4b41c17
Removed print statement
LonelyCat124 Feb 19, 2026
7339d56
Merge remote-tracking branch 'origin/master' into 3157_parallel_routi…
sergisiso Feb 20, 2026
7f2618b
#3157 Update changelog
sergisiso Feb 20, 2026
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
4 changes: 4 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
51) PR #3205 for #3306 and towards #3157. Initial implementation of
a generic MaximalRegionTrans and specialisations to create spanning
OMPParallel regions and NEMO Profiling regions.

50) PR #3338 for #1651. Fixes Reference2ArrayRange issues with
capitalisation and test nested WHEREs.

Expand Down
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)
96 changes: 40 additions & 56 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, IfBlock)
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,40 @@ def add_profiling(children: Union[List[Node], Schedule]):
attempt to add profiling regions.

'''
class MaximalProfilingOutsideDirectivesTrans(MaximalRegionTrans):
'''Applies Profiling to the largest possible region outside of
directive regions.

:param routine_name: The name of the Routine being profiled.
'''
# We purposely don't encompase Directive, or Return statements
# (which would create unclosed hooks).
_allowed_contiguous_statements = (Assignment, Call, CodeBlock)
_transformation = ProfileTrans

def _satisfies_minimum_region_rules(self, region: list[Node]) -> bool:
Comment thread
sergisiso marked this conversation as resolved.
'''Returns whether the provided node list satisfies the
requirements to create a region for the ProfileTrans.

:param region: The candidate region to have the transformation
applied.
:returns: whether the provided node list should have profiling
applied.
'''
if len(region) == 1:
if (isinstance(region[0], CodeBlock) and
len(region[0].get_ast_nodes) == 1):
# Don't create profiling regions for CodeBlocks consisting
# of a single statement.
return False
if (isinstance(region[0], IfBlock) and
"was_single_stmt" in region[0].annotations and
isinstance(region[0].if_body[0], CodeBlock)):
# We also don't put single statements consisting of
# 'If(condition) call blah()' inside profiling regions.
return False
return super()._satisfies_minimum_region_rules(region)

if children and isinstance(children, Schedule):
# If we are given a Schedule, we look at its children.
children = children.children
Expand All @@ -486,56 +520,6 @@ def add_profiling(children: Union[List[Node], Schedule]):
parent_routine = children[0].ancestor(Routine)
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
routine_name = parent_routine.name if parent_routine else ""
if routine_name not in PROFILING_IGNORE:
MaximalProfilingOutsideDirectivesTrans().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 @@ -182,6 +187,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_contiguous_statements = (
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)
Comment thread
sergisiso marked this conversation as resolved.
Loading
Loading