-
Notifications
You must be signed in to change notification settings - Fork 1
/
penguin-sc.h
1433 lines (1305 loc) · 52.4 KB
/
penguin-sc.h
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
/* Library for calling the new ioctl for prioritizing pages on the GPU */
/* SC */
#ifndef PENGUIN
#define PENGUIN // the penguin library
#define PENGUIN_PRIORITIZED_GPU_IOCTL_NUM 75
#define PENGUIN_NO_MIGRATE_IOCTL_NUM 76
#define PENGUIN_START_STAT_COLLECTION_IOCTL_NUM 77
#define PENGUIN_STOP_STAT_COLLECTION_IOCTL_NUM 78
#define PENGUIN_QUICK_MIGRATE_IOCTL_NUM 79
#define PENGUIN_ACCESS_COUNTER_ENABLE 80
/* #define PENGUIN_MIN_PREFETCH (32*1024*1024) */
#define PENGUIN_MIN_PREFETCH (8*1024*1024)
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <stdint.h>
#include <dirent.h>
#include <sys/ioctl.h>
#include <cuda_runtime.h>
#include <nvml.h>
#include <iterator>
#define NVML_PROFILER 1
#define NVML_TX 0
unsigned int nvml_running = 0;
pthread_t monitor;
#define PSF_DIR "/proc/self/fd"
#define NVIDIA_UVM_PATH "/dev/nvidia-uvm"
static int nvidia_uvm_fd = -1;
/* static volatile unsigned counter = 0; */
unsigned long long MBs = 5451ULL;
unsigned long long gpu_memory = 1 * MBs * 1024ULL * 1024ULL;
unsigned long long available = gpu_memory;
unsigned long long pinned_memory = 0;
unsigned long long SCAvail = gpu_memory;
std::map<void*, unsigned long long> SCGPUResidentAllocs;
// add code to evict anything whose use is over
// add code to prioritize higher AD temporal region over lower AD temporal region
enum State {
PENGUIN_STATE_UNKNOWN,
PENGUIN_STATE_GPU,
PENGUIN_STATE_GPU_PINNED,
PENGUIN_STATE_GPU_PINNED_PART,
PENGUIN_STATE_HOST,
PENGUIN_STATE_AC,
PENGUIN_STATE_MAX
};
enum Decision {
PENGUIN_DEC_NONE,
PENGUIN_DEC_HOST_PIN,
PENGUIN_DEC_GPU_PIN,
PENGUIN_DEC_GPU_HOST_PARTIAL_PIN,
PENGUIN_DEC_MIGRATE_ON_DEMAND,
PENGUIN_DEC_ITERATION_MIGRATION,
PENGUIN_DEC_ITERATION_MIGRATION_PLUS_GPU_HOST_PIN,
PENGUIN_DEC_ACCESS_COUNTER,
PENGUIN_DEC_MAX
};
typedef enum {
PENGUIN_OK,
PENGUIN_ERR_PATH,
PENGUIN_ERR_IOCTL,
PENGUIN_ERR_NOT_IMPLEMENTED
} penguin_error_t;
typedef struct
{
void *base;
size_t length;
uint8_t uuid[16];
int status;
} penguin_prioritized_ioctl_params;
typedef struct
{
uint8_t uuid[16];
unsigned mimc_gran;
unsigned momc_gran;
unsigned mimc_use_limit;
unsigned momc_use_limit;
unsigned threshold;
bool enable_mimc;
bool enable_momc;
int status;
} penguin_enable_access_counter_param;
typedef struct
{
void *base;
size_t length;
bool quick_migrate;
} penguin_quick_migrate_ioctl_params;
typedef struct
{
void *base;
size_t length;
bool ignore_notifications;
int status;
} penguin_ignore_notif_ioctl_params;
typedef struct
{
int status;
} penguin_start_stat_collection_params;
typedef struct
{
int status;
} penguin_stop_stat_collection_params;
std::map<void*, State> SCState;
std::map<void*, unsigned long long> allocation_size_map;
std::map<void*, unsigned long long> allocation_ac_map;
std::map<void*, unsigned long long> allocation_pd_bidx_map;
std::map<void*, unsigned long long> allocation_pd_bidy_map;
std::map<void*, unsigned long long> allocation_pd_phi_map;
std::map<void*, unsigned long long> allocation_wss_map;
std::vector<std::pair<void*, unsigned long long>> ad_vector;
std::vector<std::pair<void*, unsigned long long>> wss_vector;
std::map<unsigned, unsigned long long> aid_ac_map;
std::map<unsigned, void*> aid_allocation_map;
std::map<unsigned, unsigned long long> aid_wss_map_iterdep;
std::map<unsigned, unsigned long long> aid_wss_map;
std::map<unsigned, bool> aid_pchase_map;
std::map<unsigned, unsigned> aid_invocation_id_map;
std::map<unsigned, bool> aid_ac_incomp_map;
// Badly named
std::map<unsigned, unsigned long long> aid_ac_map_reuse;
std::map<unsigned, void*> aid_allocation_map_reuse;
std::map<unsigned, unsigned> aid_invocation_id_map_reuse;
std::map<unsigned, std::set<void*>> global_mmg_invid_alloc_list;
std::map<void*, std::map<unsigned, unsigned>> global_alloc_inv_resinv_map;
std::map<void*, unsigned long long> global_mmg_alloc_ac_map;
std::vector<std::pair<void*, unsigned long long>> global_mmg_alloc_ac_vector;
std::map<void*, unsigned> global_alloc_firstuse_map;
std::map<unsigned, std::vector<std::pair<void*, unsigned>>> global_invid_sorted_reuse_alloc_map;
extern "C"
void add_aid_ac_map_reuse(unsigned aid, unsigned long long ac) {
/* std::cout << "adi aid ac map resue " << aid << " " << ac << "\n"; */
aid_ac_map_reuse[aid] = ac;
}
extern "C"
void add_aid_allocation_map_reuse(unsigned aid, void* allocation) {
/* std::cout<< "add_aid_allocation_map reuse" << aid << " " << allocation << std::endl; */
aid_allocation_map_reuse[aid] = allocation;
}
extern "C"
void add_aid_invocation_map_reuse(unsigned aid, unsigned invocation_id) {
/* std::cout<< "add_aid_invocation_map reuse" << aid << " " << invocation_id << std::endl; */
aid_invocation_id_map_reuse[aid] = invocation_id;
}
std::map<void*, unsigned> AllocationToItersPerBatchMap;
std::map<void*, unsigned> AllocationToLengthMap;
// working data structures
std::map<void*, std::map<unsigned, unsigned long long>> AllocationToInvocationIDtoADMap;
std::map<void*, std::map<unsigned, Decision>> AllocationToInvocationIDtoDecisionMap;
std::map<unsigned, std::map<void*, Decision>> InvocationIDtoAllocationToADMap;
std::map<unsigned, std::map<void*, Decision>> InvocationIDtoAllocationToDecisionMap;
std::map<unsigned, std::map<void*, unsigned long long>> InvocationIDtoAllocationToPartialSize;
std::map<void*, Decision> AllocationToCommonDecisionMap;
std::map<void*, unsigned long long> AllocationToPartialSizeMap;
std::map<void*, Decision> AllocationToDecisionMap;
std::map<void*, bool> AllocationToPrefetchBoolMap;
std::map<void*, unsigned long long> AllocationToPrefetchSizeMap;
std::map<void*, unsigned long long> AllocationToPrefetchItersPerBatchMap;
std::map<void*, unsigned long long> AllocationToPchaseMap;
static bool is_iterative = false;
// this bool helps helper function at kernel invocations to quickly decide if
// there are any API calls to be made or it can just skip
std::map<unsigned, bool> InvocationIDtoDecisionBoolMap;
std::set<unsigned> InvocationIDs;
// state maps
std::map<void*, State> AllocationStateMap;
std::map<void*, unsigned long long> AllocationGPUResStart;
std::map<void*, unsigned long long> AllocationGPUResStop;
std::map<void*, bool> AllocationStateIterBoolMap;
std::map<void*, unsigned> AllocationStateIterStartMap;
std::map<void*, unsigned> AllocationStateIterStopMap;
unsigned max_invid = 0;
// Do we need "C" linkage?
void* round_down(void* addr) {
unsigned long long ad = (unsigned long long) addr;
return (void*) ((ad >> 16) << 16);
}
extern "C"
penguin_error_t penguinSetPrioritizedLocation(void *base, size_t length,
unsigned proc_id) {
DIR *d;
struct dirent *dir;
char psf_path[512];
char *psf_realpath;
penguin_prioritized_ioctl_params request;
int status;
/* std::cout << "set prioritized location " << base << std::endl; */
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, 0);
for(int i = 0; i < 16; i++) {
request.uuid[i] = prop.uuid.bytes[i];
/* printf("%u", prop.uuid.bytes[i]); */
}
request.base = base;
request.length = length;
d = opendir(PSF_DIR);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (dir->d_type == DT_LNK)
{
sprintf(psf_path, "%s/%s", PSF_DIR, dir->d_name);
psf_realpath = realpath(psf_path, NULL);
if (strcmp(psf_realpath, NVIDIA_UVM_PATH) == 0)
nvidia_uvm_fd = atoi(dir->d_name);
free(psf_realpath);
if (nvidia_uvm_fd >= 0)
break;
}
}
closedir(d);
}
if (nvidia_uvm_fd < 0)
{
fprintf(stderr, "Cannot open %s\n", PSF_DIR);
return PENGUIN_ERR_PATH;
}
if ((status = ioctl(nvidia_uvm_fd, PENGUIN_PRIORITIZED_GPU_IOCTL_NUM, &request)) != 0)
{
fprintf(stderr, "error: %d\n", status);
return PENGUIN_ERR_IOCTL;
}
return PENGUIN_OK;
}
extern "C"
penguin_error_t penguinSetQuickMigrate(void *base, size_t length,
bool quick_migrate) {
DIR *d;
struct dirent *dir;
char psf_path[512];
char *psf_realpath;
penguin_quick_migrate_ioctl_params request;
int status;
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, 0);
request.base = base;
request.length = length;
d = opendir(PSF_DIR);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (dir->d_type == DT_LNK)
{
sprintf(psf_path, "%s/%s", PSF_DIR, dir->d_name);
psf_realpath = realpath(psf_path, NULL);
if (strcmp(psf_realpath, NVIDIA_UVM_PATH) == 0)
nvidia_uvm_fd = atoi(dir->d_name);
free(psf_realpath);
if (nvidia_uvm_fd >= 0)
break;
}
}
closedir(d);
}
if (nvidia_uvm_fd < 0)
{
fprintf(stderr, "Cannot open %s\n", PSF_DIR);
return PENGUIN_ERR_PATH;
}
if ((status = ioctl(nvidia_uvm_fd, PENGUIN_QUICK_MIGRATE_IOCTL_NUM, &request)) != 0)
{
fprintf(stderr, "error: %d\n", status);
fprintf(stderr, "debuggy\n");
return PENGUIN_ERR_IOCTL;
}
return PENGUIN_OK;
}
extern "C"
penguin_error_t penguinSetNoMigrateRegion(void *base, size_t length,
unsigned proc_id, bool setNoMigrate) {
DIR *d;
struct dirent *dir;
char psf_path[512];
char *psf_realpath;
penguin_ignore_notif_ioctl_params request;
int status;
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, 0);
request.base = base;
request.length = length;
request.ignore_notifications = setNoMigrate;
d = opendir(PSF_DIR);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (dir->d_type == DT_LNK)
{
sprintf(psf_path, "%s/%s", PSF_DIR, dir->d_name);
psf_realpath = realpath(psf_path, NULL);
if (strcmp(psf_realpath, NVIDIA_UVM_PATH) == 0)
nvidia_uvm_fd = atoi(dir->d_name);
free(psf_realpath);
if (nvidia_uvm_fd >= 0)
break;
}
}
closedir(d);
}
if (nvidia_uvm_fd < 0)
{
fprintf(stderr, "Cannot open %s\n", PSF_DIR);
return PENGUIN_ERR_PATH;
}
if ((status = ioctl(nvidia_uvm_fd, PENGUIN_NO_MIGRATE_IOCTL_NUM, &request)) != 0)
{
fprintf(stderr, "error: %d\n", status);
return PENGUIN_ERR_IOCTL;
}
return PENGUIN_OK;
}
extern "C"
void penguinSuperPrefetch(void *base, size_t length, unsigned iter, unsigned iterPerBatch, size_t max) {
/* counter++; */
if (length == 0) return;
if ((iter % iterPerBatch) == 0) {
int prefnum = iter / iterPerBatch;
if ((prefnum+1) * length > max) {
length = max - (prefnum) * length;
/* return; // not return, use new value */
}
printf("base = %p prefnum = %d; iter = %d; length = %llu\n", base, prefnum, iter, length);
// TODO: prefetch back, but only if there is memory pressure.
auto pref_addr = prefnum*length;
if(AllocationGPUResStart[base] <= pref_addr &&
(pref_addr + length) < AllocationGPUResStop[base]){
return;
}
/* std::cout << "pref_addr = " << pref_addr << std::endl; */
/* std::cout << "alloc start on gpu = " << AllocationGPUResStart[base] << std::endl; */
/* std::cout << "alloc stop on gpu = " << AllocationGPUResStop[base] << std::endl; */
if(prefnum > 0 && available == 0) {
/* std::cout << "revpref\n"; */
cudaMemPrefetchAsync((char*)base + ((prefnum-1)*length), length, -1, 0 );
AllocationGPUResStart[base] += length;
AllocationGPUResStop[base] += length;
}
/* std::cout << "pre\n"; */
cudaMemPrefetchAsync((char*)base + (prefnum*length), length, 0, 0 );
}
return;
}
extern "C"
void penguinSuperPrefetchWrapper(unsigned iter) {
//get parameters from runtime maps and call penguinSuperPrefetch
/* std::cout << "penguinSuperPrefetchWrapper " << iter << "\n"; */
for(auto memalloc = AllocationToPrefetchBoolMap.begin();
memalloc != AllocationToPrefetchBoolMap.end(); memalloc++) {
auto base = memalloc->first;
if(AllocationToPrefetchBoolMap[base] == true) {
/* std::cout << "pspw says prefetch iter " << base << " " << iter << "\n"; */
unsigned long long length = AllocationToPrefetchSizeMap[base];
auto dsize = allocation_size_map[base];
auto iterPerBatch = AllocationToPrefetchItersPerBatchMap[base];
penguinSuperPrefetch(base, length, iter, iterPerBatch, dsize);
/* penguinSuperPrefetch(base, 512*1024*1024, iter, 128, dsize); */
}
}
}
void* nvml_monitor(void* argp) {
/* printf("ellloooo\n"); */
auto status = nvmlInit();
if(status != NVML_SUCCESS) {
printf("unable to init nvml\n");
return NULL;
}
nvmlDevice_t device_;
unsigned throughput;
unsigned long long total = 0;
unsigned long long count = 0;
status = nvmlDeviceGetHandleByIndex(0, &device_);
while(nvml_running == 1) {
#if NVML_TX
status = nvmlDeviceGetPcieThroughput(device_, NVML_PCIE_UTIL_TX_BYTES, &throughput);
#else
status = nvmlDeviceGetPcieThroughput(device_, NVML_PCIE_UTIL_RX_BYTES, &throughput);
#endif
/* printf("throughput = %u\n", throughput); */
total += throughput;
count ++;
}
#if NVML_TX
printf("total TX PCIe = %llu\n", total);
#else
printf("total RX PCIe = %llu\n", total);
#endif
/* printf("count = %u\n", count); */
return NULL;
}
void nvml_start() {
#if NVML_PROFILER
nvml_running = 1;
pthread_create(&monitor, NULL, nvml_monitor, NULL);
#endif
}
void nvml_stop() {
#if NVML_PROFILER
nvml_running = 0;
/* printf("nvml: running = 0\n"); */
pthread_join(monitor, NULL);
#endif
}
bool ac_enabled = false;
extern "C"
penguin_error_t penguinEnableAccessCounters() {
if(ac_enabled == true) {
return PENGUIN_OK;
}
ac_enabled = true;
DIR *d;
struct dirent *dir;
char psf_path[512];
char *psf_realpath;
penguin_enable_access_counter_param request;
int status;
request.enable_mimc = true;
request.enable_momc = false;
request.mimc_gran = 1;
request.momc_gran = 1;
request.mimc_use_limit = 4;
request.momc_use_limit = 4;
request.threshold = 256;
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, 0);
for(int i = 0; i < 16; i++) {
request.uuid[i] = prop.uuid.bytes[i];
printf("%u", prop.uuid.bytes[i]);
}
fprintf(stderr, "enable access counters\n");
d = opendir(PSF_DIR);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (dir->d_type == DT_LNK)
{
sprintf(psf_path, "%s/%s", PSF_DIR, dir->d_name);
psf_realpath = realpath(psf_path, NULL);
if (strcmp(psf_realpath, NVIDIA_UVM_PATH) == 0)
nvidia_uvm_fd = atoi(dir->d_name);
free(psf_realpath);
if (nvidia_uvm_fd >= 0)
break;
}
}
closedir(d);
}
if (nvidia_uvm_fd < 0)
{
fprintf(stderr, "Cannot open %s\n", PSF_DIR);
return PENGUIN_ERR_PATH;
}
if ((status = ioctl(nvidia_uvm_fd, PENGUIN_ACCESS_COUNTER_ENABLE, &request)) != 0)
{
fprintf(stderr, "error: %d\n", status);
fprintf(stderr, "debuggy\n");
return PENGUIN_ERR_IOCTL;
}
return PENGUIN_OK;
}
extern "C"
penguin_error_t penguinStartStatCollection() {
DIR *d;
struct dirent *dir;
char psf_path[512];
char *psf_realpath;
penguin_start_stat_collection_params request;
int status;
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, 0);
d = opendir(PSF_DIR);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (dir->d_type == DT_LNK)
{
sprintf(psf_path, "%s/%s", PSF_DIR, dir->d_name);
psf_realpath = realpath(psf_path, NULL);
if (strcmp(psf_realpath, NVIDIA_UVM_PATH) == 0)
nvidia_uvm_fd = atoi(dir->d_name);
free(psf_realpath);
if (nvidia_uvm_fd >= 0)
break;
}
}
closedir(d);
}
if (nvidia_uvm_fd < 0)
{
fprintf(stderr, "Cannot open %s\n", PSF_DIR);
return PENGUIN_ERR_PATH;
}
if ((status = ioctl(nvidia_uvm_fd, PENGUIN_START_STAT_COLLECTION_IOCTL_NUM, &request)) != 0)
{
fprintf(stderr, "error: %d\n", status);
return PENGUIN_ERR_IOCTL;
}
return PENGUIN_OK;
}
extern "C"
penguin_error_t penguinStopStatCollection() {
DIR *d;
struct dirent *dir;
char psf_path[512];
char *psf_realpath;
penguin_stop_stat_collection_params request;
int status;
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, 0);
d = opendir(PSF_DIR);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (dir->d_type == DT_LNK)
{
sprintf(psf_path, "%s/%s", PSF_DIR, dir->d_name);
psf_realpath = realpath(psf_path, NULL);
if (strcmp(psf_realpath, NVIDIA_UVM_PATH) == 0)
nvidia_uvm_fd = atoi(dir->d_name);
free(psf_realpath);
if (nvidia_uvm_fd >= 0)
break;
}
}
closedir(d);
}
if (nvidia_uvm_fd < 0)
{
fprintf(stderr, "Cannot open %s\n", PSF_DIR);
return PENGUIN_ERR_PATH;
}
if ((status = ioctl(nvidia_uvm_fd, PENGUIN_STOP_STAT_COLLECTION_IOCTL_NUM, &request)) != 0)
{
fprintf(stderr, "error: %d\n", status);
return PENGUIN_ERR_IOCTL;
}
return PENGUIN_OK;
}
extern "C"
void add_invocation_id(unsigned invid) {
InvocationIDs.insert(invid);
return;
}
// TODO: fix this ASAP
extern "C"
void addIntoAllocationMap(void** ptr, unsigned long long size) {
void* p = (void*) *ptr;
/* std::cout << "added to allocation map, " << p << " " << size << "\n"; */
allocation_size_map[p] = size;
return;
}
extern "C"
void printAllocationMap() {
/* std::cout << "size map\n"; */
for (auto a = allocation_size_map.begin(); a != allocation_size_map.end(); a++) {
/* std::cout << a->first << " " << a->second << "\n"; */
}
}
extern "C"
unsigned long long getAllocationSize(void* ptr) {
return allocation_size_map[ptr];
}
extern "C"
void addACToAllocation(void* ptr, unsigned long long count) {
/* void* p = (void*) *ptr; */
/* std::cout << ptr << std::endl; */
allocation_ac_map[ptr] += count;
return;
}
extern "C"
void printACToAllocationMap() {
/* std::cout << "ac map\n"; */
for (auto a = allocation_ac_map.begin(); a != allocation_ac_map.end(); a++) {
/* std::cout << a->first << " " << a->second << "\n"; */
}
}
extern "C"
float getAccessDensity(void* ptr) {
return (float) allocation_ac_map[ptr] / (float) allocation_size_map[ptr];
}
// TODO : add a method for clearing the allocation_ac_map
extern "C"
unsigned long long accessCountForAllocation(void* ptr) {
return allocation_ac_map[ptr];
}
extern "C"
void add_pd_bidx_to_allocation(void* ptr, unsigned pd_bidx) {
if(allocation_pd_bidx_map[ptr] < pd_bidx) {
allocation_pd_bidx_map[ptr] = pd_bidx;
}
}
extern "C"
void add_pd_bidy_to_allocation(void* ptr, unsigned pd_bidy) {
if(allocation_pd_bidy_map[ptr] < pd_bidy) {
allocation_pd_bidy_map[ptr] = pd_bidy;
}
}
extern "C"
void add_pd_phi_to_allocation(void* ptr, unsigned pd_phi) {
if(allocation_pd_phi_map[ptr] < pd_phi) {
allocation_pd_phi_map[ptr] = pd_phi;
}
}
extern "C"
unsigned get_pd_bidx(void* ptr) {
/* std::cout << "pd_bidx = " << allocation_pd_bidx_map[ptr] << "\n"; */
return allocation_pd_bidx_map[ptr];
}
extern "C"
unsigned get_pd_bidy(void* ptr) {
/* std::cout << "pd_bidy = " << allocation_pd_bidy_map[ptr] << "\n"; */
return allocation_pd_bidy_map[ptr];
}
extern "C"
unsigned get_pd_phi(void* ptr) {
/* std::cout << "pd_phi = " << allocation_pd_phi_map[ptr] << "\n"; */
return allocation_pd_phi_map[ptr];
}
extern "C"
void print_pd_bidx_map() {
/* std::cout << "bidx map\n"; */
for (auto a = allocation_pd_bidx_map.begin(); a != allocation_pd_bidx_map.end(); a++) {
/* std::cout << a->first << " " << a->second << "\n"; */
}
}
extern "C"
void print_pd_bidy_map() {
/* std::cout << "bidy map\n"; */
for (auto a = allocation_pd_bidy_map.begin(); a != allocation_pd_bidy_map.end(); a++) {
/* std::cout << a->first << " " << a->second << "\n"; */
}
}
extern "C"
void print_pd_phi_map() {
/* std::cout << "phi map\n"; */
for (auto a = allocation_pd_phi_map.begin(); a != allocation_pd_phi_map.end(); a++) {
/* std::cout << a->first << " " << a->second << "\n"; */
}
}
extern "C"
void add_wss_to_map(void *ptr, unsigned long long wss, unsigned aid) {
aid_wss_map[aid] = wss;
if(allocation_wss_map[ptr] < wss) {
allocation_wss_map[ptr] = wss;
}
}
extern "C"
void print_wss_map() {
/* std::cout << "wss map\n"; */
for (auto a = allocation_wss_map.begin(); a != allocation_wss_map.end(); a++) {
/* std::cout << a->first << " " << a->second << "\n"; */
}
}
extern "C"
unsigned long long get_wss(void* ptr) {
return allocation_wss_map[ptr];
}
extern "C"
void print_value_i32(uint64_t value) {
/* std::cout << "value(i32) = " << value << std::endl; */
}
extern "C"
void print_value_i64(uint64_t value) {
/* std::cout << "value(i64) = " << value << std::endl; */
}
extern "C"
void print_value_f32(float value) {
/* std::cout << "value(f32) = " << value << std::endl; */
}
extern "C"
void print_value_f64(double value) {
/* std::cout << "value(f64) = " << value << std::endl; */
}
extern "C"
float compute_access_density(void* ptr, unsigned numThreads, unsigned loopIters, unsigned long long size) {
float ad = ((float)numThreads * (float)loopIters) / (float)size;
/* std::cout << "ad is " << ad << "\n"; */
return ad;
}
bool sortfunc(std::pair<void*, unsigned long long> &a, std::pair<void*, unsigned long long> &b){
return a.second > b.second;
}
bool sortfuncf(std::pair<void*, float> &a, std::pair<void*, float> &b){
return a.second > b.second;
}
bool sortfunc_l_v_u(std::pair<void*, unsigned > &a, std::pair<void*, unsigned > &b){
return a.second < b.second;
}
/* extern "C" */
/* void sort_wss() { */
/* std::copy(allocation_wss_map.begin(), allocation_wss_map.end(),back_inserter<std::vector<std::pair<void*, unsigned long long> > >(wss_vector)); */
/* std::sort(wss_vector.begin(), wss_vector.end(), sortfunc); */
/* std::cout << "sorted wss\n"; */
/* for(auto wss = wss_vector.begin(); wss != wss_vector.end(); wss++) { */
/* std::cout << wss->first << " " << wss->second << "\n"; */
/* } */
/* } */
extern "C"
unsigned long long estimate_working_set2(unsigned long long wss_per_tb, unsigned bdimx, unsigned bdimy) {
/* std::cout << "wss 2 = " << wss_per_tb << std::endl; */
unsigned long long numTBs = (1536)/(bdimx * bdimy);
numTBs = numTBs * 82;
/* std::cout << "wss 2 sum = " << wss_per_tb * numTBs << std::endl; */
return wss_per_tb * numTBs;
}
extern "C"
unsigned estimate_working_set(unsigned long long pd_bidx, unsigned long long pd_bidy, unsigned long long pd_phi, unsigned loopiters, unsigned bdimx, unsigned bdimy, unsigned gdimx, unsigned gdimy) {
unsigned long long max = 0;
if((pd_phi * loopiters) > max) {
max = pd_phi * loopiters;
}
unsigned long long numTBs = (1536)/(bdimx * bdimy);
numTBs = numTBs * 82;
/* std::cout << "bdimx,y,liters = " << bdimx << " " << bdimy << " " << loopiters << "\n"; */
std::cout << "numTBs = " << numTBs << "\n";
unsigned long long concTBx = (gdimx > numTBs) ? numTBs: gdimx;
unsigned long long concTBy = (gdimx > numTBs) ? 1 : (numTBs/gdimx);
if((pd_bidx * concTBx) > max) {
max = pd_bidx * concTBx;
}
if((pd_bidy * concTBy) > max) {
max = pd_bidy * concTBy;
}
std::cout << "params = " << pd_phi << " " << pd_bidx << " " << loopiters << " " << gdimx << " " << concTBx << " " << "\n";
std::cout << "working set = " << max << "\n";
return max;
}
/* float compute_access_pattern_dx(void* ptr, */
extern "C"
void* identify_memory_allocation(void* addr) {
unsigned long long addr_ull = (unsigned long long) addr;
for (auto a = allocation_size_map.begin(); a != allocation_size_map.end(); a++) {
unsigned long long alloc_ull = (unsigned long long) a->first;
unsigned long long size = a->second;
if(alloc_ull < addr_ull && addr_ull < alloc_ull + size) {
return a->first;
}
}
return 0;
}
extern "C"
unsigned estimate_working_set_iteration(unsigned gdimx, unsigned gdimy, unsigned bdimx, unsigned bdimy) {
return 42;
}
extern "C"
void add_aid_pchase_map(unsigned aid, void* addr, bool pchase) {
/* std::cout << "added to pchase map " << aid << " " << addr << "\n"; */
aid_pchase_map[aid] = pchase;
aid_allocation_map[aid] = addr;
}
extern "C"
void add_aid_ac_incomp_map(unsigned aid, bool incomp) {
aid_ac_incomp_map[aid] = incomp;
}
extern "C"
void add_aid_wss_map_iterdep(unsigned aid, unsigned long long wss) {
/* std::cout << "added to iterdep map " << aid << " " << wss << "\n"; */
aid_wss_map_iterdep[aid] = wss;
}
extern "C"
void add_aid_wss_map(unsigned aid, unsigned long long wss) {
aid_wss_map[aid] = wss;
}
extern "C"
void add_aid_ac_map(unsigned aid, unsigned long long ac) {
aid_ac_map[aid] = ac;
}
extern "C"
void add_aid_allocation_map(unsigned aid, void* allocation) {
/* std::cout<< "add_aid_allocation_map " << aid << " " << allocation << std::endl; */
aid_allocation_map[aid] = allocation;
}
extern "C"
void add_aid_invocation_map(unsigned aid, unsigned invocation_id) {
aid_invocation_id_map[aid] = invocation_id;
}
extern "C"
bool is_iterdep_access(unsigned aid) {
return (aid_wss_map_iterdep.find(aid) != aid_wss_map_iterdep.end());
}
extern "C"
void print_aid_wss_map_iterdep() {
/* std::cout << "aid wss map (iterdep)\n"; */
for (auto a = aid_wss_map_iterdep.begin(); a != aid_wss_map_iterdep.end(); a++) {
/* std::cout << a->first << " " << a->second << "\n"; */
}
}
extern "C"
void process_iterdep_access() {
for (auto a = aid_wss_map_iterdep.begin(); a != aid_wss_map_iterdep.end(); a++) {
/* std::cout << a->first << " " << a->second << "\n"; */
}
}
extern "C"
void process_all_accesses() {
}
// this function is for all non-iterative kernels (and non iteration-dependent accesses within iterative kernels)
extern "C"
void perform_memory_management_global() {
// many maps
/* std::cout << "mm global \n"; */
return;
}
extern "C"
void perform_memory_management_iterative() {
std::cout << "mm iterative \n";
is_iterative = true;
/* std::cout << "data from reuse\n"; */
for (auto a = aid_ac_map_reuse.begin(); a != aid_ac_map_reuse.end(); a++) {
/* std::cout << a->first << " " << a->second << " " << aid_allocation_map_reuse[a->first] << std::endl; */
auto alloc = aid_allocation_map_reuse[a->first];
auto invid = aid_invocation_id_map_reuse[a->first];
global_mmg_invid_alloc_list[invid].insert(alloc);
}
/* std::cout << "invid to alloc list\n"; */
/* unsigned max_invid = 0; */
for(auto i = global_mmg_invid_alloc_list.begin(); i != global_mmg_invid_alloc_list.end(); i++) {
/* std::cout << i->first << " : "; */
if(i->first > max_invid) {
max_invid = i->first;
}
for(auto a = i->second.begin(); a != i->second.end(); a++) {
/* std::cout << *a << " "; */
if(global_alloc_firstuse_map.find(*a) == global_alloc_firstuse_map.end()) {
global_alloc_firstuse_map[*a] = i->first;
}
}
/* std::cout << std::endl; */
}
/* std::cout << "max invid = " << max_invid << std::endl; */
/* for each allocation, for each invocation, compute the next reuse */
for(auto alloc = allocation_size_map.begin(); alloc != allocation_size_map.end(); alloc++) {
/* std::cout << alloc->first << std::endl; */
for (auto c = 1; c <= max_invid; c++) {
/* std::cout << c << std::endl; */
unsigned nearest_reuse = 1000 ;
for(auto i = global_mmg_invid_alloc_list.begin(); i != global_mmg_invid_alloc_list.end(); i++) {
if(i->second.find(alloc->first) != i->second.end()) {
/* std::cout << "reuse at " << i->first << std::endl; */
if(i->first > c && i->first < nearest_reuse) {
nearest_reuse = i->first;
}
}
}
/* std::cout << "nearest reuse is " << nearest_reuse << std::endl; */
global_alloc_inv_resinv_map[alloc->first][c] = nearest_reuse;
}
}
// for each invid, list all allocations, sorted by reuse
/* std::cout << " each invid, list all allocations, sorted by reuse\n"; */
for (auto i = global_mmg_invid_alloc_list.begin(); i != global_mmg_invid_alloc_list.end(); i++) {
/* std::cout << i->first << " : "; */
for(auto a = i->second.begin(); a != i->second.end(); a++) {
/* std::cout << *a << " "; */
void* alloc = *a;
unsigned reuse = global_alloc_inv_resinv_map[*a][i->first];
/* std::cout << reuse << " "; */
if(global_alloc_inv_resinv_map[*a][i->first] == 1000) {
/* std::cout << global_alloc_firstuse_map[*a] << " "; */
reuse = global_alloc_firstuse_map[*a] + max_invid;
}
global_invid_sorted_reuse_alloc_map[i->first].push_back(std::pair<void*, unsigned>(alloc, reuse));
/* std::cout << std::endl; */
}
/* global_invid_sorted_reuse_alloc_map[i->first] = sorted_reuse_vector; */
std::sort(global_invid_sorted_reuse_alloc_map[i->first].begin(),
global_invid_sorted_reuse_alloc_map[i->first].end(), sortfunc_l_v_u);
/* std::cout << "sorted by reuse, allocations\n"; */
for(auto ra = global_invid_sorted_reuse_alloc_map[i->first].begin();
ra != global_invid_sorted_reuse_alloc_map[i->first].end(); ra++) {
/* std::cout << ra->first << " " ; */
}
/* std::cout << std::endl; */