Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 505b7cd

Browse files
committed
CMake minimum v3.15 for GPU builds.
1 parent b520ace commit 505b7cd

2 files changed

Lines changed: 73 additions & 17 deletions

File tree

CMake/OpenAccHelper.cmake

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,38 @@ if(CORENRN_ENABLE_GPU)
1414
if(CORENRN_ENABLE_CUDA_UNIFIED_MEMORY)
1515
add_compile_definitions(CORENEURON_UNIFIED_MEMORY)
1616
endif()
17-
# This is a lazy way of getting the major/minor versions separately without parsing
18-
# ${CMAKE_CUDA_COMPILER_VERSION}
19-
find_package(CUDAToolkit 9.0 REQUIRED)
20-
# Be a bit paranoid
21-
if(NOT ${CMAKE_CUDA_COMPILER_VERSION} STREQUAL ${CUDAToolkit_VERSION})
22-
message(
23-
FATAL_ERROR
24-
"CUDA compiler (${CMAKE_CUDA_COMPILER_VERSION}) and toolkit (${CUDAToolkit_VERSION}) versions are not the same!"
25-
)
17+
if(${CMAKE_VERSION} VERSION_LESS 3.17)
18+
# Hopefully we can drop this soon. Parse ${CMAKE_CUDA_COMPILER_VERSION} into a shorter X.Y
19+
# version without any patch version.
20+
if(NOT ${CMAKE_CUDA_COMPILER_ID} STREQUAL "NVIDIA")
21+
message(FATAL_ERROR "Unsupported CUDA compiler ${CMAKE_CUDA_COMPILER_ID}")
22+
endif()
23+
# Parse CMAKE_CUDA_COMPILER_VERSION=x.y.z into CUDA_VERSION=x.y
24+
string(FIND ${CMAKE_CUDA_COMPILER_VERSION} . first_dot)
25+
math(EXPR first_dot_plus_one "${first_dot}+1")
26+
string(SUBSTRING ${CMAKE_CUDA_COMPILER_VERSION} ${first_dot_plus_one} -1 minor_and_later)
27+
string(FIND ${minor_and_later} . second_dot_relative)
28+
if(${first_dot} EQUAL -1 OR ${second_dot_relative} EQUAL -1)
29+
message(
30+
FATAL_ERROR
31+
"Failed to parse a CUDA_VERSION from CMAKE_CUDA_COMPILER_VERSION=${CMAKE_CUDA_COMPILER_VERSION}"
32+
)
33+
endif()
34+
math(EXPR second_dot_plus_one "${first_dot}+${second_dot_relative}+1")
35+
string(SUBSTRING ${CMAKE_CUDA_COMPILER_VERSION} 0 ${second_dot_plus_one}
36+
CORENRN_CUDA_VERSION_SHORT)
37+
else()
38+
# This is a lazy way of getting the major/minor versions separately without parsing
39+
# ${CMAKE_CUDA_COMPILER_VERSION}
40+
find_package(CUDAToolkit 9.0 REQUIRED)
41+
# Be a bit paranoid
42+
if(NOT ${CMAKE_CUDA_COMPILER_VERSION} STREQUAL ${CUDAToolkit_VERSION})
43+
message(
44+
FATAL_ERROR
45+
"CUDA compiler (${CMAKE_CUDA_COMPILER_VERSION}) and toolkit (${CUDAToolkit_VERSION}) versions are not the same!"
46+
)
47+
endif()
48+
set(CORENRN_CUDA_VERSION_SHORT "${CUDAToolkit_VERSION_MAJOR}.${CUDAToolkit_VERSION_MINOR}")
2649
endif()
2750
# -acc enables OpenACC support, -cuda links CUDA libraries and (very importantly!) seems to be
2851
# required to make the NVHPC compiler do the device code linking. Otherwise the explicit CUDA
@@ -32,8 +55,7 @@ if(CORENRN_ENABLE_GPU)
3255
# due to e.g. __CUDACC__ being defined. See https://github.com/BlueBrain/CoreNeuron/issues/607 for
3356
# more information about this. -gpu=cudaX.Y ensures that OpenACC code is compiled with the same
3457
# CUDA version as is used for the explicit CUDA code.
35-
set(NVHPC_ACC_COMP_FLAGS
36-
"-acc -gpu=cuda${CUDAToolkit_VERSION_MAJOR}.${CUDAToolkit_VERSION_MINOR}")
58+
set(NVHPC_ACC_COMP_FLAGS "-acc -gpu=cuda${CORENRN_CUDA_VERSION_SHORT}")
3759
set(NVHPC_ACC_LINK_FLAGS "-cuda")
3860
# Make sure that OpenACC code is generated for the same compute capabilities as the explicit CUDA
3961
# code. Otherwise there may be confusing linker errors. We cannot rely on nvcc and nvc++ using the

CMakeLists.txt

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,17 @@ if(${CMAKE_CXX_COMPILER_ID} STREQUAL "PGI" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL
111111
else()
112112
set(CORENRN_HAVE_NVHPC_COMPILER OFF)
113113
endif()
114+
114115
if(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()
137171
endif()
138172

139173
# =============================================================================

0 commit comments

Comments
 (0)