-
Notifications
You must be signed in to change notification settings - Fork 0
/
acc_loops_trans.py
executable file
·154 lines (133 loc) · 6.39 KB
/
acc_loops_trans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# -----------------------------------------------------------------------------
# BSD 3-Clause License
#
# Copyright (c) 2023-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: S. Siso, STFC Daresbury Lab
''' PSyclone transformation script showing the introduction of OpenACC loop
directives into Nemo code. '''
from utils import insert_explicit_loop_parallelism, normalise_loops, \
enhance_tree_information, add_profiling
from psyclone.psyir.nodes import Call, Loop
from psyclone.transformations import ACCParallelTrans, ACCLoopTrans
from psyclone.transformations import ACCRoutineTrans
from psyclone.psyir.transformations import InlineTrans, LoopFuseTrans
from psyclone.domain.common.transformations import KernelModuleInlineTrans
PROFILING_ENABLED = False
def trans(psy):
''' Add OpenACC Parallel and Loop directives to all loops, including the
implicit ones, to parallelise the code and execute it in an acceleration
device.
:param psy: the PSy object which this script will transform.
:type psy: :py:class:`psyclone.psyGen.PSy`
:returns: the transformed PSy object.
:rtype: :py:class:`psyclone.psyGen.PSy`
'''
acc_region_trans = ACCParallelTrans(default_present=False)
acc_loop_trans = ACCLoopTrans()
print(f"Invokes found in {psy.name}:")
for invoke in psy.invokes.invoke_list:
print(invoke.name)
if PROFILING_ENABLED:
add_profiling(invoke.schedule.children)
# TODO #2317: Has structure accesses that can not be offloaded and has
# a problematic range to loop expansion of (1:1)
if psy.name.startswith("psy_obs_"):
print("Skipping", invoke.name)
continue
# TODO #1841: These files have a bug in the array-range-to-loop
# transformation. One leads to the following compiler error
# NVFORTRAN-S-0083-Vector expression used where scalar expression
# required, the other to an incorrect result.
if invoke.name in ("trc_oce_rgb", ):
print("Skipping", invoke.name)
continue
# This are functions with scalar bodies, we don't want to parallelise
# them, but we could:
# - Inine them
# - Annotate them with 'omp declare target' and allow to call from gpus
if invoke.name in ("q_sat", "sbc_dcy", "gamma_moist", "cd_neutral_10m",
"psi_h", "psi_m"):
print("Skipping", invoke.name)
continue
# OpenACC fails in the following routines with the Compiler error:
# Could not find allocated-variable index for symbol - xxx
# This all happen on characters arrays, e.g. cd_nat
if invoke.name in ("lbc_nfd_2d_ptr", "lbc_nfd_3d_ptr",
"lbc_nfd_4d_ptr", "bdy_dyn"):
print("Skipping", invoke.name)
continue
enhance_tree_information(invoke.schedule)
inline_trans = InlineTrans()
kern_in_trans = KernelModuleInlineTrans()
inlined_syms = []
for call in invoke.schedule.walk(Call):
if(call.routine.name == "exp_v" or call.routine.name == "sqrt_v" or call.routine.name == "rescale_tau_omega"):
try:
kern_in_trans.apply(call)
inlined_syms.append(call.routine)
except Exception as err:
pass
try:
inline_trans.apply(call)
except Exception as err:
if(call.routine.name == "exp_v"):
print("Failed to inline")
print(err)
elif call.routine.name == "rescale_tau_omega":
print("Failed to inline rescale_tau_omega")
print(err)
normalise_loops(
invoke.schedule,
hoist_local_arrays=True,
convert_array_notation=True,
convert_range_loops=True,
hoist_expressions=True
)
# For performance in lib_fortran, mark serial routines as GPU-enabled
if psy.name == "psy_lib_fortran_psy":
if not invoke.schedule.walk(Loop):
calls = invoke.schedule.walk(Call)
if all(call.is_available_on_device() for call in calls):
# SIGN_ARRAY_1D has a CodeBlock because of a WHERE without
# array notation. (TODO #717)
ACCRoutineTrans().apply(invoke.schedule,
options={"force": True})
continue
insert_explicit_loop_parallelism(
invoke.schedule,
region_directive_trans=acc_region_trans,
loop_directive_trans=acc_loop_trans,
# Collapse is necessary to give GPUs enough parallel items
collapse=True,
)
return psy