From 270cb827741578439a02cb941d6dd5a11473a577 Mon Sep 17 00:00:00 2001
From: Georgiana Mania <mania@dkrz.de>
Date: Mon, 24 Feb 2025 12:32:00 +0000
Subject: [PATCH] configure compilation for kokkos + nvidia
 (icon-libraries/libiconmath!31)

Merged-by: Pradipta Samanta <samanta@dkrz.de>
Changelog: default
---
 CMakeLists.txt                   | 11 +++++++++++
 src/horizontal/CMakeLists.txt    |  3 ++-
 src/interpolation/CMakeLists.txt |  1 +
 src/support/CMakeLists.txt       |  1 +
 4 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7cf92be..affedaa 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -29,6 +29,7 @@ option(IM_USE_CPP_BINDINGS "Use C++ bindings" OFF)
 option(IM_ENABLE_DIM_SWAP "Enable dimension swap" OFF)
 option(IM_ENABLE_OPENACC "Enable OpenACC support" OFF)
 option(IM_ENABLE_OPENMP "Enable OpenMP support" OFF)
+set(IM_ENABLE_GPU OFF CACHE STRING "Enable Kokkos GPU support for arch. Valid values: OFF, nvidia-sm80")
 
 # GNUInstallDirs issues a warning if CMAKE_SIZEOF_VOID_P is not defined, which
 # is the case with NAG. One way to circumvent that is to enable C language for
@@ -124,6 +125,16 @@ set(Kokkos_ENABLE_IMPL_MDSPAN OFF CACHE BOOL "Experimental mdspan support")
 set(Kokkos_ENABLE_SERIAL ON CACHE BOOL "Kokkos Serial backend")
 set(Kokkos_ARCH_NATIVE ON CACHE BOOL "Kokkos native architecture optimisations")
 
+if ("${IM_ENABLE_GPU}" STREQUAL "nvidia-sm80")
+  # NVIDIA A100
+  set(Kokkos_ENABLE_CUDA ON CACHE BOOL "Kokkos CUDA backend")
+  set(Kokkos_ARCH_AMPERE80 ON CACHE BOOL "CUDA architecture: Ampere cc80")
+endif()
+
+if (${IM_ENABLE_OPENMP})
+  set(Kokkos_ENABLE_OPENMP ON CACHE BOOL "Kokkos OpenMP backend")
+endif()
+
 FetchContent_MakeAvailable(kokkos)
 
 add_subdirectory(src)
diff --git a/src/horizontal/CMakeLists.txt b/src/horizontal/CMakeLists.txt
index 6cebed9..d2abe7d 100644
--- a/src/horizontal/CMakeLists.txt
+++ b/src/horizontal/CMakeLists.txt
@@ -66,7 +66,8 @@ target_include_directories(
 target_link_libraries(iconmath-horizontal PUBLIC fortran-support::fortran-support)
 target_link_libraries(iconmath-horizontal PUBLIC iconmath-support)
 target_link_libraries(iconmath-horizontal PUBLIC iconmath-interpolation)
-target_link_libraries(iconmath-interpolation PRIVATE Kokkos::kokkos)
+target_link_libraries(iconmath-horizontal PRIVATE Kokkos::kokkos)
+set_target_properties(iconmath-horizontal PROPERTIES LINKER_LANGUAGE Fortran)
 
 install(TARGETS iconmath-horizontal EXPORT "${PROJECT_NAME}-targets")
 
diff --git a/src/interpolation/CMakeLists.txt b/src/interpolation/CMakeLists.txt
index 9455f9e..346aaaa 100644
--- a/src/interpolation/CMakeLists.txt
+++ b/src/interpolation/CMakeLists.txt
@@ -70,6 +70,7 @@ target_include_directories(
 target_link_libraries(iconmath-interpolation PUBLIC fortran-support::fortran-support)
 target_link_libraries(iconmath-interpolation PUBLIC iconmath-support)
 target_link_libraries(iconmath-interpolation PRIVATE Kokkos::kokkos)
+set_target_properties(iconmath-interpolation PROPERTIES LINKER_LANGUAGE Fortran)
 
 install(TARGETS iconmath-interpolation EXPORT "${PROJECT_NAME}-targets")
 
diff --git a/src/support/CMakeLists.txt b/src/support/CMakeLists.txt
index 9f56017..ed6a4d3 100644
--- a/src/support/CMakeLists.txt
+++ b/src/support/CMakeLists.txt
@@ -80,6 +80,7 @@ target_link_libraries(iconmath-support
     PRIVATE
         Kokkos::kokkos
 )
+set_target_properties(iconmath-support PROPERTIES LINKER_LANGUAGE Fortran)
 
 install(TARGETS iconmath-support EXPORT "${PROJECT_NAME}-targets")
 
-- 
GitLab