Skip to content

Commit 30efee3

Browse files
author
Shibo Tao
authored
Revert "support CUDA using cmake built-in way (#24395). test=develop" (#24468)
This reverts commit 068d369.
1 parent 417b576 commit 30efee3

File tree

6 files changed

+63
-102
lines changed

6 files changed

+63
-102
lines changed

CMakeLists.txt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,6 @@ set(PADDLE_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
2020
include(system)
2121

2222
project(paddle CXX C)
23-
24-
include(init)
25-
26-
# enable language CUDA
27-
# TODO(Shibo Tao): remove find_package(CUDA) completely.
28-
find_package(CUDA QUIET)
29-
option(WITH_GPU "Compile PaddlePaddle with NVIDIA GPU" ${CUDA_FOUND})
30-
if(WITH_GPU AND NOT APPLE)
31-
enable_language(CUDA)
32-
message(STATUS "CUDA compiler: ${CMAKE_CUDA_COMPILER}, version: "
33-
"${CMAKE_CUDA_COMPILER_ID} ${CMAKE_CUDA_COMPILER_VERSION}")
34-
endif()
35-
3623
message(STATUS "CXX compiler: ${CMAKE_CXX_COMPILER}, version: "
3724
"${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
3825
message(STATUS "C compiler: ${CMAKE_C_COMPILER}, version: "
@@ -65,12 +52,14 @@ else(WIN32)
6552
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=deprecated-declarations -Wno-deprecated-declarations")
6653
endif(WIN32)
6754

55+
find_package(CUDA QUIET)
6856
find_package(Git REQUIRED)
6957
find_package(Threads REQUIRED)
7058

7159
include(simd)
7260

7361
################################ Exposed Configurations #######################################
62+
option(WITH_GPU "Compile PaddlePaddle with NVIDIA GPU" ${CUDA_FOUND})
7463
option(WITH_DSO "Compile PaddlePaddle with dynamic linked CUDA" ON)
7564
option(WITH_AVX "Compile PaddlePaddle with AVX intrinsics" ${AVX_FOUND})
7665
option(WITH_PYTHON "Compile PaddlePaddle with python interpreter" ON)

cmake/configure.cmake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ if(WITH_GPU)
7676

7777
FIND_PACKAGE(CUDA REQUIRED)
7878

79-
if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_LESS 7)
79+
if(${CUDA_VERSION_MAJOR} VERSION_LESS 7)
8080
message(FATAL_ERROR "Paddle needs CUDA >= 7.0 to compile")
8181
endif()
8282

@@ -89,19 +89,19 @@ if(WITH_GPU)
8989
else()
9090
message(STATUS "Cannot find CUPTI, GPU Profiling is incorrect.")
9191
endif()
92-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler=\"${SIMD_FLAG}\"")
92+
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} "-Xcompiler ${SIMD_FLAG}")
9393

9494
# Include cuda and cudnn
9595
include_directories(${CUDNN_INCLUDE_DIR})
9696
include_directories(${CUDA_TOOLKIT_INCLUDE})
9797

9898
if(TENSORRT_FOUND)
9999
if(WIN32)
100-
if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_LESS 9)
100+
if(${CUDA_VERSION_MAJOR} VERSION_LESS 9)
101101
message(FATAL_ERROR "TensorRT needs CUDA >= 9.0 to compile on Windows")
102102
endif()
103103
else()
104-
if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_LESS 8)
104+
if(${CUDA_VERSION_MAJOR} VERSION_LESS 8)
105105
message(FATAL_ERROR "TensorRT needs CUDA >= 8.0 to compile")
106106
endif()
107107
if(${CUDNN_MAJOR_VERSION} VERSION_LESS 7)

