libcuda.cpp 2.9 KB
Newer Older
1 2
#include "megbrain_build_config.h"

3 4 5 6 7 8
#pragma GCC visibility push(default)

#include <cstdio>
#define LOGE(fmt, v...) fprintf(stderr, "err: " fmt "\n", ##v)

extern "C" {
9
#include "cuda.h"
10
}
11
#include "cudaProfiler.h"
12 13 14 15 16 17 18 19 20 21 22 23

#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

static void log_failed_load(int func_idx);
namespace {
template <typename T>
T on_init_failed(int func_idx);
template <>
CUresult on_init_failed(int func_idx) {
    log_failed_load(func_idx);
    return CUDA_ERROR_UNKNOWN;
}
24 25

}  // namespace
26 27 28

#define _WRAPLIB_API_CALL CUDAAPI
#define _WRAPLIB_CALLBACK CUDA_CB
29 30 31

#if CUDA_VERSION == 10010
#include "./libcuda-wrap_10.1.h"
32 33 34 35 36 37 38 39 40 41 42

//! as some symbols link from cuda lib, but used at other module, export here
#ifdef WIN32
#pragma comment(linker, "/export:cudaSetDevice")
#pragma comment(linker, "/export:cuCtxGetCurrent")
#pragma comment(linker, "/export:cudaGetDeviceCount")
#pragma comment(linker, "/export:cudaGetDeviceProperties")
#pragma comment(linker, "/export:cudaRuntimeGetVersion")
#pragma comment(linker, "/export:cudaGetDevice")
#pragma comment(linker, "/export:cudaDeviceSynchronize")
#endif
43 44
#elif CUDA_VERSION == 10020
#include "./libcuda-wrap_10.2.h"
45 46 47 48 49 50 51 52 53 54
//! as some symbols link from cuda lib, but used at other module, export here
#ifdef WIN32
#pragma comment(linker, "/export:cudaSetDevice")
#pragma comment(linker, "/export:cuCtxGetCurrent")
#pragma comment(linker, "/export:cudaGetDeviceCount")
#pragma comment(linker, "/export:cudaGetDeviceProperties")
#pragma comment(linker, "/export:cudaRuntimeGetVersion")
#pragma comment(linker, "/export:cudaGetDevice")
#pragma comment(linker, "/export:cudaDeviceSynchronize")
#endif
55 56 57 58 59 60 61 62 63
#elif CUDA_VERSION == 11010
#include "./libcuda-wrap_11.1.h"
#elif CUDA_VERSION == 11020
#include "./libcuda-wrap_11.2.h"
#else
#error "cuda stub not support this cuda version, you can close cuda stub to passby"
#endif


64 65 66
#undef _WRAPLIB_CALLBACK
#undef _WRAPLIB_API_CALL

67 68 69 70 71 72 73 74 75
static const char* default_so_name =
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
        "nvcuda.dll";
#elif defined(__APPLE__) || defined(__MACOSX)
        "libcuda.dylib";
#else
        "libcuda.so.1";
#endif

76 77 78
// Harvested from cuda_drvapi_dynlink.c
static const char* default_so_paths[] = {
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
79 80 81
        "nvcuda.dll",
#elif defined(__unix__) || defined(__QNX__) || defined(__APPLE__) || \
        defined(__MACOSX)
82
#if defined(__APPLE__) || defined(__MACOSX)
83
        "/usr/local/cuda/lib/libcuda.dylib",
84
#elif defined(__ANDROID__)
85 86
#if defined(__aarch64__)
        "/system/vendor/lib64/libcuda.so",
87
#elif defined(__arm__)
88
        "/system/vendor/lib/libcuda.so",
89 90
#endif
#else
91
        "libcuda.so.1",
92 93 94 95 96
#endif
#else
#error "Unknown platform"
#endif
};
97

98 99 100 101
static const char* extra_so_paths[] = {
        "/usr/lib/x86_64-linux-gnu/libcuda.so",
        "/usr/local/nvidia/lib64/libcuda.so",
};
102

103
static const char* g_default_api_name = "cuda";
104
#include "./dlopen_helper.h"