# -----------------------------------------------------------------------------
# SPDX-FileCopyrightText: Copyright (c) 2020-2026 Science and Technology
#                         Facilities Council
# SPDX-License-Identifier: BSD-3-Clause
# See the full LICENSE file in the project root for details.
# -----------------------------------------------------------------------------

# The compiler to use may be specified via the F90 and F90FLAGS
# environment variables. To use the NVIDIA compiler and enable
# openacc compilation with managed memory, use:
#
# export F90=nvfortran
# export F90FLAGS="-acc=gpu -Minfo=all -gpu=managed"
# export LDFLAGS=""

include ../../common.mk

GENERATED_FILES += invoke_0_dag invoke_0_dag.png *.cl ompt_alg.f90 ompt_psy.f90
ENV = PSYCLONE_CONFIG=${PSYCLONE_DIR}/config/psyclone.cfg

KERNELS=compute_cu_mod.o compute_cv_mod.o compute_z_mod.o compute_h_mod.o \
		compute_unew_mod.o compute_vnew_mod.o compute_pnew_mod.o time_smooth_mod.o \
		infrastructure_mod.o

# Location of the infrastructure code (which is a submodule of the
# PSyclone git repo).
SHARED_DIR ?= ../../../external
INF_DIR ?= ${SHARED_DIR}/dl_esm_inf/finite_difference
INF_INC = ${INF_DIR}/src
INF_LIB = ${INF_DIR}/src/lib_fd.a

.PHONY: basic openmp mpi loop_fuse dag openacc opencl openmp_taskloop

transform: basic openmp mpi loop_fuse dag openacc opencl openmp_taskloop

# The dl_esm_inf library
fd_lib:
	${MAKE} -C ${INF_DIR} F90=${F90} fd_lib

dm_fd_lib:
	${MAKE} -C ${INF_DIR} F90=${F90} dm_fd_lib

ompt_psy.f90 ompt_alg.f90: shallow_alg.f90
	${PSYCLONE} -nodm -s ./openmp_taskloop_trans.py -api gocean \
		-I${INF_INC} -opsy ompt_psy.f90 -oalg ompt_alg.f90 \
                shallow_alg.f90

openmp_taskloop: ompt_psy.f90 ompt_alg.f90

# TODO: Cannot compile this example until #1474 is resolved.
#compile-openmptaskloop: ompt_psy.f90 ompt_alg.f90 ${KERNELS} fd_lib
#	${F90} ${F90FLAGS} ${LDFLAGS} -I${INF_INC} ompt_psy.f90 ompt_alg.f90 ${KERNELS} \
	${INF_LIB} -o shallow_ompt.exe

compile: transform
	@echo "No compile targets for example gocean/eg1, see #1474"

%.o: %.f90
	${F90} ${F90FLAGS} -I${INF_INC} -c $<

run: compile
	@echo "No run targets for example gocean/eg1"

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

openmp:
	${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:
	${PSYCLONE} -nodm -api gocean -s fuse_loops.py shallow_alg.f90

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

openacc:
	${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
