Skip to content

Commit 616a743

Browse files
rem1776rem1776
authored andcommitted
add suport for openacc in both build systems
1 parent d814ff6 commit 616a743

4 files changed

Lines changed: 209 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ option(32BIT "Build 32-bit (r4) FMS library" OFF)
5656
option(64BIT "Build 64-bit (r8) FMS library" OFF)
5757
option(FPIC "Build with position independent code" OFF)
5858
option(SHARED_LIBS "Build shared/dynamic libraries" OFF)
59+
option(OPENACC "Build FMS with OpenACC support" OFF)
5960

6061
# Options for compiler definitions
6162
option(INTERNAL_FILE_NML "Enable compiler definition -DINTERNAL_FILE_NML" ON)
@@ -82,11 +83,15 @@ endif()
8283
find_package(MPI REQUIRED COMPONENTS C Fortran)
8384
find_package(NetCDF REQUIRED COMPONENTS C Fortran)
8485

85-
# Check for the OpenMP library and set the required compile flags
86+
# Check compilers support for OpenMP and OpenACC directives, if requested
8687
if (OPENMP)
8788
find_package(OpenMP REQUIRED COMPONENTS C Fortran)
8889
endif()
8990

91+
if (OPENACC)
92+
find_package(OpenACC REQUIRED COMPONENTS C Fortran)
93+
endif()
94+
9095
if (WITH_YAML)
9196
find_package(PkgConfig REQUIRED)
9297
pkg_check_modules(YAML REQUIRED IMPORTED_TARGET "yaml-0.1")

LICENSE

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,41 @@
172172
defend, and hold each Contributor harmless for any liability
173173
incurred by, or claims asserted against, such Contributor by reason
174174
of your accepting any such warranty or additional liability.
175+
176+
-------------------------------------------------------------------------
177+
APPENDIX: Third-party components
178+
-------------------------------------------------------------------------
179+
180+
This project includes the following third-party components:
181+
182+
1. ACX_LANG_OPENACC_FLAG (located at: m4/acx_lang_openacc.m4)
183+
184+
Copyright (c) 2018-2024, MPI-M
185+
Author: Sergey Kosukhin <sergey.kosukhin@mpimet.mpg.de>
186+
SPDX-License-Identifier: BSD-3-Clause
187+
188+
All rights reserved.
189+
190+
Redistribution and use in source and binary forms, with or without
191+
modification, are permitted provided that the following conditions are met:
192+
193+
* Redistributions of source code must retain the above copyright notice,
194+
this list of conditions and the following disclaimer.
195+
* Redistributions in binary form must reproduce the above copyright notice,
196+
this list of conditions and the following disclaimer in the documentation
197+
and/or other materials provided with the distribution.
198+
* Neither the name of [Original Author/Organization Name] nor the names
199+
of its contributors may be used to endorse or promote products derived
200+
from this software without specific prior written permission.
201+
202+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
203+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
204+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
205+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
206+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
207+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
208+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
209+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
210+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
211+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
212+
POSSIBILITY OF SUCH DAMAGE.

configure.ac

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,21 @@ AC_CHECK_FUNCS([sched_getaffinity], [], [])
237237

238238
# Check if the compiler needs special OpenMP flags
239239
AC_OPENMP()
240+
241+
# Check if building with OpenACC support. Default is 'no'
242+
AC_ARG_ENABLE([openacc],
243+
[AS_HELP_STRING([--enable-openacc],
244+
[Builds with support for OpenACC directives. (default no)])])
245+
AS_IF([test ${enable_openacc:-no} = yes],
246+
[enable_openacc=yes],
247+
[enable_openacc=no])
248+
249+
# If openacc support enabled, test for correct flags
250+
AS_IF([test ${enable_openacc} = yes],
251+
[ACX_LANG_OPENACC_FLAG(
252+
[AS_VAR_APPEND([CFLAGS], [" $acx_cv_c_openacc_flag"])], []
253+
)])
254+
240255
AC_LANG_POP(C)
241256

