Skip to content

Commit ca8610a

Browse files
authored
Merge pull request #3404 from stfc/3403_modernise_runme_examples
(closes #3403) Modernise runme examples
2 parents d6fd596 + 57fd060 commit ca8610a

14 files changed

Lines changed: 304 additions & 1590 deletions

changelog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
19) PR #3404 for 3403. Modernises the remaining runme examples.
2+
13
18) PR #3410 towards #2668. Update more Transformations to use kwargs.
24

35
17) PR #3427. Preserve directive format casing.

examples/gocean/eg1/Makefile

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,23 @@ run: compile
9191
@echo "No run targets for example gocean/eg1"
9292

9393
basic:
94-
$(ENV) ${PYTHON} ./runme.py
94+
${PSYCLONE} -api gocean -s print_schedule.py shallow_alg.f90 -oalg /dev/null -opsy /dev/null
9595

9696
openmp:
97-
$(ENV) ${PYTHON} ./runme_openmp.py
97+
${PSYCLONE} -nodm -api gocean -s fuse_and_insert_openmp.py shallow_alg.f90
9898

9999

100100
mpi:
101101
${PSYCLONE} -api gocean -I${INF_INC} shallow_alg.f90
102102

103103
loop_fuse:
104-
$(ENV) ${PYTHON} ./runme_loop_fuse.py
104+
${PSYCLONE} -nodm -api gocean -s fuse_loops.py shallow_alg.f90
105105

106106
dag:
107-
$(ENV) ${PYTHON} ./runme_dag.py
107+
${PSYCLONE} -api gocean -s generate_dag.py shallow_alg.f90 -oalg /dev/null -opsy /dev/null
108108

109109
openacc:
110-
$(ENV) ${PYTHON} ./runme_openacc.py
110+
${PSYCLONE} -nodm -api gocean -s fuse_and_insert_openacc.py shallow_alg.f90
111111

112112
opencl:
113-
${PSYCLONE} -nodm -s ./opencl_transformation.py \
114-
-api gocean -I${INF_INC} shallow_alg.f90
113+
${PSYCLONE} -nodm -s ./opencl_transformation.py -api gocean -I${INF_INC} shallow_alg.f90

examples/gocean/eg1/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@ PSyclone can be run in the directory containing this file by
1111
executing, e.g.
1212

1313
```sh
14-
python ./runme.py
14+
psyclone --psykal-dsl gocean -s <script> shallow_alg.f90
1515
```
1616

17-
Examine the runme*.py scripts themselves for further details or see the
18-
Makefile.
19-
2017

2118
## OpenMP tasking transformation script
2219

examples/gocean/eg1/dag.ipynb

Lines changed: 0 additions & 976 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# -----------------------------------------------------------------------------
2+
# BSD 3-Clause License
3+
#
4+
# Copyright (c) 2018-2026, Science and Technology Facilities Council.
5+
# All rights reserved.
6+
#
7+
# Redistribution and use in source and binary forms, with or without
8+
# modification, are permitted provided that the following conditions are met:
9+
#
10+
# * Redistributions of source code must retain the above copyright notice, this
11+
# list of conditions and the following disclaimer.
12+
#
13+
# * Redistributions in binary form must reproduce the above copyright notice,
14+
# this list of conditions and the following disclaimer in the documentation
15+
# and/or other materials provided with the distribution.
16+
#
17+
# * Neither the name of the copyright holder nor the names of its
18+
# contributors may be used to endorse or promote products derived from
19+
# this software without specific prior written permission.
20+
#
21+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
# POSSIBILITY OF SUCH DAMAGE.
33+
# -----------------------------------------------------------------------------
34+
# Authors: R. W. Ford, A. R. Porter and S. Siso, STFC Daresbury Lab
35+
36+
''' PSyclone script to fuse invoke_0 loops and insert OpenACC directives '''
37+
38+
from psyclone.psyir.nodes import FileContainer
39+
from psyclone.psyGen import InvokeSchedule
40+
from psyclone.psyGen import TransInfo
41+
from psyclone.psyir.transformations import ACCLoopTrans, LoopFuseTrans
42+
43+
44+
def trans(psyir: FileContainer):
45+
'''
46+
:param psyir: the PSyIR of the PSy-layer.
47+
48+
'''
49+
invokes = psyir.walk(InvokeSchedule)
50+
51+
# Print a list of all of the invokes found
52+
print([invoke.name for invoke in invokes])
53+
54+
trans_info = TransInfo()
55+
print(trans_info.list)
56+
fuse_trans = LoopFuseTrans()
57+
ptrans = trans_info.get_trans_name('ACCParallelTrans')
58+
dtrans = trans_info.get_trans_name('ACCEnterDataTrans')
59+
ltrans = ACCLoopTrans()
60+
61+
for invoke in invokes:
62+
if invoke.name == "invoke_0":
63+
# fuse all outer loops
64+
fuse_trans.apply(invoke.children[0], invoke.children[1])
65+
fuse_trans.apply(invoke.children[0], invoke.children[1])
66+
fuse_trans.apply(invoke.children[0], invoke.children[1])
67+
# fuse all inner loops
68+
fuse_trans.apply(invoke.children[0].loop_body[0],
69+
invoke.children[0].loop_body[1])
70+
fuse_trans.apply(invoke.children[0].loop_body[0],
71+
invoke.children[0].loop_body[1])
72+
fuse_trans.apply(invoke.children[0].loop_body[0],
73+
invoke.children[0].loop_body[1])
74+
# Apply an OpenACC loop directive to the loop
75+
ltrans.apply(invoke.children[0], {"collapse": 2})
76+
77+
# Create an OpenACC parallel region around the loop
78+
ptrans.apply(invoke.children[0])
79+
80+
# Add an OpenACC enter-data directive
81+
dtrans.apply(invoke)
Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -----------------------------------------------------------------------------
22
# BSD 3-Clause License
33
#
4-
# Copyright (c) 2017-2026, Science and Technology Facilities Council
4+
# Copyright (c) 2018-2026, Science and Technology Facilities Council.
55
# All rights reserved.
66
#
77
# Redistribution and use in source and binary forms, with or without
@@ -31,54 +31,42 @@
3131
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3232
# POSSIBILITY OF SUCH DAMAGE.
3333
# -----------------------------------------------------------------------------
34-
# Authors: R. W. Ford and A. R. Porter, STFC Daresbury Lab
34+
# Authors: R. W. Ford, A. R. Porter and S. Siso, STFC Daresbury Lab
3535

