Skip to content

Commit 3d080f3

Browse files
committed
Refine cmake about CUDA to automatically detect GPU arch by default.
1. Automatically detect GPU arch by default. 2. Specify -DCUDA_ARCH_NAME=All when releasing PaddlePaddle new version
1 parent 08bc08d commit 3d080f3

File tree

3 files changed

+220
-59
lines changed

3 files changed

+220
-59
lines changed

CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,7 @@ set(EXTERNAL_LIBS
158158
)
159159

160160
if(WITH_GPU)
161-
list(APPEND EXTERNAL_LIBS ${CUDA_LIBRARIES} ${CUDA_rt_LIBRARY})
162-
if(NOT WITH_DSO)
163-
list(APPEND EXTERNAL_LIBS ${CUDNN_LIBRARY} ${CUDA_CUBLAS_LIBRARIES} ${CUDA_curand_LIBRARY} ${NCCL_LIBRARY})
164-
endif(NOT WITH_DSO)
161+
include(cuda)
165162
endif(WITH_GPU)
166163

167164
if(WITH_MKLDNN)

cmake/cuda.cmake

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
if(NOT WITH_GPU)
2+
return()
3+
endif()
4+
5+
set(paddle_known_gpu_archs "20 21(20) 30 35 50 52 60 61 70")
6+
set(paddle_known_gpu_archs7 "20 21(20) 30 35 50 52")
7+
set(paddle_known_gpu_archs8 "20 21(20) 30 35 50 52 60 61")
8+
9+
######################################################################################
10+
# A function for automatic detection of GPUs installed (if autodetection is enabled)
11+
# Usage:
12+
# detect_installed_gpus(out_variable)
13+
function(detect_installed_gpus out_variable)
14+
if(NOT CUDA_gpu_detect_output)
15+
set(cufile ${PROJECT_BINARY_DIR}/detect_cuda_archs.cu)
16+
17+
file(WRITE ${cufile} ""
18+
"#include <cstdio>\n"
19+
"int main() {\n"
20+
" int count = 0;\n"
21+
" if (cudaSuccess != cudaGetDeviceCount(&count)) return -1;\n"
22+
" if (count == 0) return -1;\n"
23+
" for (int device = 0; device < count; ++device) {\n"
24+
" cudaDeviceProp prop;\n"
25+
" if (cudaSuccess == cudaGetDeviceProperties(&prop, device))\n"
26+
" std::printf(\"%d.%d \", prop.major, prop.minor);\n"
27+
" }\n"
28+
" return 0;\n"
29+
"}\n")
30+
31+
execute_process(COMMAND "${CUDA_NVCC_EXECUTABLE}" "-ccbin=${CUDA_HOST_COMPILER}"
32+
"--run" "${cufile}"
33+
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/CMakeFiles/"
34+
RESULT_VARIABLE nvcc_res OUTPUT_VARIABLE nvcc_out
35+
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
36+
37+
if(nvcc_res EQUAL 0)
38+
# only keep the last line of nvcc_out
39+
STRING(REGEX REPLACE ";" "\\\\;" nvcc_out "${nvcc_out}")
40+
STRING(REGEX REPLACE "\n" ";" nvcc_out "${nvcc_out}")
41+
list(GET nvcc_out -1 nvcc_out)
42+
string(REPLACE "2.1" "2.1(2.0)" nvcc_out "${nvcc_out}")
43+
set(CUDA_gpu_detect_output ${nvcc_out} CACHE INTERNAL "Returned GPU architetures from caffe_detect_gpus tool" FORCE)
44+
endif()
45+
endif()
46+
47+
if(NOT CUDA_gpu_detect_output)
48+
message(STATUS "Automatic GPU detection failed. Building for all known architectures.")
49+
set(${out_variable} ${paddle_known_gpu_archs} PARENT_SCOPE)
50+
else()
51+
set(${out_variable} ${CUDA_gpu_detect_output} PARENT_SCOPE)
52+
endif()
53+
endfunction()
54+
55+
56+
########################################################################
57+
# Function for selecting GPU arch flags for nvcc based on CUDA_ARCH_NAME
58+
# Usage:
59+
# select_nvcc_arch_flags(out_variable)
60+
function(select_nvcc_arch_flags out_variable)
61+
# List of arch names
62+
set(archs_names "Kepler" "Maxwell" "Pascal" "All" "Manual")
63+
set(archs_name_default "All")
64+
if(NOT CMAKE_CROSSCOMPILING)
65+
list(APPEND archs_names "Auto")
66+
set(archs_name_default "Auto")
67+
endif()
68+
69+
# set CUDA_ARCH_NAME strings (so it will be seen as dropbox in CMake-Gui)
70+
set(CUDA_ARCH_NAME ${archs_name_default} CACHE STRING "Select target NVIDIA GPU achitecture.")
71+
set_property( CACHE CUDA_ARCH_NAME PROPERTY STRINGS "" ${archs_names} )
72+
mark_as_advanced(CUDA_ARCH_NAME)
73+
74+
# verify CUDA_ARCH_NAME value
75+
if(NOT ";${archs_names};" MATCHES ";${CUDA_ARCH_NAME};")
76+
string(REPLACE ";" ", " archs_names "${archs_names}")
77+
message(FATAL_ERROR "Only ${archs_names} architeture names are supported.")
78+
endif()
79+
80+
if(${CUDA_ARCH_NAME} STREQUAL "Manual")
81+
set(CUDA_ARCH_BIN ${paddle_known_gpu_archs} CACHE STRING "Specify 'real' GPU architectures to build binaries for, BIN(PTX) format is supported")
82+
set(CUDA_ARCH_PTX "50" CACHE STRING "Specify 'virtual' PTX architectures to build PTX intermediate code for")
83+
mark_as_advanced(CUDA_ARCH_BIN CUDA_ARCH_PTX)
84+
else()
85+
unset(CUDA_ARCH_BIN CACHE)
86+
unset(CUDA_ARCH_PTX CACHE)
87+
endif()
88+
89+
if(${CUDA_ARCH_NAME} STREQUAL "Kepler")
90+
set(cuda_arch_bin "30 35")
91+
elseif(${CUDA_ARCH_NAME} STREQUAL "Maxwell")
92+
set(cuda_arch_bin "50")
93+
elseif(${CUDA_ARCH_NAME} STREQUAL "Pascal")
94+
set(cuda_arch_bin "60 61")
95+
elseif(${CUDA_ARCH_NAME} STREQUAL "Volta")
96+
set(cuda_arch_bin "70")
97+
elseif(${CUDA_ARCH_NAME} STREQUAL "All")
98+
set(cuda_arch_bin ${paddle_known_gpu_archs})
99+
elseif(${CUDA_ARCH_NAME} STREQUAL "Auto")
100+
detect_installed_gpus(cuda_arch_bin)
101+
else() # (${CUDA_ARCH_NAME} STREQUAL "Manual")
102+
set(cuda_arch_bin ${CUDA_ARCH_BIN})
103+
endif()
104+
105+
# remove dots and convert to lists
106+
string(REGEX REPLACE "\\." "" cuda_arch_bin "${cuda_arch_bin}")
107+
string(REGEX REPLACE "\\." "" cuda_arch_ptx "${CUDA_ARCH_PTX}")
108+
string(REGEX MATCHALL "[0-9()]+" cuda_arch_bin "${cuda_arch_bin}")
109+
string(REGEX MATCHALL "[0-9]+" cuda_arch_ptx "${cuda_arch_ptx}")
110+
list(REMOVE_DUPLICATES cuda_arch_bin)
111+
list(REMOVE_DUPLICATES cuda_arch_ptx)
112+
113+
set(nvcc_flags "")
114+
set(nvcc_archs_readable "")
115+
116+
# Tell NVCC to add binaries for the specified GPUs
117+
foreach(arch ${cuda_arch_bin})
118+
if(arch MATCHES "([0-9]+)\\(([0-9]+)\\)")
119+
# User explicitly specified PTX for the concrete BIN
120+
list(APPEND nvcc_flags -gencode arch=compute_${CMAKE_MATCH_2},code=sm_${CMAKE_MATCH_1})
121+
list(APPEND nvcc_archs_readable sm_${CMAKE_MATCH_1})
122+
else()
123+
# User didn't explicitly specify PTX for the concrete BIN, we assume PTX=BIN
124+
list(APPEND nvcc_flags -gencode arch=compute_${arch},code=sm_${arch})
125+
list(APPEND nvcc_archs_readable sm_${arch})
126+
endif()
127+
endforeach()
128+
129+
# Tell NVCC to add PTX intermediate code for the specified architectures
130+
foreach(arch ${cuda_arch_ptx})
131+
list(APPEND nvcc_flags -gencode arch=compute_${arch},code=compute_${arch})
132+
list(APPEND nvcc_archs_readable compute_${arch})
133+
endforeach()
134+
135+
string(REPLACE ";" " " nvcc_archs_readable "${nvcc_archs_readable}")
136+
set(${out_variable} ${nvcc_flags} PARENT_SCOPE)
137+
set(${out_variable}_readable ${nvcc_archs_readable} PARENT_SCOPE)
138+
endfunction()
139+
140+
if(NOT CUDA_FOUND)
141+
return()
142+
endif()
143+
144+
message(STATUS "CUDA detected: " ${CUDA_VERSION})
145+
if (${CUDA_VERSION} LESS 7.0)
146+
set(paddle_known_gpu_archs ${paddle_known_gpu_archs})
147+
elseif (${CUDA_VERSION} LESS 8.0) # CUDA 7.x
148+
set(paddle_known_gpu_archs ${paddle_known_gpu_archs7})
149+
list(APPEND CUDA_NVCC_FLAGS "-D_MWAITXINTRIN_H_INCLUDED")
150+
list(APPEND CUDA_NVCC_FLAGS "-D__STRICT_ANSI__")
151+
elseif (${CUDA_VERSION} LESS 9.0) # CUDA 8.x
152+
set(paddle_known_gpu_archs ${paddle_known_gpu_archs8})
153+
list(APPEND CUDA_NVCC_FLAGS "-D_MWAITXINTRIN_H_INCLUDED")
154+
list(APPEND CUDA_NVCC_FLAGS "-D__STRICT_ANSI__")
155+
# CUDA 8 may complain that sm_20 is no longer supported. Suppress the
156+
# warning for now.
157+
list(APPEND CUDA_NVCC_FLAGS "-Wno-deprecated-gpu-targets")
158+
endif()
159+
160+
include_directories(${CUDA_INCLUDE_DIRS})
161+
list(APPEND EXTERNAL_LIBS ${CUDA_LIBRARIES} ${CUDA_rt_LIBRARY})
162+
if(NOT WITH_DSO)
163+
list(APPEND EXTERNAL_LIBS ${CUDNN_LIBRARY} ${CUDA_CUBLAS_LIBRARIES} ${CUDA_curand_LIBRARY} ${NCCL_LIBRARY})
164+
endif(NOT WITH_DSO)
165+
166+
# find libcuda.so and lbnvrtc.so
167+
# For libcuda.so, we will find it under lib, lib64, and then the
168+
# stubs folder, in case we are building on a system that does not
169+
# have cuda driver installed. On windows, we also search under the
170+
# folder lib/x64.
171+
172+
find_library(CUDA_CUDA_LIB cuda
173+
PATHS ${CUDA_TOOLKIT_ROOT_DIR}
174+
PATH_SUFFIXES lib lib64 lib/stubs lib64/stubs lib/x64)
175+
find_library(CUDA_NVRTC_LIB nvrtc
176+
PATHS ${CUDA_TOOLKIT_ROOT_DIR}
177+
PATH_SUFFIXES lib lib64 lib/x64)
178+
179+
# setting nvcc arch flags
180+
select_nvcc_arch_flags(NVCC_FLAGS_EXTRA)
181+
list(APPEND CUDA_NVCC_FLAGS ${NVCC_FLAGS_EXTRA})
182+
message(STATUS "Added CUDA NVCC flags for: ${NVCC_FLAGS_EXTRA_readable}")
183+
184+
if(CUDA_CUDA_LIB)
185+
# message(STATUS "Found libcuda: ${CUDA_CUDA_LIB}")
186+
list(APPEND Caffe2_DEPENDENCY_LIBS ${CUDA_CUDA_LIB})
187+
else()
188+
message(FATAL_ERROR "Cannot find libcuda.so.")
189+
endif()
190+
if(CUDA_NVRTC_LIB)
191+
# message(STATUS "Found libnvrtc: ${CUDA_NVRTC_LIB}")
192+
list(APPEND Caffe2_DEPENDENCY_LIBS ${CUDA_NVRTC_LIB})
193+
else()
194+
message(FATAL_ERROR "Cannot find libnvrtc.so.")
195+
endif()
196+
197+
# Set C++11 support
198+
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
199+
200+
# Release/Debug flags set by cmake. Such as -O3 -g -DNDEBUG etc.
201+
# So, don't set these flags here.
202+
list(APPEND CUDA_NVCC_FLAGS "-std=c++11")
203+
list(APPEND CUDA_NVCC_FLAGS "--use_fast_math")
204+
list(APPEND CUDA_NVCC_FLAGS "-Xcompiler -fPIC")
205+
# Set :expt-relaxed-constexpr to suppress Eigen warnings
206+
list(APPEND CUDA_NVCC_FLAGS "--expt-relaxed-constexpr")
207+
208+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
209+
list(APPEND CUDA_NVCC_FLAGS ${CMAKE_CXX_FLAGS_DEBUG})
210+
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
211+
list(APPEND CUDA_NVCC_FLAGS ${CMAKE_CXX_FLAGS_RELEASE})
212+
elseif(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
213+
list(APPEND CUDA_NVCC_FLAGS ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
214+
elseif(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
215+
list(APPEND CUDA_NVCC_FLAGS ${CMAKE_CXX_FLAGS_MINSIZEREL})
216+
endif()
217+
218+
mark_as_advanced(CUDA_BUILD_CUBIN CUDA_BUILD_EMULATION CUDA_VERBOSE_BUILD)
219+
mark_as_advanced(CUDA_SDK_ROOT_DIR CUDA_SEPARABLE_COMPILATION)

cmake/flags.cmake

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -149,58 +149,3 @@ endforeach()
149149
foreach(flag ${GPU_COMMON_FLAGS})
150150
safe_set_nvflag(${flag})
151151
endforeach()
152-
153-
154-
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
155-
156-
# Release/Debug flags set by cmake. Such as -O3 -g -DNDEBUG etc.
157-
# So, don't set these flags here.
158-
LIST(APPEND CUDA_NVCC_FLAGS -std=c++11)
159-
LIST(APPEND CUDA_NVCC_FLAGS --use_fast_math)
160-
161-
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
162-
LIST(APPEND CUDA_NVCC_FLAGS ${CMAKE_CXX_FLAGS_DEBUG})
163-
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
164-
LIST(APPEND CUDA_NVCC_FLAGS ${CMAKE_CXX_FLAGS_RELEASE})
165-
elseif(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
166-
LIST(APPEND CUDA_NVCC_FLAGS ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
167-
elseif(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
168-
LIST(APPEND CUDA_NVCC_FLAGS ${CMAKE_CXX_FLAGS_MINSIZEREL})
169-
endif()
170-
171-
function(specify_cuda_arch cuda_version cuda_arch)
172-
if(${cuda_version} VERSION_GREATER "8.0")
173-
foreach(capability 61 62)
174-
if(${cuda_arch} STREQUAL ${capability})
175-
list(APPEND __arch_flags " -gencode arch=compute_${cuda_arch},code=sm_${cuda_arch}")
176-
endif()
177-
endforeach()
178-
elseif(${cuda_version} VERSION_GREATER "7.0" and ${cuda_arch} STREQUAL "53")
179-
list(APPEND __arch_flags " -gencode arch=compute_${cuda_arch},code=sm_${cuda_arch}")
180-
endif()
181-
endfunction()
182-
183-
# Common gpu architectures: Kepler, Maxwell
184-
foreach(capability 30 35 50)
185-
list(APPEND __arch_flags " -gencode arch=compute_${capability},code=sm_${capability}")
186-
endforeach()
187-
188-
if (CUDA_VERSION VERSION_GREATER "7.0" OR CUDA_VERSION VERSION_EQUAL "7.0")
189-
list(APPEND __arch_flags " -gencode arch=compute_52,code=sm_52")
190-
endif()
191-
192-
# Modern gpu architectures: Pascal
193-
if (CUDA_VERSION VERSION_GREATER "8.0" OR CUDA_VERSION VERSION_EQUAL "8.0")
194-
list(APPEND __arch_flags " -gencode arch=compute_60,code=sm_60")
195-
list(APPEND CUDA_NVCC_FLAGS --expt-relaxed-constexpr)
196-
endif()
197-
198-
# Custom gpu architecture
199-
set(CUDA_ARCH)
200-
201-
if(CUDA_ARCH)
202-
specify_cuda_arch(${CUDA_VERSION} ${CUDA_ARCH})
203-
endif()
204-
205-
set(CUDA_NVCC_FLAGS ${__arch_flags} ${CUDA_NVCC_FLAGS})
206-

0 commit comments

Comments
 (0)