This repository was archived by the owner on Nov 12, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathpreload_cublas.cpp
More file actions
455 lines (378 loc) · 16.8 KB
/
Copy pathpreload_cublas.cpp
File metadata and controls
455 lines (378 loc) · 16.8 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include <cassert>
#include <cstdio>
#include <dlfcn.h>
#include <cublas_v2.h>
#include "allocations.hpp"
#include "apis.hpp"
#include "callbacks.hpp"
#include "driver_state.hpp"
#include "preload.hpp"
#include "thread.hpp"
#include "values.hpp"
typedef cublasStatus_t (*cublasCreateFunc)(cublasHandle_t *handle);
extern "C" cublasStatus_t cublasCreate(cublasHandle_t *handle) {
V2_LD_PRELOAD_BOILERPLATE(cublasCreate);
printf("WARN: disabling CUPTI callbacks during cublasCreate call\n");
DriverState::this_thread().pause_cupti_callbacks();
const cublasStatus_t ret = real_cublasCreate(handle);
DriverState::track_cublas_handle(*handle,
DriverState::this_thread().current_device());
DriverState::this_thread().resume_cupti_callbacks();
return ret;
}
typedef cublasStatus_t (*cublasDestroyFunc)(cublasHandle_t handle);
extern "C" cublasStatus_t cublasDestroy(cublasHandle_t handle) {
V2_LD_PRELOAD_BOILERPLATE(cublasDestroy);
DriverState::this_thread().pause_cupti_callbacks();
printf("WARN: tid=%d disabling CUPTI callbacks during cublasDestroy call\n",
get_thread_id());
const cublasStatus_t ret = real_cublasDestroy(handle);
DriverState::this_thread().resume_cupti_callbacks();
return ret;
}
typedef cublasStatus_t (*cublasDgemmFunc)(
cublasHandle_t handle, cublasOperation_t transa, cublasOperation_t transb,
int m, int n, int k, const double *alpha, const double *A, int lda,
const double *B, int ldb, const double *beta, double *C, int ldc);
extern "C" cublasStatus_t
cublasDgemm(cublasHandle_t handle, cublasOperation_t transa,
cublasOperation_t transb, int m, int n, int k, const double *alpha,
const double *A, int lda, const double *B, int ldb,
const double *beta, double *C, int ldc) {
V2_LD_PRELOAD_BOILERPLATE(cublasDgemm);
// FIXME - also depends on alpha, beta
// record data, we know things about how this API works
auto &values = Values::instance();
// Find the argument values
// http://docs.nvidia.com/cuda/cublas/index.html#cublas-lt-t-gt-gemv
Values::id_type aId, bId, cId;
Values::value_type aVal, bVal, cVal;
std::tie(aId, aVal) = values.find_live_device((uintptr_t)A, 1);
std::tie(bId, bVal) = values.find_live_device((uintptr_t)B, 1);
std::tie(cId, cVal) = values.find_live_device((uintptr_t)C, 1);
assert(aId && bId && cId && "Couldn't find Dgemm argument value on device");
Values::id_type newId;
Values::value_type newVal;
std::tie(newId, newVal) = values.duplicate_value(cVal);
newVal->add_depends_on(aId);
newVal->add_depends_on(bId);
newVal->add_depends_on(cId);
DriverState::this_thread().pause_cupti_callbacks();
printf("WARN: disabling CUPTI callbacks during cublasDgemm "
"call\n");
auto api = std::make_shared<ApiRecord>(
"cublasDgemm", DriverState::device_from_cublas_handle(handle));
api->add_output(newId);
api->add_input(aId);
api->add_input(bId);
api->add_input(cId);
APIs::record(api);
const cublasStatus_t ret = real_cublasDgemm(
handle, transa, transb, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc);
DriverState::this_thread().resume_cupti_callbacks();
return ret;
}
typedef cublasStatus_t (*cublasSaxpyFunc)(
cublasHandle_t handle, int n,
const float *alpha, /* host or device pointer */
const float *x, int incx, float *y, int incy);
extern "C" cublasStatus_t
cublasSaxpy(cublasHandle_t handle, int n,
const float *alpha, /* host or device pointer */
const float *x, int incx, float *y, int incy) {
V2_LD_PRELOAD_BOILERPLATE(cublasSaxpy);
auto &values = Values::instance();
// Find input values
Values::id_type xId, yId;
Values::value_type xVal, yVal;
std::tie(xId, xVal) = values.find_live((uintptr_t)x, AddressSpace::Cuda());
std::tie(yId, yVal) = values.find_live((uintptr_t)y, AddressSpace::Cuda());
assert(xId && "Couldn't find cublasSaxpy x value on device");
// Create output value
Values::id_type outId;
Values::value_type outVal;
std::tie(outId, outVal) = values.duplicate_value(yVal);
outVal->add_depends_on(xId);
outVal->add_depends_on(yId);
// track api
auto api = std::make_shared<ApiRecord>(
"cublasSaxpy", DriverState::device_from_cublas_handle(handle));
api->add_output(outId);
api->add_input(xId);
api->add_input(yId);
APIs::record(api);
// Do the actual call
printf("WARN: disabling CUPTI callbacks during cublasSaxpy call\n");
DriverState::this_thread().pause_cupti_callbacks();
const cublasStatus_t ret =
real_cublasSaxpy(handle, n, alpha, x, incx, y, incy);
DriverState::this_thread().resume_cupti_callbacks();
return ret;
}
typedef cublasStatus_t (*cublasSgemmFunc)(
cublasHandle_t handle, cublasOperation_t transa, cublasOperation_t transb,
int m, int n, int k, const float *alpha, /* host or device pointer */
const float *A, int lda, const float *B, int ldb,
const float *beta, /* host or device pointer */
float *C, int ldc);
extern "C" cublasStatus_t
cublasSgemm(cublasHandle_t handle, cublasOperation_t transa,
cublasOperation_t transb, int m, int n, int k,
const float *alpha, /* host or device pointer */
const float *A, int lda, const float *B, int ldb,
const float *beta, /* host or device pointer */
float *C, int ldc) {
V2_LD_PRELOAD_BOILERPLATE(cublasSgemm);
// FIXME - also depends on alpha, beta
// record data, we know things about how this API works
auto &values = Values::instance();
// Find the argument values
Values::id_type aId, bId, cId;
Values::value_type aVal, bVal, cVal;
std::tie(aId, aVal) = values.find_live_device((uintptr_t)A, 1);
std::tie(bId, bVal) = values.find_live_device((uintptr_t)B, 1);
std::tie(cId, cVal) = values.find_live_device((uintptr_t)C, 1);
assert(aId && bId && cId && "Couldn't find Dgemm argument value on device");
Values::id_type newId;
Values::value_type newVal;
std::tie(newId, newVal) = values.duplicate_value(cVal);
newVal->add_depends_on(aId);
newVal->add_depends_on(bId);
newVal->add_depends_on(cId);
DriverState::this_thread().pause_cupti_callbacks();
printf("WARN: disabling CUPTI callbacks during cublasSgemm "
"call\n");
const cublasStatus_t ret = real_cublasSgemm(
handle, transa, transb, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc);
DriverState::this_thread().resume_cupti_callbacks();
auto api = std::make_shared<ApiRecord>(
"cublasSgemm", DriverState::device_from_cublas_handle(handle));
api->add_output(newId);
api->add_input(aId);
api->add_input(bId);
api->add_input(cId);
APIs::record(api);
return ret;
}
typedef cublasStatus_t (*cublasDgemvFunc)(cublasHandle_t, cublasOperation_t,
int, int, const double *,
const double *, int, const double *,
int, const double *, double *, int);
extern "C" cublasStatus_t cublasDgemv(cublasHandle_t handle,
cublasOperation_t trans, int m, int n,
const double *alpha, const double *A,
int lda, const double *x, int incx,
const double *beta, double *y, int incy) {
V2_LD_PRELOAD_BOILERPLATE(cublasDgemv);
// record data, we know things about how this API works
auto &values = Values::instance();
// Find the argument values
// http://docs.nvidia.com/cuda/cublas/index.html#cublas-lt-t-gt-gemv
Values::id_type aKey, xKey, yKey;
Values::value_type aVal, xVal, yVal;
std::tie(aKey, aVal) = values.find_live_device((uintptr_t)A, 1);
std::tie(xKey, xVal) = values.find_live_device((uintptr_t)x, 1);
std::tie(yKey, yVal) = values.find_live_device((uintptr_t)y, 1);
assert(aKey && xKey && yKey &&
"Couldn't find Dgemv argument value on device");
// FIXME: could use these to do better on dependences
printf("WARN: not handling some values (A, alpha, beta)\n");
Values::id_type newId;
Values::value_type newVal;
std::tie(newId, newVal) = values.duplicate_value(yVal);
newVal->add_depends_on(xKey);
newVal->add_depends_on(yKey);
DriverState::this_thread().pause_cupti_callbacks();
printf("WARN: disabling CUPTI callbacks during cublasDgemv "
"call\n");
const cublasStatus_t ret = real_cublasDgemv(handle, trans, m, n, alpha, A,
lda, x, incx, beta, y, incy);
DriverState::this_thread().resume_cupti_callbacks();
auto api = std::make_shared<ApiRecord>(
"cublasDgemv", DriverState::device_from_cublas_handle(handle));
api->add_output(newId);
api->add_input(aKey);
api->add_input(xKey);
api->add_input(yKey);
APIs::record(api);
return ret;
}
typedef cublasStatus_t (*cublasSgemvFunc)(cublasHandle_t handle,
cublasOperation_t trans, int m, int n,
const float *alpha, const float *A,
int lda, const float *x, int incx,
const float *beta, float *y,
int incy);
extern "C" cublasStatus_t cublasSgemv(cublasHandle_t handle,
cublasOperation_t trans, int m, int n,
const float *alpha, const float *A,
int lda, const float *x, int incx,
const float *beta, float *y, int incy) {
V2_LD_PRELOAD_BOILERPLATE(cublasSgemv);
// record data, we know things about how this API works
auto &values = Values::instance();
// Find the argument values
// http://docs.nvidia.com/cuda/cublas/index.html#cublas-lt-t-gt-gemv
Values::id_type aKey, xKey, yKey;
Values::value_type aVal, xVal, yVal;
std::tie(aKey, aVal) = values.find_live_device((uintptr_t)A, 1);
std::tie(xKey, xVal) = values.find_live_device((uintptr_t)x, 1);
std::tie(yKey, yVal) = values.find_live_device((uintptr_t)y, 1);
assert(aKey && xKey && yKey &&
"Couldn't find cublasSgemv argument value on device");
// FIXME: could use these to do better on dependences
printf("WARN: not handling some values (A, alpha, beta)\n");
Values::id_type newId;
Values::value_type newVal;
std::tie(newId, newVal) = values.duplicate_value(yVal);
newVal->add_depends_on(xKey);
newVal->add_depends_on(yKey);
DriverState::this_thread().pause_cupti_callbacks();
printf("WARN: disabling CUPTI callbacks during cublasSgemv "
"call\n");
const cublasStatus_t ret = real_cublasSgemv(handle, trans, m, n, alpha, A,
lda, x, incx, beta, y, incy);
DriverState::this_thread().resume_cupti_callbacks();
auto api = std::make_shared<ApiRecord>(
"cublasSgemv", DriverState::device_from_cublas_handle(handle));
api->add_output(newId);
api->add_input(aKey);
api->add_input(xKey);
api->add_input(yKey);
APIs::record(api);
return ret;
}
typedef cublasStatus_t (*cublasSasumFunc)(cublasHandle_t, int, const float *,
int, float *);
extern "C" cublasStatus_t cublasSasum(cublasHandle_t handle, int n,
const float *x, int incx, float *result) {
V2_LD_PRELOAD_BOILERPLATE(cublasSasum);
// record data, we know things about how this API works
auto &values = Values::instance();
auto &allocations = Allocations::instance();
// Find the argument values
// http://docs.nvidia.com/cuda/cublas/index.html#cublas-lt-t-gt-gemv
Values::id_type xId;
std::tie(xId, std::ignore) =
values.find_live((uintptr_t)x, AddressSpace::Cuda());
assert(xId && "Couldn't find Sasum x argument value on device");
// see if we can find an allocation for the result
Allocations::id_type rAllocId;
std::tie(rAllocId, std::ignore) = allocations.find_live(
(uintptr_t)result, sizeof(float), AddressSpace::Cuda());
if (!rAllocId) {
// FIXME - can we do a better job with some parameters here
Memory AM(Memory::Unknown);
std::tie(rAllocId, std::ignore) = allocations.new_allocation(
(uintptr_t)result, sizeof(float), AddressSpace::Cuda(), AM,
AllocationRecord::PageType::Unknown);
printf("WARN: new allocId=%lu for result=%lu\n", rAllocId,
(uintptr_t)result);
}
assert(rAllocId && "If there is no allocation, we need to make one");
// Make a new value
Values::id_type rId;
Values::value_type rVal;
std::tie(rId, rVal) =
values.new_value((uintptr_t)result, sizeof(float), rAllocId);
rVal->add_depends_on(xId);
auto api = std::make_shared<ApiRecord>(
"cublasSasum", DriverState::device_from_cublas_handle(handle));
api->add_output(rId);
api->add_input(xId);
APIs::record(api);
DriverState::this_thread().pause_cupti_callbacks();
printf("WARN: tid=%d disabling CUPTI callbacks during cublasSasum call\n",
get_thread_id());
const cublasStatus_t ret = real_cublasSasum(handle, n, x, incx, result);
DriverState::this_thread().resume_cupti_callbacks();
return ret;
}
typedef cublasStatus_t (*cublasSscalFunc)(
cublasHandle_t handle, int n,
const float *alpha, /* host or device pointer */
float *x, int incx);
extern "C" cublasStatus_t
cublasSscal(cublasHandle_t handle, int n,
const float *alpha, /* host or device pointer */
float *x, int incx) {
V2_LD_PRELOAD_BOILERPLATE(cublasSscal);
auto &values = Values::instance();
// Find input values
Values::id_type xId, outId;
Values::value_type xVal, outVal;
std::tie(xId, xVal) = values.find_live((uintptr_t)x, AddressSpace::Cuda());
assert(xId && "Couldn't find cublasSscal x value on device");
// Create output value
std::tie(outId, outVal) = values.duplicate_value(xVal);
outVal->add_depends_on(xId);
// track api
auto api = std::make_shared<ApiRecord>(
"cublasSscal", DriverState::device_from_cublas_handle(handle));
api->add_output(outId);
api->add_input(xId);
APIs::record(api);
// Do the actual call
printf("WARN: disabling CUPTI callbacks during cublasSscal call\n");
DriverState::this_thread().pause_cupti_callbacks();
const cublasStatus_t ret = real_cublasSscal(handle, n, alpha, x, incx);
DriverState::this_thread().resume_cupti_callbacks();
return ret;
}
typedef cublasStatus_t (*cublasSdotFunc)(cublasHandle_t handle, int n,
const float *x, int incx,
const float *y, int incy,
float *result);
extern "C" cublasStatus_t cublasSdot(cublasHandle_t handle, int n,
const float *x, int incx, const float *y,
int incy, float *result) {
V2_LD_PRELOAD_BOILERPLATE(cublasSdot);
// record data, we know things about how this API works
auto &values = Values::instance();
auto &allocations = Allocations::instance();
// Find the argument values
// http://docs.nvidia.com/cuda/cublas/index.html#cublas-lt-t-gt-gemv
Values::id_type xId, yId;
printf("Looking for x=%lu\n", (uintptr_t)x);
std::tie(xId, std::ignore) =
values.find_live((uintptr_t)x, AddressSpace::Cuda());
assert(xId && "Couldn't find cublasSdot x argument value on device");
std::tie(yId, std::ignore) =
values.find_live((uintptr_t)y, AddressSpace::Cuda());
assert(yId && "Couldn't find cublasSdot y argument value on device");
// see if we can find an allocation for the result
printf("Looking for allocation result=%lu\n", (uintptr_t)result);
Allocations::id_type rAllocId;
std::tie(rAllocId, std::ignore) = allocations.find_live(
(uintptr_t)result, sizeof(float), AddressSpace::Cuda());
if (rAllocId == Allocations::noid) {
printf("WARN: creating implicit allocation for cublasSdot result\n");
Memory AM = Memory(Memory::Unknown);
auto pair = allocations.insert(std::shared_ptr<AllocationRecord>(
new AllocationRecord((uintptr_t)result, sizeof(float),
AddressSpace::Cuda(), AM,
AllocationRecord::PageType::Unknown)));
assert(pair.second);
rAllocId = pair.first->first;
}
printf("result allocId=%lu\n", rAllocId);
// Make a new value
Values::id_type rId;
Values::value_type rVal;
std::tie(rId, rVal) =
values.new_value((uintptr_t)result, sizeof(float), rAllocId);
rVal->add_depends_on(xId);
rVal->add_depends_on(yId);
auto api = std::make_shared<ApiRecord>(
"cublasSdot", DriverState::device_from_cublas_handle(handle));
api->add_output(rId);
api->add_input(xId);
api->add_input(yId);
APIs::record(api);
DriverState::this_thread().pause_cupti_callbacks();
printf("WARN: disabling CUPTI callbacks during cublasSdot call\n");
const cublasStatus_t ret =
real_cublasSdot(handle, n, x, incx, y, incy, result);
DriverState::this_thread().resume_cupti_callbacks();
return ret;
}