@@ -11,12 +11,14 @@ distributed under the License is distributed on an "AS IS" BASIS,
11
11
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
See the License for the specific language governing permissions and
13
13
limitations under the License. */
14
-
15
14
#include " paddle/fluid/platform/dynload/dynamic_loader.h"
15
+
16
16
#include < dlfcn.h>
17
+
17
18
#include < memory>
18
- #include < mutex>
19
+ #include < mutex> // NOLINT
19
20
#include < string>
21
+
20
22
#include " gflags/gflags.h"
21
23
#include " glog/logging.h"
22
24
#include " paddle/fluid/platform/dynload/cupti_lib_path.h"
@@ -65,22 +67,21 @@ static inline std::string join(const std::string& part1,
65
67
return ret;
66
68
}
67
69
68
- static inline void GetDsoHandleFromDefaultPath (std::string& dso_path,
69
- void ** dso_handle,
70
- int dynload_flags) {
70
+ static inline void * GetDsoHandleFromDefaultPath (const std::string& dso_path,
71
+ int dynload_flags) {
71
72
VLOG (3 ) << " Try to find library: " << dso_path
72
73
<< " from default system path." ;
73
74
// default search from LD_LIBRARY_PATH/DYLD_LIBRARY_PATH
74
- * dso_handle = dlopen (dso_path.c_str (), dynload_flags);
75
+ void * dso_handle = dlopen (dso_path.c_str (), dynload_flags);
75
76
76
77
// DYLD_LIBRARY_PATH is disabled after Mac OS 10.11 to
77
78
// bring System Integrity Projection (SIP), if dso_handle
78
79
// is null, search from default package path in Mac OS.
79
80
#if defined(__APPLE__) || defined(__OSX__)
80
- if (nullptr == * dso_handle) {
81
- dso_path = join ( " /usr/local/cuda/lib/ " , dso_path);
82
- *dso_handle = dlopen (dso_path.c_str (), dynload_flags);
83
- if (nullptr == * dso_handle) {
81
+ if (nullptr == dso_handle) {
82
+ dso_handle =
83
+ dlopen (join ( " /usr/local/cuda/lib/ " , dso_path) .c_str (), dynload_flags);
84
+ if (nullptr == dso_handle) {
84
85
if (dso_path == " libcudnn.dylib" ) {
85
86
LOG (WARNING) << " Note: [Recommend] copy cudnn into /usr/local/cuda/ \n "
86
87
" For instance, sudo tar -xzf "
@@ -91,28 +92,29 @@ static inline void GetDsoHandleFromDefaultPath(std::string& dso_path,
91
92
}
92
93
}
93
94
#endif
95
+
96
+ return dso_handle;
94
97
}
95
98
96
- static inline void GetDsoHandleFromSearchPath (const std::string& search_root,
97
- const std::string& dso_name,
98
- void ** dso_handle,
99
- bool throw_on_error = true ) {
99
+ static inline void * GetDsoHandleFromSearchPath (const std::string& search_root,
100
+ const std::string& dso_name,
101
+ bool throw_on_error = true ) {
100
102
int dynload_flags = RTLD_LAZY | RTLD_LOCAL;
101
- * dso_handle = nullptr ;
103
+ void * dso_handle = nullptr ;
102
104
103
105
std::string dlPath = dso_name;
104
106
if (search_root.empty ()) {
105
- GetDsoHandleFromDefaultPath (dlPath, dso_handle , dynload_flags);
107
+ dso_handle = GetDsoHandleFromDefaultPath (dlPath, dynload_flags);
106
108
} else {
107
109
// search xxx.so from custom path
108
110
dlPath = join (search_root, dso_name);
109
- * dso_handle = dlopen (dlPath.c_str (), dynload_flags);
111
+ dso_handle = dlopen (dlPath.c_str (), dynload_flags);
110
112
// if not found, search from default path
111
- if (nullptr == * dso_handle) {
113
+ if (nullptr == dso_handle) {
112
114
LOG (WARNING) << " Failed to find dynamic library: " << dlPath << " ("
113
115
<< dlerror () << " )" ;
114
116
dlPath = dso_name;
115
- GetDsoHandleFromDefaultPath (dlPath, dso_handle , dynload_flags);
117
+ dso_handle = GetDsoHandleFromDefaultPath (dlPath, dynload_flags);
116
118
}
117
119
}
118
120
auto error_msg =
@@ -124,70 +126,71 @@ static inline void GetDsoHandleFromSearchPath(const std::string& search_root,
124
126
" using the DYLD_LIBRARY_PATH is impossible unless System "
125
127
" Integrity Protection (SIP) is disabled." ;
126
128
if (throw_on_error) {
127
- PADDLE_ENFORCE (nullptr != * dso_handle, error_msg, dlPath, dlerror ());
128
- } else if (nullptr == * dso_handle) {
129
+ PADDLE_ENFORCE (nullptr != dso_handle, error_msg, dlPath, dlerror ());
130
+ } else if (nullptr == dso_handle) {
129
131
LOG (WARNING) << string::Sprintf (error_msg, dlPath, dlerror ());
130
132
}
133
+
134
+ return dso_handle;
131
135
}
132
136
133
- void GetCublasDsoHandle (void ** dso_handle ) {
137
+ void * GetCublasDsoHandle () {
134
138
#if defined(__APPLE__) || defined(__OSX__)
135
- GetDsoHandleFromSearchPath (FLAGS_cuda_dir, " libcublas.dylib" , dso_handle );
139
+ return GetDsoHandleFromSearchPath (FLAGS_cuda_dir, " libcublas.dylib" );
136
140
#else
137
- GetDsoHandleFromSearchPath (FLAGS_cuda_dir, " libcublas.so" , dso_handle );
141
+ return GetDsoHandleFromSearchPath (FLAGS_cuda_dir, " libcublas.so" );
138
142
#endif
139
143
}
140
144
141
- void GetCUDNNDsoHandle (void ** dso_handle ) {
145
+ void * GetCUDNNDsoHandle () {
142
146
#if defined(__APPLE__) || defined(__OSX__)
143
- GetDsoHandleFromSearchPath (FLAGS_cudnn_dir, " libcudnn.dylib" , dso_handle,
144
- false );
147
+ return GetDsoHandleFromSearchPath (FLAGS_cudnn_dir, " libcudnn.dylib" , false );
145
148
#else
146
- GetDsoHandleFromSearchPath (FLAGS_cudnn_dir, " libcudnn.so" , dso_handle , false );
149
+ return GetDsoHandleFromSearchPath (FLAGS_cudnn_dir, " libcudnn.so" , false );
147
150
#endif
148
151
}
149
152
150
- void GetCUPTIDsoHandle (void ** dso_handle ) {
153
+ void * GetCUPTIDsoHandle () {
151
154
std::string cupti_path = cupti_lib_path;
152
155
if (!FLAGS_cupti_dir.empty ()) {
153
156
cupti_path = FLAGS_cupti_dir;
154
157
}
155
158
#if defined(__APPLE__) || defined(__OSX__)
156
- GetDsoHandleFromSearchPath (cupti_path, " libcupti.dylib" , dso_handle , false );
159
+ return GetDsoHandleFromSearchPath (cupti_path, " libcupti.dylib" , false );
157
160
#else
158
- GetDsoHandleFromSearchPath (cupti_path, " libcupti.so" , dso_handle , false );
161
+ return GetDsoHandleFromSearchPath (cupti_path, " libcupti.so" , false );
159
162
#endif
160
163
}
161
164
162
- void GetCurandDsoHandle (void ** dso_handle ) {
165
+ void * GetCurandDsoHandle () {
163
166
#if defined(__APPLE__) || defined(__OSX__)
164
- GetDsoHandleFromSearchPath (FLAGS_cuda_dir, " libcurand.dylib" , dso_handle );
167
+ return GetDsoHandleFromSearchPath (FLAGS_cuda_dir, " libcurand.dylib" );
165
168
#else
166
- GetDsoHandleFromSearchPath (FLAGS_cuda_dir, " libcurand.so" , dso_handle );
169
+ return GetDsoHandleFromSearchPath (FLAGS_cuda_dir, " libcurand.so" );
167
170
#endif
168
171
}
169
172
170
- void GetWarpCTCDsoHandle (void ** dso_handle ) {
173
+ void * GetWarpCTCDsoHandle () {
171
174
#if defined(__APPLE__) || defined(__OSX__)
172
- GetDsoHandleFromSearchPath (FLAGS_warpctc_dir, " libwarpctc.dylib" , dso_handle );
175
+ return GetDsoHandleFromSearchPath (FLAGS_warpctc_dir, " libwarpctc.dylib" );
173
176
#else
174
- GetDsoHandleFromSearchPath (FLAGS_warpctc_dir, " libwarpctc.so" , dso_handle );
177
+ return GetDsoHandleFromSearchPath (FLAGS_warpctc_dir, " libwarpctc.so" );
175
178
#endif
176
179
}
177
180
178
- void GetLapackDsoHandle (void ** dso_handle ) {
181
+ void * GetLapackDsoHandle () {
179
182
#if defined(__APPLE__) || defined(__OSX__)
180
- GetDsoHandleFromSearchPath (FLAGS_lapack_dir, " liblapacke.dylib" , dso_handle );
183
+ return GetDsoHandleFromSearchPath (FLAGS_lapack_dir, " liblapacke.dylib" );
181
184
#else
182
- GetDsoHandleFromSearchPath (FLAGS_lapack_dir, " liblapacke.so" , dso_handle );
185
+ return GetDsoHandleFromSearchPath (FLAGS_lapack_dir, " liblapacke.so" );
183
186
#endif
184
187
}
185
188
186
- void GetNCCLDsoHandle (void ** dso_handle ) {
189
+ void * GetNCCLDsoHandle () {
187
190
#if defined(__APPLE__) || defined(__OSX__)
188
- GetDsoHandleFromSearchPath (FLAGS_nccl_dir, " libnccl.dylib" , dso_handle );
191
+ return GetDsoHandleFromSearchPath (FLAGS_nccl_dir, " libnccl.dylib" );
189
192
#else
190
- GetDsoHandleFromSearchPath (FLAGS_nccl_dir, " libnccl.so" , dso_handle );
193
+ return GetDsoHandleFromSearchPath (FLAGS_nccl_dir, " libnccl.so" );
191
194
#endif
192
195
}
193
196
0 commit comments