libcuda.cpp 3.1 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

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

//! 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
44 45
#elif CUDA_VERSION == 10020
#include "./libcuda-wrap_10.2.h"
46 47 48 49 50 51 52 53 54 55
//! 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
56 57 58 59
#elif CUDA_VERSION == 11010
#include "./libcuda-wrap_11.1.h"
#elif CUDA_VERSION == 11020
#include "./libcuda-wrap_11.2.h"
60 61
#elif CUDA_VERSION == 11040
#include "./libcuda-wrap_11.4.h"
62 63
#elif CUDA_VERSION == 11080
#include "./libcuda-wrap_11.8.h"
64 65 66 67 68
#else
#error "cuda stub not support this cuda version, you can close cuda stub to passby"
#endif


69 70 71
#undef _WRAPLIB_CALLBACK
#undef _WRAPLIB_API_CALL

72 73 74 75 76 77 78 79 80
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

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

103 104 105 106
static const char* extra_so_paths[] = {
        "/usr/lib/x86_64-linux-gnu/libcuda.so",
        "/usr/local/nvidia/lib64/libcuda.so",
};
107

108
static const char* g_default_api_name = "cuda";
109
#include "./dlopen_helper.h"