aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/stream_executor/dso_loader.cc
blob: ea5dffd15e50969af45e3153e648dd47ab30610b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

// TODO(jhen): Replace hardcoded, platform specific path strings in GetXXXPath()
// with a function in e.g. cuda.h.

#include "tensorflow/stream_executor/dso_loader.h"

#include <limits.h>
#include <stdlib.h>
#include <initializer_list>
#include <vector>

#include "tensorflow/core/platform/load_library.h"
#include "tensorflow/stream_executor/lib/env.h"
#include "tensorflow/stream_executor/lib/error.h"
#include "tensorflow/stream_executor/lib/path.h"
#include "tensorflow/stream_executor/lib/str_util.h"
#include "tensorflow/stream_executor/lib/strcat.h"
#include "tensorflow/stream_executor/lib/stringprintf.h"
#include "tensorflow/stream_executor/platform/logging.h"
#include "tensorflow/stream_executor/platform/port.h"

#if !defined(PLATFORM_GOOGLE)
#include "cuda/cuda_config.h"
#endif

namespace stream_executor {
namespace internal {

string GetCudaVersion() { return TF_CUDA_VERSION; }
string GetCudnnVersion() { return TF_CUDNN_VERSION; }

/* static */ port::Status DsoLoader::GetCublasDsoHandle(void** dso_handle) {
  return GetDsoHandle(FindDsoPath(port::Env::Default()->FormatLibraryFileName(
                                      "cublas", GetCudaVersion()),
                                  GetCudaLibraryDirPath()),
                      dso_handle);
}

/* static */ port::Status DsoLoader::GetCudnnDsoHandle(void** dso_handle) {
  // libcudnn is versioned differently than the other libraries and may have a
  // different version number than other CUDA libraries.  See b/22397368 for
  // some details about the complications surrounding this.
  return GetDsoHandle(FindDsoPath(port::Env::Default()->FormatLibraryFileName(
                                      "cudnn", GetCudnnVersion()),
                                  GetCudaLibraryDirPath()),
                      dso_handle);
}

/* static */ port::Status DsoLoader::GetCufftDsoHandle(void** dso_handle) {
  return GetDsoHandle(FindDsoPath(port::Env::Default()->FormatLibraryFileName(
                                      "cufft", GetCudaVersion()),
                                  GetCudaLibraryDirPath()),
                      dso_handle);
}

/* static */ port::Status DsoLoader::GetCurandDsoHandle(void** dso_handle) {
  return GetDsoHandle(FindDsoPath(port::Env::Default()->FormatLibraryFileName(
                                      "curand", GetCudaVersion()),
                                  GetCudaLibraryDirPath()),
                      dso_handle);
}

/* static */ port::Status DsoLoader::GetLibcudaDsoHandle(void** dso_handle) {
#if defined(PLATFORM_WINDOWS)
  return GetDsoHandle(
      FindDsoPath(port::Env::Default()->FormatLibraryFileName("nvcuda", ""),
                  GetCudaDriverLibraryPath()),
      dso_handle);
#else
  port::Status status = GetDsoHandle(
      FindDsoPath(port::Env::Default()->FormatLibraryFileName("cuda", "1"),
                  GetCudaDriverLibraryPath()),
      dso_handle);
#if defined(__APPLE__)
  // On Mac OS X, CUDA sometimes installs libcuda.dylib instead of
  // libcuda.1.dylib.
  return status.ok() ? status : GetDsoHandle(
     FindDsoPath(port::Env::Default()->FormatLibraryFileName("cuda", ""),
                 GetCudaDriverLibraryPath()),
     dso_handle);
#else
  return status;
#endif
#endif
}

/* static */ port::Status DsoLoader::GetLibcuptiDsoHandle(void** dso_handle) {
#if defined(ANDROID_TEGRA)
  // On Android devices the CUDA version number is not added to the library
  // name.
  return GetDsoHandle(
      FindDsoPath(port::Env::Default()->FormatLibraryFileName("cupti", ""),
                  GetCudaCuptiLibraryPath()),
      dso_handle);
#else
  return GetDsoHandle(FindDsoPath(port::Env::Default()->FormatLibraryFileName(
                                      "cupti", GetCudaVersion()),
                                  GetCudaCuptiLibraryPath()),
                      dso_handle);
#endif
}

static mutex& GetRpathMutex() {
  static mutex* mu = new mutex;
  return *mu;
}

/* static */ void DsoLoader::RegisterRpath(port::StringPiece path) {
  mutex_lock lock{GetRpathMutex()};
  GetRpaths()->emplace_back(path);
}

/* static */ port::Status DsoLoader::GetDsoHandle(port::StringPiece path,
                                                  void** dso_handle,
                                                  LoadKind load_kind) {
  if (load_kind != LoadKind::kLocal) {
    return port::Status(port::error::INVALID_ARGUMENT,
                        "Only LoadKind::kLocal is currently supported");
  }
  string path_string(path);
  port::Status s =
      port::Env::Default()->LoadLibrary(path_string.c_str(), dso_handle);
  if (!s.ok()) {
#if !defined(PLATFORM_WINDOWS)
    char* ld_library_path = getenv("LD_LIBRARY_PATH");
#endif
    LOG(INFO) << "Couldn't open CUDA library " << path
#if !defined(PLATFORM_WINDOWS)
              << ". LD_LIBRARY_PATH: "
              << (ld_library_path != nullptr ? ld_library_path : "")
#endif
    ;
    return port::Status(port::error::FAILED_PRECONDITION,
                        port::StrCat("could not dlopen DSO: ", path,
                                     "; dlerror: ", s.error_message()));
  }
  LOG(INFO) << "successfully opened CUDA library " << path << " locally";
  return port::Status::OK();
}

/* static */ string DsoLoader::GetBinaryDirectory(bool strip_executable_name) {
  string exe_path = port::Env::Default()->GetExecutablePath();
  return strip_executable_name ? string(port::Dirname(exe_path)) : exe_path;
}

// Creates a heap-allocated vector for initial rpaths.
// Ownership is transferred to the caller.
static std::vector<string>* CreatePrimordialRpaths() {
  auto rpaths = new std::vector<string>;
#if defined(__APPLE__)
  rpaths->push_back("driver/driver_sh.runfiles/local_config_cuda/cuda/lib");
#else
  rpaths->push_back("driver/driver_sh.runfiles/local_config_cuda/cuda/lib64");
#endif
  return rpaths;
}

/* static */ std::vector<string>* DsoLoader::GetRpaths() {
  static std::vector<string>* rpaths = CreatePrimordialRpaths();
  return rpaths;
}

/* static */ bool DsoLoader::TrySymbolicDereference(string* candidate) {
#if defined(PLATFORM_WINDOWS)
  return false;
#else
  char buf[PATH_MAX];
  char* result = realpath(candidate->c_str(), buf);
  if (result == nullptr) {
    return false;
  }
  VLOG(3) << "realpath resolved candidate path \"" << *candidate << "\" to \""
          << result << "\"";
  *candidate = result;
  return true;
#endif
}

/* static */ string DsoLoader::FindDsoPath(port::StringPiece library_name,
                                           port::StringPiece runfiles_relpath) {
  // Keep a record of the paths we attempted so we can dump out meaningful
  // diagnostics if no path is found.
  std::vector<string> attempted;

  using StringPieces = std::vector<port::StringPiece>;
  string candidate;

  // Otherwise, try binary-plus-rpath locations.
  string binary_directory =
      GetBinaryDirectory(true /* = strip_executable_name */);
  mutex_lock lock{GetRpathMutex()};
  for (const string& rpath : *GetRpaths()) {
    candidate =
        port::Join(StringPieces{binary_directory, rpath, library_name}, "/");
    if (TrySymbolicDereference(&candidate)) {
      return candidate;
    }
  }
  attempted.push_back(candidate);

  return string(library_name);
}

/* static */ string DsoLoader::GetCudaLibraryDirPath() {
#if defined(__APPLE__)
  return "external/local_config_cuda/cuda/lib";
#else
  return "external/local_config_cuda/cuda/lib64";
#endif
}

/* static */ string DsoLoader::GetCudaDriverLibraryPath() {
#if defined(__APPLE__)
  return "external/local_config_cuda/cuda/driver/lib";
#elif defined(PLATFORM_WINDOWS)
  return "";
#else
  return "external/local_config_cuda/cuda/driver/lib64";
#endif
}

/* static */ string DsoLoader::GetCudaCuptiLibraryPath() {
#if defined(__APPLE__)
  return "external/local_config_cuda/cuda/extras/CUPTI/lib";
#else
  return "external/local_config_cuda/cuda/extras/CUPTI/lib64";
#endif
}

// -- CachedDsoLoader

/* static */ port::StatusOr<void*> CachedDsoLoader::GetCublasDsoHandle() {
  static port::StatusOr<void*> result =
      FetchHandleResult(DsoLoader::GetCublasDsoHandle);
  return result;
}

/* static */ port::StatusOr<void*> CachedDsoLoader::GetCurandDsoHandle() {
  static port::StatusOr<void*> result =
      FetchHandleResult(DsoLoader::GetCurandDsoHandle);
  return result;
}

/* static */ port::StatusOr<void*> CachedDsoLoader::GetCudnnDsoHandle() {
  static port::StatusOr<void*> result =
      FetchHandleResult(DsoLoader::GetCudnnDsoHandle);
  return result;
}

/* static */ port::StatusOr<void*> CachedDsoLoader::GetCufftDsoHandle() {
  static port::StatusOr<void*> result =
      FetchHandleResult(DsoLoader::GetCufftDsoHandle);
  return result;
}

/* static */ port::StatusOr<void*> CachedDsoLoader::GetLibcudaDsoHandle() {
  static port::StatusOr<void*> result =
      FetchHandleResult(DsoLoader::GetLibcudaDsoHandle);
  return result;
}

/* static */ port::StatusOr<void*> CachedDsoLoader::GetLibcuptiDsoHandle() {
  static port::StatusOr<void*> result =
      FetchHandleResult(DsoLoader::GetLibcuptiDsoHandle);
  return result;
}

/* static */ port::StatusOr<void*> CachedDsoLoader::FetchHandleResult(
    std::function<port::Status(void**)> load_dso) {
  void* handle;
  auto status = load_dso(&handle);
  if (!status.ok()) {
    return status;
  }
  return handle;
}

}  // namespace internal
}  // namespace stream_executor