Skip to content

Commit 6467c38

Browse files
gangliaoreyoung
authored andcommitted
Add default cuda system path (#192)
* DYLD_LIBRARY_PATH is disable after Mac OS X 10.11 * fix clang + gpu compile error on Mac OS * fix some words and errors in build docs
1 parent 212d339 commit 6467c38

File tree

3 files changed

+108
-40
lines changed

3 files changed

+108
-40
lines changed

cmake/flags.cmake

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ function(safe_set_flag is_c src_list flag_name)
2121
endif()
2222
if(${safe_name})
2323
set(${src_list} "${${src_list}} ${flag_name}" PARENT_SCOPE)
24-
if(is_c)
25-
set(CUDA_NVCC_FLAGS
26-
--compiler-options;${flag_name}
27-
${CUDA_NVCC_FLAGS}
28-
PARENT_SCOPE)
29-
endif()
3024
endif()
3125
endfunction()
3226

@@ -40,6 +34,20 @@ macro(safe_set_cxxflag src_list flag_name)
4034
safe_set_flag(OFF ${src_list} ${flag_name})
4135
endmacro()
4236

37+
# helper macro to set nvcc flag
38+
macro(safe_set_nvflag flag_name)
39+
string(REPLACE "-" "_" safe_name ${flag_name})
40+
string(REPLACE "=" "_" safe_name ${safe_name})
41+
CHECK_C_COMPILER_FLAG(${flag_name} C_COMPILER_SUPPORT_FLAG_${safe_name})
42+
set(safe_name C_COMPILER_SUPPORT_FLAG_${safe_name})
43+
if(${safe_name})
44+
set(CUDA_NVCC_FLAGS
45+
--compiler-options;${flag_name}
46+
${CUDA_NVCC_FLAGS})
47+
endif()
48+
endmacro()
49+
50+
4351
CHECK_CXX_SYMBOL_EXISTS(UINT64_MAX "stdint.h" UINT64_MAX_EXISTS)
4452
if(NOT UINT64_MAX_EXISTS)
4553
set(CMAKE_REQUIRED_DEFINITIONS -D__STDC_LIMIT_MACROS)
@@ -63,20 +71,43 @@ set(COMMON_FLAGS
6371
-Wnon-virtual-dtor
6472
-Wdelete-non-virtual-dtor
6573
-Wno-unused-parameter
74+
-Wno-unused-function
75+
-Wno-error=literal-suffix
76+
-Wno-error=unused-local-typedefs)
77+
78+
set(GPU_COMMON_FLAGS
79+
-fPIC
80+
-fno-omit-frame-pointer
81+
-Wnon-virtual-dtor
82+
-Wdelete-non-virtual-dtor
83+
-Wno-unused-parameter
84+
-Wno-unused-function
6685
-Wno-error=literal-suffix
6786
-Wno-error=unused-local-typedefs
6887
-Wno-error=unused-function # Warnings in Numpy Header.
6988
)
7089

90+
if (APPLE)
91+
# On Mac OS X build fat binaries with x86_64 architectures by default.
92+
set (CMAKE_OSX_ARCHITECTURES "x86_64" CACHE STRING "Build architectures for OSX" FORCE)
93+
else()
94+
set(GPU_COMMON_FLAGS
95+
-Wall
96+
-Wextra
97+
-Werror
98+
${GPU_COMMON_FLAGS})
99+
endif()
100+
101+
71102
foreach(flag ${COMMON_FLAGS})
72103
safe_set_cflag(CMAKE_C_FLAGS ${flag})
73104
safe_set_cxxflag(CMAKE_CXX_FLAGS ${flag})
74105
endforeach()
75106

76-
# On Mac OS X build fat binaries with x86_64 architectures by default.
77-
if (APPLE)
78-
set (CMAKE_OSX_ARCHITECTURES "x86_64" CACHE STRING "Build architectures for OSX" FORCE)
79-
endif ()
107+
foreach(flag ${GPU_COMMON_FLAGS})
108+
safe_set_nvflag(${flag})
109+
endforeach()
110+
80111

81112
# Release/Debug flags set by cmake. Such as -O3 -g -DNDEBUG etc.
82113
# So, don't set these flags here.

doc/build/build_from_source.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ As a simple example, consider the following:
153153
- **Only CPU**
154154

155155
```bash
156-
cmake .. -DWITH_GPU=OFF -DWITH_DOC=OFF
156+
cmake .. -DWITH_GPU=OFF
157157
```
158158
- **GPU**
159159

160160
```bash
161-
cmake .. -DWITH_GPU=ON -DWITH_DOC=OFF
161+
cmake .. -DWITH_GPU=ON
162162
```
163163

164164
- **GPU with doc and swig**
@@ -171,7 +171,7 @@ Finally, you can build PaddlePaddle:
171171

172172
```bash
173173
# you can add build option here, such as:
174-
cmake .. -DWITH_GPU=ON -DWITH_DOC=OFF -DCMAKE_INSTALL_PREFIX=<path to install>
174+
cmake .. -DWITH_GPU=ON -DCMAKE_INSTALL_PREFIX=<path to install>
175175
# please use sudo make install, if you want to install PaddlePaddle into the system
176176
make -j `nproc` && make install
177177
# set PaddlePaddle installation path in ~/.bashrc
@@ -246,7 +246,7 @@ easy_install pip
246246

247247
```bash
248248
sudo tar -xzf cudnn-7.5-osx-x64-v5.0-ga.tgz -C /usr/local
249-
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
249+
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib/libcudnn*
250250
```
251251
2. Then you need to set DYLD\_LIBRARY\_PATH, PATH environment variables in ~/.bashrc.
252252

@@ -273,12 +273,12 @@ As a simple example, consider the following:
273273
- **Only CPU**
274274
275275
```bash
276-
cmake .. -DWITH_GPU=OFF -DWITH_DOC=OFF
276+
cmake .. -DWITH_GPU=OFF
277277
```
278278
- **GPU**
279279
280280
```bash
281-
cmake .. -DWITH_GPU=ON -DWITH_DOC=OFF
281+
cmake .. -DWITH_GPU=ON
282282
```
283283
284284
- **GPU with doc and swig**
@@ -291,9 +291,9 @@ Finally, you can build PaddlePaddle:
291291
292292
```bash
293293
# you can add build option here, such as:
294-
cmake .. -DWITH_GPU=ON -DWITH_DOC=OFF -DCMAKE_INSTALL_PREFIX=<installation path>
294+
cmake .. -DWITH_GPU=ON -DCMAKE_INSTALL_PREFIX=<installation path>
295295
# please use sudo make install, if you want to install PaddlePaddle into the system
296-
make -j `nproc` && make install
296+
make -j `sysctl -n hw.ncpu` && make install
297297
# set PaddlePaddle installation path in ~/.bashrc
298298
export PATH=<installation path>/bin:$PATH
299299
```

paddle/cuda/src/hl_dso_loader.cc

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,63 +46,100 @@ static inline std::string join(const std::string& part1, const std::string& part
4646
return ret;
4747
}
4848

49-
static inline void GetDsoHandleWithSearchPath(
49+
static inline void GetDsoHandleFromDefaultPath(
50+
std::string& dso_path, void** dso_handle, int dynload_flags) {
51+
LOG(INFO) << "Try to find cuda library: " << dso_path
52+
<< "from default system path.";
53+
// default search from LD_LIBRARY_PATH/DYLD_LIBRARY_PATH
54+
*dso_handle = dlopen(dso_path.c_str(), dynload_flags);
55+
56+
// DYLD_LIBRARY_PATH is disabled after Mac OS 10.11 to
57+
// bring System Integrity Projection (SIP), if dso_handle
58+
// is null, search from default package path in Mac OS.
59+
#if defined(__APPLE__) or defined(__OSX__)
60+
if (nullptr == *dso_handle) {
61+
dso_path = join("/usr/local/cuda/lib/", dso_path);
62+
*dso_handle = dlopen(dso_path.c_str(), dynload_flags);
63+
if (nullptr == *dso_handle) {
64+
if (dso_path == "libcudnn.dylib") {
65+
LOG(FATAL) << "Note: [Recommend] copy cudnn into /usr/local/cuda/ \n"
66+
<< "For instance, sudo tar -xzf cudnn-7.5-osx-x64-v5.0-ga.tgz -C "
67+
<< "/usr/local \n sudo chmod a+r /usr/local/cuda/include/cudnn.h "
68+
<< "/usr/local/cuda/lib/libcudnn*";
69+
}
70+
}
71+
}
72+
#endif
73+
}
74+
75+
static inline void GetDsoHandleFromSearchPath(
5076
const std::string& search_root,
51-
const std::string& dso_path,
77+
const std::string& dso_name,
5278
void** dso_handle) {
5379
int dynload_flags = RTLD_LAZY | RTLD_LOCAL;
5480
*dso_handle = nullptr;
5581

56-
std::string dlPath = dso_path;
82+
std::string dlPath = dso_name;
5783
if (search_root.empty()) {
58-
// default search xxx.so from LD_LIBRARY_PATH
59-
*dso_handle = dlopen(dlPath.c_str(), dynload_flags);
84+
GetDsoHandleFromDefaultPath(dlPath, dso_handle, dynload_flags);
6085
} else {
6186
// search xxx.so from custom path
62-
dlPath = join(search_root, dso_path);
87+
dlPath = join(search_root, dso_name);
6388
*dso_handle = dlopen(dlPath.c_str(), dynload_flags);
64-
// then, search xxx.so from LD_LIBRARY_PATH
65-
if (nullptr == *dso_handle) {
66-
*dso_handle = dlopen(dso_path.c_str(), dynload_flags);
89+
// if not found, search from default path
90+
if (nullptr == dso_handle) {
91+
LOG(WARNING) << "Failed to find cuda library: " << dlPath;
92+
dlPath = dso_name;
93+
GetDsoHandleFromDefaultPath(dlPath, dso_handle, dynload_flags);
6794
}
6895
}
6996

7097
CHECK(nullptr != *dso_handle)
71-
<< "For Gpu version of PaddlePaddle, it couldn't find CUDA library: "
72-
<< dlPath.c_str() << ". Please make sure you already specify its path. "
73-
<< "Note: for training data on Cpu using Gpu version of PaddlePaddle, "
74-
<< "you must specify libcudart via export LD_LIBRARY_PATH for Linux or "
75-
<< "export DYLD_LIBRARY_PATH for MAC OS.";
98+
<< "Failed to find cuda library: " << dlPath << std::endl
99+
<< "Please specify its path correctly using one of the following ideas: \n"
100+
101+
<< "Idea 1. set cuda and cudnn lib path at runtime. "
102+
<< "http://www.paddlepaddle.org/doc/ui/cmd_argument/argument_outline.html \n"
103+
<< "For instance, issue command: paddle train --use_gpu=1 "
104+
<< "--cuda_dir=/usr/local/cudnn/lib --cudnn_dir=/usr/local/cudnn/lib ...\n"
105+
106+
<< "Idea 2. set environment variable LD_LIBRARY_PATH on Linux or "
107+
<< "DYLD_LIBRARY_PATH on Mac OS. \n"
108+
<< "For instance, issue command: export LD_LIBRARY_PATH=... \n"
109+
110+
<< "Note: After Mac OS 10.11, using the DYLD_LIBRARY_PATH is impossible "
111+
<< "unless System Integrity Protection (SIP) is disabled. However, @Idea 1"
112+
<< "always work well.";
76113
}
77114

78115
void GetCublasDsoHandle(void** dso_handle) {
79116
#if defined(__APPLE__) || defined(__OSX__)
80-
GetDsoHandleWithSearchPath(FLAGS_cuda_dir, "libcublas.dylib", dso_handle);
117+
GetDsoHandleFromSearchPath(FLAGS_cuda_dir, "libcublas.dylib", dso_handle);
81118
#else
82-
GetDsoHandleWithSearchPath(FLAGS_cuda_dir, "libcublas.so", dso_handle);
119+
GetDsoHandleFromSearchPath(FLAGS_cuda_dir, "libcublas.so", dso_handle);
83120
#endif
84121
}
85122

86123
void GetCudnnDsoHandle(void** dso_handle) {
87124
#if defined(__APPLE__) || defined(__OSX__)
88-
GetDsoHandleWithSearchPath(FLAGS_cudnn_dir, "libcudnn.dylib", dso_handle);
125+
GetDsoHandleFromSearchPath(FLAGS_cudnn_dir, "libcudnn.dylib", dso_handle);
89126
#else
90-
GetDsoHandleWithSearchPath(FLAGS_cudnn_dir, "libcudnn.so", dso_handle);
127+
GetDsoHandleFromSearchPath(FLAGS_cudnn_dir, "libcudnn.so", dso_handle);
91128
#endif
92129
}
93130

94131
void GetCudartDsoHandle(void** dso_handle) {
95132
#if defined(__APPLE__) || defined(__OSX__)
96-
GetDsoHandleWithSearchPath("", "libcudart.dylib", dso_handle);
133+
GetDsoHandleFromSearchPath("", "libcudart.dylib", dso_handle);
97134
#else
98-
GetDsoHandleWithSearchPath("", "libcudart.so", dso_handle);
135+
GetDsoHandleFromSearchPath("", "libcudart.so", dso_handle);
99136
#endif
100137
}
101138

102139
void GetCurandDsoHandle(void** dso_handle) {
103140
#if defined(__APPLE__) || defined(__OSX__)
104-
GetDsoHandleWithSearchPath(FLAGS_cuda_dir, "libcurand.dylib", dso_handle);
141+
GetDsoHandleFromSearchPath(FLAGS_cuda_dir, "libcurand.dylib", dso_handle);
105142
#else
106-
GetDsoHandleWithSearchPath(FLAGS_cuda_dir, "libcurand.so", dso_handle);
143+
GetDsoHandleFromSearchPath(FLAGS_cuda_dir, "libcurand.so", dso_handle);
107144
#endif
108145
}

0 commit comments

Comments
 (0)