1# Copyright (c) 2020 - 2025 David Guibert
2# Copyright (c) 2024 - 2025 Antoine Morvan
3# Copyright (c) 2024 - 2025 Niclas Schroeter
6# SPDX-License-Identifier: Apache-2.0
8option(ENABLE_cloudsc_single_precision "Build CLOUDSC in single precision" OFF)
9set(cloudsc_block_size "" CACHE STRING "The block size argument for CLOUDSC")
11if(cloudsc_block_size STREQUAL "")
12 if(ENABLE_cloudsc_single_precision)
13 set(cloudsc_block_size "32")
15 set(cloudsc_block_size "16")
19option(ENABLE_cloudsc_gpu "Enable cloudsc gpu prototypes" OFF)
20cmake_dependent_option(ENABLE_cloudsc_acc "Enable cloudsc acc components" ON "ENABLE_cloudsc_gpu" OFF)
21option(ENABLE_cloudsc_omp "Enable cloudsc openMP components" ON)
22cmake_dependent_option(ENABLE_cloudsc_omp_gpu "Enable cloudsc openMP components (gpu)" ON "ENABLE_cloudsc_gpu" OFF)
27if(ENABLE_cloudsc_omp_gpu)
30cmake_dependent_option(ENABLE_cloudsc_cuda "Enable cloudsc cuda components" ON "ENABLE_cloudsc_gpu" OFF)
31cmake_dependent_option(ENABLE_cloudsc_hip "Enable cloudsc hip components" ON "ENABLE_cloudsc_gpu" OFF)
33set_property(GLOBAL PROPERTY cloudsc_can_use_system TRUE)
34set_property(GLOBAL PROPERTY cloudsc_depends ecbuild hdf5)
35set_property(GLOBAL PROPERTY cloudsc_depends_optional mpi)
37 set_property(GLOBAL APPEND PROPERTY cloudsc_depends cuda)
40set(cloudsc_cmake_args "" CACHE STRING "Custom CMake arguments for CloudSC")
41set(cloudsc_build_args "" CACHE STRING "Custom Make arguments for CloudSC")
44 if(USE_SYSTEM_cloudsc)
46 message(STATUS "cloudsc: enabled (system installed)")
48 function(find_cloudsc_program BIN)
49 find_program(CLOUDSC_EXE ${BIN} REQUIRED)
50 get_filename_component(CLOUDSC_EXE_DIR "${CLOUDSC_EXE}" DIRECTORY)
51 set_if_not_present(ENV{PATH} "$ENV{PATH}" "${CLOUDSC_EXE_DIR}")
54 find_cloudsc_program("dwarf-cloudsc-fortran")
56 if(ENABLE_cloudsc_acc)
57 find_cloudsc_program("dwarf-cloudsc-gpu-scc-k-caching")
59 if(ENABLE_cloudsc_omp)
60 find_cloudsc_program("dwarf-cloudsc-gpu-omp-scc-hoist")
62 if(ENABLE_cloudsc_cuda)
63 find_cloudsc_program("dwarf-cloudsc-c-cuda-k-caching")
65 if(ENABLE_cloudsc_hip)
66 find_cloudsc_program("dwarf-cloudsc-hip-k-caching")
69 # dummy cloudsc external project to get tests
70 ExternalProject_Data_Add(
76 ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR>/config-files
77 <INSTALL_DIR>/share/dwarf-p-cloudsc
78 COMMAND ${CMAKE_COMMAND} -E make_directory
79 <BINARY_DIR>/test-dwarf-p-cloudsc)
81 ExternalProject_Get_Property(cloudsc INSTALL_DIR)
82 ExternalProject_Get_Property(cloudsc BINARY_DIR)
83 set(cloudsc_DIR ${INSTALL_DIR})
84 set(cloudsc_INPUT_DIR ${cloudsc_DIR}/share/dwarf-p-cloudsc)
85 set(cloudsc_BIN_DIR ${BINARY_DIR})
88 message(STATUS "cloudsc: enabled (internally built)")
89 set(cloudsc_depends_ep ecbuild)
91 list(APPEND cloudsc_depends_ep hdf5::hdf5)
93 list(APPEND cloudsc_depends_ep hdf5)
96 list(APPEND cloudsc_depends_ep MPI::MPI_C MPI::MPI_Fortran)
99 ExternalProject_Data_Add(
102 DEPENDS ${cloudsc_depends_ep}
103 PATCH_COMMAND ${Patch_EXECUTABLE} -p1 -i ${CMAKE_SOURCE_DIR}/projects/cloudsc_omp_link.patch
104 CONFIGURE_COMMAND env
105 PATH=$ENV{PATH} CPATH=$ENV{CPATH} C_INCLUDE_PATH=$ENV{C_INCLUDE_PATH}
106 LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} LIBRARY_PATH=$ENV{LIBRARY_PATH}
107 PKG_CONFIG_PATH=<INSTALL_DIR>/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}
108 CC=${MPI_C_COMPILER} CXX=${MPI_CXX_COMPILER} FC=${MPI_Fortran_COMPILER}
109 ecbuild -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER}
110 -DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER}
111 -DCMAKE_Fortran_COMPILER:FILEPATH=${CMAKE_Fortran_COMPILER}
112 -DCMAKE_Fortran_FLAGS=${CMAKE_Fortran_FLAGS}
113 -DBUILD_SHARED_LIBS:BOOL=${BUILD_SHARED_LIBS}
114 -DCMAKE_INSTALL_PREFIX:FILEPATH=<INSTALL_DIR>
116 -DENABLE_CLOUDSC_C=OFF
118 -DENABLE_CLOUDSC_FORTRAN=ON
120 -DENABLE_ACC:BOOL=${ENABLE_cloudsc_acc}
121 -DENABLE_CLOUDSC_GPU_SCC_K_CACHING=${ENABLE_cloudsc_acc}
123 -DENABLE_OMP:BOOL=${cloudsc_omp}
124 -DENABLE_CLOUDSC_GPU_OMP_SCC_HOIST=${ENABLE_cloudsc_omp_gpu}
126 -DENABLE_CUDA:BOOL=${ENABLE_cloudsc_cuda}
127 -DENABLE_CLOUDSC_C_CUDA=${ENABLE_cloudsc_cuda}
129 -DENABLE_HIP:BOOL=${ENABLE_cloudsc_hip}
130 -DENABLE_CLOUDSC_HIP:BOOL=${ENABLE_cloudsc_hip}
132 -DENABLE_MPI:BOOL=${mpi_enabled}
133 -DENABLE_SINGLE_PRECISION=${ENABLE_cloudsc_single_precision}
135 -DCMAKE_POLICY_DEFAULT_CMP0104=NEW # initialize CMAKE_CUDA_ARCHITECTURES https://cmake.org/cmake/help/latest/policy/CMP0104.html
136 ${default_cmake_args} ${cloudsc_cmake_args}
138 BUILD_COMMAND ${CMAKE_MAKE_PROGRAM} -j${BUILD_PARALLEL_LEVEL} ${default_build_args} ${cloudsc_build_args}
139 INSTALL_COMMAND ${CMAKE_MAKE_PROGRAM} -j${BUILD_PARALLEL_LEVEL} install
140 COMMAND ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR>/config-files
141 <INSTALL_DIR>/share/dwarf-p-cloudsc
142 COMMAND ${CMAKE_COMMAND} -E make_directory
143 <BINARY_DIR>/test-dwarf-p-cloudsc)
145 ExternalProject_Get_Property(cloudsc INSTALL_DIR)
146 set(cloudsc_DIR ${INSTALL_DIR})
147 set(cloudsc_INPUT_DIR ${cloudsc_DIR}/share/dwarf-p-cloudsc)
149 set_if_not_present(ENV{PATH} "$ENV{PATH}" "${cloudsc_DIR}/bin")
151 ENV{LD_LIBRARY_PATH} "$ENV{LD_LIBRARY_PATH}" "${cloudsc_DIR}/lib"
152 "${cloudsc_DIR}/lib64")
154 ExternalProject_Get_Property(cloudsc BINARY_DIR)
155 set(cloudsc_BIN_DIR ${BINARY_DIR})
158 # common for all tests
160 NAME cloudsc-stage-in
161 COMMAND $<TARGET_FILE:job_launcher> --name=cloudsc-stage-in
164 cp ${cloudsc_INPUT_DIR}/{reference.h5,input.h5} .
165 WORKING_DIRECTORY ${cloudsc_BIN_DIR}/test-dwarf-p-cloudsc)
167 set_tests_properties(cloudsc-stage-in PROPERTIES FIXTURES_SETUP _setup-cloudsc)
169 function(link_cloudsc_tests testname validation)
170 set_tests_properties(${testname} PROPERTIES FIXTURES_REQUIRED _setup-cloudsc)
171 if(TEST ${validation})
172 set_tests_properties(${testname} PROPERTIES FIXTURES_SETUP _run-${testname})
173 set_tests_properties(${validation} PROPERTIES FIXTURES_REQUIRED _run-${testname})
177 function(add_cloudsc_test testname executable args)
179 NAME cloudsc-${testname}
181 env CPATH=$ENV{CPATH} C_INCLUDE_PATH=$ENV{C_INCLUDE_PATH}
182 PATH=$ENV{PATH} LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}
183 LIBRARY_PATH=$ENV{LIBRARY_PATH} $<TARGET_FILE:job_launcher>
184 --name=cloudsc-${testname}
187 ${executable} ${args}
188 WORKING_DIRECTORY ${cloudsc_BIN_DIR}/test-dwarf-p-cloudsc)
190 # The validation is expected to fail for single precision due to a lack
191 # of references in CLOUDSC itself.
192 if(NOT ENABLE_cloudsc_single_precision)
194 NAME cloudsc-${testname}-validation
196 $<TARGET_FILE:job_launcher> --name=cloudsc-${testname}-validation
199 ${CMAKE_SOURCE_DIR}/validation/cloudsc.sh
200 ${LOG_DIR}/cloudsc-${testname}.log
201 WORKING_DIRECTORY ${cloudsc_BIN_DIR}/test-dwarf-p-cloudsc)
204 link_cloudsc_tests(cloudsc-${testname}
205 cloudsc-${testname}-validation)
208 # the first parameter is the number of threads (need to be adapted in the job-launcher)
209 add_cloudsc_test(fortran-small dwarf-cloudsc-fortran "\${OMP_NUM_THREADS-1} 16384 ${cloudsc_block_size}")
210 add_cloudsc_test(fortran-medium dwarf-cloudsc-fortran "\${OMP_NUM_THREADS-1} 131072 ${cloudsc_block_size}")
211 add_cloudsc_test(fortran-big dwarf-cloudsc-fortran "\${OMP_NUM_THREADS-1} 524288 ${cloudsc_block_size}")
214 if(ENABLE_cloudsc_acc)
215 add_cloudsc_test(gpu-acc dwarf-cloudsc-gpu-scc-k-caching "1 163840 128")
218 if(ENABLE_cloudsc_omp_gpu)
219 add_cloudsc_test(gpu-omp dwarf-cloudsc-gpu-omp-scc-hoist "1 163840 128")
222 if(ENABLE_cloudsc_cuda)
223 add_cloudsc_test(gpu-cuda dwarf-cloudsc-c-cuda-k-caching "1 163840 128")
226 if(ENABLE_cloudsc_hip)
227 add_cloudsc_test(gpu-hip dwarf-cloudsc-hip-k-caching "1 163840 128")
231 add_cloudsc_test(mpi-nproma32 dwarf-cloudsc-fortran "\${OMP_NUM_THREADS-1} 2097152 32")
232 add_cloudsc_test(mpi-nproma64 dwarf-cloudsc-fortran "\${OMP_NUM_THREADS-1} 2097152 64")
233 add_cloudsc_test(mpi-nproma128 dwarf-cloudsc-fortran "\${OMP_NUM_THREADS-1} 2097152 128")
234 add_cloudsc_test(mpi-nproma256 dwarf-cloudsc-fortran "\${OMP_NUM_THREADS-1} 2097152 256")
237endif(cloudsc_enabled)