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
13 changes: 6 additions & 7 deletions examples/gocean/eg1/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,23 @@ run: compile
@echo "No run targets for example gocean/eg1"

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

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


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

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

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

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

opencl:
${PSYCLONE} -nodm -s ./opencl_transformation.py \
-api gocean -I${INF_INC} shallow_alg.f90
${PSYCLONE} -nodm -s ./opencl_transformation.py -api gocean -I${INF_INC} shallow_alg.f90
5 changes: 1 addition & 4 deletions examples/gocean/eg1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ PSyclone can be run in the directory containing this file by
executing, e.g.

```sh
python ./runme.py
psyclone --psykal-dsl gocean -s <script> shallow_alg.f90
```

Examine the runme*.py scripts themselves for further details or see the
Makefile.


## OpenMP tasking transformation script

Expand Down
976 changes: 0 additions & 976 deletions examples/gocean/eg1/dag.ipynb

This file was deleted.

81 changes: 81 additions & 0 deletions examples/gocean/eg1/fuse_and_insert_openacc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# -----------------------------------------------------------------------------
# BSD 3-Clause License
#
# Copyright (c) 2018-2026, 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

''' PSyclone script to fuse invoke_0 loops and insert OpenACC directives '''

from psyclone.psyir.nodes import FileContainer
from psyclone.psyGen import InvokeSchedule
from psyclone.psyGen import TransInfo
from psyclone.psyir.transformations import ACCLoopTrans, LoopFuseTrans


def trans(psyir: FileContainer):
'''
:param psyir: the PSyIR of the PSy-layer.

'''
invokes = psyir.walk(InvokeSchedule)

# Print a list of all of the invokes found
print([invoke.name for invoke in invokes])

trans_info = TransInfo()
print(trans_info.list)
fuse_trans = LoopFuseTrans()
ptrans = trans_info.get_trans_name('ACCParallelTrans')
dtrans = trans_info.get_trans_name('ACCEnterDataTrans')
ltrans = ACCLoopTrans()

for invoke in invokes:
if invoke.name == "invoke_0":
# fuse all outer loops
fuse_trans.apply(invoke.children[0], invoke.children[1])
fuse_trans.apply(invoke.children[0], invoke.children[1])
fuse_trans.apply(invoke.children[0], invoke.children[1])
# fuse all inner loops
fuse_trans.apply(invoke.children[0].loop_body[0],
invoke.children[0].loop_body[1])
fuse_trans.apply(invoke.children[0].loop_body[0],
invoke.children[0].loop_body[1])
fuse_trans.apply(invoke.children[0].loop_body[0],
invoke.children[0].loop_body[1])
# Apply an OpenACC loop directive to the loop
ltrans.apply(invoke.children[0], {"collapse": 2})

# Create an OpenACC parallel region around the loop
ptrans.apply(invoke.children[0])

# Add an OpenACC enter-data directive
dtrans.apply(invoke)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -----------------------------------------------------------------------------
# BSD 3-Clause License
#
# Copyright (c) 2017-2026, Science and Technology Facilities Council
# Copyright (c) 2018-2026, Science and Technology Facilities Council.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -31,54 +31,42 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------
# Authors: R. W. Ford and A. R. Porter, STFC Daresbury Lab
# Authors: R. W. Ford, A. R. Porter and S. Siso, STFC Daresbury Lab

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

>>> pip install --user psyclone
from psyclone.psyir.nodes import FileContainer
from psyclone.psyGen import InvokeSchedule
from psyclone.psyGen import TransInfo

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

>>> python runme.py
def trans(psyir: FileContainer):
'''
:param psyir: the PSyIR of the PSy-layer.

This should generate a lot of output, ending with a view of the
Schedules:
'''
invokes = psyir.walk(InvokeSchedule)

>>> ...
>>> Schedule[invoke='invoke_0']
>>> Loop[type='outer',field_space='cu',it_space='internal_pts']
>>> Loop[type='inner',field_space='cu',it_space='internal_pts']
>>> CodedKern compute_cu_code(cu_fld,p_fld,u_fld)
>>> Loop[type='outer',field_space='cv',it_space='internal_pts']
>>> Loop[type='inner',field_space='cv',it_space='internal_pts']
>>> CodedKern compute_cv_code(cv_fld,p_fld,v_fld)
>>>...
# Print a list of all of the invokes found
print([invoke.name for invoke in invokes])

