-
Notifications
You must be signed in to change notification settings - Fork 534
/
libcuda.cpp
98 lines (84 loc) · 2.65 KB
/
libcuda.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "megbrain_build_config.h"
#pragma GCC visibility push(default)
#include <cstdio>
#define LOGI(fmt, v...) fprintf(stderr, "info: " fmt "\n", ##v)
#define LOGD(fmt, v...) fprintf(stderr, "debug: " fmt "\n", ##v)
extern "C" {
#include "cuda.h"
}
#include "cudaProfiler.h"
#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;
}
} // namespace
#define _WRAPLIB_API_CALL CUDAAPI
#define _WRAPLIB_CALLBACK CUDA_CB
//! 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
#if CUDA_VERSION == 10010
#include "./libcuda-wrap_10.1.h"
#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"
#elif CUDA_VERSION == 11040
#include "./libcuda-wrap_11.4.h"
#elif CUDA_VERSION == 11080
#include "./libcuda-wrap_11.8.h"
#else
#error "cuda stub not support this cuda version, you can close cuda stub to passby"
#endif
#undef _WRAPLIB_CALLBACK
#undef _WRAPLIB_API_CALL
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
// 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",
#endif
#else
#error "Unknown platform"
#endif
};
static const char* extra_so_paths[] = {
"/usr/lib/x86_64-linux-gnu/libcuda.so",
"/usr/local/nvidia/lib64/libcuda.so",
};
static const char* g_default_api_name = "cuda";
#include "./dlopen_helper.h"