cmake/cuda.cmake

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ function(select_nvcc_arch_flags out_variable)
104104
elseif(${CUDA_ARCH_NAME} STREQUAL "Pascal")
105105
set(cuda_arch_bin "60 61")
106106
elseif(${CUDA_ARCH_NAME} STREQUAL "Volta")
107-
if (NOT ${CMAKE_CUDA_COMPILER_VERSION} LESS 10.0)
107+
if (NOT ${CUDA_VERSION} LESS 10.0)
108108
add_definitions("-DSUPPORTS_CUDA_FP16")
109109
endif()
110110
set(cuda_arch_bin "70")
111111
elseif(${CUDA_ARCH_NAME} STREQUAL "Turing")
112-
if (NOT ${CMAKE_CUDA_COMPILER_VERSION} LESS 10.0)
112+
if (NOT ${CUDA_VERSION} LESS 10.0)
113113
add_definitions("-DSUPPORTS_CUDA_FP16")
114114
endif()
115115
set(cuda_arch_bin "75")
@@ -142,52 +142,52 @@ function(select_nvcc_arch_flags out_variable)
142142
foreach(arch ${cuda_arch_bin})
143143
if(arch MATCHES "([0-9]+)\\(([0-9]+)\\)")
144144
# User explicitly specified PTX for the concrete BIN
145-
string(APPEND nvcc_flags " -gencode arch=compute_${CMAKE_MATCH_2},code=sm_${CMAKE_MATCH_1}")
146-
string(APPEND nvcc_archs_readable " sm_${CMAKE_MATCH_1}")
145+
list(APPEND nvcc_flags -gencode arch=compute_${CMAKE_MATCH_2},code=sm_${CMAKE_MATCH_1})
146+
list(APPEND nvcc_archs_readable sm_${CMAKE_MATCH_1})
147147
else()
148148
# User didn't explicitly specify PTX for the concrete BIN, we assume PTX=BIN
149-
string(APPEND nvcc_flags " -gencode arch=compute_${arch},code=sm_${arch}")
150-
string(APPEND nvcc_archs_readable " sm_${arch}")
149+
list(APPEND nvcc_flags -gencode arch=compute_${arch},code=sm_${arch})
150+
list(APPEND nvcc_archs_readable sm_${arch})
151151
endif()
152152
endforeach()
153153

154154
# Tell NVCC to add PTX intermediate code for the specified architectures
155155
foreach(arch ${cuda_arch_ptx})
156-
string(APPEND nvcc_flags " -gencode arch=compute_${arch},code=compute_${arch}")
157-
string(APPEND nvcc_archs_readable " compute_${arch}")
156+
list(APPEND nvcc_flags -gencode arch=compute_${arch},code=compute_${arch})
157+
list(APPEND nvcc_archs_readable compute_${arch})
158158
endforeach()
159159

160160
string(REPLACE ";" " " nvcc_archs_readable "${nvcc_archs_readable}")
161161
set(${out_variable} ${nvcc_flags} PARENT_SCOPE)
162162
set(${out_variable}_readable ${nvcc_archs_readable} PARENT_SCOPE)
163163
endfunction()
164164

