@@ -111,11 +111,17 @@ if(${CMAKE_CXX_COMPILER_ID} STREQUAL "PGI" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL
111111else ()
112112 set (CORENRN_HAVE_NVHPC_COMPILER OFF )
113113endif ()
114+
114115if (CORENRN_ENABLE_GPU)
115- # find_package(CUDAToolkit), for example
116- if (${CMAKE_VERSION } VERSION_LESS 3.17)
117- message (FATAL_ERROR "GPU support requires at least CMake v3.17!" )
116+ # Older CMake versions have not been tested for GPU/CUDA/OpenACC support after
117+ # https://github.com/BlueBrain/CoreNeuron/pull/609.
118+ # https://cmake.org/cmake/help/latest/release/3.14.html#properties suggests there would be
119+ # problems because of expressions like set_target_properties(lfp_test_bin PROPERTIES
120+ # CUDA_RESOLVE_DEVICE_SYMBOLS OFF)
121+ if (${CMAKE_VERSION } VERSION_LESS 3.15)
122+ message (FATAL_ERROR "GPU support requires at least CMake v3.15!" )
118123 endif ()
124+
119125 # Fail hard and early if we don't have the PGI/NVHPC compiler.
120126 if (NOT CORENRN_HAVE_NVHPC_COMPILER)
121127 message (
@@ -124,16 +130,44 @@ if(CORENRN_ENABLE_GPU)
124130 " Use NVIDIA HPC SDK with -DCMAKE_C_COMPILER=nvc -DCMAKE_CUDA_COMPILER=nvcc -DCMAKE_CXX_COMPILER=nvc++"
125131 )
126132 endif ()
133+
127134 # Set some sensible default CUDA architectures.
128135 if (NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
129136 set (CMAKE_CUDA_ARCHITECTURES 60 70)
130137 message (STATUS "Setting default CUDA architectures to ${CMAKE_CUDA_ARCHITECTURES} " )
131138 endif ()
139+
132140 # Enable CUDA language support.
133141 enable_language (CUDA )
134- # nvc++ -cuda implicitly links dynamically to libcudart.so. Setting this makes sure that CMake
135- # does not add -lcudart_static and trigger errors due to mixed dynamic/static linkage.
136- set (CMAKE_CUDA_RUNTIME_LIBRARY Shared)
142+
143+ # Prefer shared libcudart.so
144+ if (${CMAKE_VERSION } VERSION_LESS 3.17)
145+ # Ugly workaround from https://gitlab.kitware.com/cmake/cmake/-/issues/17559, remove when
146+ # possible
147+ if (CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES)
148+ list (REMOVE_ITEM CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES "cudart_static" )
149+ list (REMOVE_ITEM CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES "cudadevrt" )
150+ list (APPEND CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES "cudart" )
151+ endif ()
152+ if (CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES)
153+ list (REMOVE_ITEM CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES "cudart_static" )
154+ list (REMOVE_ITEM CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES "cudadevrt" )
155+ list (APPEND CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES "cudart" )
156+ endif ()
157+ else ()
158+ # nvc++ -cuda implicitly links dynamically to libcudart.so. Setting this makes sure that CMake
159+ # does not add -lcudart_static and trigger errors due to mixed dynamic/static linkage.
160+ set (CMAKE_CUDA_RUNTIME_LIBRARY Shared)
161+ endif ()
162+
163+ # Patch CUDA_ARCHITECTURES support into older CMake versions
164+ if (${CMAKE_VERSION } VERSION_LESS 3.18)
165+ foreach (cuda_arch ${CMAKE_CUDA_ARCHITECTURES} )
166+ string (
167+ APPEND CMAKE_CUDA_FLAGS
168+ "--generate-code=arch=compute_${cuda_arch} ,code=[compute_${cuda_arch} ,sm_${cuda_arch} ]" )
169+ endforeach ()
170+ endif ()
137171endif ()
138172
139173# =============================================================================
0 commit comments