-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathcublas_stub.cc
274 lines (249 loc) · 11.9 KB
/
cublas_stub.cc
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <stdexcept>
#include <cublas_v2.h>
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
#ifdef _WIN32
# include <windows.h>
# define CUBLAS_LIBNAME "cublas64_" STR(CUBLAS_VER_MAJOR) ".dll"
#else
# include <dlfcn.h>
# define CUBLAS_LIBNAME "libcublas.so." STR(CUBLAS_VER_MAJOR)
#endif
#include <spdlog/spdlog.h>
#include "env.h"
namespace ctranslate2 {
template <typename Signature>
static Signature load_symbol(void* handle, const char* name, const char* library_name) {
#ifdef _WIN32
void* symbol = reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(handle), name));
#else
void* symbol = dlsym(handle, name);
#endif
if (!symbol)
throw std::runtime_error("Cannot load symbol " + std::string(name)
+ " from library " + std::string(library_name));
return reinterpret_cast<Signature>(symbol);
}
static inline void log_cublas_version(void* handle) {
using Signature = cublasStatus_t(*)(libraryPropertyType, int*);
const auto cublas_get_property = load_symbol<Signature>(handle,
"cublasGetProperty",
CUBLAS_LIBNAME);
int major_version = 0;
int minor_version = 0;
int patch_level = 0;
cublas_get_property(MAJOR_VERSION, &major_version);
cublas_get_property(MINOR_VERSION, &minor_version);
cublas_get_property(PATCH_LEVEL, &patch_level);
spdlog::info("Loaded cuBLAS library version {}.{}.{}",
major_version, minor_version, patch_level);
}
static void* get_so_handle() {
static auto so_handle = []() {
#ifdef _WIN32
std::string cuda_path = read_string_from_env("CUDA_PATH");
if (!cuda_path.empty()) {
cuda_path += "\\bin";
SetDllDirectoryA(cuda_path.c_str());
}
void* handle = static_cast<void*>(LoadLibraryA(CUBLAS_LIBNAME));
#else
void* handle = dlopen(CUBLAS_LIBNAME, RTLD_LAZY);
#endif
if (!handle)
throw std::runtime_error("Library " + std::string(CUBLAS_LIBNAME)
+ " is not found or cannot be loaded");
log_cublas_version(handle);
return handle;
}();
return so_handle;
}
template <typename Signature>
static Signature load_symbol(const char* name) {
void* handle = get_so_handle();
return load_symbol<Signature>(handle, name, CUBLAS_LIBNAME);
}
}
// TODO: these stub functions could be automatically generated.
extern "C" {
cublasStatus_t cublasCreate_v2(cublasHandle_t *handle) {
using Signature = cublasStatus_t(*)(cublasHandle_t*);
static auto func = ctranslate2::load_symbol<Signature>("cublasCreate_v2");
return func(handle);
}
cublasStatus_t cublasDestroy_v2(cublasHandle_t handle) {
using Signature = cublasStatus_t(*)(cublasHandle_t);
static auto func = ctranslate2::load_symbol<Signature>("cublasDestroy_v2");
return func(handle);
}
cublasStatus_t cublasSetStream_v2(cublasHandle_t handle, cudaStream_t stream) {
using Signature = cublasStatus_t(*)(cublasHandle_t, cudaStream_t);
static auto func = ctranslate2::load_symbol<Signature>("cublasSetStream_v2");
return func(handle, stream);
}
cublasStatus_t cublasGetMathMode(cublasHandle_t handle, cublasMath_t *mode) {
using Signature = cublasStatus_t(*)(cublasHandle_t, cublasMath_t*);
static auto func = ctranslate2::load_symbol<Signature>("cublasGetMathMode");
return func(handle, mode);
}
cublasStatus_t cublasSgemm_v2(cublasHandle_t handle,
cublasOperation_t transa,
cublasOperation_t transb,
int m,
int n,
int k,
const float *alpha,
const float *A,
int lda,
const float *B,
int ldb,
const float *beta,
float *C,
int ldc) {
using Signature = cublasStatus_t(*)(cublasHandle_t,
cublasOperation_t,
cublasOperation_t,
int,
int,
int,
const float*,
const float*,
int,
const float*,
int,
const float*,
float*,
int);
static auto func = ctranslate2::load_symbol<Signature>("cublasSgemm_v2");
return func(handle, transa, transb, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc);
}
cublasStatus_t cublasSgemmStridedBatched(cublasHandle_t handle,
cublasOperation_t transa,
cublasOperation_t transb,
int m,
int n,
int k,
const float *alpha,
const float *A,
int lda,
long long int strideA,
const float *B,
int ldb,
long long int strideB,
const float *beta,
float *C,
int ldc,
long long int strideC,
int batchCount) {
using Signature = cublasStatus_t(*)(cublasHandle_t,
cublasOperation_t,
cublasOperation_t,
int,
int,
int,
const float*,
const float*,
int,
long long int,
const float*,
int,
long long int,
const float*,
float*,
int,
long long int,
int);
static auto func = ctranslate2::load_symbol<Signature>("cublasSgemmStridedBatched");
return func(handle, transa, transb, m, n, k, alpha, A, lda, strideA, B, ldb, strideB, beta, C, ldc, strideC, batchCount);
}
cublasStatus_t cublasGemmEx(cublasHandle_t handle,
cublasOperation_t transa,
cublasOperation_t transb,
int m,
int n,
int k,
const void *alpha,
const void *A,
cudaDataType Atype,
int lda,
const void *B,
cudaDataType Btype,
int ldb,
const void *beta,
void *C,
cudaDataType Ctype,
int ldc,
cublasComputeType_t computeType,
cublasGemmAlgo_t algo) {
using Signature = cublasStatus_t(*)(cublasHandle_t,
cublasOperation_t,
cublasOperation_t,
int,
int,
int,
const void*,
const void*,
cudaDataType,
int,
const void*,
cudaDataType,
int,
const void*,
void*,
cudaDataType,
int,
cublasComputeType_t,
cublasGemmAlgo_t);
static auto func = ctranslate2::load_symbol<Signature>("cublasGemmEx");
return func(handle, transa, transb, m, n, k, alpha, A, Atype, lda, B, Btype, ldb, beta, C, Ctype, ldc, computeType, algo);
}
cublasStatus_t cublasGemmStridedBatchedEx(cublasHandle_t handle,
cublasOperation_t transa,
cublasOperation_t transb,
int m,
int n,
int k,
const void *alpha,
const void *A,
cudaDataType Atype,
int lda,
long long int strideA,
const void *B,
cudaDataType Btype,
int ldb,
long long int strideB,
const void *beta,
void *C,
cudaDataType Ctype,
int ldc,
long long int strideC,
int batchCount,
cublasComputeType_t computeType,
cublasGemmAlgo_t algo) {
using Signature = cublasStatus_t(*)(cublasHandle_t,
cublasOperation_t,
cublasOperation_t,
int,
int,
int,
const void*,
const void*,
cudaDataType,
int,
long long int,
const void*,
cudaDataType,
int,
long long int,
const void*,
void*,
cudaDataType,
int,
long long int,
int,
cublasComputeType_t,
cublasGemmAlgo_t);
static auto func = ctranslate2::load_symbol<Signature>("cublasGemmStridedBatchedEx");
return func(handle, transa, transb, m, n, k, alpha, A, Atype, lda, strideA, B, Btype, ldb, strideB, beta, C, Ctype, ldc, strideC, batchCount, computeType, algo);
}
}