Skip to content

Commit 86a7cee

Browse files
authored
Revise CUDAARCHS handling (#812)
1 parent d046796 commit 86a7cee

2 files changed

Lines changed: 23 additions & 30 deletions

File tree

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ CXXFLAGS ?= -O3 -std=c++17 -fopenmp
4040
NVCCFLAGS ?= -O3 --std c++17 -Wno-deprecated-gpu-targets
4141
HIPCCFLAGS ?= -O3
4242

43+
# For compatibility with CMake, if $CUDAARCHS is set, use it to set the
44+
# architecture options to nvcc. Otherwise, default to the "native" option,
45+
# which is what our pybind11_interface/GetCUDAARCHS.cmake also does.
46+
ifneq (,$(CUDAARCHS))
47+
# Avoid a common mistake that leads to difficult-to-diagnose errors.
48+
COMMA := ,
49+
ifeq ($(COMMA),$(findstring $(COMMA),$(CUDAARCHS)))
50+
$(error Error: the value of the CUDAARCHS environment variable \
51+
must use semicolons as separators, not commas)
52+
endif
53+
ARCHS := $(subst ;, ,$(CUDAARCHS))
54+
NVCCFLAGS += $(foreach a,$(ARCHS),--generate-code arch=compute_$(a),code=sm_$(a))
55+
else
56+
NVCCFLAGS += -arch=native
57+
endif
58+
4359
# Determine whether we can include CUDA and cuStateVec support. We build for
4460
# CUDA if (i) we find $NVCC or (ii) $CUDA_PATH is set. For cuStateVec, there's
4561
# no way to find the cuQuantum libraries other than by being told, so we rely

pybind_interface/GetCUDAARCHS.cmake

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,12 @@
1515

1616
# Check whether the user has provided info about the GPU(s) installed
1717
# on their system. If not, try to determine what it is automaticaly.
18-
if(DEFINED ENV{CUDAARCHS})
19-
set(CMAKE_CUDA_ARCHITECTURES "$ENV{CUDAARCHS}")
20-
message(STATUS "qsim: using CUDA archs string $ENV{CUDAARCHS}")
18+
if(CMAKE_CUDA_ARCHITECTURES)
19+
# CMake 3.18+ sets this variable from $CUDAARCHS automatically.
20+
message(STATUS "qsim: using CUDA architectures "
21+
"${CMAKE_CUDA_ARCHITECTURES}")
2122
else()
22-
find_program(NVIDIA_SMI nvidia-smi)
23-
if(NVIDIA_SMI)
24-
execute_process(
25-
COMMAND ${NVIDIA_SMI} --query-gpu=compute_cap --format=csv,noheader
26-
COMMAND tr -d .
27-
OUTPUT_VARIABLE DETECTED_ARCHS
28-
RESULT_VARIABLE NVIDIA_SMI_RESULT
29-
OUTPUT_STRIP_TRAILING_WHITESPACE
30-
)
31-
if(NVIDIA_SMI_RESULT EQUAL 0 AND DETECTED_ARCHS)
32-
# The command was successful. The output may contain
33-
# multiple lines if there are multiple GPUs.
34-
string(REPLACE "\n" ";" ARCHS_LIST "${DETECTED_ARCHS}")
35-
set(CMAKE_CUDA_ARCHITECTURES "${ARCHS_LIST}")
36-
else()
37-
message(FATAL_ERROR "nvidia-smi failed or returned no GPU"
38-
" architectures. Please check your"
39-
" NVIDIA driver installation and your"
40-
" command search PATH variable.")
41-
endif()
42-
else()
43-
message(FATAL_ERROR "nvidia-smi not found. Cannot autodetect"
44-
" the current GPU architecture(s). Please"
45-
" set the environment variable CUDAARCHS"
46-
" to an appropriate value and try again.")
47-
endif()
23+
# Compile for all supported major and minor real architectures, and the
24+
# highest major virtual architecture.
25+
set(CMAKE_CUDA_ARCHITECTURES native)
4826
endif()
49-

0 commit comments

Comments
 (0)