36-
'''A simple test script showing basic usage of the PSyclone API.
37-
In order to use it you must first install PSyclone like so:
36+
''' PSyclone script to fuse invoke_0 loops and insert OpenMP directives '''
3837

39-
>>> pip install --user psyclone
38+
from psyclone.psyir.nodes import FileContainer
39+
from psyclone.psyGen import InvokeSchedule
40+
from psyclone.psyGen import TransInfo
4041

41-
(or see the Getting Going section in the User Guide). Once PSyclone
42-
is installed this script may be run by doing:
4342

44-
>>> python runme.py
43+
def trans(psyir: FileContainer):
44+
'''
45+
:param psyir: the PSyIR of the PSy-layer.
4546
46-
This should generate a lot of output, ending with a view of the
47-
Schedules:
47+
'''
48+
invokes = psyir.walk(InvokeSchedule)
4849

49-
>>> ...
50-
>>> Schedule[invoke='invoke_0']
51-
>>> Loop[type='outer',field_space='cu',it_space='internal_pts']
52-
>>> Loop[type='inner',field_space='cu',it_space='internal_pts']
53-
>>> CodedKern compute_cu_code(cu_fld,p_fld,u_fld)
54-
>>> Loop[type='outer',field_space='cv',it_space='internal_pts']
55-
>>> Loop[type='inner',field_space='cv',it_space='internal_pts']
56-
>>> CodedKern compute_cv_code(cv_fld,p_fld,v_fld)
57-
>>>...
50+
# Print a list of all of the invokes found
51+
print([invoke.name for invoke in invokes])
5852

59-
'''
53+
trans_info = TransInfo()
54+
print(trans_info.list)
55+
fuse_trans = trans_info.get_trans_name('LoopFuseTrans')
56+
omp_trans = trans_info.get_trans_name('GOceanOMPParallelLoopTrans')
6057

61-
from psyclone.parse.algorithm import parse
62-
from psyclone.psyGen import PSyFactory
63-
from psyclone.psyir.backend.fortran import FortranWriter
64-
65-
API = "gocean"
66-
_, INVOKEINFO = parse("shallow_alg.f90", api=API)
67-
PSY = PSyFactory(API).create(INVOKEINFO)
68-
69-
# Print the 'vanilla' generated Fortran
70-
writer = FortranWriter()
71-
print(writer(PSY.container))
72-
73-
# Print a list of all of the invokes found
74-
print(PSY.invokes.names)
75-
76-
# Print the Schedule of each of these Invokes
77-
SCHEDULE = PSY.invokes.get('invoke_0').schedule
78-
print(SCHEDULE.view())
79-
80-
SCHEDULE = PSY.invokes.get('invoke_1').schedule
81-
print(SCHEDULE.view())
82-
83-
SCHEDULE = PSY.invokes.get('invoke_2').schedule
84-
print(SCHEDULE.view())
58+
for invoke in invokes:
59+
if invoke.name == "invoke_0":
60+
# fuse all outer loops
61+
fuse_trans.apply(invoke.children[0], invoke.children[1])
62+
fuse_trans.apply(invoke.children[0], invoke.children[1])
63+
fuse_trans.apply(invoke.children[0], invoke.children[1])
64+
# fuse all inner loops
65+
fuse_trans.apply(invoke.children[0].loop_body[0],
66+
invoke.children[0].loop_body[1])
67+
fuse_trans.apply(invoke.children[0].loop_body[0],
68+
invoke.children[0].loop_body[1])
69+
fuse_trans.apply(invoke.children[0].loop_body[0],
70+
invoke.children[0].loop_body[1])
71+
# Add OpenMP
72+
omp_trans.apply(invoke.children[0])

