hl_dso_loader.cc 3.8 KB
Newer Older
Z
zhangjinchao01 已提交
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
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

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. */


#include "hl_dso_loader.h"
#include "paddle/utils/Logging.h"
#include "paddle/utils/CommandLineParser.h"

P_DEFINE_string(cudnn_dir, "",
                "Specify path for loading libcudnn.so. For instance, "
                "/usr/local/cudnn/lib64. If empty [default], dlopen will search "
                "cudnn from LD_LIBRARY_PATH");

P_DEFINE_string(cuda_dir, "",
                "Specify path for loading cuda library, such as libcublas, "
                "libcurand. For instance, /usr/local/cuda/lib64. "
                "(Note: libcudart can not be specified by cuda_dir, since some "
                "build-in function in cudart already ran before main entry). "
                "If empty [default], dlopen will search cuda from LD_LIBRARY_PATH");

static inline std::string join(const std::string& part1, const std::string& part2) {
  // directory separator
  const char sep = '/';

  if (!part2.empty() && part2.front() == sep) {
    return part2;
  }
  std::string ret;
  ret.reserve(part1.size() + part2.size() + 1);
  ret = part1;
  if (!ret.empty() && ret.back() != sep) {
    ret += sep;
  }
  ret += part2;
  return ret;
}

static inline void GetDsoHandleWithSearchPath(
        const std::string& search_root,
        const std::string& dso_path,
        void** dso_handle) {
    int dynload_flags = RTLD_LAZY | RTLD_LOCAL;
    *dso_handle = nullptr;

    std::string dlPath = dso_path;
    if (search_root.empty()) {
        // default search xxx.so from LD_LIBRARY_PATH
        *dso_handle = dlopen(dlPath.c_str(), dynload_flags);
    } else {
        // search xxx.so from custom path
        dlPath = join(search_root, dso_path);
        *dso_handle = dlopen(dlPath.c_str(), dynload_flags);
        // then, search xxx.so from LD_LIBRARY_PATH
        if (nullptr == *dso_handle) {
            *dso_handle = dlopen(dso_path.c_str(), dynload_flags);
        }
    }

    CHECK(nullptr != *dso_handle)
      << "For Gpu version of PaddlePaddle, it couldn't find CUDA library: "
72 73 74 75
      << dlPath.c_str() << ". Please make sure you already specify its path. "
      << "Note: for training data on Cpu using Gpu version of PaddlePaddle, "
      << "you must specify libcudart via export LD_LIBRARY_PATH for Linux or "
      << "export DYLD_LIBRARY_PATH for MAC OS.";
Z
zhangjinchao01 已提交
76 77 78
}

void GetCublasDsoHandle(void** dso_handle) {
79 80 81
#if defined(__APPLE__) || defined(__OSX__)
    GetDsoHandleWithSearchPath(FLAGS_cuda_dir, "libcublas.dylib", dso_handle);
#else
Z
zhangjinchao01 已提交
82
    GetDsoHandleWithSearchPath(FLAGS_cuda_dir, "libcublas.so", dso_handle);
83
#endif
Z
zhangjinchao01 已提交
84 85 86
}

void GetCudnnDsoHandle(void** dso_handle) {
87 88 89
#if defined(__APPLE__) || defined(__OSX__)
    GetDsoHandleWithSearchPath(FLAGS_cudnn_dir, "libcudnn.dylib", dso_handle);
#else
Z
zhangjinchao01 已提交
90
    GetDsoHandleWithSearchPath(FLAGS_cudnn_dir, "libcudnn.so", dso_handle);
91
#endif
Z
zhangjinchao01 已提交
92 93 94
}

void GetCudartDsoHandle(void** dso_handle) {
95 96 97
#if defined(__APPLE__) || defined(__OSX__)
    GetDsoHandleWithSearchPath("", "libcudart.dylib", dso_handle);
#else
Z
zhangjinchao01 已提交
98
    GetDsoHandleWithSearchPath("", "libcudart.so", dso_handle);
99
#endif
Z
zhangjinchao01 已提交
100 101 102
}

void GetCurandDsoHandle(void** dso_handle) {
103 104 105
#if defined(__APPLE__) || defined(__OSX__)
    GetDsoHandleWithSearchPath(FLAGS_cuda_dir, "libcurand.dylib", dso_handle);
#else
Z
zhangjinchao01 已提交
106
    GetDsoHandleWithSearchPath(FLAGS_cuda_dir, "libcurand.so", dso_handle);
107
#endif
Z
zhangjinchao01 已提交
108
}