This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
CMakeLists.txt
259 lines (226 loc) · 9.45 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
##########################
## Set Project version
##########################
cmake_minimum_required(VERSION 3.21)
set(LOGO [=[
░█░░░▀█▀░█▀▀░█░█░▀█▀░█▀█░▀█▀░█▀█░█▀▀░░░░█▀▀░█▀█░█░█
░█░░░░█░░█░█░█▀█░░█░░█░█░░█░░█░█░█░█░░░░█░█░█▀▀░█░█
░▀▀▀░▀▀▀░▀▀▀░▀░▀░░▀░░▀░▀░▀▀▀░▀░▀░▀▀▀░▀░░▀▀▀░▀░░░▀▀▀
]=])
message(${LOGO})
project(pennylane_lightning_gpu
DESCRIPTION "Lightning-GPU bindings for PennyLane. Backed by NVIDIA cuQuantum SDK."
LANGUAGES CXX C CUDA
)
##########################
## Utility methods
##########################
# Read and set pennylane_lightning_gpu version
function(set_pennylane_lightning_gpu_version VERSION_FILE_PATH)
file(STRINGS ${VERSION_FILE_PATH} VERSION_FILE_STR)
foreach (LINE IN LISTS VERSION_FILE_STR)
if("${LINE}" MATCHES "__version__.*")
set(VERSION_LINE_STR "${LINE}")
endif()
endforeach()
string(REGEX REPLACE "__version__ = \"(.*)\"" "\\1" VERSION_STRING ${VERSION_LINE_STR})
set(VERSION_STRING ${VERSION_STRING} PARENT_SCOPE)
endfunction()
set_pennylane_lightning_gpu_version(${PROJECT_SOURCE_DIR}/pennylane_lightning_gpu/_version.py)
message(STATUS "pennylane_lightning_gpu version ${VERSION_STRING}")
set(PROJECT_VERSION ${VERSION_STRING})
##########################
## Enfore C++ Standard
##########################
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
##########################
## Set Default Options
##########################
# Compiler options
option(PLLGPU_ENABLE_NATIVE "Enable native CPU build tuning" OFF)
option(PLLGPU_BUILD_TESTS "Build cpp tests" OFF)
option(PLLGPU_ENABLE_WARNINGS "Enable warnings" ON)
option(PLLGPU_ENABLE_CLANG_TIDY "Enable clang-tidy build checks" OFF)
option(PLLGPU_DISABLE_CUDA_SAFETY "Build without CUDA call safety checks" OFF)
option(PLLGPU_ENABLE_PYTHON "Build Python bindings" ON)
option(PLLGPU_ENABLE_SANITIZER "Enable address sanitizer" OFF)
option(PLLGPU_ENABLE_COVERAGE "Enable code coverage" OFF)
option(PLLGPU_ENABLE_OPENMP "Enable OpenMP support" ON)
option(PLLGPU_ENABLE_MPI "Enable MPI support" OFF)
set(CRAY_MPICH OFF)
# Build options
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RelWithDebInfo)
endif()
# Ensure the libraries can see additional libs at same level;
# Required for external deps when loading in Python
set(CMAKE_BUILD_RPATH_USE_ORIGIN ON)
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_RPATH "$ORIGIN/../cuquantum/lib:$ORIGIN/../cuquantum/lib64:$ORIGIN/")
set(CMAKE_INSTALL_RPATH "$ORIGIN/../cuquantum/lib:$ORIGIN/../cuquantum/lib64:$ORIGIN/")
if(PLLGPU_ENABLE_CLANG_TIDY)
if (NOT DEFINED CLANG_TIDY_BINARY)
set(CLANG_TIDY_BINARY clang-tidy)
endif()
set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_BINARY};
-extra-arg=-std=c++17;
)
endif()
if(PLLGPU_ENABLE_SANITIZER)
add_compile_options(-fsanitize=address)
add_link_options(-fsanitize=address -static-libasan)
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
ENDIF()
##########################
## Fetch dependencies
##########################
# Add pybind11
include(FetchContent)
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11.git
GIT_TAG v2.10.1
)
FetchContent_MakeAvailable(pybind11)
# Add PennyLane-Lightning
if (NOT DEFINED LIGHTNING_RELEASE_TAG)
set(LIGHTNING_RELEASE_TAG "latest_release")
endif()
# Disable non-required lengthy build-steps from PennyLane Lightning
set(ENABLE_OPENMP OFF)
set(BUILD_TESTS OFF)
set(ENABLE_KOKKOS OFF)
FetchContent_Declare(
pennylane_lightning
GIT_REPOSITORY https://github.com/PennyLaneAI/pennylane-lightning.git
GIT_TAG master
)
FetchContent_MakeAvailable(pennylane_lightning)
find_package(CUDA REQUIRED)
find_package(CUDAToolkit REQUIRED)
find_package (Python COMPONENTS Interpreter Development)
if(PLLGPU_ENABLE_MPI)
find_package(MPI REQUIRED)
if(MPI_FOUND)
message(STATUS "MPI found.")
else()
message(FATAL_ERROR "MPI is NOT found.")
endif()
string(FIND "${CMAKE_SYSTEM}" "cray" subStrIdx)
if(NOT subStrIdx EQUAL -1)
if(DEFINED ENV{CRAY_MPICH_VERSION} AND NOT "$ENV{CRAY_MPICH_VERSION}" STREQUAL "none")
find_library(MPI_GTL
NAMES libmpi_gtl_cuda.so libmpi_gtl_cuda.so.0 libmpi_gtl_cuda.so.0.0.0
HINTS ENV CRAY_LD_LIBRARY_PATH
ENV LD_LIBRARY_PATH
)
set(CRAY_MPICH ON)
endif()
endif()
endif()
find_library(CUSTATEVEC_LIB
NAMES libcustatevec.so.1 custatevec.so.1
HINTS /usr/lib
/usr/local/cuda
/usr/local/lib
/opt
/opt/cuda
lib
lib64
${CUQUANTUM_SDK}/lib
${CUQUANTUM_SDK}/lib64
${CUDAToolkit_LIBRARY_DIR}
${CUDA_TOOLKIT_ROOT_DIR}/lib
${CUDA_TOOLKIT_ROOT_DIR}/lib64
${Python_SITELIB}/cuquantum/lib
ENV LD_LIBRARY_PATH
)
find_file( CUSTATEVEC_INC
NAMES custatevec.h
HINTS /usr/include
/usr/local/cuda
/usr/local/include
/opt
/opt/cuda
include
${CUQUANTUM_SDK}/include
${CUDAToolkit_INCLUDE_DIRS}
${CUDA_TOOLKIT_ROOT_DIR}/include
${Python_SITELIB}/cuquantum/include
ENV CPATH
)
if(NOT CUSTATEVEC_LIB OR NOT CUSTATEVEC_INC)
message(FATAL_ERROR "\nUnable to find cuQuantum SDK installation. Please ensure it is correctly installed and available on path.")
endif()
##########################
## Compile options
##########################
set(CUDA_SEPARABLE_COMPILATION ON)
############################
## Create libraries
############################
add_subdirectory(pennylane_lightning_gpu/src)
add_library(pennylane_lightning_gpu INTERFACE)
target_link_libraries(pennylane_lightning_gpu INTERFACE pennylane_lightning
lightning_gpu_utils
lightning_gpu_simulator
lightning_gpu_algorithms)
target_include_directories(pennylane_lightning_gpu INTERFACE "pennylane_lightning_gpu/src")
if(PLLGPU_ENABLE_MPI AND ${CRAY_MPICH})
target_link_libraries(pennylane_lightning_gpu INTERFACE ${CUSTATEVEC_LIB} ${CUDA_SHARED_RT} ${MPI_GTL})
else()
target_link_libraries(pennylane_lightning_gpu INTERFACE ${CUSTATEVEC_LIB} ${CUDA_SHARED_RT})
endif()
# Create binding module
if(PLLGPU_ENABLE_PYTHON)
pybind11_add_module(lightning_gpu_qubit_ops "pennylane_lightning_gpu/src/bindings/Bindings.cpp")
target_link_libraries(lightning_gpu_qubit_ops PRIVATE lightning_algorithms lightning_qubit lightning_qubit_algorithms pennylane_lightning_gpu)
set_target_properties(lightning_gpu_qubit_ops PROPERTIES CXX_VISIBILITY_PRESET hidden)
set_target_properties(lightning_gpu_qubit_ops PROPERTIES INSTALL_RPATH "$ORIGIN/../cuquantum/lib:$ORIGIN/../cuquantum/lib64:$ORIGIN/")
target_compile_options(lightning_gpu_qubit_ops PRIVATE "$<$<CONFIG:RELEASE>:-W>")
target_compile_definitions(lightning_gpu_qubit_ops PRIVATE VERSION_INFO=${VERSION_STRING})
if(PLLGPU_ENABLE_MPI)
option(ENABLE_MPI "Enable MPI support" ON)
target_compile_definitions(lightning_gpu_qubit_ops PRIVATE ENABLE_MPI)
endif()
target_include_directories(lightning_gpu_qubit_ops PRIVATE ${CUDA_TOOLKIT_ROOT_DIR}/include)
target_link_libraries(lightning_gpu_qubit_ops PRIVATE CUDA::cudart)
endif()
# To avoid DSO errors on platforms preferring static linkage, uncomment the following line:
# string(REPLACE "libcudart_static.a" "libcudart.so" CUDA_SHARED_RT "${CUDA_LIBRARIES}")
target_include_directories(pennylane_lightning_gpu INTERFACE ${CUDA_TOOLKIT_ROOT_DIR}/include)
target_link_libraries(pennylane_lightning_gpu INTERFACE CUDA::cudart)
set_target_properties(pennylane_lightning_gpu PROPERTIES INSTALL_RPATH "$ORIGIN/../cuquantum/lib:$ORIGIN/../cuquantum/lib64:$ORIGIN/")
if(PLLGPU_ENABLE_OPENMP)
find_package(OpenMP REQUIRED)
target_link_libraries(pennylane_lightning_gpu INTERFACE OpenMP::OpenMP_CXX)
endif()
if(PLLGPU_ENABLE_MPI)
find_package(MPI REQUIRED)
target_link_libraries(pennylane_lightning_gpu INTERFACE MPI::MPI_CXX)
endif()
if(PLLGPU_DISABLE_CUDA_SAFETY)
target_compile_options(pennylane_lightning_gpu INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-DCUDA_UNSAFE>)
endif()
if(PLLGPU_ENABLE_WARNINGS)
target_compile_options(pennylane_lightning_gpu INTERFACE
$<$<COMPILE_LANGUAGE:CXX>:-Wall;-Wextra;-Werror>
)
endif()
if(PLLGPU_ENABLE_NATIVE)
message(STATUS "ENABLE_NATIVE is ON. Using -march=native")
add_compile_options(-march=native)
target_compile_options(pennylane_lightning_gpu INTERFACE -march=native)
target_compile_options(lightning_gpu_qubit_ops PRIVATE -march=native)
endif()
if(PLLGPU_ENABLE_COVERAGE)
message(STATUS "ENABLE_COVERAGE is ON.")
target_compile_options(pennylane_lightning_gpu INTERFACE
$<$<COMPILE_LANGUAGE:CXX>:-fprofile-arcs;-ftest-coverage>)
target_link_libraries(pennylane_lightning_gpu INTERFACE gcov)
endif()
if (PLLGPU_BUILD_TESTS)
enable_testing()
endif()