242257
# Fortran specific checks
@@ -306,6 +321,15 @@ GX_FC_INTERNAL_FILE_NML()
306321
# Check if the compiler needs special OpenMP flags
307322
AC_OPENMP()
308323

324+
# need to set preprocessor for openacc check below
325+
AC_FC_PP_SRCEXT([F90])
326+
327+
# Check for openacc fortran flags
328+
AS_IF([test ${enable_openacc} = yes],
329+
[ACX_LANG_OPENACC_FLAG(
330+
[AS_VAR_APPEND([FCFLAGS], [" $acx_cv_fc_openacc_flag"])], []
331+
)])
332+
309333
AC_LANG_POP(Fortran)
310334

311335
# We passed all the tests. Set the required defines.

m4/acx_lang_openacc.m4

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Copyright (c) 2018-2024, MPI-M
2+
#
3+
# Author: Sergey Kosukhin <sergey.kosukhin@mpimet.mpg.de>
4+
#
5+
# SPDX-License-Identifier: BSD-3-Clause
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+
# 1. Redistributions of source code must retain the above copyright notice,
11+
# this list of conditions and the following disclaimer.
12+
# 2. Redistributions in binary form must reproduce the above copyright
13+
# notice, this list of conditions and the following disclaimer in the
14+
# documentation and/or other materials provided with the distribution.
15+
# 3. Neither the name of the copyright holder nor the names of its
16+
# contributors may be used to endorse or promote products derived from
17+
# this software without specific prior written permission.
18+
#
19+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
# POSSIBILITY OF SUCH DAMAGE.
30+
31+
# ACX_LANG_OPENACC_FLAG([ACTION-IF-SUCCESS],
32+
# [ACTION-IF-FAILURE = FAILURE])
33+
# -----------------------------------------------------------------------------
34+
# Finds the compiler flag needed to enable OpenACC support. The result is
35+
# either "unknown", or the actual compiler flag required to enable OpenACC
36+
# support, which may be an empty string.
37+
#
38+
# Known flags:
39+
# GNU: -fopenacc
40+
# Cray: -hacc
41+
# Nvidia: -acc
42+
#
43+
# If successful, runs ACTION-IF-SUCCESS, otherwise runs ACTION-IF-FAILURE
44+
# (defaults to failing with an error message).
45+
#
46+
# The flag is cached in the acx_cv_[]_AC_LANG_ABBREV[]_openacc_flag variable.
47+
#
48+
# Upon successful run, you can check for the version of the standard supported
49+
# by the compiler by expanding:
50+
# AS_VAR_APPEND([_AC_LANG_PREFIX[]FLAGS],
51+
# [" $acx_cv_[]_AC_LANG_ABBREV[]_openacc_flag"])
52+
# ACX_LANG_MACRO_CHECK_VALUE([_OPENACC])
53+
# and checking for the value of the
54+
# acx_cv_[]_AC_LANG_ABBREV[]_macro__OPENACC_value shell variable. The possible
55+
# (successful) values of the variable are dates, which map to the versions of
56+
# the standard in the following way:
57+
# 202111 3.2
58+
# 202011 3.1
59+
# 201911 3.0
60+
# 201811 2.7
61+
# 201711 2.6
62+
# 201510 2.5
63+
# 201308 2.0 (the corrected version)
64+
# 201306 2.0
65+
# 201111 1.0
66+
#
67+
AC_DEFUN([ACX_LANG_OPENACC_FLAG],
68+
[m4_pushdef([acx_cache_var], [acx_cv_[]_AC_LANG_ABBREV[]_openacc_flag])dnl
69+
AC_MSG_CHECKING([for _AC_LANG compiler flag needed to enable OpenACC dnl
70+
support])
71+
AC_CACHE_VAL([acx_cache_var],
72+
[acx_cache_var=unknown
73+
acx_save_[]_AC_LANG_PREFIX[]FLAGS=$[]_AC_LANG_PREFIX[]FLAGS
74+
AC_LANG_CONFTEST([_ACX_LANG_OPENACC])
75+
for acx_lang_openacc_flag in '' -fopenacc -hacc -acc; do
76+
_AC_LANG_PREFIX[]FLAGS="${acx_save_[]_AC_LANG_PREFIX[]FLAGS} dnl
77+
$acx_lang_openacc_flag"
78+
AC_LINK_IFELSE([], [acx_cache_var=$acx_lang_openacc_flag])
79+
test "x$acx_cache_var" != xunknown && break
80+
done
81+
rm -f conftest.$ac_ext
82+
_AC_LANG_PREFIX[]FLAGS=$acx_save_[]_AC_LANG_PREFIX[]FLAGS])
83+
AS_IF([test -n "$acx_cache_var"],
84+
[AC_MSG_RESULT([$acx_cache_var])],
85+
[AC_MSG_RESULT([none needed])])
86+
AS_VAR_IF([acx_cache_var], [unknown], [m4_default([$2],
87+
[AC_MSG_FAILURE([unable to detect _AC_LANG compiler flag needed to dnl
88+
enable OpenACC support])])], [$1])
89+
m4_popdef([acx_cache_var])])
90+
91+
# _ACX_LANG_OPENACC()
92+
# -----------------------------------------------------------------------------
93+
# Expands into the source code of a program in the current language that is
94+
# compiled successfully only when OpenACC support is enabled for the current
95+
# compiler. By default, expands to m4_fatal with the message saying that
96+
# _AC_LANG is not supported.
97+
#
98+
m4_define([_ACX_LANG_OPENACC],
99+
[m4_ifdef([$0(]_AC_LANG[)],
100+
[m4_indir([$0(]_AC_LANG[)], $@)],
101+
[m4_fatal([the OpenACC test program is not defined for ]dnl
102+
_AC_LANG[ language])])])])
103+
104+
# _ACX_LANG_OPENACC(C)()
105+
# -----------------------------------------------------------------------------
106+
# Implementation of _ACX_LANG_OPENACC for C language.
107+
#
108+
m4_define([_ACX_LANG_OPENACC(C)],
109+
[AC_LANG_PROGRAM([[#include <openacc.h>]],
110+
[[#ifndef _OPENACC
111+
choke me
112+
#endif
113+
return (int) acc_get_device_type()]])])
114+
115+
# _ACX_LANG_OPENACC(C++)
116+
# -----------------------------------------------------------------------------
117+
# Implementation of _ACX_LANG_OPENACC for C++ language.
118+
#
119+
m4_copy([_ACX_LANG_OPENACC(C)], [_ACX_LANG_OPENACC(C++)])
120+
121+
# _ACX_LANG_OPENACC(Fortran)()
122+
# -----------------------------------------------------------------------------
123+
# Implementation of _ACX_LANG_OPENACC for Fortran language.
124+
#
125+
# The implementation implies that the Fortran module should be available when
126+
# OpenACC is enabled.
127+
#
128+
m4_define([_ACX_LANG_OPENACC(Fortran)],
129+
[AC_PROVIDE_IFELSE([AC_FC_PP_SRCEXT], [],
130+
[m4_warn([syntax],
131+
[ACX_LANG_OPENACC_FLAG requires calling the Fortran compiler with ]dnl
132+
[a preprocessor but no call to AC_FC_PP_SRCEXT is detected])])dnl
133+
AC_LANG_PROGRAM([],
134+
[[#ifndef _OPENACC
135+
choke me
136+
#endif
137+
use openacc, only : acc_get_device_type
138+
implicit none
139+
integer :: t
140+
t = acc_get_device_type()]])])
141+

0 commit comments

Comments
 (0)