'''
trans_info = TransInfo()
print(trans_info.list)
fuse_trans = trans_info.get_trans_name('LoopFuseTrans')
omp_trans = trans_info.get_trans_name('GOceanOMPParallelLoopTrans')

from psyclone.parse.algorithm import parse
from psyclone.psyGen import PSyFactory
from psyclone.psyir.backend.fortran import FortranWriter

API = "gocean"
_, INVOKEINFO = parse("shallow_alg.f90", api=API)
PSY = PSyFactory(API).create(INVOKEINFO)

# Print the 'vanilla' generated Fortran
writer = FortranWriter()
print(writer(PSY.container))

# Print a list of all of the invokes found
print(PSY.invokes.names)

# Print the Schedule of each of these Invokes
SCHEDULE = PSY.invokes.get('invoke_0').schedule
print(SCHEDULE.view())

SCHEDULE = PSY.invokes.get('invoke_1').schedule
print(SCHEDULE.view())

SCHEDULE = PSY.invokes.get('invoke_2').schedule
print(SCHEDULE.view())
for invoke in invokes:
if invoke.name == "invoke_0":
# fuse all outer loops
fuse_trans.apply(invoke.children[0], invoke.children[1])
fuse_trans.apply(invoke.children[0], invoke.children[1])
fuse_trans.apply(invoke.children[0], invoke.children[1])
# fuse all inner loops
fuse_trans.apply(invoke.children[0].loop_body[0],
invoke.children[0].loop_body[1])
fuse_trans.apply(invoke.children[0].loop_body[0],
invoke.children[0].loop_body[1])
fuse_trans.apply(invoke.children[0].loop_body[0],
invoke.children[0].loop_body[1])
# Add OpenMP
omp_trans.apply(invoke.children[0])
69 changes: 69 additions & 0 deletions examples/gocean/eg1/fuse_loops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# -----------------------------------------------------------------------------
# BSD 3-Clause License
#
# Copyright (c) 2018-2026, 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

''' PSyclone script to fuse invoke_0 loops '''

from psyclone.psyir.nodes import FileContainer
from psyclone.psyGen import InvokeSchedule
from psyclone.psyGen import TransInfo


def trans(psyir: FileContainer):
'''
:param psyir: the PSyIR of the PSy-layer.

'''
invokes = psyir.walk(InvokeSchedule)

# Print a list of all of the invokes found
print([invoke.name for invoke in invokes])

trans_info = TransInfo()
print(trans_info.list)
fuse_trans = trans_info.get_trans_name('LoopFuseTrans')

for invoke in invokes:
if invoke.name == "invoke_0":
# fuse all outer loops
fuse_trans.apply(invoke.children[0], invoke.children[1])
fuse_trans.apply(invoke.children[0], invoke.children[1])
fuse_trans.apply(invoke.children[0], invoke.children[1])
# fuse all inner loops
fuse_trans.apply(invoke.children[0].loop_body[0],
invoke.children[0].loop_body[1])
fuse_trans.apply(invoke.children[0].loop_body[0],
invoke.children[0].loop_body[1])
fuse_trans.apply(invoke.children[0].loop_body[0],
invoke.children[0].loop_body[1])
59 changes: 59 additions & 0 deletions examples/gocean/eg1/generate_dag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# -----------------------------------------------------------------------------
# BSD 3-Clause License
#
# Copyright (c) 2018-2026, 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, N. Nobre, and S. Siso, STFC Daresbury Lab

''' PSyclone script to generate DAG of the invoke_0 '''

import os
from psyclone.psyir.nodes import FileContainer
from psyclone.psyGen import InvokeSchedule


def trans(psyir: FileContainer):
'''
:param psyir: the PSyIR of the PSy-layer.

'''
for invoke in psyir.walk(InvokeSchedule):
if invoke.name == "invoke_0":
# Generate a DAG for it. If graphviz is not available this call
# just returns without doing anything.
dag_name = "invoke_0_dag"
invoke.dag(file_name=dag_name, file_format="png")
dag_name += ".png"
if os.path.isfile(os.path.join(os.getcwd(), dag_name)):
print(f"Wrote DAG to file: {dag_name}")
else:
print("Failed to generate DAG image. Do you have the graphviz "
"library and Python\nbindings installed?")
Loading
Loading