-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
358 lines (324 loc) · 9.62 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#
# Copyright (c) 2023 Armin Sobhani (https://arminsobhani.ca)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
cmake_minimum_required(VERSION 3.21...3.26)
## detect if p2rng is being bundled, disable installations in that case
#
if(NOT DEFINED PROJECT_NAME)
set(NOT_SUBPROJECT ON)
else()
set(NOT_SUBPROJECT OFF)
set(P2RNG_COMPONENTS openmp CACHE STRING "Required components")
endif()
## Set default build type to Release if none was specified
#
include(cmake/BuildType.cmake)
## life is about choices...
#
option(P2RNG_ENABLE_TESTS "Enable the unit tests ?" ON)
option(P2RNG_ENABLE_BENCHMARKS "Enable benchmarks ?" ON)
## finally our project...
#
project(
p2rng
VERSION 1.0.0
DESCRIPTION "A modern header-only C++ library for parallel algorithmic (pseudo) random number generation supporting OpenMP, CUDA, ROCm and oneAPI"
LANGUAGES CXX
)
## prevent in-source builds
#
if (CMAKE_BINARY_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
message(FATAL_ERROR "Building in-source is not supported! Create a build dir and remove ${CMAKE_SOURCE_DIR}/CMakeCache.txt")
endif()
## necessary modules
#
include(FetchContent)
include(CheckLanguage)
## for systems with main libraries in non-standard locations
#
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH true)
## make cache variables for install destinations
#
include(GNUInstallDirs)
## set target API
#
set(P2RNG_TARGET_API "openmp" CACHE
STRING "Choose target API ?")
set_property(CACHE P2RNG_TARGET_API PROPERTY STRINGS
"cuda"
"oneapi"
"openmp"
"rocm"
)
## set HIP target
#
set(HIP_TARGET "hip::device" CACHE
STRING "Choose HIP target ?"
)
set_property(CACHE HIP_TARGET PROPERTY STRINGS
"hip::host"
"hip::device"
)
## switch to the target API
#
if(${P2RNG_TARGET_API} STREQUAL cuda)
check_language(CUDA)
if(CMAKE_CUDA_COMPILER)
enable_language(CUDA)
find_package(CUDAToolkit 11 REQUIRED)
else()
message(FATAL_ERROR "No CUDA support found")
endif()
elseif(${P2RNG_TARGET_API} STREQUAL oneapi)
find_package(IntelDPCPP REQUIRED)
find_package(oneDPL REQUIRED)
elseif(${P2RNG_TARGET_API} STREQUAL rocm)
check_language(HIP)
if(CMAKE_HIP_COMPILER)
enable_language(HIP)
else()
list(APPEND CMAKE_PREFIX_PATH /opt/rocm/hip /opt/rocm)
endif()
find_package(hip REQUIRED)
find_package(rocThrust REQUIRED)
elseif(${P2RNG_TARGET_API} STREQUAL openmp)
find_package(OpenMP REQUIRED)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
else()
message(FATAL_ERROR "Wrong P2RNG_TARGET_API: ${P2RNG_TARGET_API}")
endif()
## try to find dependency for requested components based on the
# P2RNG_COMPONENTS cache variable when p2rng is being bundled
#
if (NOT NOT_SUBPROJECT)
foreach(_comp ${P2RNG_COMPONENTS})
if (${_comp} STREQUAL cuda)
find_package(CUDAToolkit 11 QUIET)
elseif(${_comp} STREQUAL oneapi)
find_package(IntelDPCPP QUIET)
find_package(oneDPL QUIET)
elseif(${_comp} STREQUAL rocm)
find_package(hip QUIET)
find_package(rocThrust QUIET)
elseif(${_comp} STREQUAL openmp)
find_package(OpenMP QUIET)
else()
message(FATAL_ERROR "Unsupported p2rng component: ${_comp}")
endif()
endforeach()
endif()
## define p2rng target
#
add_library(${PROJECT_NAME} INTERFACE)
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)
target_include_directories(${PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
set_target_properties(${PROJECT_NAME} PROPERTIES
VERSION ${p2rng_VERSION}
SOVERSION ${p2rng_VERSION_MAJOR}
)
## define p2rng::cuda target
#
add_library(cuda INTERFACE)
add_library(${PROJECT_NAME}::cuda ALIAS cuda)
target_compile_options(cuda INTERFACE
$<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>
$<$<AND:$<CONFIG:Debug>,$<COMPILE_LANGUAGE:CUDA>>:-G;-src-in-ptx>
)
target_link_libraries(cuda INTERFACE
${PROJECT_NAME}::${PROJECT_NAME}
)
## define p2rng::oneapi target
#
add_library(oneapi INTERFACE)
add_library(${PROJECT_NAME}::oneapi ALIAS oneapi)
target_link_libraries(oneapi INTERFACE
${PROJECT_NAME}::${PROJECT_NAME}
oneDPL
)
## define p2rng::rocm target
#
add_library(rocm INTERFACE)
add_library(${PROJECT_NAME}::rocm ALIAS rocm)
target_compile_options(rocm INTERFACE -Wno-unused-result)
target_link_libraries(rocm INTERFACE
${PROJECT_NAME}::${PROJECT_NAME}
roc::rocthrust
)
## define p2rng::openmp target
#
add_library(openmp INTERFACE)
add_library(${PROJECT_NAME}::openmp ALIAS openmp)
target_link_libraries(openmp INTERFACE
${PROJECT_NAME}::${PROJECT_NAME}
OpenMP::OpenMP_CXX
)
## only perform the installation when p2rng is not being used as a subproject
#
if (NOT_SUBPROJECT)
## install the target and create export-set
#
install(
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}-targets
)
## install p2rng header files
#
add_subdirectory(include)
## generate and install export file
#
install(
EXPORT ${PROJECT_NAME}-targets
FILE ${PROJECT_NAME}-targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
## install the target and create export-set for cuda
#
install(
TARGETS cuda
EXPORT cuda-targets
)
## generate and install export file for cuda
#
install(
EXPORT cuda-targets
FILE ${PROJECT_NAME}-cuda-targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
## install the target and create export-set for oneapi
#
install(
TARGETS oneapi
EXPORT oneapi-targets
)
## generate and install export file for oneapi
#
install(
EXPORT oneapi-targets
FILE ${PROJECT_NAME}-oneapi-targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
## install the target and create export-set for rocm
#
install(
TARGETS rocm
EXPORT rocm-targets
)
## generate and install export file for rocm
#
install(
EXPORT rocm-targets
FILE ${PROJECT_NAME}-rocm-targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
## install the target and create export-set for openmp
#
install(
TARGETS openmp
EXPORT openmp-targets
)
## generate and install export file for openmp
#
install(
EXPORT openmp-targets
FILE ${PROJECT_NAME}-openmp-targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
## generate the config file that includes the exports
#
include(CMakePackageConfigHelpers)
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/${PROJECT_NAME}-config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
NO_CHECK_REQUIRED_COMPONENTS_MACRO
)
## create version file
#
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake"
VERSION "${p2rng_VERSION_MAJOR}.${p2rng_VERSION_MINOR}"
COMPATIBILITY AnyNewerVersion
)
## install p2rng export config files
#
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
## make export target available from the build tree
#
export(EXPORT ${PROJECT_NAME}-targets
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-targets.cmake"
NAMESPACE ${PROJECT_NAME}::
)
## add unit tests
#
if(${P2RNG_ENABLE_TESTS})
enable_testing()
find_package(
Catch2 3
HINTS $ENV{HOME} /usr/local /opt/local /opt
)
if(NOT Catch2_FOUND)
message(STATUS "Fetching Catch2 library...")
FetchContent_Declare(
catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.4.0
)
FetchContent_MakeAvailable(Catch2)
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
endif()
include(Catch)
add_subdirectory(test)
endif()
## add benchmarks
#
if(${P2RNG_ENABLE_BENCHMARKS})
find_package(benchmark
CONFIG
HINTS $ENV{HOME} /usr/local /opt/local /opt
)
if(NOT benchmark_FOUND)
message(STATUS "Fetching Google Benchmark library...")
FetchContent_Declare(
benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG v1.7.1
)
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL
"Enable testing of the benchmark library."
)
FetchContent_MakeAvailable(benchmark)
endif()
add_subdirectory(perf)
endif()
endif(NOT_SUBPROJECT)