提交 ea6bfe6c 编写于 作者: M Megvii Engine Team 提交者: Xu Xinran

fix(dnn/cuda-stub): simplify and use proper search paths

Removed the `access()` call before `dlopen()`.
It was copy-pasted from the opencl-stub, does not make sense here, and
prevents `dlopen()` from loading `libcuda.so` from non-default path.

Updated the name of the library providing CUDA Driver API on different
platforms, these are harvested from the following file in a CUDA
install:
samples/6_Advanced/matrixMulDynlinkJIT/cuda_drvapi_dynlink.c
GitOrigin-RevId: ed43cab8c8b4a9b9f8baaa958e8b5123b72dd179
上级 01092feb
/*
* LIBCUDA_PATH: candidate paths to libcuda.so; multiple paths are
* splitted by colons
**/
#pragma GCC visibility push(default)
#include <cstdio>
......@@ -15,25 +10,12 @@ extern "C" {
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
static const char* default_so_paths[] = {
"/usr/local/nvidia/lib64/libcuda.so",
"/usr/lib/x86_64-linux-gnu/libcuda.so",
"libcuda.so",
};
#if defined(_WIN32)
#include <io.h>
#include <windows.h>
#define F_OK 0
#define RTLD_LAZY 0
// On the windows platform we use a lib_filename without a full path so
// the win-api "LoadLibrary" would uses a standard search strategy to
// find the lib module. As we cannot access to the lib_filename without a
// full path, we should not use "access(a, b)" to verify it.
#define access(a, b) false
static void* dlopen(const char* file, int) {
return static_cast<void*>(LoadLibrary(file));
return static_cast<void*>(LoadLibraryA(file));
}
static void* dlerror() {
......@@ -68,55 +50,43 @@ CUresult on_init_failed(int func_idx) {
#undef _WRAPLIB_CALLBACK
#undef _WRAPLIB_API_CALL
static bool open_shared_lib(const char* path, void*& handle) {
if (!access(path, F_OK)) {
handle = dlopen(path, RTLD_LAZY);
if (handle)
return true;
LOGE("cuda lib found but can not be opened: %s err=%s", path,
dlerror());
}
return false;
}
// Harvested from cuda_drvapi_dynlink.c
static const char* default_so_paths[] = {
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
"nvcuda.dll",
#elif defined(__unix__) || defined (__QNX__) || defined(__APPLE__) || defined(__MACOSX)
#if defined(__APPLE__) || defined(__MACOSX)
"/usr/local/cuda/lib/libcuda.dylib",
#elif defined(__ANDROID__)
#if defined (__aarch64__)
"/system/vendor/lib64/libcuda.so",
#elif defined(__arm__)
"/system/vendor/lib/libcuda.so",
#endif
#else
"libcuda.so.1",
// In case some users does not have correct search path configured in
// /etc/ld.so.conf
"/usr/lib/x86_64-linux-gnu/libcuda.so",
"/usr/local/nvidia/lib64/libcuda.so",
#endif
#else
#error "Unknown platform"
#endif
};
static void* get_library_handle() {
const char* path = nullptr;
auto str_cptr = getenv("LIBCUDA_PATH");
std::string str;
void* handle = nullptr;
if (str_cptr) {
str = str_cptr;
char* p = &str[0];
const char* begin = p;
while (*p) {
if (*p == ':') {
*p = 0;
if (open_shared_lib(begin, handle)) {
path = begin;
break;
}
begin = p + 1;
}
++p;
}
if (open_shared_lib(begin, handle)) {
path = begin;
}
}
if (!path) {
for (size_t i = 0; i < (sizeof(default_so_paths) / sizeof(char*));
i++) {
if (open_shared_lib(default_so_paths[i], handle)) {
path = default_so_paths[i];
break;
}
for (size_t i = 0; i < (sizeof(default_so_paths) / sizeof(char*)); i++) {
handle = dlopen(default_so_paths[i], RTLD_LAZY);
if (handle) {
break;
}
}
if (!path) {
LOGE("can not find cuda");
if (!handle) {
LOGE("Failed to load CUDA Driver API library");
return nullptr;
}
return handle;
......@@ -137,4 +107,3 @@ static void* resolve_library_func(void* handle, const char* func) {
}
return ret;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册