-
Notifications
You must be signed in to change notification settings - Fork 15
/
talshc.cpp
6255 lines (6024 loc) · 241 KB
/
talshc.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
/** ExaTensor::TAL-SH: Device-unified user-level C API implementation.
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
-------------------------------------------------------------------
FOR DEVELOPER(s):
# TAL-SH runtime provides a device-kind unified API for performing basic
tensor algebra operations on multicore CPU Host, Nvidia GPU, Intel MIC, etc.
Tensor algebra tasks scheduled on Host are blocking (the scheduling call
returns only after completion of the task). Tensor algebra tasks scheduled
on an accelerator are non-blocking/asynchronous. Each TAL-SH tensor may
be present on multiple devices at a time, where the data transfers and
data consistency are taken care of by the TAL-SH runtime. Underneath,
the TAL-SH runtime dispatches tasks to device-kind specific (lower-level)
runtimes called:
CP-TAL(multicore CPU, synchronous),
NV-TAL (Nvidia GPU, asynchronous),
XP-TAL (Intel MIC, asynchronous),
AM-TAL (AMD GPU, asynchronous),
etc.
Contrary to accelerators, the Host does not have a dedicated runtime layer
for managing resource acquisition, data transfers and consistency, and
asynchronous execution. Except the latter, these functions are delegated
to the TAL-SH layer (resource acquisition, data transfers/consistency).
# Outstanding problems:
1. Tensor body images participating in tensor operations must be marked
as "IN_USE" even if they are not to be discarded because other tensor
operations may mark them "TO_BE_DISCARDED" and then discard them before
the former tensor operation finishes (inter-task data synchronization).
So far it is the user responsibility to avoid race conditions.
2. .data_kind[] array in <talsh_tens_t> is redundant because all images
have the same data kind (because new tensor body images can only be
created in tensor operations). Thus, it can be reduced to a scalar.
Alternatively, by enabling coexistence of images of different data
kinds, the data kind runtime check needs to be implemented in tensor
operations and image selecting functions, with a possible data kind
conversion.
**/
#include "talsh.h"
#include "mem_manager.h"
#include "timer.h"
#include <cstdio>
#include <cstdlib>
#include <ctime>
#ifndef NO_OMP
#include <omp.h>
#endif
#ifdef USE_HIP
#include "talsh_complex.hip.h"
#include "device_algebra.hip.h"
#else
#include "talsh_complex.h"
#include "device_algebra.h"
#endif
//PARAMETERS:
static int VERBOSE=1; //verbosity for errors
static int LOGGING_OPS=0; //logging basic tensor operations: Add, Contract
//GLOBALS:
// General:
int talsh_on=0; //TAL-SH initialization flag (1:initalized; 0:not)
#ifndef NO_OMP
static omp_nest_lock_t talsh_lock; //TAL-SH global lock for thread safety
#endif
clock_t talsh_begin_time; //TAL-SH begin time (zero time reference)
// Accelerator configuration:
int talsh_gpu_beg; //first Nvidia GPU in the assigned range
int talsh_gpu_end; //last Nvidia GPU in the assigned range
// Device status:
int talsh_cpu=DEV_OFF; //current CPU status: {DEV_OFF,DEV_ON,DEV_ON_BLAS}
int talsh_gpu[MAX_GPUS_PER_NODE]={DEV_OFF}; //current GPU status: {DEV_OFF,DEV_ON,DEV_ON_BLAS}
int talsh_mic[MAX_MICS_PER_NODE]={DEV_OFF}; //current MIC status: {DEV_OFF,DEV_ON,DEV_ON_BLAS}
int talsh_amd[MAX_AMDS_PER_NODE]={DEV_OFF}; //current AMD status: {DEV_OFF,DEV_ON,DEV_ON_BLAS}
// Failure statistics:
unsigned long long int not_clean_count=0LL; //number of times a NOT_CLEAN status was returned (possible indication of a memory leak)
//INTERNAL TYPES:
// Host task:
typedef struct{
int task_error; //task error code (-1:empty or in progress; 0:success; >0:error code)
int host_id; //-1:uninitialized (empty task); 0:initialized (non-empty)
unsigned int coherence; //coherence control value
} host_task_t;
//PROTOTYPES OF IMPORTED FUNCTIONS:
extern "C"{
// CP-TAL tensor operations:
int cpu_tensor_block_init(void * dftr, double val_real, double val_imag, int arg_conj);
int cpu_tensor_block_scale(void * dftr, double val_real, double val_imag, int arg_conj);
int cpu_tensor_block_slice(void * lftr, void * dftr, const int * offsets, int accumulative);
int cpu_tensor_block_insert(void * lftr, void * dftr, const int * offsets, int accumulative);
int cpu_tensor_block_copy(const int * contr_ptrn, void * lftr, void * dftr, int arg_conj);
int cpu_tensor_block_add(const int * contr_ptrn, void * lftr, void * dftr,
double scale_real, double scale_imag, int arg_conj);
int cpu_tensor_block_contract(const int * contr_ptrn, void * lftr, void * rftr, void * dftr,
double scale_real, double scale_imag, int arg_conj, int accumulative);
int cpu_tensor_block_decompose_svd(const char absorb, void * dftr, void * lftr, void * rftr, void * sftr);
int cpu_print_stats();
// Contraction pattern conversion:
int talsh_get_contr_ptrn_str2dig(const char * c_str, int * dig_ptrn,
int * drank, int * lrank, int * rrank, int * conj_bits);
// Fortran tensor block aliasing:
int talsh_tensor_f_assoc(const talsh_tens_t * talsh_tens, int image_id, void ** tensF);
int talsh_tensor_f_dissoc(void * tensF);
int talsh_update_f_scalar(void * tensF, int data_kind, void * gmem_p);
}
//PROTOTYPES OF INTERNAL FUNCTIONS:
extern "C"{
// Error counters:
static void talsh_raise_not_clean();
// Tensor body image info (exported to talshf.F90):
int talsh_tensor_image_info(const talsh_tens_t * talsh_tens, //in: TAL-SH tensor block
int image_id, //in: tensor body image id
int * dev_id, //out: flat device id where the image resides
int * data_kind, //out: data kind of the image
void ** gmem_p, //out: global memory pointer to the image location
int * buf_entry); //out: argument buffer entry holding the image (-1:none)
// Discard tensor body images:
static int talsh_tensor_image_discard(talsh_tens_t * talsh_tens, int image_id);
static int talsh_tensor_image_discard_other(talsh_tens_t * talsh_tens, int image_id);
// Choose an appropriate tensor body image to use in a tensor operation:
static int talsh_choose_image_for_device(talsh_tens_t * tens, unsigned int coh_ctrl, int * copied, int dvk, int dvn = DEV_NULL);
// Host task API:
static int host_task_create(host_task_t ** host_task);
static int host_task_clean(host_task_t * host_task);
static int host_task_is_empty(const host_task_t * host_task);
static int host_task_record(host_task_t * host_task, unsigned int coh_ctrl, unsigned int error_code);
static int host_task_status(host_task_t * host_task);
static int host_task_error_code(const host_task_t * host_task);
static int host_task_destroy(host_task_t * host_task);
static void host_task_print(const host_task_t * host_task);
// C tensor block aliasing:
static int talsh_tensor_c_assoc(const talsh_tens_t * talsh_tens, int image_id, tensBlck_t ** tensC);
static int talsh_tensor_c_dissoc(tensBlck_t * tensC);
// Additional TAL-SH tensor API:
static int talshTensorIsHealthy(const talsh_tens_t * talsh_tens);
// Additional TAL-SH task API:
static int talshTaskConstruct(talsh_task_t * talsh_task, int dev_kind, int coh_ctrl, int data_kind = NO_TYPE);
static int talshTaskSetArg(talsh_task_t * talsh_task, talsh_tens_t * talsh_tens_p, int image_id);
static int talshTaskFinalize(talsh_task_t * talsh_task, int task_status);
}
//INTERNAL FUNCTIONS:
// Error counters:
static void talsh_raise_not_clean(){++not_clean_count;}
// Host task API:
static int host_task_create(host_task_t ** host_task)
/** Creates an empty (clean) Host task. **/
{
*host_task=(host_task_t*)malloc(sizeof(host_task_t));
if(*host_task == NULL) return TRY_LATER;
return host_task_clean(*host_task);
}
static int host_task_clean(host_task_t * host_task)
{
if(host_task == NULL) return TALSH_INVALID_ARGS;
host_task->task_error=-1;
host_task->host_id=-1;
return TALSH_SUCCESS;
}
static int host_task_is_empty(const host_task_t * host_task)
/** Returns YEP if the Host task is empty, NOPE otherwise, unless an error occurs. **/
{
if(host_task == NULL) return TALSH_INVALID_ARGS;
if(host_task->host_id < 0){
if(host_task->task_error >= 0) return TALSH_FAILURE;
return YEP;
}
return NOPE;
}
static int host_task_record(host_task_t * host_task, unsigned int coh_ctrl, unsigned int error_code)
/** Records a Host task. A Host task does not require argument finalization
because it is done by (blocking) CP-TAL and by higher-level TAL-SH runtimes. **/
{
if(host_task == NULL) return TALSH_INVALID_ARGS;
if(host_task_is_empty(host_task) == YEP){
host_task->task_error=(int)error_code; if(host_task->task_error < 0) return TALSH_INTEGER_OVERFLOW;
host_task->host_id=0; //Host device kind comprises only one device (multicore CPU Host #0)
host_task->coherence=coh_ctrl;
}else{
return TALSH_OBJECT_NOT_EMPTY;
}
return TALSH_SUCCESS;
}
static int host_task_status(host_task_t * host_task)
{
int errc;
if(host_task == NULL) return TALSH_INVALID_ARGS;
errc=host_task_is_empty(host_task);
if(errc == NOPE){
if(host_task->task_error == 0){
return TALSH_TASK_COMPLETED;
}else if(host_task->task_error > 0){
return TALSH_TASK_ERROR;
}
}else if(errc == YEP){
return TALSH_TASK_EMPTY;
}else{
return TALSH_FAILURE;
}
return TALSH_TASK_SCHEDULED;
}
static int host_task_error_code(const host_task_t * host_task)
{return host_task->task_error;}
static int host_task_destroy(host_task_t * host_task)
{
if(host_task == NULL) return TALSH_INVALID_ARGS;
free(host_task);
return TALSH_SUCCESS;
}
static void host_task_print(const host_task_t * host_task)
/** Prints Host task info. **/
{
#pragma omp flush
if(host_task != NULL){
printf("#MESSAGE: Printing Host task info:\n");
printf(" Host task status : %d\n",host_task->task_error);
printf(" Host task device id : %d\n",host_task->host_id);
printf(" Host task coherence_var: %u\n",host_task->coherence);
printf("#END OF MESSAGE\n");
}
return;
}
// Tensor image API:
int talsh_tensor_image_info(const talsh_tens_t * talsh_tens, int image_id,
int * dev_id, int * data_kind, void ** gmem_p, int * buf_entry)
/** Returns the information on a specific tensor body image. A return status
TALSH_NOT_ALLOWED indicates that the image is no longer available (discarded). **/
{
talsh_dev_rsc_t *drsc;
if(talsh_tens == NULL) return TALSH_INVALID_ARGS;
if(talshTensorIsEmpty(talsh_tens) != NOPE) return TALSH_OBJECT_IS_EMPTY;
if(talshTensorIsHealthy(talsh_tens) != YEP) return TALSH_FAILURE;
if(image_id < 0 || image_id >= talsh_tens->ndev) return TALSH_INVALID_ARGS;
drsc=&(talsh_tens->dev_rsc[image_id]);
if(tensDevRsc_is_empty(drsc) != NOPE) return TALSH_FAILURE;
if(talsh_tens->avail[image_id] == YEP){
*data_kind=talsh_tens->data_kind[image_id];
*dev_id=drsc->dev_id; //flat device id
*gmem_p=drsc->gmem_p;
*buf_entry=drsc->buf_entry;
}else{
return TALSH_NOT_ALLOWED; //image is no longer available (to be discarded)
}
return TALSH_SUCCESS;
}
static int talsh_tensor_image_discard(talsh_tens_t * talsh_tens, int image_id)
/** Discards a specific tensor body image. A return status TALSH_NOT_ALLOWED
indicates that this is the last available image and it cannot be released. **/
{
int i,n,errc;
if(talsh_tens == NULL) return TALSH_INVALID_ARGS;
if(talshTensorIsEmpty(talsh_tens) != NOPE) return TALSH_OBJECT_IS_EMPTY;
if(talshTensorIsHealthy(talsh_tens) != YEP) return TALSH_FAILURE;
if(image_id < 0 || image_id >= talsh_tens->ndev) return TALSH_INVALID_ARGS;
n=0; for(i=0;i<talsh_tens->ndev;++i){if(i != image_id && talsh_tens->avail[i] == YEP) ++n;}
if(n == 0) return TALSH_NOT_ALLOWED; //at least one tensor body image must exist, otherwise just destroy the tensor
errc=tensDevRsc_release_all(&(talsh_tens->dev_rsc[image_id]));
if(errc != 0 && errc != NOT_CLEAN) errc=TALSH_FAILURE;
if(image_id < talsh_tens->ndev-1){
talsh_tens->dev_rsc[image_id]=talsh_tens->dev_rsc[talsh_tens->ndev-1];
talsh_tens->data_kind[image_id]=talsh_tens->data_kind[talsh_tens->ndev-1];
talsh_tens->avail[image_id]=talsh_tens->avail[talsh_tens->ndev-1];
}
--(talsh_tens->ndev);
return errc;
}
static int talsh_tensor_image_discard_other(talsh_tens_t * talsh_tens, int image_id)
/** Discards all other tensor body images except the one specified. **/
{
int i,j,errc;
if(talsh_tens == NULL) return TALSH_INVALID_ARGS;
if(talshTensorIsEmpty(talsh_tens) != NOPE) return TALSH_OBJECT_IS_EMPTY;
if(talshTensorIsHealthy(talsh_tens) != YEP) return TALSH_FAILURE;
if(image_id < 0 || image_id >= talsh_tens->ndev) return TALSH_INVALID_ARGS;
if(talsh_tens->avail[image_id] != YEP) return TALSH_NOT_ALLOWED; //at least one tensor body image must exist, otherwise just destroy the tensor
errc=TALSH_SUCCESS;
for(i=0;i<talsh_tens->ndev;++i){
if(i != image_id){
j=tensDevRsc_release_all(&(talsh_tens->dev_rsc[i]));
if(j != 0){if(j == NOT_CLEAN){if(errc == TALSH_SUCCESS) errc=j;}else{errc=TALSH_FAILURE;}}
}else{
if(image_id > 0){
talsh_tens->dev_rsc[0]=talsh_tens->dev_rsc[image_id];
talsh_tens->data_kind[0]=talsh_tens->data_kind[image_id];
talsh_tens->avail[0]=talsh_tens->avail[image_id];
}
}
}
talsh_tens->ndev=1;
return errc;
}
static int talsh_tensor_c_assoc(const talsh_tens_t * talsh_tens, //in: TAL-SH tensor
int image_id, //in: id of the tensor body image to be used
tensBlck_t ** tensC) //out: newly created <tensBlck_t> object
/** Creates a <tensBlck_t> object for a specific image of <talsh_tens>.
A return status TRY_LATER indicates temporary shortage in available resources.
A return status TALSH_NOT_ALLOWED indicates that the requested image is no longer available
(marked to be discarded). **/
{
int i,errc;
tensBlck_t *ctens;
talsh_dev_rsc_t *src_rsc_p;
if(talsh_on == 0) return TALSH_NOT_INITIALIZED;
if(talsh_tens == NULL) return TALSH_INVALID_ARGS;
if(talshTensorIsEmpty(talsh_tens) != NOPE) return TALSH_OBJECT_IS_EMPTY;
if(talshTensorIsHealthy(talsh_tens) != YEP) return TALSH_FAILURE;
if(image_id < 0 || image_id >= talsh_tens->ndev) return TALSH_INVALID_ARGS;
if(tens_valid_data_kind(talsh_tens->data_kind[image_id]) != YEP) return TALSH_FAILURE;
if(talsh_tens->avail[image_id] == YEP){
src_rsc_p=&(talsh_tens->dev_rsc[image_id]);
errc=tensBlck_create(&ctens); if(errc){if(errc != TRY_LATER) errc=TALSH_FAILURE; return errc;}
errc=tensBlck_construct(ctens,YEP,talsh_tens->shape_p->num_dim,talsh_tens->shape_p->dims, //YEP: shape in pinned memory
talsh_tens->shape_p->divs,talsh_tens->shape_p->grps);
if(errc){if(errc != TRY_LATER) errc=TALSH_FAILURE; i=tensBlck_destroy(ctens); return errc;}
errc=tensBlck_attach_body(ctens,talsh_tens->data_kind[image_id],src_rsc_p->dev_id,src_rsc_p->gmem_p,src_rsc_p->buf_entry);
if(errc){if(errc != TRY_LATER) errc=TALSH_FAILURE; i=tensBlck_destroy(ctens); return errc;}
*tensC=ctens; //tensC has the right shape, data_kind, and source data
}else{
return TALSH_NOT_ALLOWED; //image is no longer available (to be discarded)
}
return TALSH_SUCCESS;
}
static int talsh_tensor_c_dissoc(tensBlck_t * tensC) //inout: <tensBlck_t> created by <talsh_tensor_c_assoc()>
/** Destroys the <tensBlck_t> object created by <talsh_tensor_c_assoc()>. **/
{
int errc;
if(talsh_on == 0) return TALSH_NOT_INITIALIZED;
if(tensC == NULL) return TALSH_INVALID_ARGS;
errc=TALSH_SUCCESS;
if(tensBlck_volume(tensC) > 0){
errc=tensBlck_destroy(tensC); if(errc){if(errc != NOT_CLEAN) errc=TALSH_FAILURE;}
}
return errc;
}
int talshDetermineOptimalDevice(const talsh_tens_t * tens0, const talsh_tens_t * tens1, const talsh_tens_t * tens2)
/** Given tensor arguments, returns a flat id of the most appropriate device
based on the data residence, tensor sizes, and current device occupation.
A negative return status indicates an error. **/
{
int i,j,devid,al,am,as,good_for_gpu,ov[3][TALSH_MAX_DEV_PRESENT],ovl[3];
const int idx[3][3]={-1,0,1, 0,-1,2, 1,2,-1};
size_t s[3];
double gflops;
gflops=0.0; good_for_gpu=0;
s[0]=0; if(tens0 != NULL){s[0]=talshTensorVolume(tens0);}else{return DEV_NULL;}
s[1]=0; if(tens1 != NULL) s[1]=talshTensorVolume(tens1);
s[2]=0; if(tens2 != NULL) s[2]=talshTensorVolume(tens2);
if(s[0] > 0 && s[1] > 0 && s[2] > 0){
gflops=sqrt(((double)(s[0]))*((double)(s[1]))*((double)(s[2])))/(1e9); //FMA GFlop (no FMA or complex factors)
if(gflops > (double)(TALSH_GFLOP_THRESH_GPU)) good_for_gpu=1;
}
devid=DEV_NULL; for(i=0;i<3;++i) ovl[i]=0;
//Overlap tens0 and tens1:
if(s[0] > 0 && s[1] > 0){
for(i=0;i<tens0->ndev;++i){
if(tens0->avail[i] == YEP){
for(j=0;j<tens1->ndev;++j){
if(tens1->avail[j] == YEP){
if(tens1->dev_rsc[j].dev_id == tens0->dev_rsc[i].dev_id){
if(tens1->dev_rsc[j].dev_id != talshFlatDevId(DEV_HOST,0) || good_for_gpu == 0){
ov[0][(ovl[0])++]=tens1->dev_rsc[j].dev_id;
}
}
}
}
}
}
if(s[2] > 0){
for(j=0;j<tens2->ndev;++j){
if(tens2->avail[j] == YEP){
for(i=0;i<ovl[0];++i){
if(tens2->dev_rsc[j].dev_id == ov[0][i]) return ov[0][i]; //triple match
}
}
}
}
}
//Overlap tens0 and tens2:
if(s[0] > 0 && s[2] > 0){
for(i=0;i<tens0->ndev;++i){
if(tens0->avail[i] == YEP){
for(j=0;j<tens2->ndev;++j){
if(tens2->avail[j] == YEP){
if(tens2->dev_rsc[j].dev_id == tens0->dev_rsc[i].dev_id){
if(tens2->dev_rsc[j].dev_id != talshFlatDevId(DEV_HOST,0) || good_for_gpu == 0){
ov[1][(ovl[1])++]=tens2->dev_rsc[j].dev_id;
}
}
}
}
}
}
if(s[1] > 0){
for(j=0;j<tens1->ndev;++j){
if(tens1->avail[j] == YEP){
for(i=0;i<ovl[1];++i){
if(tens1->dev_rsc[j].dev_id == ov[1][i]) return ov[1][i]; //triple match
}
}
}
}
}
//Overlap tens1 and tens2:
if(s[1] > 0 && s[2] > 0){
for(i=0;i<tens1->ndev;++i){
if(tens1->avail[i] == YEP){
for(j=0;j<tens2->ndev;++j){
if(tens2->avail[j] == YEP){
if(tens2->dev_rsc[j].dev_id == tens1->dev_rsc[i].dev_id){
if(tens2->dev_rsc[j].dev_id != talshFlatDevId(DEV_HOST,0) || good_for_gpu == 0){
ov[2][(ovl[2])++]=tens2->dev_rsc[j].dev_id;
}
}
}
}
}
}
if(s[0] > 0){
for(j=0;j<tens0->ndev;++j){
if(tens0->avail[j] == YEP){
for(i=0;i<ovl[2];++i){
if(tens0->dev_rsc[j].dev_id == ov[2][i]) return ov[2][i]; //triple match
}
}
}
}
}
//No triple match happened => communication is necessary.
//Order the arguments by size (#al >= #am >= #as):
if(s[1] >= s[2]){al=1; am=2;}else{al=2; am=1;}
if(s[0] >= al){
as=am; am=al; al=0;
}else{
if(s[0] >= am){as=am; am=0;}else{as=0;}
}
//Find the optimal device to minimize the communication:
if(s[al] > s[am] + s[as]){
i=idx[al][am]; if(ovl[i] > 0) return ov[i][0]; //Large/medium overlap
i=idx[al][as]; if(ovl[i] > 0) return ov[i][0]; //Large/small overlap
}else{
i=idx[al][am]; if(ovl[i] > 0) return ov[i][0]; //Large/medium overlap
i=idx[al][as]; if(ovl[i] > 0) return ov[i][0]; //Large/small overlap
i=idx[am][as]; if(ovl[i] > 0) return ov[i][0]; //Medium/small overlap
}
switch(al){ //Large does not overlap with other arguments
case 0: devid=tens0->dev_rsc[0].dev_id; break;
case 1: devid=tens1->dev_rsc[0].dev_id; break;
case 2: devid=tens2->dev_rsc[0].dev_id; break;
}
if(devid == talshFlatDevId(DEV_HOST,0) && good_for_gpu){
i=talshDeviceBusyLeast(DEV_NVIDIA_GPU); if(i >= 0 && i < DEV_MAX) return i;
i=talshDeviceBusyLeast(DEV_INTEL_MIC); if(i >= 0 && i < DEV_MAX) return i;
i=talshDeviceBusyLeast(DEV_AMD_GPU); if(i >= 0 && i < DEV_MAX) return i;
}
return devid;
}
static int talsh_choose_image_for_device(talsh_tens_t * tens, unsigned int coh_ctrl, int * copied, int dvk, int dvn)
/** For a given execution device <[dvk,dvn]>, chooses the most appropriate
tensor body image to be used on that device. Priority is given to the
same device, then to the same device kind, then to the Host. If no image
is found in that sequence, a blocking copy will be posted to the Host,
thus creating an additional image of the tensor body (on Host).
A negative return code indicates an error. **/
{
int i,image_id,host_image,dn,dk,coh;
*copied=0; image_id=-1; host_image=-1;
if(tens == NULL) return -1;
if(talshTensorIsEmpty(tens) != NOPE) return -2;
if(talshTensorIsHealthy(tens) != YEP) return -3;
for(i=0;i<tens->ndev;++i){
if(tens->avail[i] == YEP){
dn=talshKindDevId(tens->dev_rsc[i].dev_id,&dk); if(dn < 0) return -4;
if(dk == dvk){image_id=i; if(dn == dvn) return image_id;}
if(dk == DEV_HOST) host_image=i;
}
}
if(image_id < 0){
if(host_image < 0){ //create an image on Host
switch(coh_ctrl){
case COPY_D: coh=COPY_M; break;
case COPY_M: coh=COPY_M; break;
case COPY_T: coh=COPY_K; break;
case COPY_K: coh=COPY_K; break;
default: return -5;
}
i=talshTensorPlace(tens,0,DEV_HOST,NULL,coh); if(i != TALSH_SUCCESS) return -6;
*copied=1; image_id=tens->ndev-1; //newly added Host image is the last
if(tens->dev_rsc[image_id].dev_id != talshFlatDevId(DEV_HOST,0)) return -7; //trap
}else{
image_id=host_image;
}
}
return image_id;
}
//EXPORTED FUNCTIONS:
// TAL-SH helper functions:
int talsh_tens_no_init(const talsh_tens_data_t * tens_data,
const talsh_tens_shape_t * tens_shape,
const talsh_tens_signature_t * tens_signa)
{
return TALSH_SUCCESS;
}
int talshValidDataKind(int datk, int * datk_size)
/** Returns YEP if <datk> is a valid data kind (also returns its size in bytes in <datk_size>). **/
{
return tens_valid_data_kind(datk,datk_size);
}
// TAL-SH control API:
int talshInit(size_t * host_buf_size, //inout: Host Argument Buffer size in bytes (in: suggested; out: actual)
int * host_arg_max, //out: Max number of arguments that can fit into the Host Argument Buffer
int ngpus, int gpu_list[], //in: number of Nvidia GPU(s) to use and the list of Nvidia GPU(s) to use
int nmics, int mic_list[], //in: number of Intel Xeon Phi(s) to use and the list of Intel Xeon Phi(s) to use
int namds, int amd_list[]) //in: number of AMD GPU(s) to use and the list of AMD GPU(s) to use
/** Initializes the TAL-SH runtime. **/
{
int i,j,gpu_beg,gpu_end,errc;
#pragma omp flush
if(talsh_on) return TALSH_ALREADY_INITIALIZED;
//CPU Host:
#ifndef NO_BLAS
talsh_cpu=DEV_ON_BLAS;
#else
talsh_cpu=DEV_ON;
#endif
//NVidia GPU accelerators:
#ifndef NO_GPU
if(ngpus > 0){
if(ngpus > MAX_GPUS_PER_NODE) return TALSH_INVALID_ARGS;
gpu_beg=gpu_list[0]; gpu_end=gpu_list[ngpus-1]; //`Allow for non-consecutive GPU ranges in arg_buf_allocate()
if(gpu_beg < 0 || gpu_beg >= MAX_GPUS_PER_NODE) return TALSH_INVALID_ARGS;
if(gpu_end < 0 || gpu_end >= MAX_GPUS_PER_NODE) return TALSH_INVALID_ARGS;
for(i=1;i<ngpus;i++){
if(gpu_list[i] != gpu_list[i-1]+1){
printf("#FATAL(TALSH::talshInit): The current version only supports consecutive GPU ranges!");
return TALSH_FAILURE;
}
}
}else{
#endif
gpu_beg=0; gpu_end=-1;
#ifndef NO_GPU
}
#endif
//Intel Xeon Phi accelerators:
#ifndef NO_PHI
if(nmics > 0){
printf("#FATAL(TALSH::talshInit): Intel Xeon Phi is not fully supported yet!");
return TALSH_NOT_IMPLEMENTED; //`Future
}
#endif
//AMD GPU accelerators:
#ifndef NO_AMD
if(namds > 0){
printf("#FATAL(TALSH::talshInit): AMD GPU is not supported yet!");
return TALSH_NOT_IMPLEMENTED; //`Future
}
#endif
errc=arg_buf_allocate(host_buf_size,host_arg_max,gpu_beg,gpu_end);
if(errc){
printf("#ERROR(talshInit): arg_buf_allocate error %d\n",errc);
return TALSH_FAILURE;
}
if(*host_buf_size >= TALSH_CPTAL_MIN_BUF_SIZE){ //Host argument buffer is big enough to be used in CP-TAL
talshSetMemAllocPolicyHost(TALSH_MEM_ALLOC_POLICY_HOST,TALSH_MEM_ALLOC_FALLBACK_HOST,&errc);
if(errc != 0){
printf("#FATAL(TALSH::talshInit): Host memory allocation policy setting failed: Error %d",errc);
return TALSH_FAILURE;
}
}
#ifndef NO_GPU
for(i=0;i<ngpus;i++){
j=gpu_list[i]; if(j < 0 || j >= MAX_GPUS_PER_NODE) return TALSH_INVALID_ARGS;
talsh_gpu[j]=gpu_is_mine(j);
}
#endif
talsh_gpu_beg=gpu_beg; talsh_gpu_end=gpu_end;
#ifndef NO_OMP
omp_init_nest_lock(&talsh_lock);
#endif
talsh_on=1; talsh_begin_time=clock();
#pragma omp flush
return TALSH_SUCCESS;
}
int talshShutdown()
/** Shuts down the TAL-SH runtime. **/
{
int i,errc;
#pragma omp flush
if(talsh_on == 0) return TALSH_NOT_INITIALIZED;
talshSetMemAllocPolicyHost(TALSH_MEM_ALLOC_POLICY_HOST,TALSH_MEM_ALLOC_FALLBACK_HOST,&i);
errc=arg_buf_deallocate(talsh_gpu_beg,talsh_gpu_end);
talsh_gpu_beg=0; talsh_gpu_end=-1; talsh_on=0;
talsh_cpu=DEV_OFF;
for(i=0;i<MAX_GPUS_PER_NODE;i++) talsh_gpu[i]=DEV_OFF;
for(i=0;i<MAX_MICS_PER_NODE;i++) talsh_mic[i]=DEV_OFF;
for(i=0;i<MAX_AMDS_PER_NODE;i++) talsh_amd[i]=DEV_OFF;
#ifndef NO_OMP
omp_destroy_nest_lock(&talsh_lock);
#endif
#pragma omp flush
if(errc) return TALSH_FAILURE;
return TALSH_SUCCESS;
}
int talshEnableFastMath(int dev_kind, int dev_id)
/** Enable fast math on a given device. **/
{
int errc;
#pragma omp flush
errc=TALSH_SUCCESS;
switch(dev_kind){
case DEV_NVIDIA_GPU:
#ifndef NO_GPU
if(dev_id >= 0){
errc=gpu_enable_fast_math(dev_id);
}else{
errc=gpu_enable_fast_math();
}
#endif
break;
default:
errc=TALSH_NOT_AVAILABLE;
}
#pragma omp flush
return errc;
}
int talshDisableFastMath(int dev_kind, int dev_id)
/** Disable fast math on a given device. **/
{
int errc;
#pragma omp flush
errc=TALSH_SUCCESS;
switch(dev_kind){
case DEV_NVIDIA_GPU:
#ifndef NO_GPU
if(dev_id >= 0){
errc=gpu_disable_fast_math(dev_id);
}else{
errc=gpu_disable_fast_math();
}
#endif
break;
default:
errc=TALSH_NOT_AVAILABLE;
}
#pragma omp flush
return errc;
}
int talshQueryFastMath(int dev_kind, int dev_id)
/** Query fast math on a given device. **/
{
int ans;
#pragma omp flush
ans=NOPE;
switch(dev_kind){
case DEV_NVIDIA_GPU:
#ifndef NO_GPU
ans=gpu_query_fast_math(dev_id);
#endif
break;
}
return ans;
}
int talshDeviceCount(int dev_kind, int * dev_count)
/** Returns the total number of devices of specific kind found on node. **/
{
int errc;
errc=TALSH_SUCCESS; *dev_count=0;
switch(dev_kind){
case DEV_HOST:
*dev_count=1; //CPU Host is always assumed a single device (multicore)
break;
case DEV_NVIDIA_GPU:
#ifndef NO_GPU
errc=gpu_get_device_count(dev_count);
if(errc != 0) errc=TALSH_FAILURE;
#endif
break;
case DEV_INTEL_MIC:
errc=TALSH_NOT_IMPLEMENTED;
break;
case DEV_AMD_GPU:
errc=TALSH_NOT_IMPLEMENTED;
break;
}
return errc;
}
int talshFlatDevId(int dev_kind, //in: device kind
int dev_num) //in: device Id within its kind (0..MAX)
/** Converts a kind-specific device Id into the flat device Id.
DEV_MAX return status indicates invalidity of the arguments. **/
{
return encode_device_id(dev_kind,dev_num); //Success:[0..DEV_MAX-1]; Failure: DEV_MAX
}
int talshKindDevId(int dev_id, //in: flat device Id: [0:DEV_MAX-1]
int * dev_kind) //out: device kind
/** Converts a flat device Id into the kind specific device Id.
A negative return value indicates an invalid flat device Id. **/
{
return decode_device_id(dev_id,dev_kind);
}
int talshDeviceState(int dev_num, //in: either a flat or kind specific (when <dev_kind> is present) device id
int dev_kind) //in: device kind (note that it changes the meaning of the <dev_num> argument)
/** Returns device state (Success:[DEV_OFF,DEV_ON,DEV_ON_BLAS]) **/
{
int devk,i,sts;
if(talsh_on == 0) return TALSH_NOT_INITIALIZED;
if(dev_kind == DEV_NULL){
i=talshKindDevId(dev_num,&devk);
if(i < 0) return TALSH_INVALID_ARGS;
}else{
devk=dev_kind;
i=dev_num;
}
switch(devk){
case DEV_HOST:
sts=talsh_cpu;
break;
case DEV_NVIDIA_GPU:
sts=talsh_gpu[i];
break;
case DEV_INTEL_MIC:
sts=talsh_mic[i];
break;
case DEV_AMD_GPU:
sts=talsh_amd[i];
break;
default:
return TALSH_INVALID_ARGS;
}
return sts;
}
int talshDeviceState_(int dev_num, int dev_kind) //Fortran wrapper
{
return talshDeviceState(dev_num,dev_kind);
}
int talshDeviceBusyLeast(int dev_kind) //in: device kind (defaults to any kind)
/** Returns the least busy flat device id. **/
{
int i;
if(talsh_on == 0) return TALSH_NOT_INITIALIZED;
switch(dev_kind){
case DEV_NULL:
return talshFlatDevId(DEV_HOST,0);
case DEV_HOST:
return talshFlatDevId(DEV_HOST,0);
case DEV_NVIDIA_GPU:
#ifndef NO_GPU
i=gpu_busy_least();
if(i < 0 || i >= MAX_GPUS_PER_NODE) return TALSH_FAILURE;
return talshFlatDevId(DEV_NVIDIA_GPU,i);
#else
return TALSH_NOT_AVAILABLE;
#endif
case DEV_INTEL_MIC:
#ifndef NO_PHI
return TALSH_NOT_IMPLEMENTED; //`Implement in future
#else
return TALSH_NOT_AVAILABLE;
#endif
case DEV_AMD_GPU:
#ifndef NO_AMD
return TALSH_NOT_IMPLEMENTED; //`Implement in future
#else
return TALSH_NOT_AVAILABLE;
#endif
}
return TALSH_INVALID_ARGS;
}
int talshDeviceBusyLeast_(int dev_kind) //Fortran wrapper
{
return talshDeviceBusyLeast(dev_kind);
}
size_t talshDeviceMemorySize(int dev_num,
int dev_kind)
{
int devk,i;
size_t bytes;
bytes=0;
if(talsh_on != 0){
if(dev_kind == DEV_NULL){
i=talshKindDevId(dev_num,&devk); if(i < 0) return 0;
}else{
devk=dev_kind;
i=dev_num;
}
switch(devk){
case DEV_HOST:
//`Implement
break;
case DEV_NVIDIA_GPU:
#ifndef NO_GPU
bytes=gpu_device_memory_size(i);
#endif
break;
case DEV_INTEL_MIC:
#ifndef NO_PHI
//`Implement
#endif
break;
case DEV_AMD_GPU:
#ifndef NO_AMD
//`Implement
#endif
break;
}
}
return bytes;
}
size_t talshDeviceMemorySize_(int dev_num, int dev_kind) //Fortran wrapper
{
return talshDeviceMemorySize(dev_num,dev_kind);
}
size_t talshDeviceBufferSize(int dev_num,
int dev_kind)
{
int devk,i;
size_t bytes;
bytes=0;
if(talsh_on != 0){
if(dev_kind == DEV_NULL){
i=talshKindDevId(dev_num,&devk); if(i < 0) return bytes;
}else{
devk=dev_kind;
i=dev_num;
}
switch(devk){
case DEV_HOST:
bytes = get_arg_buf_size_host();
break;
case DEV_NVIDIA_GPU:
#ifndef NO_GPU
bytes = get_arg_buf_size_gpu(i);
#endif
break;
case DEV_INTEL_MIC:
#ifndef NO_PHI
//`Implement
#endif
break;
case DEV_AMD_GPU:
#ifndef NO_AMD
//`Implement
#endif
break;
}
}
return bytes;
}
size_t talshDeviceBufferSize_(int dev_num, int dev_kind) //Fortran wrapper
{
return talshDeviceBufferSize(dev_num,dev_kind);
}
size_t talshDeviceTensorSize(int dev_num,
int dev_kind)
/** Returns the max size (bytes) of a tensor that
can fit in the argument buffer of a given device. **/
{
int devk,i;
size_t bytes;
bytes=0;
if(talsh_on != 0){
if(dev_kind == DEV_NULL){
i=talshKindDevId(dev_num,&devk); if(i < 0) return 0;
}else{
devk=dev_kind;
i=dev_num;
}
switch(devk){
case DEV_HOST:
bytes=get_blck_max_size_host();
break;
case DEV_NVIDIA_GPU:
#ifndef NO_GPU
bytes=get_blck_max_size_gpu(i);
#endif
break;
case DEV_INTEL_MIC:
#ifndef NO_PHI
//`Implement
#endif
break;
case DEV_AMD_GPU:
#ifndef NO_AMD
//`Implement
#endif
break;
}
}
return bytes;
}
size_t talshDeviceTensorSize_(int dev_num, int dev_kind) //Fortran wrapper
{
return talshDeviceTensorSize(dev_num,dev_kind);
}
size_t talshDeviceBufferFreeSize(int dev_num,
int dev_kind)
/** Returns the amount of free memory (bytes)
available in an argument buffer on a given device. **/
{
int dev_id,errc;
size_t bytes;
bytes=0;
if(talsh_on != 0){
if(dev_kind != DEV_NULL){
dev_id=talshFlatDevId(dev_kind,dev_num);
}else{
dev_id=dev_num;
}
errc=mem_free_left(dev_id,&bytes); if(errc != 0) bytes=0;
}
return bytes;
}
size_t talshDeviceBufferFreeSize_(int dev_num, int dev_kind) //Fortran wrapper
{
return talshDeviceBufferFreeSize(dev_num,dev_kind);
}
void * talshDeviceBufferBasePtr(int dev_num, int dev_kind)
{
void * base_ptr = NULL;
#pragma omp flush
if(talsh_on != 0){
if(dev_kind == DEV_NULL) dev_num=talshKindDevId(dev_num,&dev_kind);
switch(dev_kind){
case DEV_HOST:
base_ptr=get_arg_buf_ptr_host();
break;
case DEV_NVIDIA_GPU:
#ifndef NO_GPU
base_ptr=get_arg_buf_ptr_gpu(dev_num);
#endif
break;