-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmm-utils.cuh
842 lines (718 loc) · 24.7 KB
/
pmm-utils.cuh
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
#ifndef _pmm_utils_cuh
#define _pmm_utils_cuh
#include "device/Ouroboros_impl.cuh"
#include "device/MemoryInitialization.cuh"
#include "InstanceDefinitions.cuh"
#include "PerformanceMeasure.cuh"
#include "Utility.h"
#include "cuda.h"
#include "cuda_profiler_api.h"
#include <algorithm>
#include <iostream>
#include <string>
#include <cassert>
#include "pmm-utils.h"
using namespace std;
//#define DEBUG
#ifdef DEBUG
#define debug(a...) printf(a)
#else
#define debug(a...)
#endif
#ifdef OUROBOROS__
//Ouroboros initialization
#define MemoryManagerType OuroPQ
#else
#define MemoryManagerType void*
#endif
#define EMPTY 0
#define DONE 2
#define MALLOC 3
#define FREE 5
#define GC 7
#define CB 11
#define MPS 0
#define MPS_mono 1
#define simple_mono 2
#define one_per_warp 3
#define one_per_block 4
#define async_request 5
#define async_one_per_warp 6
#define async_one_per_block 7
#define callback_type 8
#define GUARD_CU(cuda_call) \
{ \
if (cuda_call != (enum cudaError) CUDA_SUCCESS){ \
printf("--- ERROR(%d:%s) --- %s:%d\n", cuda_call, cudaGetErrorString(cuda_call), __FILE__, __LINE__);\
} \
}\
#define GUARD_CU_DEV(cuda_call) \
{ \
if (cuda_call != (enum cudaError) CUDA_SUCCESS){ \
printf("--- ERROR(%d) --- %s:%d\n", cuda_call, __FILE__, __LINE__);\
} \
}\
#define CB_MAX 100
//TODO:
//New data structure for queue to link all allocated memory pointers to one within a warp.
enum request_type {
request_empty = EMPTY,
request_done = DONE,
request_malloc = MALLOC,
request_free = FREE,
request_gc = GC
};
cudaError_t GRError(cudaError_t error, const char *message,
const char *filename, int line, bool print) {
if (error && print) {
int gpu;
cudaGetDevice(&gpu);
fprintf(stderr, "[%s, %d @ gpu %d] %s (CUDA error %d: %s)\n", filename,
line, gpu, message, error, cudaGetErrorString(error));
fflush(stderr);
}
return error;
}
__device__ void acquire_semaphore(volatile int* lock, int i){
while (atomicCAS((int*)&lock[i], 0, 1) != 0){
}
__threadfence();
}
__device__ void release_semaphore(volatile int* lock, int i){
__threadfence();
lock[i] = 0;
}
__forceinline__ __device__ unsigned lane_id()
{
unsigned ret;
asm volatile ("mov.u32 %0, %laneid;" : "=r"(ret));
return ret;
}
__forceinline__ __device__ unsigned warp_id()
{
// this is not equal to threadIdx.x / 32
unsigned ret;
asm volatile ("mov.u32 %0, %warpid;" : "=r"(ret));
return ret;
}
__forceinline__ __device__ unsigned sm_id()
{
unsigned ret;
asm volatile ("mov.u32 %0, %smid;" : "=r"(ret));
return ret;
}
struct Runtime;
struct Future;
struct Service;
struct RequestType{
volatile size_t* requests_number;
volatile int* request_counter;
volatile int* request_signal;
volatile int* request_id;
volatile int* request_mem_size;
volatile int* lock;
volatile int** d_memory;
volatile int** request_dest;
void init(size_t Size);
void memset(size_t Size);
void free();
__forceinline__ __device__ size_t number(){ return requests_number[0]; }
__forceinline__ __device__ int type(int id){ return request_signal[id]; }
};
void RequestType::init(size_t Size){
GUARD_CU(cudaMalloc((void**)&requests_number, sizeof(volatile size_t)));
GUARD_CU(cudaMalloc((void**)&request_counter, sizeof(volatile int)));
GUARD_CU(cudaMalloc((void**)&request_signal, Size * sizeof(volatile int)));
GUARD_CU(cudaMalloc((void**)&request_id, Size * sizeof(volatile int)));
GUARD_CU(cudaMalloc((void**)&request_mem_size, Size * sizeof(volatile int)));
GUARD_CU(cudaMalloc((void**)&lock, Size * sizeof(volatile int)));
GUARD_CU(cudaMalloc((void**)&d_memory, Size * sizeof(volatile int*)));
GUARD_CU(cudaMalloc((void**)&request_dest, Size * sizeof(volatile int*)));
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
}
void RequestType::memset(size_t Size){
int zero = 0;
int* tab_zero = (int*)std::malloc(Size * sizeof(int));
volatile int** null_tab = (volatile int**)std::malloc(Size * sizeof(volatile int*));
for (int i=0; i<Size; ++i){
tab_zero[i] = 0;
null_tab[i] = NULL;
}
GUARD_CU(cudaMemcpy((void*)d_memory, null_tab, Size * sizeof(volatile int*), cudaMemcpyHostToDevice));
GUARD_CU(cudaMemcpy((void*)request_dest, null_tab, Size * sizeof(volatile int*), cudaMemcpyHostToDevice));
GUARD_CU(cudaMemcpy((void*)requests_number, &Size, sizeof(size_t), cudaMemcpyHostToDevice));
GUARD_CU(cudaMemcpy((void*)request_counter, &zero, sizeof(int), cudaMemcpyHostToDevice));
GUARD_CU(cudaMemcpy((void*)request_signal, tab_zero, Size * sizeof(int), cudaMemcpyHostToDevice));
GUARD_CU(cudaMemcpy((void*)request_mem_size, tab_zero, Size * sizeof(int), cudaMemcpyHostToDevice));
GUARD_CU(cudaMemcpy((void*)lock, tab_zero, Size * sizeof(int), cudaMemcpyHostToDevice));
for (int i=0; i<Size; ++i) tab_zero[i] = -1;
GUARD_CU(cudaMemcpy((void*)request_id, tab_zero, Size * sizeof(int), cudaMemcpyHostToDevice));
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
delete [] tab_zero;
delete [] null_tab;
}
void RequestType::free(){
GUARD_CU(cudaFree((void*)requests_number));
GUARD_CU(cudaFree((void*)request_counter));
GUARD_CU(cudaFree((void*)request_signal));
GUARD_CU(cudaFree((void*)request_id));
GUARD_CU(cudaFree((void*)request_mem_size));
GUARD_CU(cudaFree((void*)lock));
GUARD_CU(cudaFree((void*)d_memory));
GUARD_CU(cudaFree((void*)request_dest));
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
}
struct Service{
volatile int* started;
//HOST
void init(CUdevice device);
void free();
__forceinline__ int is_running(){ return started[0]; }
//DEVICE
__forceinline__ __host__ __device__ void start(){ *started = 1; }
};
void Service::free(){
GUARD_CU(cudaFree((void*)started));
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
}
void Service::init(CUdevice device){
GUARD_CU(cudaMallocManaged(&started, sizeof(uint32_t)));
GUARD_CU(cudaPeekAtLastError());
GUARD_CU(cudaDeviceSynchronize());
*started = 0;
GUARD_CU((cudaError_t)cudaMemPrefetchAsync((int*)started, sizeof(int), device, NULL));
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
}
typedef void(*Callback_fn)(int*);
struct Callback{
volatile Callback_fn* ptr;
void init(int cb_size){
GUARD_CU(cudaMallocManaged(&ptr, cb_size*sizeof(Callback_fn)));
}
void free(){
GUARD_CU(cudaFree((void*)ptr));
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
}
};
struct Runtime{
int app_threads_num;
int callbacks_index;
volatile int* exit_signal;
volatile int* exit_counter;
//Services
Service* mm;
Service* gc;
Service* cb;
//Callbacks
Callback callbacks;
volatile int* request_callbacks;
RequestType* requests;
MemoryManagerType* mem_manager;
//HOST
void init(size_t Size, CUdevice device, MemoryManagerType&, int);
void free();
__host__
void register_cb(Callback_fn cb_fn){
callbacks.ptr[callbacks_index] = cb_fn;
++callbacks_index;
debug("cb %d registered\n", callbacks_index);
}
__forceinline__ void stop(){
*exit_signal = 1;
}
__forceinline__ __host__ __device__
bool there_is_a_callback(int i){
return (request_callbacks[i] >= 0);
}
__forceinline__ int callback_id(int i){
return request_callbacks[i];
}
__forceinline__ Callback_fn callback_run(int i){
assert(i < callbacks_index);
assert(callbacks.ptr);
return callbacks.ptr[i];
}
__forceinline__ void callback_close(int i){
request_callbacks[i] = -1;
}
//DEVICE
__forceinline__ __device__ size_t size(){
return requests->requests_number[0];
}
__forceinline__ __device__ __host__ int is_working(){
return (! exit_signal[0]);
}
__forceinline__ __device__ int is_available(int thid){
return (requests->request_signal[thid] == request_empty);
}
__forceinline__ __device__ int cb_is_finished(int thid){
return (request_callbacks[thid] == -1);
}
__forceinline__ __device__ int is_finished(int thid){
return (requests->request_signal[thid] == request_done);
}
__forceinline__ __device__ int type(int thid){
return (requests->request_signal[thid]);
}
//TODO: put it inside the MM Service
__device__ void malloc(volatile int**, size_t);
__device__ void malloc_warp(volatile int**, volatile int**, size_t);
__device__ void malloc_block(volatile int**, volatile int**, size_t);
__device__ void malloc_warp_async(Future&, size_t);
__device__ void malloc_block_async(Future&, size_t);
__device__ void malloc_async(volatile int**, size_t);
__device__ void malloc_async(Future&, size_t);
__device__ void free(volatile int*);
__device__ void free_warp(volatile int*);
__device__ void free_block(volatile int*);
__device__ void free_async(volatile int**);
__device__ void free_async(Future&);
__device__ void free_warp_async(Future&);
__device__ void free_block_async(Future&);
// DONE
__device__ void callback(volatile int**, int lambda);
__device__ void callback_async(Future& ptr, int lambda);
__device__ void request_cb(request_type, volatile int**, int lambda);
__device__ void post_request_cb(request_type, int lambda);
//TODO: put it to the intra-communicator
__device__ void request(request_type, volatile int**, int);
__device__ void request_async(request_type, volatile int**, int);
__device__ void post_request(request_type, int);
__device__ void request_processed(request_type, volatile int**);
__device__ void _request_processing(int);
__device__ void wait(request_type, int, volatile int** new_ptr);
__device__ void cb_wait(request_type, int, volatile int** new_ptr);
};
struct Future{
volatile int* ptr;
int thid;
Runtime* runtime;
request_type type;
__device__ volatile int* get();
__device__ volatile int* cb_get();
__device__ volatile int* get_warp_async(size_t);
__device__ volatile int* get_block_async(size_t);
};
__device__
volatile int* Future::cb_get(){
//printf("cb_wait for %d thread %d\n", type, thid);
runtime->cb_wait(type, thid, &ptr);
__threadfence();
return ptr;
}
__device__
volatile int* Future::get(){
runtime->wait(type, thid, &ptr);
__threadfence();
return ptr;
}
__device__
volatile int* Future::get_warp_async(size_t size){
int lane_id = threadIdx.x%32;
int offset = lane_id * size;
if (lane_id == 0){
runtime->wait(type, thid, &ptr);
}
__threadfence();
__syncthreads();
return (volatile int*)(((volatile char*)ptr) + offset);
}
__device__
volatile int* Future::get_block_async(size_t size){
int offset = threadIdx.x * size;
if (threadIdx.x == 0){
runtime->wait(type, thid, &ptr);
}
__threadfence();
__syncthreads();
return (volatile int*)(((volatile char*)ptr) + offset);
}
__device__
void Runtime::cb_wait(request_type type, int thid, volatile int** new_ptr){
while (is_working()){
if (cb_is_finished(thid)){
break;
}
}
}
__device__
void Runtime::wait(request_type type, int thid, volatile int** new_ptr){
// wait for request to be completed
while (is_working()){
if (is_finished(thid)){
request_processed(type, new_ptr);
break;
}
}
__threadfence();
assert(*new_ptr);
}
void Runtime::init(size_t APP_threads_number, CUdevice device, MemoryManagerType& memory_manager, int cb_num){
app_threads_num = APP_threads_number;
GUARD_CU(cudaMallocManaged(&exit_signal, sizeof(int32_t)));
GUARD_CU(cudaMallocManaged(&exit_counter, sizeof(uint32_t)));
GUARD_CU(cudaMallocManaged(&mm, sizeof(Service)));
GUARD_CU(cudaMallocManaged(&gc, sizeof(Service)));
GUARD_CU(cudaMallocManaged(&cb, sizeof(Service)));
GUARD_CU(cudaMallocManaged(&requests, sizeof(RequestType)));
GUARD_CU(cudaMallocManaged((void**)&request_callbacks, app_threads_num * sizeof(int)));
for (int i=0; i<app_threads_num; ++i) request_callbacks[i] = -1;
*exit_signal = 0;
*exit_counter = 0;
assert(mm);
assert(gc);
assert(cb);
assert(requests);
callbacks_index = 0;
callbacks.init(cb_num);
mm->init(device);
gc->init(device);
cb->init(device);
requests->init(APP_threads_number);
requests->memset(APP_threads_number);
GUARD_CU((cudaError_t)cudaMemPrefetchAsync((int*)exit_signal, sizeof(int), device, NULL));
GUARD_CU((cudaError_t)cudaMemPrefetchAsync((int*)exit_counter, sizeof(int), device, NULL));
GUARD_CU((cudaError_t)cudaMemPrefetchAsync((Service*)mm, sizeof(Service), device, NULL));
GUARD_CU((cudaError_t)cudaMemPrefetchAsync((Service*)gc, sizeof(Service), device, NULL));
GUARD_CU((cudaError_t)cudaMemPrefetchAsync((Service*)cb, sizeof(Service), device, NULL));
GUARD_CU((cudaError_t)cudaMemPrefetchAsync((RequestType*)requests, sizeof(RequestType), device, NULL));
#ifdef OUROBOROS__
mem_manager = memory_manager.getDeviceMemoryManager();
#else
//mem_manager = NULL;
#endif
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
}
void Runtime::free(){
mm->free();
gc->free();
cb->free();
requests->free();
callbacks.free();
GUARD_CU(cudaFree((void*)exit_signal));
GUARD_CU(cudaFree((void*)exit_counter));
GUARD_CU(cudaFree((void*)mm));
GUARD_CU(cudaFree((void*)gc));
GUARD_CU(cudaFree((void*)cb));
GUARD_CU(cudaFree((void*)requests));
GUARD_CU(cudaFree((void*)request_callbacks));
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
}
__device__
void Runtime::post_request_cb(request_type type, int callback){
int thid = blockDim.x * blockIdx.x + threadIdx.x;
// SEMAPHORE
__threadfence();
acquire_semaphore((int*)(requests->lock), thid);
if (type == CB){
//assert(callback.ptr);
request_callbacks[thid] = callback;
__threadfence();
//assert(request_callbacks[thid].ptr);
}
// SIGNAL update
atomicExch((int*)&(requests->request_signal[thid]), type);
debug("APP %s: thid %d, block ID %d, warp ID %d, lane ID %d, sm ID %d\n", __FUNCTION__, thid, blockIdx.x, warp_id(), lane_id(), sm_id());
release_semaphore((int*)(requests->lock), thid);
debug("semapohre released\n");
__threadfence();
// SEMAPHORE
}
__device__
void Runtime::post_request(request_type type, int size_to_alloc){
int thid = blockDim.x * blockIdx.x + threadIdx.x;
// SEMAPHORE
__threadfence();
acquire_semaphore((int*)(requests->lock), thid);
if (type == MALLOC){
requests->request_mem_size[thid] = size_to_alloc;
}
// SIGNAL update
atomicExch((int*)&(requests->request_signal[thid]), type);
debug("APP %s: thid %d, block ID %d, warp ID %d, lane ID %d, sm ID %d\n", __FUNCTION__, thid, blockIdx.x, warp_id(), lane_id(), sm_id());
release_semaphore((int*)(requests->lock), thid);
__threadfence();
// SEMAPHORE
}
__device__
void Runtime::request_processed(request_type type, volatile int** dest){
int thid = blockDim.x * blockIdx.x + threadIdx.x;
int req_id = -1;
// SEMAPHORE
__threadfence();
acquire_semaphore((int*)(requests->lock), thid);
debug("APP %s: thid %d, block ID %d, warp ID %d, lane ID %d, sm ID %d\n", __FUNCTION__, thid, blockIdx.x, warp_id(), lane_id(), sm_id());
switch (type){
case MALLOC:
req_id = requests->request_id[thid];
if (req_id >= 0 && !exit_signal[0]) {
*dest = requests->request_dest[thid];
assert(requests->d_memory[req_id] != NULL);
if (requests->d_memory[req_id][0] != 0)
printf("d_memory[%d] = %d\n", req_id, requests->d_memory[req_id][0]);
//assert(d_memory[req_id][0] == 0);
assert(*dest);
assert(requests->request_dest[thid] == *dest);
}
assert(*dest);
break;
case FREE:
//assert(d_memory[req_id] == NULL);
break;
case GC:
//assert(d_memory[req_id] == NULL);
break;
case CB:
break;
default:
printf("error\n");
break;
}
requests->request_signal[thid] = request_empty;
release_semaphore((int*)requests->lock, thid);
__threadfence();
// SEMAPHORE
}
__device__
void Runtime::request_async(request_type type,
volatile int** dest,
int size_to_alloc = 0){
int thid = blockDim.x * blockIdx.x + threadIdx.x;
// wait for request to be posted
while (is_working()){
if (is_available(thid)){
post_request(type, size_to_alloc);
break;
}
__threadfence();
}
__threadfence();
// do not wait request to be completed
}
__device__
void Runtime::callback_async(Future& ptr, int function){
ptr.ptr = NULL;
ptr.thid = blockIdx.x * blockIdx.x + threadIdx.x;
ptr.runtime = this;
ptr.type = (request_type)CB;
request_cb((request_type)CB, &ptr.ptr, function);
debug("callback done\n");
}
__device__
void Runtime::callback(volatile int** ptr, int function){
request_cb((request_type)CB, ptr, function);
debug("callback done\n");
}
__device__
void Runtime::request_cb(request_type type,
volatile int** dest,
int callback){
int thid = blockDim.x * blockIdx.x + threadIdx.x;
// wait for request to be posted
while (is_working()){
if (cb_is_finished(thid)){
request_callbacks[thid] = callback;
__threadfence();
break;
}
}
debug("request_cb done [%s:%d]\n", __FILE__, __LINE__);
__threadfence();
}
__device__
void Runtime::request(request_type type,
volatile int** dest,
int size_to_alloc = 0){
int thid = blockDim.x * blockIdx.x + threadIdx.x;
// wait for request to be posted
while (is_working()){
if (is_available(thid)){
post_request(type, size_to_alloc);
break;
}
__threadfence();
}
__threadfence();
// wait for request to be completed
while (is_working()){
if (is_finished(thid)){
request_processed(type, dest);
break;
}
__threadfence();
}
__threadfence();
}
__device__
void Runtime::_request_processing(int request_id){
debug("request processing!\n");
// SEMAPHORE
acquire_semaphore((int*)(requests->lock), request_id);
debug("MEM MANAGER %s: thid %d, block ID %d, warp ID %d, lane ID %d, sm ID %d\n",
__FUNCTION__, request_id, blockIdx.x, warp_id(), lane_id(), sm_id());
auto addr_id = requests->request_id[request_id];
int request_status;
switch (requests->type(request_id)){
case MALLOC:
if (addr_id == -1){
addr_id = atomicAdd((int*)&(requests->request_counter[0]), 1);
requests->request_id[request_id] = addr_id;
}else{
assert(requests->d_memory[addr_id] == NULL);
}
__threadfence();
#ifdef OUROBOROS__
requests->d_memory[addr_id] = reinterpret_cast<volatile int*>
(mem_manager->malloc(4+requests->request_mem_size[request_id]));
#else
GUARD_CU_DEV(cudaMalloc((void**)&requests->d_memory[addr_id], 4+requests->request_mem_size[request_id]));
#endif
__threadfence();
assert(requests->d_memory[addr_id]);
requests->d_memory[addr_id][0] = 0;
requests->request_dest[request_id] = &(requests->d_memory[addr_id][1]);
atomicExch((int*)&(requests->request_signal[request_id]), request_done);
if (requests->d_memory[addr_id][0] != 0)
printf("d_memory{%d} = %d\n", addr_id, requests->d_memory[addr_id][0]);
assert(requests->d_memory[addr_id][0] == 0);
__threadfence();
break;
case FREE:
assert(requests->d_memory[addr_id]);
if (requests->d_memory[addr_id][0] != 0)
printf("d_memory{%d} = %d\n", addr_id, requests->d_memory[addr_id][0]);
assert(requests->d_memory[addr_id][0] == 0);
request_status = requests->d_memory[addr_id][0] - 1;
requests->d_memory[addr_id][0] -= 1;
requests->request_dest[request_id] = NULL;
assert(requests->d_memory[addr_id][0] == -1);
if (request_status < 0){
atomicExch((int*)&(requests->request_signal[request_id]), request_gc);
}else{
assert(1);
printf("should not be here!\n");
atomicExch((int*)&requests->request_signal[request_id], request_done);
}
break;
case GC:
assert(requests->d_memory[addr_id]);
assert(requests->d_memory[addr_id][0] == -1);
__threadfence();
#ifdef OUROBOROS__
mem_manager->free((void*)requests->d_memory[addr_id]);
#else
//GUARD_CU_DEV(cudaFree((void*)requests->d_memory[addr_id]));
#endif
__threadfence();
requests->d_memory[addr_id] = NULL;
atomicExch((int*)&requests->request_signal[request_id], request_done);
break;
case CB:
//assert(request_callbacks[request_id]);
default:
printf("request processing fail\n");
}
release_semaphore((int*)(requests->lock), request_id);
// SEMAPHORE
}
__device__
void Runtime::malloc(volatile int** ptr, size_t size){
request((request_type)MALLOC, ptr, size);
}
__forceinline__ __device__
void Runtime::free_warp(volatile int* ptr){
if (threadIdx.x%32 == 0) free(ptr);
}
__forceinline__ __device__
void Runtime::free_block(volatile int* ptr){
if (threadIdx.x == 0) free(ptr);
}
__device__
void Runtime::malloc_block(volatile int** ptr, volatile int** tmp, size_t size){
int offset = threadIdx.x * size;
*tmp = NULL;
if (threadIdx.x == 0){
malloc(tmp, blockDim.x * size);
}
__threadfence();
__syncthreads();
*ptr = (volatile int*)(((volatile char*)*tmp) + offset);
}
__device__
void Runtime::malloc_block_async(Future& future_tmp, size_t size){
if (threadIdx.x == 0){
malloc_async(future_tmp, blockDim.x * size);
}
__threadfence();
__syncthreads();
}
__device__
void Runtime::malloc_warp(volatile int** ptr, volatile int** tmp, size_t size){
int lane_id = threadIdx.x%32;
int offset = lane_id * size;
*tmp = NULL;
if (lane_id == 0){
malloc(tmp, 32*size);
}
__threadfence();
__syncthreads();
*ptr = (volatile int*)(((volatile char*)*tmp) + offset);
}
__device__
void Runtime::malloc_warp_async(Future& future_tmp, size_t size){
if (threadIdx.x%32 == 0){
malloc_async(future_tmp, 32*size);
}
__threadfence();
__syncthreads();
}
__device__
void Runtime::free_warp_async(Future& future_tmp){
if (threadIdx.x%32 == 0){
free_async(&(future_tmp.ptr));
}
__threadfence();
__syncthreads();
}
__device__
void Runtime::free_block_async(Future& future_tmp){
if (threadIdx.x == 0){
free_async(&(future_tmp.ptr));
}
__threadfence();
__syncthreads();
}
__device__
void Runtime::malloc_async(volatile int** ptr, size_t size){
request_async((request_type)MALLOC, ptr, size);
}
__device__
void Runtime::malloc_async(Future& tab, size_t size){
tab.ptr = NULL;
tab.thid = blockDim.x * blockIdx.x + threadIdx.x;
tab.runtime = this;
tab.type = (request_type)MALLOC;
request_async((request_type)MALLOC, &tab.ptr, size);
__threadfence();
}
__forceinline__ __device__
void Runtime::free(volatile int* ptr){
request((request_type)FREE, &ptr, 0);
}
__device__
void Runtime::free_async(volatile int** ptr){
request_async((request_type)FREE, ptr, 0);
}
__device__
void Runtime::free_async(Future& future){
request_async((request_type)FREE, &future.ptr, 0);
}
#endif