Skip to content

Commit 6a6287c

Browse files
bmhowe23justinlietz
authored andcommitted
Add CUDA build support to libraries (NVIDIA#32)
1 parent 8fa54ad commit 6a6287c

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

libs/qec/CMakeLists.txt

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# the terms of the Apache License 2.0 which accompanies this distribution. #
77
# ============================================================================ #
88

9-
# Requering the same version as the others.
9+
# Requiring the same version as the others.
1010
cmake_minimum_required(VERSION 3.28 FATAL_ERROR)
1111

1212
# Project setup
@@ -52,6 +52,44 @@ option(CUDAQX_QEC_INSTALL_PYTHON
5252
"Install python files alongside the library."
5353
${CUDAQX_INSTALL_PYTHON})
5454

55+
# Check for CUDA Support (ref: cuda-quantum/CMakeLists.txt)
56+
# ==============================================================================
57+
include(CheckLanguage)
58+
check_language(CUDA)
59+
set(CUDA_FOUND FALSE)
60+
# Generate -gencode arch=compute_XX,code=sm_XX for list of supported
61+
# arch values.
62+
# List should be sorted in increasing order.
63+
function(CUDA_get_gencode_args out_args_string arch_values)
64+
# allow the user to pass the list like a normal variable
65+
set(arch_list ${arch_values} ${ARGN})
66+
set(out "")
67+
foreach(arch IN LISTS arch_list)
68+
set(out "${out} -gencode arch=compute_${arch},code=sm_${arch}")
69+
endforeach(arch)
70+
71+
# Repeat the last one as to ensure the generation of PTX for most
72+
# recent virtual architecture for forward compatibility
73+
list(GET arch_list -1 last_arch)
74+
set(out "${out} -gencode arch=compute_${last_arch},code=compute_${last_arch}")
75+
set(${out_args_string} ${out} PARENT_SCOPE)
76+
endfunction()
77+
78+
if(CMAKE_CUDA_COMPILER)
79+
if (NOT CUDA_TARGET_ARCHS)
80+
# Volta, Ampere, Hopper
81+
set(CUDA_TARGET_ARCHS "70;80;90")
82+
endif()
83+
CUDA_get_gencode_args(CUDA_gencode_flags ${CUDA_TARGET_ARCHS})
84+
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -shared -std=c++17 ${CUDA_gencode_flags} --compiler-options -fPIC")
85+
86+
enable_language(CUDA)
87+
set(CUDA_FOUND TRUE)
88+
set(CMAKE_CUDA_STANDARD 17)
89+
set(CMAKE_CUDA_STANDARD_REQUIRED TRUE)
90+
message(STATUS "Cuda language found.")
91+
endif()
92+
5593
# External Dependencies
5694
# ==============================================================================
5795

libs/solvers/CMakeLists.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,44 @@ option(CUDAQX_SOLVERS_INSTALL_PYTHON
6363
"Install python files alongside the library."
6464
${CUDAQX_INSTALL_PYTHON})
6565

66+
# Check for CUDA Support (ref: cuda-quantum/CMakeLists.txt)
67+
# ==============================================================================
68+
include(CheckLanguage)
69+
check_language(CUDA)
70+
set(CUDA_FOUND FALSE)
71+
# Generate -gencode arch=compute_XX,code=sm_XX for list of supported
72+
# arch values.
73+
# List should be sorted in increasing order.
74+
function(CUDA_get_gencode_args out_args_string arch_values)
75+
# allow the user to pass the list like a normal variable
76+
set(arch_list ${arch_values} ${ARGN})
77+
set(out "")
78+
foreach(arch IN LISTS arch_list)
79+
set(out "${out} -gencode arch=compute_${arch},code=sm_${arch}")
80+
endforeach(arch)
81+
82+
# Repeat the last one as to ensure the generation of PTX for most
83+
# recent virtual architecture for forward compatibility
84+
list(GET arch_list -1 last_arch)
85+
set(out "${out} -gencode arch=compute_${last_arch},code=compute_${last_arch}")
86+
set(${out_args_string} ${out} PARENT_SCOPE)
87+
endfunction()
88+
89+
if(CMAKE_CUDA_COMPILER)
90+
if (NOT CUDA_TARGET_ARCHS)
91+
# Volta, Ampere, Hopper
92+
set(CUDA_TARGET_ARCHS "70;80;90")
93+
endif()
94+
CUDA_get_gencode_args(CUDA_gencode_flags ${CUDA_TARGET_ARCHS})
95+
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -shared -std=c++17 ${CUDA_gencode_flags} --compiler-options -fPIC")
96+
97+
enable_language(CUDA)
98+
set(CUDA_FOUND TRUE)
99+
set(CMAKE_CUDA_STANDARD 17)
100+
set(CMAKE_CUDA_STANDARD_REQUIRED TRUE)
101+
message(STATUS "Cuda language found.")
102+
endif()
103+
66104
# External Dependencies
67105
# ==============================================================================
68106

0 commit comments

Comments
 (0)