diff --git a/dnn/cuda-stub/src/libcuda.cpp b/dnn/cuda-stub/src/libcuda.cpp index cf55fac74ec79bbc70c0056022bd447b0f2ea5d8..50f25c007419226a65ef6a162b2655ae3de4528c 100644 --- a/dnn/cuda-stub/src/libcuda.cpp +++ b/dnn/cuda-stub/src/libcuda.cpp @@ -1,8 +1,3 @@ -/* - * LIBCUDA_PATH: candidate paths to libcuda.so; multiple paths are - * splitted by colons - **/ - #pragma GCC visibility push(default) #include @@ -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 #include -#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(LoadLibrary(file)); + return static_cast(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; } -