165-
message(STATUS "CUDA detected: " ${CMAKE_CUDA_COMPILER_VERSION})
166-
if (${CMAKE_CUDA_COMPILER_VERSION} LESS 7.0)
165+
message(STATUS "CUDA detected: " ${CUDA_VERSION})
166+
if (${CUDA_VERSION} LESS 7.0)
167167
set(paddle_known_gpu_archs ${paddle_known_gpu_archs})
168-
elseif (${CMAKE_CUDA_COMPILER_VERSION} LESS 8.0) # CUDA 7.x
168+
elseif (${CUDA_VERSION} LESS 8.0) # CUDA 7.x
169169
set(paddle_known_gpu_archs ${paddle_known_gpu_archs7})
170-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -D_MWAITXINTRIN_H_INCLUDED")
171-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -D__STRICT_ANSI__")
172-
elseif (${CMAKE_CUDA_COMPILER_VERSION} LESS 9.0) # CUDA 8.x
170+
list(APPEND CUDA_NVCC_FLAGS "-D_MWAITXINTRIN_H_INCLUDED")
171+
list(APPEND CUDA_NVCC_FLAGS "-D__STRICT_ANSI__")
172+
elseif (${CUDA_VERSION} LESS 9.0) # CUDA 8.x
173173
set(paddle_known_gpu_archs ${paddle_known_gpu_archs8})
174-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -D_MWAITXINTRIN_H_INCLUDED")
175-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -D__STRICT_ANSI__")
174+
list(APPEND CUDA_NVCC_FLAGS "-D_MWAITXINTRIN_H_INCLUDED")
175+
list(APPEND CUDA_NVCC_FLAGS "-D__STRICT_ANSI__")
176176
# CUDA 8 may complain that sm_20 is no longer supported. Suppress the
177177
# warning for now.
178-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
179-
elseif (${CMAKE_CUDA_COMPILER_VERSION} LESS 10.0) # CUDA 9.x
178+
list(APPEND CUDA_NVCC_FLAGS "-Wno-deprecated-gpu-targets")
179+
elseif (${CUDA_VERSION} LESS 10.0) # CUDA 9.x
180180
set(paddle_known_gpu_archs ${paddle_known_gpu_archs9})
181-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -D_MWAITXINTRIN_H_INCLUDED")
182-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -D__STRICT_ANSI__")
183-
elseif (${CMAKE_CUDA_COMPILER_VERSION} LESS 11.0) # CUDA 10.x
181+
list(APPEND CUDA_NVCC_FLAGS "-D_MWAITXINTRIN_H_INCLUDED")
182+
list(APPEND CUDA_NVCC_FLAGS "-D__STRICT_ANSI__")
183+
elseif (${CUDA_VERSION} LESS 11.0) # CUDA 10.x
184184
set(paddle_known_gpu_archs ${paddle_known_gpu_archs10})
185-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -D_MWAITXINTRIN_H_INCLUDED")
186-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -D__STRICT_ANSI__")
185+
list(APPEND CUDA_NVCC_FLAGS "-D_MWAITXINTRIN_H_INCLUDED")
186+
list(APPEND CUDA_NVCC_FLAGS "-D__STRICT_ANSI__")
187187
endif()
188+
add_definitions("-DPADDLE_CUDA_BINVER=\"${CUDA_VERSION_MAJOR}${CUDA_VERSION_MINOR}\"")
188189

189-
message(STATUS "PADDLE_CUDA_BINVER=${CUDA_VERSION_MAJOR}${CUDA_VERSION_MINOR}")
190-
190+
include_directories(${CUDA_INCLUDE_DIRS})
191191
if(NOT WITH_DSO)
192192
if(WIN32)
193193
set_property(GLOBAL PROPERTY CUDA_MODULES ${CUDNN_LIBRARY} ${CUDA_CUBLAS_LIBRARIES} ${CUDA_curand_LIBRARY} ${CUDA_cusolver_LIBRARY})
@@ -196,24 +196,37 @@ endif(NOT WITH_DSO)
196196

197197
# setting nvcc arch flags
198198
select_nvcc_arch_flags(NVCC_FLAGS_EXTRA)
199-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} ${NVCC_FLAGS_EXTRA}")
200-
message(STATUS "NVCC_FLAGS_EXTRA: ${NVCC_FLAGS_EXTRA}")
199+
list(APPEND CUDA_NVCC_FLAGS ${NVCC_FLAGS_EXTRA})
200+
message(STATUS "Added CUDA NVCC flags for: ${NVCC_FLAGS_EXTRA_readable}")
201201

