-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmm.cu
1138 lines (947 loc) · 40.4 KB
/
pmm.cu
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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <string>
#include <cassert>
#include <algorithm>
#include <thread>
#include <chrono>
#include <any>
#include "device/Ouroboros_impl.cuh"
#include "device/MemoryInitialization.cuh"
#include "InstanceDefinitions.cuh"
#include "PerformanceMeasure.cuh"
#include "Utility.h"
#include "cuda.h"
#include "pmm-utils.cuh"
#include "pmm-utils.h"
using namespace std;
// Launch tests
void mps_monolithic_app(int, int, int, size_t*, size_t, int, int*, int*,
int*, int*, float*, int*, int, int, int);
void simple_monolithic_app(int, int, int, size_t*, size_t, int, int*,
int*, int*, int*, float*, int*, int, int);
void mps_app(int, int, int, size_t*, size_t, int, int*, int*, int*,
int*, float*, int*, int, int, int);
// Servies
// Memory Manager Service
void start_memory_manager(PerfMeasure&, uint32_t, uint32_t, CUcontext&, Runtime&);
__global__ void mem_manager(Runtime);
// Garbage Collector Service
void start_garbage_collector(PerfMeasure&, uint32_t, uint32_t, CUcontext&, Runtime&);
__global__ void garbage_collector(Runtime);
// Callback Service
void start_callback_server(PerfMeasure&, uint32_t, uint32_t, CUcontext&, Runtime&);
// TODO: Queue Service TODO
// TODO: Fency mechanism of turning on/off services from Runtime.
// TODO: Testing without garbage collector.
// Clean up
void clean_memory(uint32_t, uint32_t, Runtime&);
__global__ void mem_free(Runtime);
// TESTS
void start_application(PerfMeasure&, uint32_t, uint32_t, volatile int*, volatile int*,
int*, int*, int, bool&, Runtime&);
__global__ void app_test(int*, int*, int, Runtime);
__global__ void mono_app_test(volatile int**, volatile int*, int*, int*, MemoryManagerType*);
__global__ void app_async_request_test(int*, int*, int, Runtime);
__global__ void app_one_per_warp_test(int*, int*, int, Runtime);
__global__ void app_async_one_per_warp_test(int*, int*, int, Runtime);
__global__ void app_one_per_block_test(int*, int*, int, Runtime);
__global__ void app_async_one_per_block_test(int*, int*, int, Runtime);
__global__ void callback_test(int*, Runtime);
/* TODO: write down the pmm_init arguments:
* INPUT
* mono
* kernel_iteration_num
* size_to_alloc
* ins_size
* num_iterations
* SMs
* OUTPUT
* sm_app
* sm_mm
* sm_gc
* allocs
* uni_req_per_sec
* array_size
*/
// TODO test of memory allocation of random sizes within a kernel
extern "C" void pmm_init(int mono, int kernel_iteration_num, int size_to_alloc,
size_t* ins_size, size_t num_iterations, int SMs, int* sm_app,
int* sm_mm, int* sm_gc, int* allocs, float* uni_req_per_sec,
int* array_size, int block_size, int device, int cb_number){
GUARD_CU(cudaSetDevice(device));
printf("mono %d\n", mono);
printf("device %d\n", device);
if (mono == MPS_mono){
printf("MPS_mono\n");
mps_monolithic_app(mono, kernel_iteration_num,
size_to_alloc, ins_size, num_iterations,
SMs, sm_app, sm_mm, sm_gc, allocs,
uni_req_per_sec, array_size, block_size, device, cb_number);
printf("MPS_mono\n");
}else if (mono == simple_mono){
printf("simple mono\n");
simple_monolithic_app(mono, kernel_iteration_num,
size_to_alloc, ins_size, num_iterations,
SMs, sm_app, sm_mm, sm_gc, allocs,
uni_req_per_sec, array_size, block_size, device);
printf("simple mono\n");
}else{
printf("other mono\n");
mps_app(mono, kernel_iteration_num, size_to_alloc, ins_size,
num_iterations, SMs, sm_app, sm_mm, sm_gc, allocs,
uni_req_per_sec, array_size, block_size, device, cb_number);
}
}
__global__
void mem_free(Runtime runtime){
int thid = blockDim.x * blockIdx.x + threadIdx.x;
if (thid >= runtime.app_threads_num){
return;
}
__threadfence();
if (runtime.requests->d_memory[thid]){
printf("sync error: %d was not released before\n", thid);
#ifdef OUROBOROS__
runtime.mem_manager->free((void*)runtime.requests->d_memory[thid]);
#else
//GUARD_CU_DEV(cudaFree((void*)runtime.requests->d_memory[thid]));
//cudaFree((void*)runtime.requests->d_memory[thid]);
#endif
}
}
__global__
void garbage_collector(Runtime runtime){
int thid = blockIdx.x * blockDim.x + threadIdx.x;
runtime.gc->start();
RequestType* reqs = runtime.requests;
while (runtime.is_working()){
//debug("hello gc! %d\n", thid);
for (int request_id = thid; !runtime.exit_signal[0] &&
request_id < runtime.size();
request_id += blockDim.x*gridDim.x){
__threadfence();
if (reqs->type(request_id) == GC){
runtime._request_processing(request_id);
__threadfence();
}
}
__threadfence();
}
}
__global__
void mem_manager(Runtime runtime){
int thid = blockIdx.x * blockDim.x + threadIdx.x;
runtime.mm->start();
RequestType* reqs = runtime.requests;
while (runtime.is_working()){
//debug("hello mm %d, request no %d!\n", thid, reqs->number());
for (int request_id = thid; !runtime.exit_signal[0] &&
request_id < runtime.size();
request_id += blockDim.x*gridDim.x){
__threadfence();
if (reqs->type(request_id) == MALLOC or reqs->type(request_id) == FREE){
runtime._request_processing(request_id);
__threadfence();
debug("mm: request done %d\n", request_id);
}
}
__threadfence();
}
}
// TESTS
__global__
void mono_app_test(volatile int** d_memory,
volatile int* exit_counter,
int* size_to_alloc,
int* iter_num,
MemoryManagerType* mm){
int thid = blockDim.x * blockIdx.x + threadIdx.x;
assert(d_memory);
assert(exit_counter);
assert(size_to_alloc);
assert(iter_num);
for (int i=0; i<iter_num[0]; ++i){
__threadfence();
volatile int* new_ptr = NULL;
#ifdef OUROBOROS__
d_memory[thid] = reinterpret_cast<volatile int*>(mm->malloc(4+size_to_alloc[0]));
#else
GUARD_CU_DEV(cudaMalloc((void**)&d_memory[thid], 4+size_to_alloc[0]));
#endif
d_memory[thid][0] = 0;
new_ptr = &d_memory[thid][1];
new_ptr[0] = thid;
__threadfence();
assert(d_memory[thid]);
//int value = d_memory[thid][0];
//if (value != 0) printf("val = %d\n", value);
//value = d_memory[thid][1];
assert(new_ptr[0] == thid);
__threadfence();
#ifdef OUROBOROS__
mm->free((void*)d_memory[thid]);
#else
//GUARD_CU_DEV(cudaFree((void*)d_memory[thid]));
#endif
__threadfence();
d_memory[thid] = NULL;
__threadfence();
}
atomicAdd((int*)&exit_counter[0], 1);
__threadfence();
}
__global__
void app_one_per_block_test(int* size_to_alloc,
int* iter_num,
int MONO,
Runtime runtime){
int thid = blockDim.x * blockIdx.x + threadIdx.x;
__shared__ int* ptr_tab;
for (int i=0; i<iter_num[0]; ++i){
// ALLOCTAION
volatile int* new_ptr = NULL;
runtime.malloc_block((volatile int**)&new_ptr, (volatile int**)&ptr_tab, size_to_alloc[0]);
assert(new_ptr);
// WRITE
new_ptr[0] = thid;
// READ
assert(new_ptr[0] == thid);
// RECLAMATION
runtime.free_block(new_ptr);
}
atomicAdd((int*)(runtime.exit_counter), 1);
__threadfence();
}
__global__
void app_one_per_warp_test(int* size_to_alloc,
int* iter_num,
int MONO,
Runtime runtime){
int thid = blockDim.x * blockIdx.x + threadIdx.x;
int warp_id = threadIdx.x/32;
__shared__ int* ptr_tab[32];
for (int i=0; i<iter_num[0]; ++i){
// ALLOCTAION
volatile int* new_ptr = NULL;
runtime.malloc_warp((volatile int**)&new_ptr, (volatile int**)&ptr_tab[warp_id], size_to_alloc[0]);
assert(new_ptr);
// WRITE
new_ptr[0] = thid;
// READ
assert(new_ptr[0] == thid);
// RECLAMATION
runtime.free_warp(new_ptr);
}
atomicAdd((int*)(runtime.exit_counter), 1);
__threadfence();
}
__global__
void app_async_one_per_block_test(int* size_to_alloc,
int* iter_num,
int MONO,
Runtime runtime){
int thid = blockDim.x * blockIdx.x + threadIdx.x;
__shared__ Future future_tmp;
for (int i=0; i<iter_num[0]; ++i){
// ALLOC
runtime.malloc_block_async(future_tmp, size_to_alloc[0]);
volatile int* new_ptr = future_tmp.get_block_async(size_to_alloc[0]);
__threadfence();
assert(new_ptr);
// WRITE
new_ptr[0] = thid;
__threadfence();
// READ
assert(new_ptr[0] == thid);
// RECLAMATION
runtime.free_block_async(future_tmp);
if (threadIdx.x == 0)
runtime.wait((request_type)FREE, thid, &(future_tmp.ptr));
}
atomicAdd((int*)(runtime.exit_counter), 1);
__threadfence();
}
__global__
void app_async_one_per_warp_test(int* size_to_alloc,
int* iter_num,
int MONO,
Runtime runtime){
int thid = blockDim.x * blockIdx.x + threadIdx.x;
int warp_id = threadIdx.x/32;
__shared__ Future future_tmp[32];
for (int i=0; i<iter_num[0]; ++i){
// ALLOC
runtime.malloc_warp_async(future_tmp[warp_id], size_to_alloc[0]);
volatile int* new_ptr = future_tmp[warp_id].get_warp_async(size_to_alloc[0]);
__threadfence();
assert(new_ptr);
// WRITE
new_ptr[0] = thid;
__threadfence();
// READ
assert(new_ptr[0] == thid);
// RECLAMATION
runtime.free_warp_async(future_tmp[warp_id]);
if (threadIdx.x%32 == 0)
runtime.wait((request_type)FREE, thid, &(future_tmp[warp_id].ptr));
}
atomicAdd((int*)(runtime.exit_counter), 1);
__threadfence();
}
__global__
void app_async_request_test(int* size_to_alloc,
int* iter_num,
int MONO,
Runtime runtime){
int thid = blockDim.x * blockIdx.x + threadIdx.x;
for (int i=0; i<iter_num[0]; ++i){
// MEMORY ALLOCATION
Future future_ptr; //Can I pass it outside of the kernel?
runtime.malloc_async(future_ptr, size_to_alloc[0]);
// WRITE TEST
volatile int* new_ptr = future_ptr.get();
__threadfence();
assert(new_ptr);
new_ptr[0] = thid;
// READ TEST
assert(new_ptr[0] == thid);
// MEMORY RECLAMATION
//__threadfence();
runtime.free_async(future_ptr);
runtime.wait((request_type)FREE, thid, &future_ptr.ptr);
}
atomicAdd((int*)(runtime.exit_counter), 1);
__threadfence();
}
__global__
void callback_async_test(int* iter_num, Runtime runtime){
//int thid = blockDim.x * blockIdx.x + threadIdx.x;
for (int i=0; i<iter_num[0]; ++i){
// CALLBACK
//volatile int* new_ptr;
Future complete;
runtime.callback_async(complete, 1);
runtime.callback_async(complete, 2);
debug("callback done [%s:%d]\n", __FILE__, __LINE__);
//complete.cb_get(); hangs because one thread on CPU is a bottleneck
__threadfence();
}
atomicAdd((int*)(runtime.exit_counter), 1);
__threadfence();
}
__global__
void callback_test(int* iter_num, Runtime runtime){
for (int i=0; i<iter_num[0]; ++i){
// CALLBACK
volatile int* new_ptr;
runtime.callback(&new_ptr, 0);
debug("callback done [%s:%d]\n", __FILE__, __LINE__);
}
atomicAdd((int*)(runtime.exit_counter), 1);
__threadfence();
}
__global__
void app_test(int* size_to_alloc, int* iter_num,
int MONO, Runtime runtime){
int thid = blockDim.x * blockIdx.x + threadIdx.x;
for (int i=0; i<iter_num[0]; ++i){
// ALLOC
volatile int* new_ptr;
runtime.malloc(&new_ptr, size_to_alloc[0]);
assert(new_ptr);
// WRITE
new_ptr[0] = thid;
// READ
assert(new_ptr[0] == thid);
// RECLAMATION
runtime.free(new_ptr);
}
atomicAdd((int*)(runtime.exit_counter), 1);
__threadfence();
}
//TODO: put into runtime class
void start_memory_manager(PerfMeasure& timing_mm,
uint32_t mm_grid_size,
uint32_t block_size,
CUcontext& mm_ctx,
Runtime& runtime){
timing_mm.startMeasurement();
void *args[] = {&runtime};
GUARD_CU(cudaLaunchCooperativeKernel((void*)mem_manager, mm_grid_size, block_size, args));
//GUARD_CU(cudaLaunchKernel((void*)mem_manager, mm_grid_size, block_size, args));
GUARD_CU((cudaError_t)cudaGetLastError());
GUARD_CU(cudaPeekAtLastError());
timing_mm.stopMeasurement();
}
void start_garbage_collector(PerfMeasure& timing_gc,
uint32_t gc_grid_size,
uint32_t block_size,
CUcontext& gc_ctx,
Runtime& runtime){
timing_gc.startMeasurement();
void *args[] = {&runtime};
GUARD_CU(cudaLaunchCooperativeKernel((void*)garbage_collector, gc_grid_size, block_size, args));
//GUARD_CU(cudaLaunchKernel((void*)garbage_collector, gc_grid_size, block_size, args));
GUARD_CU((cudaError_t)cudaGetLastError());
GUARD_CU(cudaPeekAtLastError());
timing_gc.stopMeasurement();
}
void start_callback_server(PerfMeasure& timing_cb,
uint32_t cb_grid_size,
uint32_t block_size,
CUcontext& cb_ctx,
Runtime& runtime){
Callback_fn fn1 = [](int* ptr){
printf("Registered callback\n");
};
Callback_fn fn2 = [](int* ptr){
//printf("cudaMalloc within HOST\n");
GUARD_CU(cudaMalloc((void**)&ptr, sizeof(int)));
};
Callback_fn fn3 = [](int* ptr){
//printf("cudaFree within HOST\n");
GUARD_CU(cudaFree(ptr));
};
runtime.register_cb(fn1);
runtime.register_cb(fn2);
// runtime.register_cb(fn3);
timing_cb.startMeasurement();
runtime.cb->start();
while (runtime.is_working()){
for (int i = 0; i < runtime.app_threads_num; ++i){
//printf("for thread %d\n", i);
if (runtime.there_is_a_callback(i)){
int cb_id = runtime.callback_id(i);
debug("callback by %d of id %d\n", i, cb_id);
runtime.callback_run(cb_id)(NULL);
runtime.callback_close(i);
fflush(stdout);
}
}
}
GUARD_CU((cudaError_t)cudaGetLastError());
GUARD_CU(cudaPeekAtLastError());
timing_cb.stopMeasurement();
}
void clean_memory(uint32_t grid_size,
uint32_t block_size,
Runtime& runtime){
runtime.exit_signal[0] = 1;
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
void* args[] = {&runtime};
GUARD_CU(cudaLaunchCooperativeKernel((void*)mem_free, grid_size, block_size, args));
GUARD_CU(cudaPeekAtLastError());
GUARD_CU(cudaDeviceSynchronize());
runtime.free();
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
}
void start_application(PerfMeasure& timing_sync,
uint32_t grid_size,
uint32_t block_size,
volatile int* exit_signal,
volatile int* exit_counter,
int* dev_size_to_alloc,
int* dev_iter_num,
int mono,
bool& kernel_complete,
Runtime& runtime){
auto dev_mm = runtime.mem_manager;
if (mono == MPS_mono){
void* args[] = {&(runtime.requests->d_memory), &exit_counter, &dev_size_to_alloc, &dev_iter_num, &dev_mm};
//GUARD_CU(cudaProfilerStart());
timing_sync.startMeasurement();
debug("start application: MPS mono!\n");
//GUARD_CU(cudaLaunchKernel((void*)mono_app_test, grid_size, block_size, args, 0, 0));
GUARD_CU(cudaLaunchCooperativeKernel((void*)mono_app_test, grid_size, block_size, args, 0, 0));
GUARD_CU((cudaError_t)cuCtxSynchronize());
GUARD_CU(cudaPeekAtLastError());
timing_sync.stopMeasurement();
debug("ctx sync done!\n");
fflush(stdout);
//GUARD_CU(cudaProfilerStop());
}else if (mono == one_per_warp){
void* args[] = {&dev_size_to_alloc, &dev_iter_num, &mono, &runtime};
//GUARD_CU(cudaProfilerStart());
timing_sync.startMeasurement();
debug("start application one per warp\n");
//GUARD_CU(cudaLaunchKernel((void*)app_one_per_warp_test, grid_size, block_size, args, 0, 0));
GUARD_CU(cudaLaunchCooperativeKernel((void*)app_one_per_warp_test, grid_size, block_size, args));
GUARD_CU((cudaError_t)cuCtxSynchronize());
GUARD_CU(cudaPeekAtLastError());
timing_sync.stopMeasurement();
debug("stop application one per warp\n");
fflush(stdout);
//GUARD_CU(cudaProfilerStop());
}else if (mono == async_request){
void* args[] = {&dev_size_to_alloc, &dev_iter_num, &mono, &runtime};
//GUARD_CU(cudaProfilerStart());
timing_sync.startMeasurement();
//GUARD_CU(cudaLaunchKernel((void*)app_async_request_test, grid_size, block_size, args, 0, 0));
GUARD_CU(cudaLaunchCooperativeKernel((void*)app_async_request_test, grid_size, block_size, args));
GUARD_CU((cudaError_t)cuCtxSynchronize());
GUARD_CU(cudaPeekAtLastError());
timing_sync.stopMeasurement();
//GUARD_CU(cudaProfilerStop());
}else if (mono == one_per_block){
void* args[] = {&dev_size_to_alloc, &dev_iter_num, &mono, &runtime};
//GUARD_CU(cudaProfilerStart());
timing_sync.startMeasurement();
GUARD_CU(cudaLaunchCooperativeKernel((void*)app_one_per_block_test, grid_size, block_size, args));
GUARD_CU((cudaError_t)cuCtxSynchronize());
GUARD_CU(cudaPeekAtLastError());
timing_sync.stopMeasurement();
//GUARD_CU(cudaProfilerStop());
}else if (mono == async_one_per_warp){
void* args[] = {&dev_size_to_alloc, &dev_iter_num, &mono, &runtime};
//GUARD_CU(cudaProfilerStart());
timing_sync.startMeasurement();
GUARD_CU(cudaLaunchCooperativeKernel((void*)app_async_one_per_warp_test, grid_size, block_size, args));
GUARD_CU((cudaError_t)cuCtxSynchronize());
GUARD_CU(cudaPeekAtLastError());
timing_sync.stopMeasurement();
//GUARD_CU(cudaProfilerStop());
}else if (mono == async_one_per_block){
void* args[] = {&dev_size_to_alloc, &dev_iter_num, &mono, &runtime};
//GUARD_CU(cudaProfilerStart());
timing_sync.startMeasurement();
GUARD_CU(cudaLaunchCooperativeKernel((void*)app_async_one_per_block_test, grid_size, block_size, args));
GUARD_CU((cudaError_t)cuCtxSynchronize());
GUARD_CU(cudaPeekAtLastError());
timing_sync.stopMeasurement();
//GUARD_CU(cudaProfilerStop());
}else if (mono == callback_type){
void* args[] = {&dev_iter_num, &runtime};
//GUARD_CU(cudaProfilerStart());
timing_sync.startMeasurement();
GUARD_CU(cudaLaunchCooperativeKernel((void*)callback_async_test, grid_size, block_size, args));
GUARD_CU((cudaError_t)cuCtxSynchronize());
GUARD_CU(cudaPeekAtLastError());
timing_sync.stopMeasurement();
//GUARD_CU(cudaProfilerStop());
}else{
void* args[] = {&dev_size_to_alloc, &dev_iter_num, &mono, &runtime};
//GUARD_CU(cudaProfilerStart());
timing_sync.startMeasurement();
//GUARD_CU(cudaLaunchKernel((void*)app_test, grid_size, block_size, args, 0, 0));
GUARD_CU(cudaLaunchCooperativeKernel((void*)app_test, grid_size, block_size, args));
GUARD_CU((cudaError_t)cuCtxSynchronize());
GUARD_CU(cudaPeekAtLastError());
timing_sync.stopMeasurement();
//GUARD_CU(cudaProfilerStop());
}
kernel_complete = true;
}
void simple_monolithic_app(int mono, int kernel_iteration_num,
int size_to_alloc, size_t* ins_size, size_t num_iterations,
int SMs, int* sm_app, int* sm_mm, int* sm_gc, int* allocs,
float* uni_req_per_sec, int* array_size, int block_size,
int device_id){
CUcontext default_ctx;
GUARD_CU((cudaError_t)cuCtxGetCurrent(&default_ctx));
#ifdef OUROBOROS__
//Ouroboros initialization
auto instant_size = *ins_size;
MemoryManagerType memory_manager;
memory_manager.initialize(instant_size);
#endif
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
//Creat two asynchronous streams which may run concurrently with the default stream 0.
//The streams are not synchronized with the default stream.
volatile int* exit_signal;
volatile int* exit_counter;
int* dev_size_to_alloc;
int* dev_kernel_iteration_num;
GUARD_CU(cudaMallocManaged(&exit_signal, sizeof(int32_t)));
GUARD_CU(cudaMallocManaged(&exit_counter, sizeof(uint32_t)));
GUARD_CU(cudaMallocManaged(&dev_size_to_alloc, sizeof(int)));
GUARD_CU(cudaMallocManaged(&dev_kernel_iteration_num, sizeof(int)));
*dev_size_to_alloc = size_to_alloc;
*dev_kernel_iteration_num = kernel_iteration_num;
CUdevice device;
GUARD_CU((cudaError_t)cuDeviceGet(&device, device_id));
GUARD_CU((cudaError_t)cudaMemPrefetchAsync(
(int*)dev_size_to_alloc, sizeof(int), device, NULL));
GUARD_CU((cudaError_t)cudaMemPrefetchAsync(
(int*)dev_kernel_iteration_num, sizeof(int), device, NULL));
GUARD_CU(cudaPeekAtLastError());
GUARD_CU(cudaDeviceSynchronize());
int app_grid_size = SMs;
int requests_num = app_grid_size * block_size;
sm_app[0] = app_grid_size;
sm_mm[0] = 0;
sm_gc[0] = 0;
allocs[0] = requests_num;
cudaStream_t app_stream;
GUARD_CU(cudaStreamCreateWithFlags(&app_stream, cudaStreamNonBlocking));
GUARD_CU(cudaPeekAtLastError());
PerfMeasure malloc_total_sync;
for (int iteration = 0; iteration < num_iterations; ++iteration){
*exit_counter = 0;
volatile int** d_memory{nullptr};
GUARD_CU(cudaMalloc(&d_memory, requests_num * sizeof(volatile int*)));
GUARD_CU(cudaPeekAtLastError());
malloc_total_sync.startMeasurement();
#ifdef OUROBOROS__
auto dev_mm = memory_manager.getDeviceMemoryManager();
#else
void** dev_mm = NULL;
#endif
mono_app_test<<<app_grid_size, block_size, 0, app_stream>>>(
d_memory, exit_counter, dev_size_to_alloc,
dev_kernel_iteration_num, dev_mm);
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU((cudaError_t)cuCtxSynchronize());
GUARD_CU(cudaPeekAtLastError());
malloc_total_sync.stopMeasurement();
GUARD_CU(cudaFree(d_memory));
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
}
auto malloc_total_sync_res = malloc_total_sync.generateResult();
auto total_iters = kernel_iteration_num*num_iterations;
//uni_req_per_sec[0] = (requests_num * 1000.0)/(malloc_total_sync_res.mean_/total_iters);
uni_req_per_sec[0] = (requests_num * 2000.0)/malloc_total_sync_res.mean_;
printf("#measurements %d, mean %.2lf, #total iters %lu (host: %lu)\n", malloc_total_sync_res.num_,
malloc_total_sync_res.mean_, total_iters, num_iterations);
printf(" %d\t\t %d\t\t %d\t\t %d\t\t %.2lf\t\t \n", requests_num,
app_grid_size, 0, 0, uni_req_per_sec[0]);
*array_size = 1;
}
__host__
void mps_monolithic_app(int mono, int kernel_iteration_num, int size_to_alloc,
size_t* ins_size, size_t num_iterations, int SMs, int* sm_app,
int* sm_mm, int* sm_gc, int* allocs, float* uni_req_per_sec,
int* array_size, int block_size, int device_id, int cb_number = 0){
//Ouroboros initialization
MemoryManagerType memory_manager;
#ifdef OUROBOROS__
auto instant_size = *ins_size;
memory_manager.initialize(instant_size);
#else
#endif
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
volatile int* exit_signal;
volatile int* exit_counter;
GUARD_CU(cudaMallocManaged(&exit_signal, sizeof(int32_t)));
GUARD_CU(cudaMallocManaged(&exit_counter, sizeof(uint32_t)));
CUcontext app_ctx;
CUdevice device;
GUARD_CU((cudaError_t)cuDeviceGet(&device, device_id));
GUARD_CU((cudaError_t)cuCtxCreate(&app_ctx, 0, device));
CUcontext default_ctx;
GUARD_CU((cudaError_t)cuCtxGetCurrent(&default_ctx));
int* dev_size_to_alloc;
int* dev_kernel_iteration_num;
GUARD_CU(cudaMallocManaged(&dev_size_to_alloc, sizeof(int)));
GUARD_CU(cudaMallocManaged(&dev_kernel_iteration_num, sizeof(int)));
GUARD_CU(cudaPeekAtLastError());
GUARD_CU(cudaDeviceSynchronize());
*dev_size_to_alloc = size_to_alloc;
*dev_kernel_iteration_num = kernel_iteration_num;
GUARD_CU((cudaError_t)cudaMemPrefetchAsync((int*)dev_size_to_alloc, sizeof(int), device, NULL));
GUARD_CU((cudaError_t)cudaMemPrefetchAsync((int*)dev_kernel_iteration_num, sizeof(int), device, NULL));
int app_grid_size = SMs;
int requests_num = app_grid_size * block_size;
sm_app[0] = app_grid_size;
sm_mm[0] = 0;
sm_gc[0] = 0;
allocs[0] = requests_num;
PerfMeasure malloc_total_sync;
for (int iteration = 0; iteration < num_iterations; ++iteration){
//printf("iter %d, requests_num %d\n", iteration, requests_num);
*exit_signal = 0;
*exit_counter = 0;
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(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
Runtime runtime;
runtime.init(requests_num, device, memory_manager, cb_number);
// Run APP (all threads do malloc)
bool kernel_complete = false;
std::thread app_thread{[&] {
GUARD_CU((cudaError_t)cuCtxSetCurrent(app_ctx));
//GUARD_CU((cudaError_t)cuCtxSynchronize());
debug("start app\n");
//malloc_total_sync.startMeasurement();
//mps_monolithic_app
start_application(malloc_total_sync, app_grid_size,
block_size, exit_signal, exit_counter,
dev_size_to_alloc, dev_kernel_iteration_num,
mono, kernel_complete, runtime);
debug("app done, sync\n");
GUARD_CU((cudaError_t)cuCtxSynchronize());
//malloc_total_sync.stopMeasurement();
GUARD_CU(cudaPeekAtLastError());
debug("done\n");
}};
debug("join app\n");
app_thread.join();
debug("app joined\n");
if (not kernel_complete){
printf("kernel is not completed, free memory which app allocated\n");
clean_memory(app_grid_size, block_size, runtime);
continue;
}
*exit_signal = 1;
clean_memory(app_grid_size, block_size, runtime);
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
}
GUARD_CU((cudaError_t)cuCtxDestroy(app_ctx));
//GUARD_CU((cudaError_t)cuCtxSetCurrent(default_ctx));
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
auto malloc_total_sync_res = malloc_total_sync.generateResult();
auto total_iters = kernel_iteration_num*num_iterations;
//uni_req_per_sec[0] = (requests_num * 1000.0)/(malloc_total_sync_res.mean_/total_iters);
uni_req_per_sec[0] = (requests_num * 2000.0)/malloc_total_sync_res.mean_;
printf("#measurements %d, mean %.2lf, #total iters %lu (host: %lu)\n", malloc_total_sync_res.num_,
malloc_total_sync_res.mean_, total_iters, num_iterations);
printf(" %d\t\t %d\t\t %d\t\t %d\t\t %.2lf\t\t \n", requests_num,
app_grid_size, 0, 0, uni_req_per_sec[0]);
*array_size = 1;
}
__host__
void mps_app(int mono, int kernel_iteration_num, int size_to_alloc,
size_t* ins_size, size_t num_iterations, int SMs, int* sm_app,
int* sm_mm, int* sm_gc, int* allocs, float* uni_req_per_sec,
int* array_size, int block_size, int device_id, int cb_number){
CUcontext default_ctx;
GUARD_CU((cudaError_t)cuCtxGetCurrent(&default_ctx));
//Ouroboros initialization
MemoryManagerType memory_manager;
#ifdef OUROBOROS__
auto instant_size = *ins_size;
memory_manager.initialize(instant_size);
#endif
int total_gpus = 0;
GUARD_CU((cudaError_t)cudaGetDeviceCount(&total_gpus));
debug("device %d/%d\n", device_id, total_gpus);
debug("Block size = %d\n", block_size);
CUdevice device;
GUARD_CU((cudaError_t)cuDeviceGet(&device, device_id));
int* dev_size_to_alloc;
int* dev_kernel_iteration_num;
GUARD_CU(cudaMallocManaged(&dev_size_to_alloc, sizeof(int)));
GUARD_CU(cudaMallocManaged(&dev_kernel_iteration_num, sizeof(int)));
*dev_size_to_alloc = size_to_alloc;
*dev_kernel_iteration_num = kernel_iteration_num;
GUARD_CU((cudaError_t)cudaMemPrefetchAsync(
(int*)dev_size_to_alloc, sizeof(int), device, NULL));
GUARD_CU((cudaError_t)cudaMemPrefetchAsync(
(int*)dev_kernel_iteration_num, sizeof(int), device, NULL));
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU(cudaPeekAtLastError());
int it = 0;
int cb_grid_size = 1;
for (int app_grid_size=55; app_grid_size<SMs-cb_grid_size; ++app_grid_size){
for (int mm_grid_size=1; mm_grid_size<(SMs-app_grid_size-cb_grid_size); ++mm_grid_size){
int gc_grid_size = SMs - app_grid_size - mm_grid_size - cb_grid_size;
if (gc_grid_size < 1) continue;
int requests_num{app_grid_size * block_size};
//printf("SMs: app %d, mm %d, gc %d, cb %d, total %d\n",
// app_grid_size, mm_grid_size, gc_grid_size, cb_grid_size, SMs);
//printf("requests_num %d\n", requests_num);
//fflush(stdout);
//output
sm_app[it] = app_grid_size;
sm_mm [it] = mm_grid_size;
sm_gc [it] = gc_grid_size;
allocs[it] = requests_num;
int app_numBlocksPerSm = 1;
int gc_numBlocksPerSm = 1;
int mm_numBlocksPerSm = 1;
int cb_numBlocksPerSm = 1;
debug("num blocks per sm by cudaOccMaxActBlPerSM: app %d, mm %d, gc %d\n",
app_numBlocksPerSm, gc_numBlocksPerSm, mm_numBlocksPerSm);
//fflush(stdout);
CUexecAffinityParam_v1 app_param{CUexecAffinityType::CU_EXEC_AFFINITY_TYPE_SM_COUNT,
(unsigned int)app_grid_size * app_numBlocksPerSm};
CUexecAffinityParam_v1 mm_param{CUexecAffinityType::CU_EXEC_AFFINITY_TYPE_SM_COUNT,
(unsigned int)mm_grid_size * mm_numBlocksPerSm};
CUexecAffinityParam_v1 gc_param{CUexecAffinityType::CU_EXEC_AFFINITY_TYPE_SM_COUNT,
(unsigned int)gc_grid_size * gc_numBlocksPerSm};
CUexecAffinityParam_v1 cb_param{CUexecAffinityType::CU_EXEC_AFFINITY_TYPE_SM_COUNT,
(unsigned int)cb_grid_size * cb_numBlocksPerSm};
auto affinity_flags = CUctx_flags::CU_CTX_SCHED_AUTO;
//auto affinity_flags = CUctx_flags::CU_CTX_SCHED_SPIN;
//auto affinity_flags = CUctx_flags::CU_CTX_SCHED_YIELD;
//auto affinity_flags = CUctx_flags::CU_CTX_SCHED_BLOCKING_SYNC;
//auto affinity_flags = CUctx_flags::CU_CTX_BLOCKING_SYNC;
//auto affinity_flags = CUctx_flags::CU_CTX_MAP_HOST;
//auto affinity_flags = CUctx_flags::CU_CTX_LMEM_RESIZE_TO_MAX;
CUcontext app_ctx, mm_ctx, gc_ctx, cb_ctx;
GUARD_CU((cudaError_t)cuCtxCreate_v3(
&app_ctx, &app_param, 1, affinity_flags, device));
GUARD_CU((cudaError_t)cuCtxCreate_v3(
&mm_ctx, &mm_param, 1, affinity_flags, device));
GUARD_CU((cudaError_t)cuCtxCreate_v3(
&gc_ctx, &gc_param, 1, affinity_flags, device));
GUARD_CU((cudaError_t)cuCtxCreate_v3(
&cb_ctx, &cb_param, 1, affinity_flags, device));
GUARD_CU(cudaPeekAtLastError());
GUARD_CU(cudaDeviceSynchronize());
GUARD_CU((cudaError_t)cudaGetLastError());
//Timing variables
PerfMeasure malloc_total_sync, timing_mm, timing_gc, timing_cb;
for (int iteration = 0; iteration < num_iterations; ++iteration){
debug("iteration %d/%d\n", iteration, num_iterations);
Runtime runtime;
runtime.init(requests_num, device, memory_manager, cb_number);
debug("start threads\n");
// Run Memory Manager (Presistent kernel)
std::thread mm_thread{[&] {
GUARD_CU((cudaError_t)cuCtxSetCurrent(mm_ctx));
GUARD_CU((cudaError_t)cuCtxSynchronize());
GUARD_CU(cudaPeekAtLastError());
debug("start mm\n");
start_memory_manager(timing_mm, mm_numBlocksPerSm*mm_grid_size,
block_size, mm_ctx, runtime);
debug("mm done, sync\n");
GUARD_CU((cudaError_t)cuCtxSynchronize());
GUARD_CU(cudaPeekAtLastError());
debug("done\n");
}};
//std::this_thread::sleep_for(std::chrono::seconds(1));
// Run Garbage Collector (persistent kernel)
std::thread gc_thread{[&] {
GUARD_CU((cudaError_t)cuCtxSetCurrent(gc_ctx));
GUARD_CU((cudaError_t)cuCtxSynchronize());
GUARD_CU(cudaPeekAtLastError());
debug("start gc\n");
start_garbage_collector(timing_gc, gc_numBlocksPerSm*gc_grid_size,
block_size, gc_ctx, runtime);
debug("gc done, sync\n");
GUARD_CU((cudaError_t)cuCtxSynchronize());
GUARD_CU(cudaPeekAtLastError());
debug("done\n");
}};
//std::this_thread::sleep_for(std::chrono::seconds(1));
// Callback Server (persistent kernel)
std::thread cb_thread{[&] {
GUARD_CU((cudaError_t)cuCtxSetCurrent(cb_ctx));
GUARD_CU((cudaError_t)cuCtxSynchronize());