-
Notifications
You must be signed in to change notification settings - Fork 15
/
mem_manager.cpp
1809 lines (1731 loc) · 54.4 KB
/
mem_manager.cpp
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
/** Explicit memory management for the accelerator-enabled
implementation of the tensor algebra library TAL-SH:
CP-TAL (TAL for CPU), NV-TAL (TAL for NVidia GPU),
XP-TAL (TAL for Intel Xeon Phi), AM-TAL (TAL for AMD GPU).
REVISION: 2021/12/29
Copyright (C) 2014-2022 Dmitry I. Lyakh (Liakh)
Copyright (C) 2014-2022 Oak Ridge National Laboratory (UT-Battelle)
LICENSE: BSD 3-Clause
-------------------------------------------------------------------
OPTIONS (PREPROCESSOR):
# -DNO_GPU: disables GPU usage.
# -DNO_PHI: disables Intel MIC usage (future).
# -DNO_AMD: disables AMD GPU usage (future).
FOR DEVELOPERS ONLY:
# So far each argument buffer entry is occupied as a whole,
making it impossible to track the actual amount of memory
requested by the application. This needs to be fixed.
**/
#include "mem_manager.h"
#include "device_algebra.h"
#include "tensor_algebra.h"
#include <cstdio>
#include <cstdlib>
#include <ctime>
#ifndef NO_OMP
#include <omp.h>
#endif
#define GPU_MEM_PART_USED 90 //percentage of free GPU global memory to be actually allocated for GPU argument buffers
#define MEM_ALIGN GPU_CACHE_LINE_LEN //memory alignment (in bytes) for argument buffers
//Host argument buffer structure (adjust TALSH_NO_HOST_BUFFER in talsh.h as well):
#define BLCK_BUF_DEPTH_HOST 13 //number of distinct tensor block buffer levels on Host
#define BLCK_BUF_TOP_HOST 3 //number of argument buffer entries of the largest size (level 0) on Host: multiple of 3
#define BLCK_BUF_BRANCH_HOST 2 //branching factor for each subsequent buffer level on Host
//GPU argument buffer structure:
#define BLCK_BUF_DEPTH_GPU 12 //number of distinct tensor block buffer levels on GPU
#define BLCK_BUF_TOP_GPU 6 //number of argument buffer entries of the largest size (level 0) on GPU: multiple of 3
#define BLCK_BUF_BRANCH_GPU 2 //branching factor for each subsequent buffer level on GPU
static int VERBOSE=1; //verbosity (for errors)
static int DEBUG=0; //debugging
static int LOGGING=0; //logging
//DERIVED TYPES:
// Argument buffer configuration:
typedef struct{
int buf_top; //amount of top-level blocks (of the largest size)
int buf_depth; //number of levels
int buf_branch; //branching factor for each subsequent level
} ab_conf_t;
//MODULE DATA:
// Buffer memory management:
#ifndef NO_OMP
static omp_nest_lock_t mem_lock; //global lock for serializing memory allocation/deallocation in buffers
#endif
int bufs_ready=0; //status of the Host and GPU argument buffers
ab_conf_t ab_conf_host; //Host argument buffer configuration
ab_conf_t ab_conf_gpu[MAX_GPUS_PER_NODE]; //GPU argument buffer configuration (for each GPU)
void *arg_buf_host; //base address of the argument buffer in Host memory (page-locked)
void *arg_buf_gpu[MAX_GPUS_PER_NODE]; //base addresses of argument buffers in GPUs Global memories
size_t arg_buf_host_size=0; //total size of the Host argument buffer in bytes
size_t arg_buf_gpu_size[MAX_GPUS_PER_NODE]; //total sizes of each GPU argument buffer in bytes
int max_args_host=0; //max number of arguments (those of the lowest size level) which can reside in Host buffer
int max_args_gpu[MAX_GPUS_PER_NODE]; //max number of arguments (those of the lowest size level) which can reside in a GPU buffer
size_t blck_sizes_host[BLCK_BUF_DEPTH_HOST]; //distinct tensor block buffered sizes (in bytes) on Host
size_t blck_sizes_gpu[MAX_GPUS_PER_NODE][BLCK_BUF_DEPTH_GPU]; //distinct tensor block buffered sizes (in bytes) on GPUs
int const_args_link[MAX_GPUS_PER_NODE][MAX_GPU_ARGS]; //linked list of free entries in constant memory banks for each GPU
int const_args_ffe[MAX_GPUS_PER_NODE]; //FFE of the const_args_link[] for each GPU
size_t *abh_occ=NULL; //occupation status for each buffer entry in Host argument buffer (*arg_buf_host)
size_t *abg_occ[MAX_GPUS_PER_NODE]; //occupation status for each buffer entry in GPU argument buffers (*arg_buf_gpu)
size_t abh_occ_size=0; //total number of entries in the multi-level Host argument buffer occupancy table
size_t abg_occ_size[MAX_GPUS_PER_NODE]; //total numbers of entries in the multi-level GPUs argument buffer occupancy tables
// Buffer memory status:
int num_args_host=0; //number of occupied entries in the Host argument buffer
int num_args_gpu[MAX_GPUS_PER_NODE]={0}; //number of occupied entries in each GPU argument buffer
size_t occ_size_host=0; //total size (bytes) of all occupied entries in the Host argument buffer
size_t occ_size_gpu[MAX_GPUS_PER_NODE]={0}; //total size (bytes) of all occupied entries in each GPU buffer
size_t args_size_host=0; //total size (bytes) of all arguments in the Host argument buffer !`Not used now
size_t args_size_gpu[MAX_GPUS_PER_NODE]={0}; //total size (bytes) of all arguments in each GPU buffer !`Not used now
// Slab for multi-index storage (pinned Host memory):
int miBank[MAX_GPU_ARGS*MAX_MLNDS_PER_TENS][MAX_TENSOR_RANK]; //All active .dims[], .divs[], .grps[], .prmn[] will be stored here
int miFreeHandle[MAX_GPU_ARGS*MAX_MLNDS_PER_TENS]; //free entries for storing multi-indices
int miFFE=0; //number of free handles left in miBank
//LOCAL (PRIVATE) FUNCTION PROTOTYPES:
static int const_args_link_init(int gpu_beg, int gpu_end);
static int ab_get_2d_pos(ab_conf_t ab_conf, int entry_num, int *level, int *offset);
static int ab_get_1d_pos(ab_conf_t ab_conf, int level, int offset);
static int ab_get_parent(ab_conf_t ab_conf, int level, int offset);
static int ab_get_1st_child(ab_conf_t ab_conf, int level, int offset);
static size_t ab_get_offset(ab_conf_t ab_conf, int level, int offset, const size_t *blck_sizes);
static int get_buf_entry(ab_conf_t ab_conf, size_t bsize, void *arg_buf_ptr, size_t *ab_occ, size_t ab_occ_size,
const size_t *blck_sizes, char **entry_ptr, int *entry_num);
static int free_buf_entry(ab_conf_t ab_conf, size_t *ab_occ, size_t ab_occ_size, const size_t *blck_sizes, int entry_num);
static void ab_conf_print(ab_conf_t ab_conf);
static int mi_entry_init();
static int mi_entry_stop();
//------------------------------------------------------------------------------------------------------------------------
//FUNCTION DEFINITIONS:
static int ab_get_2d_pos(ab_conf_t ab_conf, int entry_num, int *level, int *offset)
/** Given an argument buffer entry number, this function returns the
corresponding buffer level and offset within that level **/
{
int i,j,k,m;
if(entry_num >= 0){
m=ab_conf.buf_top; k=0; j=m;
for(i=0;i<ab_conf.buf_depth;i++){
if(entry_num<j){*level=i; *offset=entry_num-k; return 0;};
m*=ab_conf.buf_branch; k=j; j=k+m;
}
return 1; //entry number is out of range
}else{
return 2;
}
}
static int ab_get_1d_pos(ab_conf_t ab_conf, int level, int offset)
/** Given a buffer level and offset within it,
this function returns the plain buffer entry number **/
{
int i,j,k;
if(level >= 0 && level < ab_conf.buf_depth && offset >= 0){
if(level == 0) return offset; j=ab_conf.buf_top; k=ab_conf.buf_top;
for(i=1;i<ab_conf.buf_depth;i++){k*=ab_conf.buf_branch; if(i==level) break; j+=k;}
if(offset < k){return j+offset;}else{return -1;}
}else{
return -2; //invalid buffer level
}
}
static int ab_get_parent(ab_conf_t ab_conf, int level, int offset)
/** This function returns the offset of the parent of a given buffer entry {level, offset} **/
{
if(level >= 0 && level < ab_conf.buf_depth && offset >= 0 && ab_conf.buf_branch > 0){
return offset/ab_conf.buf_branch;
}else{
return -1;
}
}
static int ab_get_1st_child(ab_conf_t ab_conf, int level, int offset)
{
/** This function returns the offset of the 1st child for a given buffer entry {level, offset} **/
if(level >= 0 && level < ab_conf.buf_depth && offset >= 0 && ab_conf.buf_branch > 0){
return offset*ab_conf.buf_branch;
}else{
return -1;
}
}
static size_t ab_get_offset(ab_conf_t ab_conf, int level, int offset, const size_t *blck_sizes)
/** This function returns a byte offset in the argument buffer space
corresponding to a given buffer entry {level, offset}.
Note that the base address of the argument buffer must be added a posteriori!
No arguments bounds check here! **/
{
int i,j;
size_t ab_offset=0;
ab_offset=offset*blck_sizes[level]; j=offset;
for(i=level;i>0;i--){
j=ab_get_parent(ab_conf,i,j);
ab_offset+=(blck_sizes[i-1]%ab_conf.buf_branch)*j;
}
return ab_offset;
}
int arg_buf_allocate(size_t *arg_buf_size, int *arg_max, int gpu_beg, int gpu_end)
/** This function initializes all argument buffers on the Host and GPUs in the range [gpu_beg..gpu_end].
INPUT:
# arg_buf_size - requested size of the page-locked Host argument buffer in bytes;
# [gpu_beg..gpu_end] - range of GPUs assigned to the current MPI process;
OUTPUT:
# arg_buf_size - actual size of the allocated page-locked Host argument buffer in bytes;
# arg_max - max number of arguments the Host buffer can contain (those of the lowest size level).
**/
{
size_t hsize,total,mem_alloc_dec;
int i,j,err_code;
const char *err_msg;
#ifndef NO_GPU
cudaError_t err=cudaSuccess;
#endif
#pragma omp flush
if(bufs_ready != 0) return 1; //buffers are already allocated
#ifndef NO_OMP
omp_init_nest_lock(&mem_lock);
#endif
*arg_max=0; abh_occ=NULL; abh_occ_size=0; max_args_host=0; arg_buf_host_size=0;
for(i=0;i<MAX_GPUS_PER_NODE;i++){abg_occ[i]=NULL; abg_occ_size[i]=0; max_args_gpu[i]=0; arg_buf_gpu_size[i]=0;}
//Allocate the Host argument buffer:
mem_alloc_dec=MEM_ALIGN*BLCK_BUF_TOP_HOST; for(i=1;i<BLCK_BUF_DEPTH_HOST;i++) mem_alloc_dec*=BLCK_BUF_BRANCH_HOST;
hsize=*arg_buf_size; hsize-=hsize%mem_alloc_dec; err_code=1;
while(hsize > mem_alloc_dec){
#ifndef NO_GPU
err=cudaHostAlloc(&arg_buf_host,hsize,cudaHostAllocPortable);
if(err != cudaSuccess){
hsize-=mem_alloc_dec;
}else{
*arg_buf_size=hsize; arg_buf_host_size=hsize; err_code=0;
if(DEBUG) printf("\n#DEBUG(mem_manager:arg_buf_allocate): Pinned Host argument buffer address/size: %p %lu\n",arg_buf_host,hsize); //debug
break;
}
#else
arg_buf_host=malloc(hsize);
if(arg_buf_host == NULL){
hsize-=mem_alloc_dec;
}else{
*arg_buf_size=hsize; arg_buf_host_size=hsize; err_code=0;
if(DEBUG) printf("\n#DEBUG(mem_manager:arg_buf_allocate): Host buffer address/size: %p %lu\n",arg_buf_host,hsize); //debug
break;
}
#endif /*NO_GPU*/
}
if(err_code == 0){
//Store Host argument buffer configuration:
ab_conf_host.buf_top=BLCK_BUF_TOP_HOST; ab_conf_host.buf_depth=BLCK_BUF_DEPTH_HOST; ab_conf_host.buf_branch=BLCK_BUF_BRANCH_HOST;
//Set buffered block sizes hierarchy (buffer levels) for the Host argument buffer:
hsize=BLCK_BUF_TOP_HOST; max_args_host=BLCK_BUF_TOP_HOST; blck_sizes_host[0]=arg_buf_host_size/BLCK_BUF_TOP_HOST;
for(i=1;i<BLCK_BUF_DEPTH_HOST;i++){
blck_sizes_host[i]=blck_sizes_host[i-1]/BLCK_BUF_BRANCH_HOST; max_args_host*=BLCK_BUF_BRANCH_HOST;
hsize+=max_args_host;
}
*arg_max=max_args_host;
//Initialize the Host argument buffer occupancy tables:
abh_occ=(size_t*)malloc(hsize*sizeof(size_t)); if(abh_occ == NULL) return 2; //Host buffer occupancy table
abh_occ_size=hsize;
for(hsize=0;hsize<abh_occ_size;hsize++){abh_occ[hsize]=0;} //initialize zero occupancy for each buffer entry
num_args_host=0; occ_size_host=0; args_size_host=0; //clear Host memory statistics
//Initialize the multi-index entry bank (slab) in pinned Host memory:
err_code=mi_entry_init(); if(err_code) return 3;
#ifndef NO_GPU
//Allocate GPUs buffers, if needed:
if(gpu_beg >= 0 && gpu_end >= gpu_beg){ //GPU exist for this MPI process
err=cudaGetDeviceCount(&i); if(err != cudaSuccess) return 6;
if(gpu_end < MAX_GPUS_PER_NODE && gpu_end < i){
err_code=init_gpus(gpu_beg,gpu_end); if(err_code < 0) return 7;
// Constant memory banks for all GPUs:
err_code=const_args_link_init(gpu_beg,gpu_end); if(err_code != 0) return 8;
// Global memory banks for each GPU:
mem_alloc_dec=MEM_ALIGN*BLCK_BUF_TOP_GPU; for(i=1;i<BLCK_BUF_DEPTH_GPU;i++) mem_alloc_dec*=BLCK_BUF_BRANCH_GPU;
for(i=gpu_beg;i<=gpu_end;i++){
if(gpu_is_mine(i) != 0){ //Initialize only my GPUs
err=cudaSetDevice(i); if(err != cudaSuccess) return 9;
err=cudaGetLastError(); //check for pre-existing CUDA errors for this GPU
if(err != cudaSuccess){
err_msg=cudaGetErrorString(err);
if(VERBOSE) printf("#ERROR(TALSH:mem_manager:arg_buf_allocate): CUDA pre-error: %s\n",err_msg);
return 10;
}
err=cudaMemGetInfo(&hsize,&total);
if(err != cudaSuccess){
err_msg=cudaGetErrorString(err);
if(VERBOSE) printf("#ERROR(TALSH:mem_manager:arg_buf_allocate): CUDA error: %s\n",err_msg);
return 10;
}
hsize=(size_t)(float(hsize)/100.0f*float(GPU_MEM_PART_USED)); hsize-=hsize%mem_alloc_dec; err_code=1;
while(hsize > mem_alloc_dec){
err=cudaMalloc(&arg_buf_gpu[i],hsize);
if(err != cudaSuccess){
hsize-=mem_alloc_dec;
}else{
arg_buf_gpu_size[i]=hsize; err_code=0;
if(DEBUG) printf("\n#DEBUG(mem_manager:arg_buf_allocate): GPU#%d argument buffer address/size: %p %lu\n",i,arg_buf_gpu[i],hsize); //debug
break;
}
}
if(err_code == 0){
// Store GPU argument buffer configuration:
ab_conf_gpu[i].buf_top=BLCK_BUF_TOP_GPU; ab_conf_gpu[i].buf_depth=BLCK_BUF_DEPTH_GPU; ab_conf_gpu[i].buf_branch=BLCK_BUF_BRANCH_GPU;
// Set buffered block sizes hierarchy (buffer levels) for each GPU argument buffer:
hsize=BLCK_BUF_TOP_GPU; max_args_gpu[i]=BLCK_BUF_TOP_GPU; blck_sizes_gpu[i][0]=arg_buf_gpu_size[i]/BLCK_BUF_TOP_GPU;
for(j=1;j<BLCK_BUF_DEPTH_GPU;j++){
blck_sizes_gpu[i][j]=blck_sizes_gpu[i][j-1]/BLCK_BUF_BRANCH_GPU; max_args_gpu[i]*=BLCK_BUF_BRANCH_GPU;
hsize+=max_args_gpu[i];
}
// Initialize each GPU argument buffer occupancy tables:
abg_occ[i]=(size_t*)malloc(hsize*sizeof(size_t)); if(abg_occ[i] == NULL) return 12; //GPU#i buffer occupancy table
abg_occ_size[i]=hsize;
for(hsize=0;hsize<abg_occ_size[i];hsize++){abg_occ[i][hsize]=0;} //initialize each buffer entry to zero occupancy
num_args_gpu[i]=0; occ_size_gpu[i]=0; args_size_gpu[i]=0; //clear GPU memory statistics
}else{
return 13;
}
}
}
}else{
return 14;
}
}
#endif /*NO_GPU*/
}else{
if(VERBOSE) printf("#ERROR(arg_buf_allocate): Host buffer memory allocation failed: Size = %zu\n",hsize);
return 15;
}
bufs_ready=1; //mark the Host and GPU argument buffers as ready
#pragma omp flush
return 0;
}
int arg_buf_deallocate(int gpu_beg, int gpu_end)
/** This function deallocates all argument buffers on the Host and GPUs in the range [gpu_beg..gpu_end] **/
{
int i,err_code;
#ifndef NO_GPU
cudaError_t err=cudaSuccess;
#endif
#pragma omp flush
if(bufs_ready == 0) return -1; //buffers are not allocated
#ifndef NO_OMP
omp_set_nest_lock(&mem_lock);
#endif
#pragma omp flush
err_code=0;
if(abh_occ != NULL) free(abh_occ); abh_occ=NULL; abh_occ_size=0; max_args_host=0;
for(i=0;i<MAX_GPUS_PER_NODE;i++){
if(abg_occ[i] != NULL) free(abg_occ[i]); abg_occ[i]=NULL; abg_occ_size[i]=0; max_args_gpu[i]=0;
}
arg_buf_host_size=0; num_args_host=0; occ_size_host=0; args_size_host=0; //clear Host memory statistics
i=mi_entry_stop(); if(i != 0) err_code+=100000; //deactivate multi-index bank
#ifndef NO_GPU
err=cudaFreeHost(arg_buf_host);
if(err != cudaSuccess){
if(VERBOSE) printf("\n#ERROR(mem_manager:arg_buf_deallocate): Host argument buffer deallocation failed!");
err_code+=1000;
}
if(gpu_beg >= 0 && gpu_end >= gpu_beg){
for(i=gpu_beg;i<=gpu_end;i++){
if(i < MAX_GPUS_PER_NODE){
if(gpu_is_mine(i) != 0){
err=cudaSetDevice(i); if(err == cudaSuccess){
arg_buf_gpu_size[i]=0; num_args_gpu[i]=0; occ_size_gpu[i]=0; args_size_gpu[i]=0; //clear GPU memory statistics
err=cudaFree(arg_buf_gpu[i]);
if(err != cudaSuccess){
if(VERBOSE) printf("\n#ERROR(mem_manager:arg_buf_deallocate): GPU# %d argument buffer deallocation failed!",i);
err_code++;
}
}else{
if(VERBOSE) printf("\n#ERROR(mem_manager:arg_buf_deallocate): Unable to set GPU# %d!",i);
err_code++;
}
}
}else{
err_code++;
}
}
i=free_gpus(gpu_beg,gpu_end); if(i != 0) err_code+=100;
}
#else
free(arg_buf_host); arg_buf_host=NULL;
#endif /*NO_GPU*/
bufs_ready=0;
#pragma omp flush
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
omp_destroy_nest_lock(&mem_lock);
#endif
return err_code;
}
int arg_buf_clean_host()
/** Returns zero if all entries of the Host argument buffer are free.
The first buffer entry, which is not free, will cause positive return status.
Negative return status means that an error occurred. **/
{
#ifndef NO_OMP
omp_set_nest_lock(&mem_lock);
#endif
#pragma omp flush
if(bufs_ready == 0){ //memory buffers are not initialized
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return -1;
}
for(size_t i=0;i<abh_occ_size;i++){
if(abh_occ[i] != 0){
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return (int)(i+1);
}
}
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return 0;
}
#ifndef NO_GPU
int arg_buf_clean_gpu(int gpu_num)
/** Returns zero if all entries of the GPU#gpu_num argument buffer are free.
The first buffer entry, which is not free, will cause positive return status.
Negative return status means that an error occurred. **/
{
#ifndef NO_OMP
omp_set_nest_lock(&mem_lock);
#endif
#pragma omp flush
if(bufs_ready == 0){ //memory buffers are not initialized
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return -1;
}
if(gpu_num >= 0 && gpu_num < MAX_GPUS_PER_NODE){
if(gpu_is_mine(gpu_num) != 0){
for(size_t i=0;i<abg_occ_size[gpu_num];i++){
if(abg_occ[gpu_num][i] != 0){
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return (int)(i+1);
}
}
}else{
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return -2;
}
}else{
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return -3; //invalid GPU number
}
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return 0;
}
#endif /*NO_GPU*/
void * get_arg_buf_ptr_host()
{
#pragma omp flush
if(bufs_ready == 0) return NULL;
return arg_buf_host;
}
#ifndef NO_GPU
void * get_arg_buf_ptr_gpu(int gpu_num)
{
#pragma omp flush
if(bufs_ready == 0) return NULL;
if(gpu_num < 0 || gpu_num >= MAX_GPUS_PER_NODE) return NULL;
if(gpu_is_mine(gpu_num) == 0) return NULL;
return arg_buf_gpu[gpu_num];
}
#endif /*NO_GPU*/
size_t get_arg_buf_size_host()
{
#pragma omp flush
if(bufs_ready == 0) return 0;
return arg_buf_host_size;
}
#ifndef NO_GPU
size_t get_arg_buf_size_gpu(int gpu_num)
{
#pragma omp flush
if(bufs_ready == 0) return 0;
if(gpu_num < 0 || gpu_num >= MAX_GPUS_PER_NODE) return 0;
if(gpu_is_mine(gpu_num) == 0) return 0;
return arg_buf_gpu_size[gpu_num];
}
#endif /*NO_GPU*/
size_t get_blck_max_size_host()
{
#pragma omp flush
if(bufs_ready == 0) return 0;
return blck_sizes_host[0];
}
#ifndef NO_GPU
size_t get_blck_max_size_gpu(int gpu_num)
{
#pragma omp flush
if(bufs_ready == 0) return 0;
if(gpu_num < 0 || gpu_num >= MAX_GPUS_PER_NODE) return 0;
if(gpu_is_mine(gpu_num) == 0) return 0;
return blck_sizes_gpu[gpu_num][0];
}
#endif /*NO_GPU*/
int get_blck_buf_sizes_host(size_t *blck_sizes)
/** This function returns the registered block (buffered) sizes for each level of the Host argument buffer.
Negative return status means that an error occurred. **/
{
#pragma omp flush
if(bufs_ready == 0) return -1;
for(int i=0;i<BLCK_BUF_DEPTH_HOST;i++){blck_sizes[i]=blck_sizes_host[i];}
return BLCK_BUF_DEPTH_HOST; //depth of the argument buffer
}
#ifndef NO_GPU
int get_blck_buf_sizes_gpu(int gpu_num, size_t *blck_sizes)
/** This function returns the registered block (buffered) sizes for each level of the GPU#gpu_num argument buffer.
Negative return status means that an error occurred. **/
{
#pragma omp flush
if(bufs_ready == 0) return -1;
if(gpu_num >= 0 && gpu_num < MAX_GPUS_PER_NODE){
if(gpu_is_mine(gpu_num) != 0){
for(int i=0;i<BLCK_BUF_DEPTH_GPU;i++){blck_sizes[i]=blck_sizes_gpu[gpu_num][i];}
}else{
return -2;
}
}else{
return -3;
}
return BLCK_BUF_DEPTH_GPU; //depth of the argument buffer
}
#endif /*NO_GPU*/
void print_blck_buf_sizes_host()
{
int dpth,i;
size_t bsz[BLCK_BUF_DEPTH_HOST];
#pragma omp flush
printf("\n#INFO(TALSH:mem_manager): Host Buffer structure:\n");
printf(" Host Buffer base address: %p\n",arg_buf_host);
printf(" Host Buffer size (bytes): %zu\n",arg_buf_host_size);
printf(" Block sizes (bytes) at levels:\n");
fflush(stdout);
dpth=get_blck_buf_sizes_host(bsz);
for(i=0;i<dpth;++i) printf(" Level %d: %zu\n",i,bsz[i]);
fflush(stdout);
return;
}
static int get_buf_entry(ab_conf_t ab_conf, size_t bsize, void *arg_buf_ptr, size_t *ab_occ, size_t ab_occ_size,
const size_t *blck_sizes, char **entry_ptr, int *entry_num)
/** This function finds an appropriate argument buffer entry in any given argument buffer **/
{
int i,j,k,l,m,n;
size_t bsz;
#ifndef NO_OMP
omp_set_nest_lock(&mem_lock);
#endif
#pragma omp flush
if(DEBUG){
printf("\n#DEBUG(mem_manager:get_buf_entry): %lu %lu\n",bsize,blck_sizes[0]); //debug
printf("\n#DEBUG(mem_manager:get_buf_entry): Occupancy table:"); //debug
for(bsz=0;bsz<ab_occ_size;++bsz) printf(" %lu",ab_occ[bsz]); //debug
}
*entry_ptr=NULL; *entry_num=-1;
n=0; j=0; i=0; l=0; //l is a base offset within level i
while(i<ab_conf.buf_depth){ //argument buffer level
if(i > 0){k=ab_conf.buf_branch;}else{k=ab_conf.buf_top;};
j=l%k; l-=j; j+=n;
while(j<k){ //(l+j) is an offset within level i
m=ab_get_1d_pos(ab_conf,i,l+j);
if(m < 0 || m >= ab_occ_size){ //m is an absolute offset in an occupancy table
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return 1;
}
//if(DEBUG) printf("\n#DEBUG(mem_manager:get_buf_entry): Current level/offset/sizes: %d %d %lu\n",i,l+j,blck_sizes[i]); //debug
if(bsize <= blck_sizes[i]-ab_occ[m]){ //there is a good chance to find a free entry along this path
if(i == ab_conf.buf_depth-1 && ab_occ[m] == 0){
*entry_num=m; *entry_ptr=&(((char*)arg_buf_ptr)[ab_get_offset(ab_conf,i,l+j,blck_sizes)]); //entry found
break;
}else{
if(blck_sizes[i+1] < bsize && ab_occ[m] == 0){
*entry_num=m; *entry_ptr=&(((char*)arg_buf_ptr)[ab_get_offset(ab_conf,i,l+j,blck_sizes)]); //entry found
break;
}else{
if(i < ab_conf.buf_depth-1){if(blck_sizes[i+1] >= bsize) break;} //initiate passing to the next level
}
}
}
j++; //horizontal shift
} //enddo j
if(*entry_num >= 0) break; //entry found
if(j < k){ //proceed to the next level
l=ab_get_1st_child(ab_conf,i,l+j);
if(l < 0 || l >= ab_occ_size){
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return 2;
}
i++; n=0; //go to the next level
}else{ //back to the upper level
if(i > 0){
l=ab_get_parent(ab_conf,i,l);
if(l < 0 || l >= ab_occ_size){
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return 3;
}
i--; n=1; //go back to the previous level
}else{
break;
}
}
} //enddo i
if(*entry_num >= 0 && *entry_num < ab_occ_size){
bsz=blck_sizes[i]; ab_occ[m]=bsz;
while(i>0){ //modify occupancy of the upper-level parental entries
l=ab_get_parent(ab_conf,i,l); i--; m=ab_get_1d_pos(ab_conf,i,l);
if(m < 0 || m >= ab_occ_size){
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return 4;
}
ab_occ[m]+=bsz;
}
}else{ //no appropriate entry found: not an error
if(bsize > blck_sizes[0]){
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return DEVICE_UNABLE; //device memory buffer can never provide such a big chunk
}else{
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return TRY_LATER; //device memory buffer currently cannot provide the requested memory chunk due to occupation
}
}
#pragma omp flush
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return 0;
}
static int free_buf_entry(ab_conf_t ab_conf, size_t *ab_occ, size_t ab_occ_size, const size_t *blck_sizes, int entry_num)
/** This function releases an argument buffer entry in any given argument buffer **/
{
int i,j,k,m;
size_t bsz;
#ifndef NO_OMP
omp_set_nest_lock(&mem_lock);
#endif
#pragma omp flush
k=ab_get_2d_pos(ab_conf,entry_num,&i,&j);
if(k != 0){
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return 1;
}
if(ab_occ[entry_num] == blck_sizes[i]){ //buffer entries are always occupied as a whole
bsz=blck_sizes[i]; ab_occ[entry_num]=0;
while(i>0){ //modify occupancy of the upper-level parental entries
j=ab_get_parent(ab_conf,i,j); i--; m=ab_get_1d_pos(ab_conf,i,j);
if(m < 0 || m >= ab_occ_size){
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return 2;
}
ab_occ[m]-=bsz;
}
}else{
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
if(VERBOSE){
if(ab_occ[entry_num] == 0){
printf("#ERROR(TAL-SH:mem_manager:free_buf_entry): Attempt to free an empty buffer entry %d\n",entry_num);
}else{
printf("#ERROR(TAL-SH:mem_manager:free_buf_entry): Partially occupied buffer entry detected: %zu < %zu\n",
ab_occ[entry_num],blck_sizes[i]);
}
}
return 3;
}
#pragma omp flush
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return 0;
}
int get_buf_entry_host(size_t bsize, char **entry_ptr, int *entry_num)
/** This function returns a pointer to a free argument buffer space in the Host argument buffer.
INPUT:
# bsize - requested size of a tensor block (in bytes);
OUTPUT:
# entry_ptr - pointer to a free space in the argument buffer where the tensor block or packet can be put;
# entry_num - entry number corresponding to the free space assigned to the tensor block or packet;
RETURN STATUS:
# 0 - success (*entry_num>=0, *entry_ptr!=NULL);
# TRY_LATER - the argument buffer currently does not have enough space left;
# DEVICE_UNABLE - the argument buffer can never satisfy this request;
# Other - an error occurred.
**/
{
int i,j,err_code;
ab_conf_t ab_conf;
#ifndef NO_OMP
omp_set_nest_lock(&mem_lock);
#endif
#pragma omp flush
if(bufs_ready == 0){
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return -1;
}
err_code=0;
ab_conf.buf_top=BLCK_BUF_TOP_HOST; ab_conf.buf_depth=BLCK_BUF_DEPTH_HOST; ab_conf.buf_branch=BLCK_BUF_BRANCH_HOST;
if(DEBUG) printf("\n#DEBUG(mem_manager:get_buf_entry_host): Allocating buffer entry for size %lu: ",bsize); //debug
err_code=get_buf_entry(ab_conf,bsize,arg_buf_host,abh_occ,abh_occ_size,blck_sizes_host,entry_ptr,entry_num);
if(DEBUG) printf("Status %d: Buffer entry %d: Address %p\n",err_code,*entry_num,*entry_ptr); //debug
if(err_code == 0){
err_code=ab_get_2d_pos(ab_conf,*entry_num,&i,&j);
if(err_code == 0){num_args_host++; occ_size_host+=blck_sizes_host[i]; args_size_host+=bsize;}
}
if(LOGGING && err_code == 0){
printf("\n#DEBUG(TALSH:mem_manager): Host Buffer alloc %lu B -> Entry %d: Buffer use = %lu B\n",bsize,*entry_num,occ_size_host);
fflush(stdout);
}
#pragma omp flush
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return err_code;
}
int free_buf_entry_host(int entry_num)
/** This function releases a Host argument buffer entry.
INPUT:
# entry_num - argument buffer entry number.
**/
{
int i,j,err_code;
ab_conf_t ab_conf;
#ifndef NO_OMP
omp_set_nest_lock(&mem_lock);
#endif
#pragma omp flush
if(bufs_ready == 0){
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return -1;
}
err_code=0;
ab_conf.buf_top=BLCK_BUF_TOP_HOST; ab_conf.buf_depth=BLCK_BUF_DEPTH_HOST; ab_conf.buf_branch=BLCK_BUF_BRANCH_HOST;
if(DEBUG) printf("\n#DEBUG(mem_manager:free_buf_entry_host): Deallocating buffer entry %d: ",entry_num); //debug
err_code=free_buf_entry(ab_conf,abh_occ,abh_occ_size,blck_sizes_host,entry_num);
if(DEBUG) printf("Status %d\n",err_code); //debug
if(err_code == 0){
err_code=ab_get_2d_pos(ab_conf,entry_num,&i,&j);
if(err_code == 0){num_args_host--; occ_size_host-=blck_sizes_host[i]; args_size_host=0;} //`args_size_host is not used (ignore it)
}
if(LOGGING && err_code == 0){
printf("\n#DEBUG(TALSH:mem_manager): Host Buffer free -> Entry %d: Buffer use = %lu B\n",entry_num,occ_size_host);
fflush(stdout);
}
#pragma omp flush
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return err_code;
}
#ifndef NO_GPU
int get_buf_entry_gpu(int gpu_num, size_t bsize, char **entry_ptr, int *entry_num)
/** This function returns a pointer to a free argument buffer space in the GPU#gpu_num argument buffer.
INPUT:
# gpu_num - GPU number;
# bsize - requested size of a tensor block (in bytes);
OUTPUT:
# entry_ptr - pointer to a free space in the argument buffer where the tensor block elements can be put;
# entry_num - entry number corresponding to the free space assigned to the tensor block elements.
RETURN STATUS:
# 0 - success (*entry_num>=0, *entry_ptr!=NULL);
# TRY_LATER - the argument buffer currently does not have enough space left;
# DEVICE_UNABLE - the argument buffer can never satisfy this request;
# Other - an error occurred.
**/
{
int i,j,err_code;
ab_conf_t ab_conf;
#ifndef NO_OMP
omp_set_nest_lock(&mem_lock);
#endif
#pragma omp flush
if(bufs_ready == 0){
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return -1;
}
err_code=0;
if(gpu_num >= 0 && gpu_num < MAX_GPUS_PER_NODE){
if(gpu_is_mine(gpu_num) != 0){
ab_conf.buf_top=BLCK_BUF_TOP_GPU; ab_conf.buf_depth=BLCK_BUF_DEPTH_GPU; ab_conf.buf_branch=BLCK_BUF_BRANCH_GPU;
err_code=get_buf_entry(ab_conf,bsize,arg_buf_gpu[gpu_num],abg_occ[gpu_num],abg_occ_size[gpu_num],&blck_sizes_gpu[gpu_num][0],entry_ptr,entry_num);
if(err_code == 0 && DEBUG != 0) printf("\n#DEBUG(mem_manager:get_buf_entry_gpu): Entry allocated: %d %d %p\n",gpu_num,*entry_num,*entry_ptr); //debug
if(err_code == 0){
err_code=ab_get_2d_pos(ab_conf,*entry_num,&i,&j);
if(err_code == 0){num_args_gpu[gpu_num]++; occ_size_gpu[gpu_num]+=blck_sizes_gpu[gpu_num][i]; args_size_gpu[gpu_num]+=bsize;}
}
if(LOGGING && err_code == 0){
printf("\n#DEBUG(TALSH:mem_manager): GPU %d Buffer alloc %lu B -> Entry %d: Buffer use = %lu B\n",gpu_num,bsize,*entry_num,occ_size_gpu[gpu_num]);
fflush(stdout);
}
}else{
err_code=-2;
}
}else{
err_code=-3;
}
#pragma omp flush
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return err_code;
}
int free_buf_entry_gpu(int gpu_num, int entry_num)
/** This function releases a GPU#gpu_num argument buffer entry.
INPUT:
# gpu_num - GPU number;
# entry_num - argument buffer entry number.
**/
{
int i,j,err_code;
ab_conf_t ab_conf;
#ifndef NO_OMP
omp_set_nest_lock(&mem_lock);
#endif
#pragma omp flush
if(bufs_ready == 0){
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return -1;
}
err_code=0;
if(gpu_num >= 0 && gpu_num < MAX_GPUS_PER_NODE){
if(gpu_is_mine(gpu_num) != 0){
ab_conf.buf_top=BLCK_BUF_TOP_GPU; ab_conf.buf_depth=BLCK_BUF_DEPTH_GPU; ab_conf.buf_branch=BLCK_BUF_BRANCH_GPU;
err_code=free_buf_entry(ab_conf,abg_occ[gpu_num],abg_occ_size[gpu_num],&blck_sizes_gpu[gpu_num][0],entry_num);
if(err_code == 0 && DEBUG != 0) printf("\n#DEBUG(mem_manager:free_buf_entry_gpu): Entry deallocated: %d %d\n",gpu_num,entry_num); //debug
if(err_code == 0){
err_code=ab_get_2d_pos(ab_conf,entry_num,&i,&j);
if(err_code == 0){num_args_gpu[gpu_num]--; occ_size_gpu[gpu_num]-=blck_sizes_gpu[gpu_num][i]; args_size_gpu[gpu_num]=0;} //`args_size_gpu is not used here (ignore it)
}
if(LOGGING && err_code == 0){
printf("\n#DEBUG(TALSH:mem_manager): GPU %d Buffer free -> Entry %d: Buffer use = %lu B\n",gpu_num,entry_num,occ_size_gpu[gpu_num]);
fflush(stdout);
}
}else{
err_code=-2;
}
}else{
err_code=-3;
}
#pragma omp flush
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return err_code;
}
static int const_args_link_init(int gpu_beg, int gpu_end)
/** This function initializes the linked list const_args_link[]
for GPU constant memory buffers (for each GPU in the range [gpu_beg..gpu_end]) **/
{
#pragma omp flush
if(gpu_beg >= 0 && gpu_end >= gpu_beg){
for(int gpu_num=gpu_beg;gpu_num<=gpu_end;gpu_num++){
if(gpu_num < MAX_GPUS_PER_NODE){
const_args_ffe[gpu_num]=0; //first free entry for each GPU
for(int i=0;i<MAX_GPU_ARGS;i++) const_args_link[gpu_num][i]=i+1; //linked list of free entries for each GPU
}else{
return 1;
}
}
}
#pragma omp flush
return 0;
}
int const_args_entry_get(int gpu_num, int *entry_num)
/** This function returns the number of a free const_args[] entry for GPU#gpu_num.
TRY_LATER return status means that currently all entries are busy. **/
{
#ifndef NO_OMP
omp_set_nest_lock(&mem_lock);
#endif
#pragma omp flush
*entry_num=-1; if(bufs_ready == 0){
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return -1;
}
if(gpu_num >= 0 && gpu_num < MAX_GPUS_PER_NODE){
if(gpu_is_mine(gpu_num) != 0){
if(const_args_ffe[gpu_num] >= 0 && const_args_ffe[gpu_num] < MAX_GPU_ARGS){ //free entry exists
*entry_num=const_args_ffe[gpu_num];
const_args_ffe[gpu_num]=const_args_link[gpu_num][const_args_ffe[gpu_num]];
}else{ //no free entry is currently available
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return TRY_LATER;
}
}else{
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return -2;
}
}else{
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return -3;
}
#pragma omp flush
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return 0;
}
int const_args_entry_free(int gpu_num, int entry_num)
/** This function frees an entry of const_args[] for GPU#gpu_num **/
{
#ifndef NO_OMP
omp_set_nest_lock(&mem_lock);
#endif
#pragma omp flush
if(bufs_ready == 0){
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return -1;
}
if(gpu_num >= 0 && gpu_num < MAX_GPUS_PER_NODE){
if(gpu_is_mine(gpu_num) != 0){
if(entry_num >= 0 && entry_num < MAX_GPU_ARGS){ //valid entry number
if(const_args_ffe[gpu_num] < MAX_GPU_ARGS && const_args_ffe[gpu_num] >= 0){
const_args_link[gpu_num][entry_num]=const_args_ffe[gpu_num];
}
const_args_ffe[gpu_num]=entry_num;
}else{ //invalid entry number
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return 1;
}
}else{
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return -2;
}
}else{
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return -3;
}
#pragma omp flush
#ifndef NO_OMP
omp_unset_nest_lock(&mem_lock);
#endif
return 0;
}
#endif /*NO_GPU*/
static void ab_conf_print(ab_conf_t ab_conf)
{
printf("\n#INFO: Argument buffer configuration: Top = %d, Depth = %d, Branch factor = %d\n",ab_conf.buf_top,ab_conf.buf_depth,ab_conf.buf_branch);
fflush(stdout);
return;
}
int get_buf_entry_from_address(int dev_id, const void * addr)
/** If the address lies within the device argument buffer, returns
the corresponding argument buffer entry number. Otherwise returns -1.
Other negative integers on return mean an error. **/
{
int i,ben,dev_kind,dev_num,lev;