202202
# Set C++11 support
203203
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
204+
204205
# Release/Debug flags set by cmake. Such as -O3 -g -DNDEBUG etc.
205206
# So, don't set these flags here.
206-
if (NOT WIN32) # windows msvc2015 support c++11 natively.
207-
# -std=c++11 -fPIC not recoginize by msvc, -Xcompiler will be added by cmake.
208-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -std=c++11")
207+
if (NOT WIN32) # windows msvc2015 support c++11 natively.
208+
# -std=c++11 -fPIC not recoginize by msvc, -Xcompiler will be added by cmake.
209+
list(APPEND CUDA_NVCC_FLAGS "-std=c++11")
210+
list(APPEND CUDA_NVCC_FLAGS "-Xcompiler -fPIC")
209211
endif(NOT WIN32)
210212

211-
# in cuda9, suppress cuda warning on eigen
212-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -w")
213+
# in cuda9, suppress cuda warning on eigen
214+
list(APPEND CUDA_NVCC_FLAGS "-w")
213215
# Set :expt-relaxed-constexpr to suppress Eigen warnings
214-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --expt-relaxed-constexpr")
216+
list(APPEND CUDA_NVCC_FLAGS "--expt-relaxed-constexpr")
215217

216-
if (WIN32)
218+
if (NOT WIN32)
219+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
220+
list(APPEND CUDA_NVCC_FLAGS ${CMAKE_CXX_FLAGS_DEBUG})
221+
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
222+
list(APPEND CUDA_NVCC_FLAGS ${CMAKE_CXX_FLAGS_RELEASE})
223+
elseif(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
224+
list(APPEND CUDA_NVCC_FLAGS ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
225+
elseif(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
226+
# nvcc 9 does not support -Os. Use Release flags instead
227+
list(APPEND CUDA_NVCC_FLAGS ${CMAKE_CXX_FLAGS_RELEASE})
228+
endif()
229+
else(NOT WIN32)
217230
list(APPEND CUDA_NVCC_FLAGS "-Xcompiler \"/wd 4244 /wd 4267 /wd 4819\"")
218231
list(APPEND CUDA_NVCC_FLAGS "--compiler-options;/bigobj")
219232
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
@@ -223,9 +236,9 @@ if (WIN32)
223236
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
224237
list(APPEND CUDA_NVCC_FLAGS "-O3 -DNDEBUG")
225238
else()
226-
message(FATAL "Windows only support Release or Debug build now. Please set visual studio build type to Release/Debug, x64 build.")
227-
endif()
228-
endif(WIN32)
239+
message(FATAL "Windows only support Release or Debug build now. Please set visual studio build type to Release/Debug, x64 build.")
240+
endif()
241+
endif(NOT WIN32)
229242

230243
mark_as_advanced(CUDA_BUILD_CUBIN CUDA_BUILD_EMULATION CUDA_VERBOSE_BUILD)
231244
mark_as_advanced(CUDA_SDK_ROOT_DIR CUDA_SEPARABLE_COMPILATION)

cmake/flags.cmake

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ macro(safe_set_nvflag flag_name)
7676
CHECK_C_COMPILER_FLAG(${flag_name} C_COMPILER_SUPPORT_FLAG_${safe_name})
7777
set(safe_name C_COMPILER_SUPPORT_FLAG_${safe_name})
7878
if(${safe_name})
79-
set(SAFE_GPU_COMMON_FLAGS "${SAFE_GPU_COMMON_FLAGS} -Xcompiler=\"${flag_name}\"")
79+
LIST(APPEND CUDA_NVCC_FLAGS -Xcompiler ${flag_name})
8080
endif()
8181
endmacro()
8282

@@ -169,7 +169,7 @@ if(NOT APPLE)
169169
-Wno-error=nonnull-compare # Warning in boost gcc 8.2
170170
-Wno-error=address # Warning in boost gcc 8.2
171171
-Wno-ignored-qualifiers # Warning in boost gcc 8.2
172-
-Wno-ignored-attributes # Warning in Eigen gcc 8.3
172+
-Wno-ignored-attributes # Warning in Eigen gcc 8.3
173173
-Wno-parentheses # Warning in Eigen gcc 8.3
174174
)
175175
endif()
@@ -187,7 +187,7 @@ set(GPU_COMMON_FLAGS
187187
-Wno-error=unused-function # Warnings in Numpy Header.
188188
-Wno-error=array-bounds # Warnings in Eigen::array
189189
)
190-
if (NOT WITH_NV_JETSON)
190+
if (NOT WITH_NV_JETSON)
191191
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
192192
endif()
193193
endif(NOT WIN32)
@@ -212,14 +212,10 @@ foreach(flag ${COMMON_FLAGS})
212212
safe_set_cxxflag(CMAKE_CXX_FLAGS ${flag})
213213
endforeach()
214214

215-
set(SAFE_GPU_COMMON_FLAGS "")
216215
foreach(flag ${GPU_COMMON_FLAGS})
217216
safe_set_nvflag(${flag})
218217
endforeach()
219218

220-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} ${SAFE_GPU_COMMON_FLAGS}")
221-
222-
223219
if(WIN32 AND MSVC_STATIC_CRT)
224220
# windows build turn off warnings.
225221
safe_set_static_flag()

cmake/generic.cmake

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,10 @@ function(nv_library TARGET_NAME)
412412
set(multiValueArgs SRCS DEPS)
413413
cmake_parse_arguments(nv_library "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
414414
if(nv_library_SRCS)
415-
# Attention:
416-
# 1. cuda_add_library is deprecated after cmake v3.10, use add_library for CUDA please.
417-
# 2. cuda_add_library does not support ccache.
418-
# Reference: https://cmake.org/cmake/help/v3.10/module/FindCUDA.html
419415
if (nv_library_SHARED OR nv_library_shared) # build *.so
420-
add_library(${TARGET_NAME} SHARED ${nv_library_SRCS})
416+
cuda_add_library(${TARGET_NAME} SHARED ${nv_library_SRCS})
421417
else()
422-
add_library(${TARGET_NAME} STATIC ${nv_library_SRCS})
418+
cuda_add_library(${TARGET_NAME} STATIC ${nv_library_SRCS})
423419
find_fluid_modules(${TARGET_NAME})
424420
endif()
425421
if (nv_library_DEPS)
@@ -454,7 +450,7 @@ function(nv_binary TARGET_NAME)
454450
set(oneValueArgs "")
455451
set(multiValueArgs SRCS DEPS)
456452
cmake_parse_arguments(nv_binary "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
457-
add_executable(${TARGET_NAME} ${nv_binary_SRCS})
453+
cuda_add_executable(${TARGET_NAME} ${nv_binary_SRCS})
458454
if(nv_binary_DEPS)
459455
target_link_libraries(${TARGET_NAME} ${nv_binary_DEPS})
460456
add_dependencies(${TARGET_NAME} ${nv_binary_DEPS})
@@ -468,11 +464,7 @@ function(nv_test TARGET_NAME)
468464
set(oneValueArgs "")
469465
set(multiValueArgs SRCS DEPS)
470466
cmake_parse_arguments(nv_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
471-
# Attention:
472-
# 1. cuda_add_executable is deprecated after cmake v3.10, use cuda_add_executable for CUDA please.
473-
# 2. cuda_add_executable does not support ccache.
474-
# Reference: https://cmake.org/cmake/help/v3.10/module/FindCUDA.html
475-
add_executable(${TARGET_NAME} ${nv_test_SRCS})
467+
cuda_add_executable(${TARGET_NAME} ${nv_test_SRCS})
476468
get_property(os_dependency_modules GLOBAL PROPERTY OS_DEPENDENCY_MODULES)
477469
target_link_libraries(${TARGET_NAME} ${nv_test_DEPS} paddle_gtest_main lod_tensor memory gtest gflags glog ${os_dependency_modules})
478470
add_dependencies(${TARGET_NAME} ${nv_test_DEPS} paddle_gtest_main lod_tensor memory gtest gflags glog)

cmake/init.cmake

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)