Skip to content

Commit e6d509b

Browse files
peterbell10facebook-github-bot
authored andcommitted
Add option to build with CUDAToolkit and enable_language(CUDA) (#336)
Summary: `find_package(CUDA)` is deprecated in newer versions of cmake. This adds the `GLOO_USE_CUDA_TOOLKIT` option to build with `enable_language(CUDA)` and `find_package(CUDAToolkit)` which are the modern cmake replacements. cc malfet Pull Request resolved: #336 Reviewed By: jiayisuse Differential Revision: D38714555 Pulled By: jiayisuse fbshipit-source-id: 3eb5f9c5280f9799e3e910d47b198da651a77331
1 parent 950c0e2 commit e6d509b

File tree

6 files changed

+101
-58
lines changed

6 files changed

+101
-58
lines changed

.circleci/config.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ jobs:
9292
cmake_args:
9393
type: string
9494
default: ""
95+
build_openssl:
96+
type: boolean
97+
default: false
9598
docker:
9699
- image: << parameters.docker_image >>
97100
steps:
@@ -101,6 +104,19 @@ jobs:
101104
command: |
102105
apt-get update
103106
apt-get install -y build-essential cmake libibverbs-dev libssl-dev << parameters.apt_get >>
107+
- when:
108+
condition: << parameters.build_openssl >>
109+
steps:
110+
- run:
111+
name: Install openssl
112+
command: |
113+
apt-get install -y wget perl
114+
wget -q https://www.openssl.org/source/openssl-1.1.1b.tar.gz
115+
tar -xzf openssl-1.1.1b.tar.gz
116+
cd openssl-1.1.1b
117+
./config --prefix=/opt/openssl --openssldir=/opt/openssl/ssl
118+
make -j 2
119+
make install
104120
- run:
105121
name: Install libuv
106122
command: |
@@ -236,6 +252,13 @@ workflows:
236252
name: cuda10.1-all-transports
237253
docker_image: nvidia/cuda:10.1-devel-ubuntu18.04
238254
cmake_args: -DUSE_IBVERBS=ON -DUSE_LIBUV=ON -DUSE_TCP_OPENSSL_LINK=ON -DUSE_CUDA=ON
255+
- build:
256+
name: cuda11.7-all-transports
257+
apt_get: "gcc-9 g++-9"
258+
docker_image: nvidia/cuda:11.7.1-devel-ubuntu22.04
259+
cmake_compiler: -DCMAKE_C_COMPILER=gcc-9 -DCMAKE_CXX_COMPILER=g++-9 -DCMAKE_CUDA_HOST_COMPILER=g++-9
260+
cmake_args: -DUSE_IBVERBS=ON -DUSE_LIBUV=ON -DUSE_TCP_OPENSSL_LINK=ON -DUSE_CUDA=ON -DGLOO_USE_CUDA_TOOLKIT=ON -DOPENSSL_ROOT_DIR=/opt/openssl/
261+
build_openssl: true
239262
- build:
240263
name: tsan-all-transports
241264
docker_image: ubuntu:18.04

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ option(USE_TCP_OPENSSL_LOAD "Build TCP-TLS transport with OpenSSL dynamically lo
4040
if(${USE_TCP_OPENSSL_LINK} AND ${USE_TCP_OPENSSL_LOAD})
4141
message(FATAL_ERROR "USE_TCP_OPENSSL_LINK and USE_TCP_OPENSSL_LOAD are mutually exclusive")
4242
endif()
43+
option(USE_CUDA "Build with CUDA support" OFF)
44+
option(GLOO_USE_CUDA_TOOLKIT "Build CUDA with FindCUDATookit.cmake and enable_language(CUDA)" OFF)
4345

4446
if(MSVC)
4547
message(STATUS "MSVC detected")

cmake/Cuda.cmake

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Known NVIDIA GPU achitectures Gloo can be compiled for.
22
# This list will be used for CUDA_ARCH_NAME = All option
3-
set(gloo_known_gpu_archs "30 35 50 52 60 61 70")
4-
set(gloo_known_gpu_archs7 "30 35 50 52")
5-
set(gloo_known_gpu_archs8 "30 35 50 52 60 61")
3+
set(gloo_known_gpu_archs "")
64

75
################################################################################
86
# Function for selecting GPU arch flags for nvcc based on CUDA_ARCH_NAME
@@ -104,78 +102,88 @@ function(gloo_list_append_if_unique list)
104102
endfunction()
105103

106104
################################################################################
107-
# Short command for cuda compilation
108-
# Usage:
109-
# gloo_cuda_compile(<objlist_variable> <cuda_files>)
110-
macro(gloo_cuda_compile objlist_variable)
111-
foreach(var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
112-
set(${var}_backup_in_cuda_compile_ "${${var}}")
113-
endforeach()
105+
### Non macro section
106+
################################################################################
114107

115-
if(APPLE)
116-
list(APPEND CUDA_NVCC_FLAGS -Xcompiler -Wno-unused-function)
117-
endif()
108+
if(GLOO_USE_CUDA_TOOLKIT)
109+
find_package(CUDAToolkit 7.0 REQUIRED)
110+
set(GLOO_CUDA_VERSION ${CUDAToolkit_VERSION})
118111

119-
cuda_compile(cuda_objcs ${ARGN})
112+
# Convert -O2 -Xcompiler="-O2 -Wall" to "-O2;-Xcompiler=-O2,-Wall"
113+
separate_arguments(GLOO_NVCC_FLAGS UNIX_COMMAND "${CMAKE_CUDA_FLAGS}")
114+
string(REPLACE " " "," GLOO_NVCC_FLAGS "${GLOO_NVCC_FLAGS}")
120115

121-
foreach(var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
122-
set(${var} "${${var}_backup_in_cuda_compile_}")
123-
unset(${var}_backup_in_cuda_compile_)
124-
endforeach()
116+
if(CUDA_USE_STATIC_CUDA_RUNTIME)
117+
set(GLOO_CUDA_LIBRARIES CUDA::cudart_static)
118+
else()
119+
set(GLOO_CUDA_LIBRARIES CUDA::cudart)
120+
endif()
121+
else()
122+
find_package(CUDA 7.0)
123+
if(NOT CUDA_FOUND)
124+
return()
125+
endif()
126+
set(GLOO_CUDA_VERSION ${CUDA_VERSION})
127+
set(GLOO_NVCC_FLAGS "${CUDA_NVCC_FLAGS}")
125128

126-
set(${objlist_variable} ${cuda_objcs})
127-
endmacro()
129+
include_directories(SYSTEM ${CUDA_INCLUDE_DIRS})
130+
set(GLOO_CUDA_LIBRARIES ${CUDA_CUDART_LIBRARY})
128131

129-
################################################################################
130-
### Non macro section
131-
################################################################################
132+
# If the project including us doesn't set any -std=xxx directly, we set it to C++11 here.
133+
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
134+
if((NOT "${GLOO_NVCC_FLAGS}" MATCHES "-std=c\\+\\+") AND (NOT "${GLOO_NVCC_FLAGS}" MATCHES "-std=gnu\\+\\+"))
135+
if(NOT MSVC)
136+
gloo_list_append_if_unique(GLOO_NVCC_FLAGS "-std=c++11")
137+
endif()
138+
endif()
132139

133-
find_package(CUDA 7.0)
134-
if(NOT CUDA_FOUND)
135-
return()
140+
mark_as_advanced(CUDA_BUILD_CUBIN CUDA_BUILD_EMULATION CUDA_VERBOSE_BUILD)
141+
mark_as_advanced(CUDA_SDK_ROOT_DIR CUDA_SEPARABLE_COMPILATION)
136142
endif()
137143

138144
set(HAVE_CUDA TRUE)
139-
message(STATUS "CUDA detected: " ${CUDA_VERSION})
140-
if (${CUDA_VERSION} LESS 8.0)
141-
set(gloo_known_gpu_archs ${gloo_known_gpu_archs7})
142-
list(APPEND CUDA_NVCC_FLAGS "-D_MWAITXINTRIN_H_INCLUDED")
143-
list(APPEND CUDA_NVCC_FLAGS "-D__STRICT_ANSI__")
144-
elseif (${CUDA_VERSION} LESS 9.0)
145-
set(gloo_known_gpu_archs ${gloo_known_gpu_archs8})
146-
list(APPEND CUDA_NVCC_FLAGS "-D_MWAITXINTRIN_H_INCLUDED")
147-
list(APPEND CUDA_NVCC_FLAGS "-D__STRICT_ANSI__")
145+
message(STATUS "CUDA detected: " ${GLOO_CUDA_VERSION})
146+
if (${GLOO_CUDA_VERSION} LESS 9.0)
147+
list(APPEND GLOO_NVCC_FLAGS "-D_MWAITXINTRIN_H_INCLUDED")
148+
list(APPEND GLOO_NVCC_FLAGS "-D__STRICT_ANSI__")
148149
else()
149-
# CUDA 8 may complain that sm_20 is no longer supported. Suppress the warning for now.
150-
list(APPEND CUDA_NVCC_FLAGS "-Wno-deprecated-gpu-targets")
150+
# nvcc may complain that sm_xx is no longer supported. Suppress the warning for now.
151+
list(APPEND GLOO_NVCC_FLAGS "-Wno-deprecated-gpu-targets")
152+
endif()
153+
154+
if(GLOO_CUDA_VERSION VERSION_LESS 8.0)
155+
set(gloo_known_gpu_archs "30 35 50 52")
156+
elseif(GLOO_CUDA_VERSION VERSION_LESS 9.0)
157+
set(gloo_known_gpu_archs "30 35 50 52 60 61")
158+
elseif(GLOO_CUDA_VERSION VERSION_LESS 10.0)
159+
set(gloo_known_gpu_archs "30 35 50 52 60 61 70")
160+
elseif(GLOO_CUDA_VERSION VERSION_LESS 11.0)
161+
set(gloo_known_gpu_archs "35 50 52 60 61 70 75")
162+
elseif(GLOO_CUDA_VERSION VERSION_LESS 12.0)
163+
set(gloo_known_gpu_archs "35 50 52 60 61 70 75 80 86")
151164
endif()
152165

153-
include_directories(SYSTEM ${CUDA_INCLUDE_DIRS})
154-
list(APPEND gloo_DEPENDENCY_LIBS ${CUDA_CUDART_LIBRARY})
166+
list(APPEND gloo_cuda_DEPENDENCY_LIBS ${GLOO_CUDA_LIBRARIES})
155167

156168
# Setting nvcc arch flags (or inherit if already set)
157-
if (NOT ";${CUDA_NVCC_FLAGS};" MATCHES ";-gencode;")
169+
if (NOT ";${GLOO_NVCC_FLAGS};" MATCHES ";-gencode;")
158170
gloo_select_nvcc_arch_flags(NVCC_FLAGS_EXTRA)
159-
list(APPEND CUDA_NVCC_FLAGS ${NVCC_FLAGS_EXTRA})
171+
list(APPEND GLOO_NVCC_FLAGS ${NVCC_FLAGS_EXTRA})
160172
message(STATUS "Added CUDA NVCC flags for: ${NVCC_FLAGS_EXTRA_readable}")
161173
endif()
162174

163175
# Disable some nvcc diagnostic that apears in boost, glog, glags, opencv, etc.
164176
foreach(diag cc_clobber_ignored integer_sign_change useless_using_declaration set_but_not_used)
165-
gloo_list_append_if_unique(CUDA_NVCC_FLAGS -Xcudafe --diag_suppress=${diag})
177+
gloo_list_append_if_unique(GLOO_NVCC_FLAGS -Xcudafe --diag_suppress=${diag})
166178
endforeach()
167179

168-
# If the project including us doesn't set any -std=xxx directly, we set it to C++11 here.
169-
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
170-
if((NOT "${CUDA_NVCC_FLAGS}" MATCHES "-std=c\\+\\+") AND (NOT "${CUDA_NVCC_FLAGS}" MATCHES "-std=gnu\\+\\+"))
171-
if(NOT MSVC)
172-
gloo_list_append_if_unique(CUDA_NVCC_FLAGS "-std=c++11")
173-
endif()
174-
endif()
175-
176180
if(NOT MSVC)
177-
gloo_list_append_if_unique(CUDA_NVCC_FLAGS "-Xcompiler" "-fPIC")
181+
gloo_list_append_if_unique(GLOO_NVCC_FLAGS "-Xcompiler" "-fPIC")
178182
endif()
179183

180-
mark_as_advanced(CUDA_BUILD_CUBIN CUDA_BUILD_EMULATION CUDA_VERBOSE_BUILD)
181-
mark_as_advanced(CUDA_SDK_ROOT_DIR CUDA_SEPARABLE_COMPILATION)
184+
if(GLOO_USE_CUDA_TOOLKIT)
185+
# Convert list to space-separated string
186+
string(REPLACE ";" " " CMAKE_CUDA_FLAGS "${GLOO_NVCC_FLAGS}")
187+
else()
188+
set(CUDA_NVCC_FLAGS "${GLOO_NVCC_FLAGS}")
189+
endif()

gloo/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ configure_file(config.h.in config.h)
140140

141141
add_library(gloo ${GLOO_STATIC_OR_SHARED} ${GLOO_SRCS})
142142
if(USE_CUDA)
143-
cuda_add_library(gloo_cuda ${GLOO_CUDA_SRCS} ${GLOO_STATIC_OR_SHARED})
143+
if(GLOO_USE_CUDA_TOOLKIT)
144+
enable_language(CUDA)
145+
add_library(gloo_cuda ${GLOO_STATIC_OR_SHARED} ${GLOO_CUDA_SRCS})
146+
else()
147+
cuda_add_library(gloo_cuda ${GLOO_CUDA_SRCS} ${GLOO_STATIC_OR_SHARED})
148+
endif()
144149
target_link_libraries(gloo_cuda gloo ${gloo_cuda_DEPENDENCY_LIBS})
145150
endif()
146151
if(USE_ROCM)

gloo/benchmark/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ if(USE_CUDA)
1818
"${CMAKE_CURRENT_SOURCE_DIR}/runner.cc"
1919
)
2020

21-
cuda_add_executable(benchmark_cuda ${GLOO_BENCHMARK_CUDA_SRCS})
22-
target_link_libraries(benchmark_cuda gloo_cuda)
21+
add_executable(benchmark_cuda ${GLOO_BENCHMARK_CUDA_SRCS})
22+
target_link_libraries(benchmark_cuda gloo_cuda ${GLOO_CUDA_LIBRARIES})
2323

2424
if(GLOO_INSTALL)
2525
install(TARGETS benchmark_cuda DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)

gloo/test/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ if(USE_CUDA)
5050
"${CMAKE_CURRENT_SOURCE_DIR}/main.cc"
5151
)
5252

53-
cuda_add_executable(gloo_test_cuda ${GLOO_TEST_CUDA_SRCS})
54-
target_link_libraries(gloo_test_cuda gloo_cuda gtest OpenSSL::SSL OpenSSL::Crypto)
53+
if(GLOO_USE_CUDA_TOOLKIT)
54+
enable_language(CUDA)
55+
add_executable(gloo_test_cuda ${GLOO_TEST_CUDA_SRCS})
56+
else()
57+
cuda_add_executable(gloo_test_cuda ${GLOO_TEST_CUDA_SRCS})
58+
endif()
59+
target_link_libraries(gloo_test_cuda gloo_cuda gtest OpenSSL::SSL OpenSSL::Crypto ${GLOO_CUDA_LIBRARIES})
5560
endif()
5661
endif()
5762

0 commit comments

Comments
 (0)