Skip to content
Snippets Groups Projects
Commit 55913610 authored by Yen-Chen Chen's avatar Yen-Chen Chen
Browse files

Change OpenACC configuration to match with math-support (!73)

<!--
ICON

---------------------------------------------------------------
Copyright (C) 2004-2024, DWD, MPI-M, DKRZ, KIT, ETH, MeteoSwiss
Contact information: icon-model.org

See AUTHORS.TXT for a list of authors
See LICENSES/ for license information
SPDX-License-Identifier: CC0-1.0
---------------------------------------------------------------
-->

## What is the new feature
Improve OpenACC configuration
## How is it implemented
This MR follows the configuration originally set up in the `math-support` library. See [here](https://gitlab.dkrz.de/icon-libraries/libmath-support/-/blob/6ad1577a20bbae01be6050bdb8263f39653cd348/cmake/check_macro.cmake) for the reference.  
~~This MR waits for !69

 to be merged first.~~

## Mandatory steps before review
- [x] Gitlab CI passes _(Hint: use `make format` for linting)_ 
- [x] New feature is covered by additional unit tests
- [x] Mark the merge request as ready by removing `Draft:`

## Mandatory steps before merge
- [x] Test coverage does not decrease
- [x] Reviewed by a maintainer
- [x] Incorporate review suggestions
- [ ] Remember to edit the commit message and select the proper changelog category (feature/bugfix/other)

**You are not supposed to merge this request by yourself, the maintainers of fortan-support take care of this action!**

Approved-by: default avatarPradipta Samanta <samanta@dkrz.de>
Merged-by: default avatarYen-Chen Chen <yen-chen.chen@kit.edu>
Changelog: feature
parent 01e5c4c0
No related branches found
No related tags found
1 merge request!73Change OpenACC configuration to match with math-support
Pipeline #61387 passed
# ICON
#
# ---------------------------------------------------------------
# Copyright (C) 2004-2024, DWD, MPI-M, DKRZ, KIT, ETH, MeteoSwiss
# Contact information: icon-model.org
#
# See AUTHORS.TXT for a list of authors
# See LICENSES/ for license information
# SPDX-License-Identifier: BSD-3-Clause
# ---------------------------------------------------------------
# check_macro_defined(<output>
# <macro>
# [LANG] <lang>
# [QUIET])
# -----------------------------------------------------------------------------
# Sets <output> to ON or OFF depending on whether <macro> is already defined
# via CMAKE_${lang}_FLAGS or not. However, this only works when <lang> is Fortran
# and fails otherwise.
#
# <lang> can be set using LANG. <lang> is set as Fortran by default.
#
function(check_macro_defined output macro)
cmake_parse_arguments(PARSE_ARGV 1 ARG
"QUIET"
"LANG"
""
)
if(NOT ARG_LANG)
set(ARG_LANG "Fortran")
endif()
if(NOT ARG_LANG STREQUAL "Fortran")
message(FATAL_ERROR "check_macro_defined supports only LANG Fortran")
endif()
if(NOT ARG_QUIET)
message(CHECK_START "Checking whether ${macro} is defined")
endif()
# Write a simple Fortran program that checks for ${macro}
set(check_source_code "
program main
implicit none
#ifdef ${macro}
integer a
#else
#endif
#ifndef ${macro}
#else
integer b
#endif
a = 4
b = 2
end
")
# Write the Fortran code to a file
file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.F90" "${check_source_code}")
# Try to compile the program
try_compile(result
"${CMAKE_BINARY_DIR}"
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.F90"
)
if(NOT ARG_QUIET)
if(${result})
message(CHECK_PASS "yes")
else()
message(CHECK_PASS "no")
endif()
endif()
set(${output} ${result} PARENT_SCOPE)
endfunction()
......@@ -19,16 +19,6 @@ configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/config.h.in
${CMAKE_CURRENT_BINARY_DIR}/config.h)
if (FS_ENABLE_OPENACC)
message(VERBOSE "Compiler id is ${CMAKE_Fortran_COMPILER_ID}")
if (CMAKE_Fortran_COMPILER_ID STREQUAL "Cray")
set(OpenACC_FLAGS "-hacc")
else () # Compiler ID for NVHPC is only supported after CMake 3.20
set(OpenACC_FLAGS "-Minfo=all -acc=gpu")
endif ()
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${OpenACC_FLAGS}")
endif ()
if (FS_ENABLE_OMP)
find_package(OpenMP QUIET)
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${OpenMP_Fortran_FLAGS}")
......@@ -89,6 +79,25 @@ if (FS_ENABLE_MIXED_PRECISION)
target_compile_definitions(fortran-support PRIVATE __MIXED_PRECISION)
endif ()
include("${PROJECT_SOURCE_DIR}/cmake/check_macro.cmake")
check_macro_defined(HAS_OPENACC_MACRO _OPENACC QUIET)
if (FS_ENABLE_OPENACC)
# If _OPENACC is defined, assume that the required compiler flags are already
# provided, e.g. in CMAKE_Fortran_FLAGS:
if(NOT HAS_OPENACC_MACRO)
find_package(OpenACC REQUIRED)
target_compile_options(fortran-support PRIVATE ${OpenACC_Fortran_OPTIONS})
# This make sures that unit tests (FortUTF) compiles without the need of
# passing OpenACC compile option.
target_link_libraries(fortran-support PRIVATE OpenACC::OpenACC_Fortran)
endif ()
else ()
if(HAS_OPENACC_MACRO)
message(FATAL_ERROR
"The OpenACC support is disabled but the _OPENACC macro is defined")
endif ()
endif ()
target_include_directories(fortran-support
PUBLIC
# Path to the Fortran modules:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment