-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstr-cuda.c
395 lines (304 loc) · 11.2 KB
/
instr-cuda.c
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
#include <stdio.h>
#include <stdlib.h>
#include <cuda_runtime.h>
#include "ldChecker.h"
cudaError_t (*real_cudaGetDeviceCount)(int *count);
cudaError_t (*real_cudaMalloc) (void **devPtr, size_t size);
cudaError_t (*real_cudaMemcpy) (void *dst, const void *src,
size_t count, enum cudaMemcpyKind kind);
cudaError_t (*real_cudaMemcpyToSymbol)(const void *symbol, const void *src,
size_t count, size_t offset,
enum cudaMemcpyKind kind );
cudaError_t (*real_cudaMemcpyAsync)(void *dst, const void * src, size_t count,
enum cudaMemcpyKind kind, cudaStream_t stream);
cudaError_t (*real_cudaMemset) (void *devPtr, int value, size_t count);
cudaError_t (*real_cudaConfigureCall) (dim3 gridDim, dim3 blockDim,
size_t sharedMem, cudaStream_t stream);
cudaError_t (*real_cudaSetupArgument) (const void *arg, size_t size, size_t offset);
cudaError_t (*real_cudaLaunch) (const void *entry);
cudaError_t (*real_cudaGetSymbolAddress) (void **devPtr, const void *symbol);
cudaError_t (*real_cudaFree) (void *devPtr);
cudaError_t (*real_cudaStreamCreate) (cudaStream_t *stream);
cudaError_t (*real_cudaStreamSynchronize) (cudaStream_t stream);
cudaError_t (*real_cudaStreamDestroy) (cudaStream_t stream);
struct ld_bindings_s cuda_bindings[] = {
{"cudaGetDeviceCount", (void **) &real_cudaGetDeviceCount},
{"cudaMalloc", (void **) &real_cudaMalloc},
{"cudaMemcpy", (void **) &real_cudaMemcpy},
{"cudaMemcpyToSymbol", (void **) &real_cudaMemcpyToSymbol},
{"cudaMemcpyAsync", (void **) &real_cudaMemcpyAsync},
{"cudaMemset", (void **) &real_cudaMemset},
{"cudaConfigureCall", (void **) &real_cudaConfigureCall},
{"cudaSetupArgument", (void **) &real_cudaSetupArgument},
{"cudaLaunch", (void **) &real_cudaLaunch},
{"cudaGetSymbolAddress", (void **) &real_cudaGetSymbolAddress},
{"cudaFree", (void **) &real_cudaFree},
{"cudaStreamCreate", (void **) &real_cudaStreamCreate},
{"cudaStreamSynchronize", (void **) &real_cudaStreamSynchronize},
{"cudaStreamDestroy", (void **) &real_cudaStreamDestroy},
{NULL, NULL}
};
#include "cuda_helper.h"
static struct kernel_lookup_s *cuda_lookup_table;
int cuda_getBufferContent (struct ld_mem_s *ldBuffer, void *buffer,
size_t offset, size_t size);
static struct work_size_s *configure_get_worksizes(dim3 *gridDim, dim3 *blockDim);
CREATE_HASHMAP(mem, void*, 200)
CREATE_HASHMAP(kernel, void*, 100)
struct ld_mem_s *create_cuda_buffer(const void *devPtr, size_t size);
static void init_cuda_ldchecker(void) {
static int inited = 0;
struct callback_s callbacks = {cuda_getBufferContent};
int i, j;
if (inited) {
return;
}
init_ldchecker(callbacks, cuda_bindings);
cuda_init_helper();
inited = 1;
cuda_lookup_table = cuda_get_lookup_table();
for (i = 0; cuda_lookup_table[i].address != NULL; i++) {
struct kernel_lookup_s *kernel = &cuda_lookup_table[i];
struct ld_kernel_s *ldKernel = get_next_kernel_spot();
ldKernel->handle = kernel;
ldKernel->name = kernel->name;
ldKernel->nb_params = kernel->nb_params;
ldKernel->params = malloc(sizeof(struct ld_kern_param_s) * ldKernel->nb_params);
for (j = 0; j < ldKernel->nb_params; j++) {
ldKernel->params[j].name = kernel->params[j].name;
ldKernel->params[j].type = kernel->params[j].type;
ldKernel->params[i].index = j;
}
kernel_created_event(ldKernel);
}
create_cuda_buffer(NULL, 0);
}
#define ELT_SIZE 4
struct ld_mem_s *find_off_mem_entry(const void *handle, size_t *offset) {
struct ld_mem_s *ldBuffer = find_mem_entry((void *) handle);
int i;
if (ldBuffer) {
return ldBuffer;
}
*offset = -1;
for (i = 0; i < mem_elt_count; i++) {
struct ld_mem_s *current = &mem_map[i];
size_t distance = ((float *) handle - (float *) current->handle) * ELT_SIZE;
if (distance > 0 && distance < current->size) {
if (*offset == -1 || distance < *offset) {
if (*offset != -1) {
warning("find_off_mem_entry found multiple matching buffers "
" ptr=%p, found=%p+%zu\n", handle, ldBuffer->handle,
*offset);
}
*offset = distance;
ldBuffer = current;
}
}
}
return ldBuffer;
}
cudaError_t cudaGetDeviceCount (int *count) {
init_cuda_ldchecker();
return real_cudaGetDeviceCount (count);
}
int cuda_getBufferContent (struct ld_mem_s *ldBuffer, void *buffer,
size_t offset, size_t size)
{
cudaError_t err = cudaSuccess;
if (offset != 0) {
/* need to use the type to multiply correctly the buffer handle. */
warning("offset == %zu != 0, not handled yet in Cuda's ldChecker\n",
offset);
}
err = real_cudaMemcpy (buffer,
ldBuffer->handle,
size,
cudaMemcpyDeviceToHost);
return err == cudaSuccess;
}
static struct {
const void *arg;
size_t size;
size_t offset;
} temp_arguments[90];
static int temp_argument_cnt = 0;
cudaError_t cudaLaunch (const void *entry) {
struct ld_kernel_s *ldKernel;
struct kernel_lookup_s *kernel;
cudaError_t err;
int i;
for (i = 0; cuda_lookup_table[i].address != NULL; i++) {
if (cuda_lookup_table[i].address == entry) {
break;
}
}
if (!cuda_lookup_table[i].address) {
warning("Couldn't find kernel @%p\n", entry);
goto error;
}
kernel = &cuda_lookup_table[i];
ldKernel = find_kernel_entry(kernel);
assert(ldKernel);
dbg_notify_event();
for (i = 0; i < ldKernel->nb_params; i++) {
struct ld_kern_param_s *ldParam = &ldKernel->params[i];
const void **current_arg = (const void **) temp_arguments[i].arg;
if (ldParam->is_pointer) {
size_t offset = 0;
struct ld_mem_s *ldBuffer = find_off_mem_entry(*current_arg, &offset);
/* better handling of invalid buffers ?*/
if (!ldBuffer) {
error("in kernel %s, arg #%d, invalid bufer %s@%p", ldKernel->name, i, ldParam->name, *current_arg);
}
assert(ldBuffer);
kernel_set_buffer_arg_event (ldKernel, ldParam, i, ldBuffer, offset);
} else {
kernel_set_scalar_arg_event (ldKernel, ldParam, i, current_arg);
}
}
kernel_executed_event(ldKernel, configure_get_worksizes(NULL, NULL), 3);
err = real_cudaLaunch(entry);
kernel_finished_event(ldKernel, configure_get_worksizes(NULL, NULL), 3);
return err;
error:
return real_cudaLaunch(entry);
}
cudaError_t cudaSetupArgument (const void *arg, size_t size, size_t offset) {
temp_arguments[temp_argument_cnt].arg = arg;
temp_arguments[temp_argument_cnt].size = size;
temp_arguments[temp_argument_cnt].offset = offset;
temp_argument_cnt++;
return real_cudaSetupArgument(arg, size, offset);
}
struct ld_mem_s *create_cuda_buffer(const void *devPtr, size_t size) {
static int uid = 0;
struct ld_mem_s *ldBuffer = get_next_mem_spot();
assert(ldBuffer);
ldBuffer->handle = (void *) devPtr;
ldBuffer->uid = uid++;
ldBuffer->size = size;
ldBuffer->flags = 0;
ldBuffer->has_values = 0;
ldBuffer->values_outdated = 0;
buffer_created_event(ldBuffer);
return ldBuffer;
}
cudaError_t cudaStreamCreate (cudaStream_t *stream) {
return real_cudaStreamCreate(stream);
}
cudaError_t cudaStreamSynchronize (cudaStream_t stream) {
return real_cudaStreamSynchronize(stream);
}
cudaError_t cudaStreamDestroy (cudaStream_t stream) {
return real_cudaStreamDestroy(stream);
}
cudaError_t cudaMalloc (void **devPtr, size_t size) {
cudaError_t retcode;
retcode = real_cudaMalloc(devPtr, size);
create_cuda_buffer(*devPtr, size);
return retcode;
}
cudaError_t cudaMemcpyToSymbol(const void *symbol, const void *src, size_t count, size_t offset , enum cudaMemcpyKind kind ) {
if (kind == cudaMemcpyHostToDevice) {
struct ld_mem_s *ldBuffer;
void *devPtr;
cudaError_t err = real_cudaGetSymbolAddress(&devPtr, symbol);
if (err != cudaSuccess) {
warning("cudaMemcpyToSymbol failed ...\n");
goto finish;
}
ldBuffer = find_mem_entry(devPtr);
if (!ldBuffer) {
ldBuffer = create_cuda_buffer(devPtr, count + offset);
}
buffer_copy_event(ldBuffer, LD_WRITE, (void **) src, count, offset);
} else {
warning("cudaMemcpyToSymbol kind=%d not handled\n", kind);
}
finish:
return real_cudaMemcpyToSymbol(symbol, src, count, offset, kind);
}
cudaError_t cudaGetSymbolAddress (void **devPtr, const void *symbol) {
cudaError_t err;
err = real_cudaGetSymbolAddress(devPtr, symbol);
return err;
}
cudaError_t cudaMemset ( void *devPtr, int value, size_t count) {
cudaError_t retcode = real_cudaMemset(devPtr, value, count);
size_t offset;
struct ld_mem_s *ldBuffer = find_off_mem_entry(devPtr, &offset);
warning("cudaMemset %d (unhandled)\n", ldBuffer->uid);
return retcode;
}
void handle_cudaMemcpy(void *dst, const void * src, size_t count,
enum cudaMemcpyKind kind, cudaStream_t stream)
{
struct ld_mem_s *ldBuffer;
void *pointer;
int direction;
size_t offset = 0;
if (kind == cudaMemcpyHostToDevice) /* write */ {
ldBuffer = find_off_mem_entry(dst, &offset);
assert(ldBuffer);
pointer = (void *) src;
direction = LD_WRITE;
} else if (kind == cudaMemcpyDeviceToHost) /* read */ {
ldBuffer = find_off_mem_entry((void *) src, &offset);
assert(ldBuffer);
pointer = dst;
direction = LD_READ;
} else {
warning("Unhandled memcopy\n");
return;
}
if (!ldBuffer) {
warning("Coundln't find buffer structure, maybe it's offsetted, "\
"and it's time to handle it ?\n");
return;
}
buffer_copy_event(ldBuffer, direction, (void **) pointer, count, offset);
}
cudaError_t cudaMemcpyAsync(void *dst, const void * src, size_t count,
enum cudaMemcpyKind kind, cudaStream_t stream)
{
cudaError_t retcode = real_cudaMemcpyAsync(dst, src, count, kind, stream);
handle_cudaMemcpy(dst, src, count, kind, stream);
return retcode;
}
cudaError_t cudaMemcpy (void *dst, const void *src,
size_t count, enum cudaMemcpyKind kind)
{
cudaError_t retcode = real_cudaMemcpy(dst, src, count, kind);
handle_cudaMemcpy(dst, src, count, kind, (void *) -1);
return retcode;
}
cudaError_t cudaFree (void *devPtr) {
struct ld_mem_s *ldBuffer = find_mem_entry(devPtr);
buffer_released(ldBuffer);
return real_cudaFree(devPtr);
}
static struct work_size_s *configure_get_worksizes(dim3 *gridDim, dim3 *blockDim) {
static struct work_size_s work_sizes;
if (gridDim != NULL) {
work_sizes.global[0] = gridDim->x;
work_sizes.global[1] = gridDim->y;
work_sizes.global[2] = gridDim->z;
}
if (blockDim != NULL) {
work_sizes.local[0] = blockDim->x;
work_sizes.local[1] = blockDim->y;
work_sizes.local[2] = blockDim->z;
}
return &work_sizes;
}
cudaError_t cudaConfigureCall (dim3 gridDim, dim3 blockDim,
size_t sharedMem, cudaStream_t stream)
{
cudaError_t errcode;
configure_get_worksizes(&gridDim, &blockDim);
temp_argument_cnt = 0;
errcode = real_cudaConfigureCall(gridDim, blockDim, sharedMem, stream);
return errcode;
//return 1 ? cudaSuccess : errcode;
}