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

3 4 5
#pragma GCC visibility push(default)

#include <cstdio>
6 7
#define LOGI(fmt, v...) fprintf(stderr, "info: " fmt "\n", ##v)
#define LOGD(fmt, v...) fprintf(stderr, "debug: " fmt "\n", ##v)
8 9

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

#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;
}
25 26

}  // namespace
27 28 29

#define _WRAPLIB_API_CALL CUDAAPI
#define _WRAPLIB_CALLBACK CUDA_CB
30

31 32 33 34 35 36 37 38 39 40
//! 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
41 42 43

#if CUDA_VERSION == 10010
#include "./libcuda-wrap_10.1.h"
44 45 46 47 48 49
#elif CUDA_VERSION == 10020
#include "./libcuda-wrap_10.2.h"
#elif CUDA_VERSION == 11010
#include "./libcuda-wrap_11.1.h"
#elif CUDA_VERSION == 11020
#include "./libcuda-wrap_11.2.h"
50 51
#elif CUDA_VERSION == 11040
#include "./libcuda-wrap_11.4.h"
52 53
#elif CUDA_VERSION == 11080
#include "./libcuda-wrap_11.8.h"
54 55 56 57
#else
#error "cuda stub not support this cuda version, you can close cuda stub to passby"
#endif

58 59 60
#undef _WRAPLIB_CALLBACK
#undef _WRAPLIB_API_CALL

61 62 63 64 65 66 67 68 69
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

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

92 93 94 95
static const char* extra_so_paths[] = {
        "/usr/lib/x86_64-linux-gnu/libcuda.so",
        "/usr/local/nvidia/lib64/libcuda.so",
};
96

97
static const char* g_default_api_name = "cuda";
98
#include "./dlopen_helper.h"