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_gpu)
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}
104 # execute 'true' instead of the patch command when condition is true
105 PATCH_COMMAND $<IF:$<NOT:$<BOOL:${ENABLE_cloudsc_omp}>>,true,${Patch_EXECUTABLE} -p1 -i ${CMAKE_SOURCE_DIR}/projects/cloudsc_omp_link.patch>
106 COMMAND $<IF:$<BOOL:${ENABLE_cloudsc_omp_gpu}>,true,${Patch_EXECUTABLE} -p1 -i ${CMAKE_SOURCE_DIR}/projects/cloudsc_no_omp_target.patch>
108 CONFIGURE_COMMAND env
109 PATH=$ENV{PATH} CPATH=$ENV{CPATH} C_INCLUDE_PATH=$ENV{C_INCLUDE_PATH}
110 LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} LIBRARY_PATH=$ENV{LIBRARY_PATH}
111 PKG_CONFIG_PATH=<INSTALL_DIR>/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}
112 CC=${MPI_C_COMPILER} CXX=${MPI_CXX_COMPILER} FC=${MPI_Fortran_COMPILER}
113 ecbuild -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER}
114 -DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER}
115 -DCMAKE_Fortran_COMPILER:FILEPATH=${CMAKE_Fortran_COMPILER}
116 -DCMAKE_Fortran_FLAGS=${CMAKE_Fortran_FLAGS}
117 -DBUILD_SHARED_LIBS:BOOL=${BUILD_SHARED_LIBS}
118 -DCMAKE_INSTALL_PREFIX:FILEPATH=<INSTALL_DIR>
120 -DENABLE_CLOUDSC_C=OFF
121 -DENABLE_CLOUDSC_FORTRAN=ON
123 -DENABLE_ACC:BOOL=${ENABLE_cloudsc_acc}
124 -DENABLE_CLOUDSC_GPU_SCC_K_CACHING=${ENABLE_cloudsc_acc}
126 -DENABLE_OMP:BOOL=${cloudsc_omp}
127 -DENABLE_CLOUDSC_GPU_OMP_SCC_HOIST=${ENABLE_cloudsc_omp_gpu}
129 -DENABLE_CUDA:BOOL=${ENABLE_cloudsc_cuda}
130 -DENABLE_CLOUDSC_C_CUDA=${ENABLE_cloudsc_cuda}
132 -DENABLE_HIP:BOOL=${ENABLE_cloudsc_hip}
133 -DENABLE_CLOUDSC_HIP:BOOL=${ENABLE_cloudsc_hip}
135 -DENABLE_MPI:BOOL=${mpi_enabled}
136 -DENABLE_SINGLE_PRECISION=${ENABLE_cloudsc_single_precision}
138 -DCMAKE_POLICY_DEFAULT_CMP0104=NEW # initialize CMAKE_CUDA_ARCHITECTURES https://cmake.org/cmake/help/latest/policy/CMP0104.html
139 ${default_cmake_args} ${cloudsc_cmake_args}
141 BUILD_COMMAND ${CMAKE_MAKE_PROGRAM} -j${BUILD_PARALLEL_LEVEL} ${default_build_args} ${cloudsc_build_args}
142 INSTALL_COMMAND ${CMAKE_MAKE_PROGRAM} -j${BUILD_PARALLEL_LEVEL} install
143 COMMAND ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR>/config-files
144 <INSTALL_DIR>/share/dwarf-p-cloudsc
145 COMMAND ${CMAKE_COMMAND} -E make_directory
146 <BINARY_DIR>/test-dwarf-p-cloudsc)
148 ExternalProject_Get_Property(cloudsc INSTALL_DIR)
149 set(cloudsc_DIR ${INSTALL_DIR})
150 set(cloudsc_INPUT_DIR ${cloudsc_DIR}/share/dwarf-p-cloudsc)
152 set_if_not_present(ENV{PATH} "$ENV{PATH}" "${cloudsc_DIR}/bin")
154 ENV{LD_LIBRARY_PATH} "$ENV{LD_LIBRARY_PATH}" "${cloudsc_DIR}/lib"
155 "${cloudsc_DIR}/lib64")
157 ExternalProject_Get_Property(cloudsc BINARY_DIR)
158 set(cloudsc_BIN_DIR ${BINARY_DIR})
161 # common for all tests
163 NAME cloudsc-stage-in
164 COMMAND $<TARGET_FILE:job_launcher> --name=cloudsc-stage-in
167 cp ${cloudsc_INPUT_DIR}/{reference.h5,input.h5} .
168 WORKING_DIRECTORY ${cloudsc_BIN_DIR}/test-dwarf-p-cloudsc)
170 set_tests_properties(cloudsc-stage-in PROPERTIES FIXTURES_SETUP _setup-cloudsc)
172 function(link_cloudsc_tests testname validation)
173 set_tests_properties(${testname} PROPERTIES FIXTURES_REQUIRED _setup-cloudsc)
174 if(TEST ${validation})
175 set_tests_properties(${testname} PROPERTIES FIXTURES_SETUP _run-${testname})
176 set_tests_properties(${validation} PROPERTIES FIXTURES_REQUIRED _run-${testname})
180 function(add_cloudsc_test testname executable args)
182 NAME cloudsc-${testname}
184 env CPATH=$ENV{CPATH} C_INCLUDE_PATH=$ENV{C_INCLUDE_PATH}
185 PATH=$ENV{PATH} LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}
186 LIBRARY_PATH=$ENV{LIBRARY_PATH} $<TARGET_FILE:job_launcher>
187 --name=cloudsc-${testname}
190 ${executable} ${args}
191 WORKING_DIRECTORY ${cloudsc_BIN_DIR}/test-dwarf-p-cloudsc)
193 # The validation is expected to fail for single precision due to a lack
194 # of references in CLOUDSC itself.
195 if(NOT ENABLE_cloudsc_single_precision)
197 NAME cloudsc-${testname}-validation
199 $<TARGET_FILE:job_launcher> --name=cloudsc-${testname}-validation
202 ${CMAKE_SOURCE_DIR}/validation/cloudsc.sh
203 ${LOG_DIR}/cloudsc-${testname}.log
204 WORKING_DIRECTORY ${cloudsc_BIN_DIR}/test-dwarf-p-cloudsc)
207 link_cloudsc_tests(cloudsc-${testname}
208 cloudsc-${testname}-validation)
211 # the first parameter is the number of threads (need to be adapted in the job-launcher)
212 add_cloudsc_test(fortran-small dwarf-cloudsc-fortran "\${OMP_NUM_THREADS-1} 16384 ${cloudsc_block_size}")
213 add_cloudsc_test(fortran-medium dwarf-cloudsc-fortran "\${OMP_NUM_THREADS-1} 131072 ${cloudsc_block_size}")
214 add_cloudsc_test(fortran-big dwarf-cloudsc-fortran "\${OMP_NUM_THREADS-1} 524288 ${cloudsc_block_size}")
217 if(ENABLE_cloudsc_acc)
218 add_cloudsc_test(gpu-acc dwarf-cloudsc-gpu-scc-k-caching "1 163840 128")
221 if(ENABLE_cloudsc_omp_gpu)
222 add_cloudsc_test(gpu-omp dwarf-cloudsc-gpu-omp-scc-hoist "1 163840 128")
225 if(ENABLE_cloudsc_cuda)
226 add_cloudsc_test(gpu-cuda dwarf-cloudsc-c-cuda-k-caching "1 163840 128")
229 if(ENABLE_cloudsc_hip)
230 add_cloudsc_test(gpu-hip dwarf-cloudsc-hip-k-caching "1 163840 128")
234 add_cloudsc_test(mpi-nproma32 dwarf-cloudsc-fortran "\${OMP_NUM_THREADS-1} 2097152 32")
235 add_cloudsc_test(mpi-nproma64 dwarf-cloudsc-fortran "\${OMP_NUM_THREADS-1} 2097152 64")
236 add_cloudsc_test(mpi-nproma128 dwarf-cloudsc-fortran "\${OMP_NUM_THREADS-1} 2097152 128")
237 add_cloudsc_test(mpi-nproma256 dwarf-cloudsc-fortran "\${OMP_NUM_THREADS-1} 2097152 256")
240endif(cloudsc_enabled)