-
Notifications
You must be signed in to change notification settings - Fork 11
/
CMakeLists.txt
279 lines (234 loc) · 9.95 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
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_DISABLE_SOURCE_CHANGES ON)
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RelWithDebInfo)
endif()
# User-defined build options
option(CUDECOMP_BUILD_FORTRAN "Build Fortran bindings" ON)
option(CUDECOMP_ENABLE_NVTX "Enable NVTX ranges" ON)
option(CUDECOMP_ENABLE_NVSHMEM "Enable NVSHMEM" OFF)
option(CUDECOMP_BUILD_EXTRAS "Build benchmark, examples, and tests" OFF)
set(CUDECOMP_CUDA_CC_LIST "70;80;90" CACHE STRING "List of CUDA compute capabilities to build cuDecomp for.")
set(CUDECOMP_NCCL_HOME CACHE STRING "Path to search for NCCL installation. Use to override NVHPC provided NCCL version.")
set(CUDECOMP_NVSHMEM_HOME CACHE STRING "Path to search for NVSHMEM installation. Use to override NVHPC provided NVSHMEM version.")
# Use NVHPC compilers by default
find_program(NVHPC_CXX_BIN "nvc++" REQUIRED)
set(CMAKE_CXX_COMPILER ${NVHPC_CXX_BIN})
if (CUDECOMP_BUILD_FORTRAN)
find_program(NVHPC_Fortran_BIN "nvfortran" REQUIRED)
set(CMAKE_Fortran_COMPILER ${NVHPC_Fortran_BIN})
endif()
# Locate and use NVHPC CMake configuration
string(REPLACE "compilers/bin/nvc++" "cmake" NVHPC_CMAKE_DIR ${NVHPC_CXX_BIN})
set(CMAKE_PREFIX_PATH ${NVHPC_CMAKE_DIR})
# Limit optimization levels of CPU code compilation
set(CMAKE_CXX_FLAGS_RELEASE "-O1 -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O1 -g -DNDEBUG")
set(CMAKE_Fortran_FLAGS_RELEASE "-O1 -DNDEBUG")
set(CMAKE_Fortran_FLAGS_RELWITHDEBINFO "-O1 -g -DNDEBUG")
if (CUDECOMP_BUILD_FORTRAN)
set(LANGS CXX CUDA Fortran)
else()
set(LANGS CXX CUDA)
endif()
project(cudecomp LANGUAGES ${LANGS})
# Detect if Cray compiler wrappers are available to assess if in Cray environment.
# We do not use the Cray compiler wrappers directly for greater flexibility.
find_program(CRAY_CC_BIN "CC")
if (CRAY_CC_BIN)
message(STATUS "Found Cray CC wrapper. Compiling for Cray programming environment.")
endif()
# MPI
find_package(MPI REQUIRED)
if (CRAY_CC_BIN)
# FindMPI does not include Cray GTL (e.g. CUDA-aware) libs
# automatically in Cray environment. Locate it to include in linking.
string(REPLACE ":" ";" CRAY_LIB_PATHS $ENV{CRAY_LD_LIBRARY_PATH})
find_library(CRAY_MPI_GTL_CUDA_LIBRARY REQUIRED
NAMES mpi_gtl_cuda
HINTS ${CRAY_LIB_PATHS}
)
# Cray GTL libs benefit from linking against gdrcopy, so also
# locating that library.
find_library(GDRCOPY_LIBRARY REQUIRED
NAMES gdrapi
)
message(STATUS "Found Cray GTL library: " ${CRAY_MPI_GTL_CUDA_LIBRARY})
message(STATUS "Found GDRCopy library: " ${GDRCOPY_LIBRARY})
endif()
# HPC SDK
find_package(NVHPC REQUIRED COMPONENTS CUDA MATH)
# Set up required include directory flags, NVHPC CMake config only defined library directories
string(REGEX REPLACE "(.+)(\/lib64)$" "\\1/include" NVHPC_CUDA_INCLUDE_DIR ${NVHPC_CUDA_LIBRARY_DIR})
string(REGEX REPLACE "(.+)(\/lib64)$" "\\1/include" NVHPC_CUFFT_INCLUDE_DIR ${NVHPC_MATH_LIBRARY_DIR})
string(REGEX REPLACE "(.+)(\/lib64)$" "\\1/include" NVHPC_CUTENSOR_INCLUDE_DIR ${NVHPC_MATH_LIBRARY_DIR})
# Get NCCL library (with optional override)
if (CUDECOMP_NCCL_HOME)
find_path(NCCL_INCLUDE_DIR REQUIRED
NAMES nccl.h
HINTS ${CUDECOMP_NCCL_HOME}/include
)
find_library(NCCL_LIBRARY REQUIRED
NAMES nccl
HINTS ${CUDECOMP_NCCL_HOME}/lib
)
else()
find_package(NVHPC REQUIRED COMPONENTS NCCL)
find_library(NCCL_LIBRARY
NAMES nccl
HINTS ${NVHPC_NCCL_LIBRARY_DIR}
)
string(REGEX REPLACE "(.+)(\/lib)$" "\\1/include" NCCL_INCLUDE_DIR ${NVHPC_NCCL_LIBRARY_DIR})
endif()
message(STATUS "Using NCCL library: ${NCCL_LIBRARY}")
if (CUDECOMP_ENABLE_NVSHMEM)
# Get NVSHMEM library (with optional override)
if (CUDECOMP_NVSHMEM_HOME)
find_path(NVSHMEM_INCLUDE_DIR REQUIRED
NAMES nvshmem.h
HINTS ${CUDECOMP_NVSHMEM_HOME}/include
)
find_path(NVSHMEM_LIBRARY_DIR REQUIRED
NAMES libnvshmem.a
HINTS ${CUDECOMP_NVSHMEM_HOME}/lib
)
else()
find_package(NVHPC REQUIRED COMPONENTS NVSHMEM)
set(NVSHMEM_LIBRARY_DIR ${NVHPC_NVSHMEM_LIBRARY_DIR})
string(REGEX REPLACE "(.+)(\/lib)$" "\\1/include" NVSHMEM_INCLUDE_DIR ${NVHPC_NVSHMEM_LIBRARY_DIR})
endif()
message(STATUS "Using NVSHMEM installation at: ${NVSHMEM_LIBRARY_DIR}")
endif()
# Building cuDecomp shared lib
add_library(cudecomp SHARED)
set_target_properties(cudecomp PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
# Set NVCC flags for requested compute capability
if (CMAKE_VERSION VERSION_LESS 3.18)
foreach(CUDA_CC ${CUDECOMP_CUDA_CC_LIST})
list(APPEND CUDA_CC_FLAGS -gencode=arch=compute_${CUDA_CC},code=sm_${CUDA_CC})
endforeach()
target_compile_options(cudecomp PRIVATE $<$<COMPILE_LANGUAGE:CUDA>: ${CUDA_CC_FLAGS}>)
else()
set_target_properties(cudecomp PROPERTIES CUDA_ARCHITECTURES "${CUDECOMP_CUDA_CC_LIST}")
endif()
target_compile_options(cudecomp PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--ptxas-options=-v >)
target_sources(cudecomp
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/autotune.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/cudecomp_kernels.cu
${CMAKE_CURRENT_SOURCE_DIR}/src/cudecomp_kernels_rdc.cu
${CMAKE_CURRENT_SOURCE_DIR}/src/cudecomp.cc
)
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/cudecomp_kernels_rdc.cu PROPERTIES COMPILE_FLAGS -rdc=true)
set_target_properties(cudecomp PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON)
target_include_directories(cudecomp
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${MPI_CXX_INCLUDE_DIRS}
${NVHPC_CUDA_INCLUDE_DIR}
${NVHPC_CUTENSOR_INCLUDE_DIR}
${NCCL_INCLUDE_DIR}
)
target_link_libraries(cudecomp PUBLIC NVHPC::CUDART)
target_link_libraries(cudecomp PUBLIC MPI::MPI_CXX)
target_link_libraries(cudecomp PRIVATE NVHPC::CUTENSOR)
target_link_libraries(cudecomp PRIVATE ${NCCL_LIBRARY})
if (CRAY_CC_BIN)
# In Cray environments, add links to GTL and GDRCopy libs for CUDA-aware support
target_link_libraries(cudecomp PRIVATE ${CRAY_MPI_GTL_CUDA_LIBRARY})
target_link_libraries(cudecomp PRIVATE ${GDRCOPY_LIBRARY})
endif()
if (CUDECOMP_ENABLE_NVTX)
target_compile_definitions(cudecomp PRIVATE ENABLE_NVTX)
endif()
if (CUDECOMP_ENABLE_NVSHMEM)
target_compile_definitions(cudecomp PRIVATE ENABLE_NVSHMEM)
target_include_directories(cudecomp
PRIVATE
${NVSHMEM_INCLUDE_DIR}
)
# Get NVSHMEM version from header
if (EXISTS ${NVSHMEM_INCLUDE_DIR}/non_abi/nvshmem_version.h)
file(READ ${NVSHMEM_INCLUDE_DIR}/non_abi/nvshmem_version.h NVSHMEM_VERSION_RAW)
elseif (EXISTS ${NVSHMEM_INCLUDE_DIR}/nvshmem_version.h)
file(READ ${NVSHMEM_INCLUDE_DIR}/nvshmem_version.h NVSHMEM_VERSION_RAW)
else()
file(READ ${NVSHMEM_INCLUDE_DIR}/common/nvshmem_version.h NVSHMEM_VERSION_RAW)
endif()
string(REGEX MATCH "NVSHMEM_VENDOR_MAJOR_VERSION ([0-9]*)" _ ${NVSHMEM_VERSION_RAW})
list(APPEND NVSHMEM_VERSION ${CMAKE_MATCH_1})
string(REGEX MATCH "NVSHMEM_VENDOR_MINOR_VERSION ([0-9]*)" _ ${NVSHMEM_VERSION_RAW})
list(APPEND NVSHMEM_VERSION ${CMAKE_MATCH_1})
list(JOIN NVSHMEM_VERSION "." NVSHMEM_VERSION)
if (NVSHMEM_VERSION VERSION_LESS "2.7")
# NVSHMEM versions before 2.7 will export NCCL symbols erroneously, need to define this flag
target_compile_definitions(cudecomp PRIVATE NVSHMEM_USE_NCCL)
endif()
if (NVSHMEM_VERSION VERSION_LESS "2.5")
target_link_libraries(cudecomp PRIVATE ${NVSHMEM_LIBRARY_DIR}/libnvshmem.a)
else()
target_link_libraries(cudecomp PRIVATE ${NVSHMEM_LIBRARY_DIR}/libnvshmem_host.so)
target_link_libraries(cudecomp PRIVATE ${NVSHMEM_LIBRARY_DIR}/libnvshmem_device.a)
target_link_libraries(cudecomp PUBLIC -L${NVHPC_CUDA_LIBRARY_DIR}/stubs -lnvidia-ml)
endif()
target_link_libraries(cudecomp PUBLIC -L${NVHPC_CUDA_LIBRARY_DIR}/stubs -lcuda)
endif()
set_target_properties(cudecomp PROPERTIES PUBLIC_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/include/cudecomp.h)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/include/cudecomp.h ${CMAKE_BINARY_DIR}/include/cudecomp.h)
install(
TARGETS cudecomp
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include
)
# Building Fortran shared lib and module
if (CUDECOMP_BUILD_FORTRAN)
# Creating -gpu argument string for Fortran files
foreach(CUDA_CC ${CUDECOMP_CUDA_CC_LIST})
list(APPEND CUF_GPU_ARG "cc${CUDA_CC}")
endforeach()
list(APPEND CUF_GPU_ARG "cuda${NVHPC_CUDA_VERSION}")
list(JOIN CUF_GPU_ARG "," CUF_GPU_ARG)
add_library(cudecomp_fort SHARED)
set_target_properties(cudecomp_fort PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set_target_properties(cudecomp_fort PROPERTIES Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/include)
set_target_properties(cudecomp_fort PROPERTIES LINKER_LANGUAGE Fortran)
target_compile_options(cudecomp_fort PRIVATE $<$<COMPILE_LANGUAGE:Fortran>:-cpp -cuda -gpu=${CUF_GPU_ARG}>)
target_sources(
cudecomp_fort
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/cudecomp_m.cuf
)
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/cudecomp_m.cuf PROPERTIES LANGUAGE Fortran)
target_link_libraries(cudecomp_fort PUBLIC MPI::MPI_Fortran)
# Test for MPI_Comm_f2c/c2f
try_compile(
TEST_F2C_RESULT
${CMAKE_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/cmake/test_mpi_f2c.f90
LINK_LIBRARIES MPI::MPI_Fortran
)
if (NOT TEST_F2C_RESULT)
message(STATUS "Could not link MPI_Comm_f2c in Fortran module. Setting -DMPICH flag during module compilation.")
target_compile_definitions(cudecomp_fort PRIVATE MPICH)
endif()
install(
TARGETS cudecomp_fort
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
)
# Install cuDecomp module
install(FILES ${CMAKE_BINARY_DIR}/include/cudecomp.mod DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
endif()
if (CUDECOMP_BUILD_EXTRAS)
add_subdirectory(benchmark)
add_subdirectory(tests/cc)
add_subdirectory(examples/cc/basic_usage)
add_subdirectory(examples/cc/taylor_green)
if (CUDECOMP_BUILD_FORTRAN)
add_subdirectory(tests/fortran)
add_subdirectory(examples/fortran/basic_usage)
add_subdirectory(examples/fortran/poisson)
add_subdirectory(examples/fortran/taylor_green)
endif()
endif()