examples/gocean/eg1/fuse_loops.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# -----------------------------------------------------------------------------
2+
# BSD 3-Clause License
3+
#
4+
# Copyright (c) 2018-2026, Science and Technology Facilities Council.
5+
# All rights reserved.
6+
#
7+
# Redistribution and use in source and binary forms, with or without
8+
# modification, are permitted provided that the following conditions are met:
9+
#
10+
# * Redistributions of source code must retain the above copyright notice, this
11+
# list of conditions and the following disclaimer.
12+
#
13+
# * Redistributions in binary form must reproduce the above copyright notice,
14+
# this list of conditions and the following disclaimer in the documentation
15+
# and/or other materials provided with the distribution.
16+
#
17+
# * Neither the name of the copyright holder nor the names of its
18+
# contributors may be used to endorse or promote products derived from
19+
# this software without specific prior written permission.
20+
#
21+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
# POSSIBILITY OF SUCH DAMAGE.
33+
# -----------------------------------------------------------------------------
34+
# Authors: R. W. Ford, A. R. Porter and S. Siso, STFC Daresbury Lab
35+
36+
''' PSyclone script to fuse invoke_0 loops '''
37+
38+
from psyclone.psyir.nodes import FileContainer
39+
from psyclone.psyGen import InvokeSchedule
40+
from psyclone.psyGen import TransInfo
41+
42+
43+
def trans(psyir: FileContainer):
44+
'''
45+
:param psyir: the PSyIR of the PSy-layer.
46+
47+
'''
48+
invokes = psyir.walk(InvokeSchedule)
49+
50+
# Print a list of all of the invokes found
51+
print([invoke.name for invoke in invokes])
52+
53+
trans_info = TransInfo()
54+
print(trans_info.list)
55+
fuse_trans = trans_info.get_trans_name('LoopFuseTrans')
56+
57+
for invoke in invokes:
58+
if invoke.name == "invoke_0":
59+
# fuse all outer loops
60+
fuse_trans.apply(invoke.children[0], invoke.children[1])
61+
fuse_trans.apply(invoke.children[0], invoke.children[1])
62+
fuse_trans.apply(invoke.children[0], invoke.children[1])
63+
# fuse all inner loops
64+
fuse_trans.apply(invoke.children[0].loop_body[0],
65+
invoke.children[0].loop_body[1])
66+
fuse_trans.apply(invoke.children[0].loop_body[0],
67+
invoke.children[0].loop_body[1])
68+
fuse_trans.apply(invoke.children[0].loop_body[0],
69+
invoke.children[0].loop_body[1])
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# -----------------------------------------------------------------------------
2+
# BSD 3-Clause License
3+
#
4+
# Copyright (c) 2018-2026, Science and Technology Facilities Council.
5+
# All rights reserved.
6+
#
7+
# Redistribution and use in source and binary forms, with or without
8+
# modification, are permitted provided that the following conditions are met:
9+
#
10+
# * Redistributions of source code must retain the above copyright notice, this
11+
# list of conditions and the following disclaimer.
12+
#
13+
# * Redistributions in binary form must reproduce the above copyright notice,
14+
# this list of conditions and the following disclaimer in the documentation
15+
# and/or other materials provided with the distribution.
16+
#
17+
# * Neither the name of the copyright holder nor the names of its
18+
# contributors may be used to endorse or promote products derived from
19+
# this software without specific prior written permission.
20+
#
21+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
# POSSIBILITY OF SUCH DAMAGE.
33+
# -----------------------------------------------------------------------------
34+
# Authors: R. W. Ford, A. R. Porter, N. Nobre, and S. Siso, STFC Daresbury Lab
35+
36+
''' PSyclone script to generate DAG of the invoke_0 '''
37+
38+
import os
39+
from psyclone.psyir.nodes import FileContainer
40+
from psyclone.psyGen import InvokeSchedule
41+
42+
43+
def trans(psyir: FileContainer):
44+
'''
45+
:param psyir: the PSyIR of the PSy-layer.
46+
47+
'''
48+
for invoke in psyir.walk(InvokeSchedule):
49+
if invoke.name == "invoke_0":
50+
# Generate a DAG for it. If graphviz is not available this call
51+
# just returns without doing anything.
52+
dag_name = "invoke_0_dag"
53+
invoke.dag(file_name=dag_name, file_format="png")
54+
dag_name += ".png"
55+
if os.path.isfile(os.path.join(os.getcwd(), dag_name)):
56+
print(f"Wrote DAG to file: {dag_name}")
57+
else:
58+
print("Failed to generate DAG image. Do you have the graphviz "
59+
"library and Python\nbindings installed?")

0 commit comments

Comments
 (0)