/
global.npatch.c
2820 lines (2572 loc) · 97.1 KB
/
global.npatch.c
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
#if HAVE_CONFIG_H
# include "config.h"
#endif
/*
* module: global.npatch.c
* author: Jialin Ju
* description: Implements the n-dimensional patch operations:
* - fill patch
* - copy patch
* - scale patch
* - dot patch
* - add patch
*
* DISCLAIMER
*
* This material was prepared as an account of work sponsored by an
* agency of the United States Government. Neither the United States
* Government nor the United States Department of Energy, nor Battelle,
* nor any of their employees, MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR
* ASSUMES ANY LEGAL LIABILITY OR RESPONSIBILITY FOR THE ACCURACY,
* COMPLETENESS, OR USEFULNESS OF ANY INFORMATION, APPARATUS, PRODUCT,
* SOFTWARE, OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD NOT
* INFRINGE PRIVATELY OWNED RIGHTS.
*
*
* ACKNOWLEDGMENT
*
* This software and its documentation were produced with United States
* Government support under Contract Number DE-AC06-76RLO-1830 awarded by
* the United States Department of Energy. The United States Government
* retains a paid-up non-exclusive, irrevocable worldwide license to
* reproduce, prepare derivative works, perform publicly and display
* publicly by or for the US Government, including the right to
* distribute to other US Government contractors.
*/
#if HAVE_MATH_H
# include <math.h>
#endif
#include "message.h"
#include "globalp.h"
#include "armci.h"
#include "ga-papi.h"
#include "ga-wapi.h"
#ifdef MSG_COMMS_MPI
extern ARMCI_Group* ga_get_armci_group_(int);
#endif
/**********************************************************
* n-dimensional utilities *
**********************************************************/
/*\ compute Index from subscript and convert it back to subscript
* in another array
\*/
static void snga_dest_indices(Integer ndims, Integer *los, Integer *blos, Integer *dimss,
Integer ndimd, Integer *lod, Integer *blod, Integer *dimsd)
{
Integer idx = 0, i, factor=1;
for(i=0;i<ndims;i++) {
idx += (los[i] - blos[i])*factor;
factor *= dimss[i];
}
for(i=0;i<ndims;i++) {
lod[i] = idx % dimsd[i] + blod[i];
idx /= dimsd[i];
}
}
/* check if I own data in the patch */
logical pnga_patch_intersect(Integer *lo, Integer *hi,
Integer *lop, Integer *hip, Integer ndim)
{
Integer i;
/* check consistency of patch coordinates */
for(i=0; i<ndim; i++) {
if(hi[i] < lo[i]) return FALSE; /* inconsistent */
if(hip[i] < lop[i]) return FALSE; /* inconsistent */
}
/* find the intersection and update (ilop: ihip, jlop: jhip) */
for(i=0; i<ndim; i++) {
if(hi[i] < lop[i]) return FALSE; /* don't intersect */
if(hip[i] < lo[i]) return FALSE; /* don't intersect */
}
for(i=0; i<ndim; i++) {
lop[i] = GA_MAX(lo[i], lop[i]);
hip[i] = GA_MIN(hi[i], hip[i]);
}
return TRUE;
}
/*\ check if patches are identical
\*/
logical pnga_comp_patch(Integer andim, Integer *alo, Integer *ahi,
Integer bndim, Integer *blo, Integer *bhi)
{
Integer i;
Integer ndim;
if(andim > bndim) {
ndim = bndim;
for(i=ndim; i<andim; i++)
if(alo[i] != ahi[i]) return FALSE;
}
else if(andim < bndim) {
ndim = andim;
for(i=ndim; i<bndim; i++)
if(blo[i] != bhi[i]) return FALSE;
}
else ndim = andim;
for(i=0; i<ndim; i++)
if((alo[i] != blo[i]) || (ahi[i] != bhi[i])) return FALSE;
return TRUE;
}
/* test two GAs to see if they have the same shape */
static logical snga_test_shape(Integer *alo, Integer *ahi, Integer *blo,
Integer *bhi, Integer andim, Integer bndim)
{
Integer i;
if(andim != bndim) return FALSE;
for(i=0; i<andim; i++)
if((ahi[i] - alo[i]) != (bhi[i] - blo[i])) return FALSE;
return TRUE;
}
/**********************************************************
* n-dimensional functions *
**********************************************************/
/*\ COPY A PATCH AND POSSIBLY RESHAPE
*
* . the element capacities of two patches must be identical
* . copy by column order - Fortran convention
\*/
#if HAVE_SYS_WEAK_ALIAS_PRAGMA
# pragma weak wnga_copy_patch = pnga_copy_patch
#endif
void pnga_copy_patch(char *trans,
Integer g_a, Integer *alo, Integer *ahi,
Integer g_b, Integer *blo, Integer *bhi)
{
Integer i, j;
Integer idx, factor;
Integer atype, btype, andim, adims[MAXDIM], bndim, bdims[MAXDIM];
Integer nelem;
Integer atotal, btotal;
Integer los[MAXDIM], his[MAXDIM];
Integer lod[MAXDIM], hid[MAXDIM];
Integer ld[MAXDIM], ald[MAXDIM], bld[MAXDIM];
void *src_data_ptr, *tmp_ptr;
Integer *src_idx_ptr, *dst_idx_ptr;
Integer bvalue[MAXDIM], bunit[MAXDIM];
Integer factor_idx1[MAXDIM], factor_idx2[MAXDIM], factor_data[MAXDIM];
Integer base;
Integer me_a, me_b;
Integer a_grp, b_grp, anproc, bnproc;
Integer num_blocks_a, num_blocks_b/*, chk*/;
int use_put, has_intersection;
int local_sync_begin,local_sync_end;
local_sync_begin = _ga_sync_begin; local_sync_end = _ga_sync_end;
_ga_sync_begin = 1; _ga_sync_end=1; /*remove any previous masking*/
a_grp = pnga_get_pgroup(g_a);
b_grp = pnga_get_pgroup(g_b);
me_a = pnga_pgroup_nodeid(a_grp);
me_b = pnga_pgroup_nodeid(b_grp);
anproc = pnga_get_pgroup_size(a_grp);
bnproc = pnga_get_pgroup_size(b_grp);
if (anproc <= bnproc) {
use_put = 1;
} else {
use_put = 0;
}
/*if (a_grp != b_grp)
pnga_error("All matrices must be on same group for pnga_copy_patch", 0L); */
if(local_sync_begin) {
if (anproc <= bnproc) {
pnga_pgroup_sync(a_grp);
} else if (a_grp == pnga_pgroup_get_world() &&
b_grp == pnga_pgroup_get_world()) {
pnga_sync();
} else {
pnga_pgroup_sync(b_grp);
}
}
GA_PUSH_NAME("pnga_copy_patch");
pnga_inquire(g_a, &atype, &andim, adims);
pnga_inquire(g_b, &btype, &bndim, bdims);
if(g_a == g_b) {
/* they are the same patch */
if(pnga_comp_patch(andim, alo, ahi, bndim, blo, bhi)) {
return;
/* they are in the same GA, but not the same patch */
} else if (pnga_patch_intersect(alo, ahi, blo, bhi, andim)) {
pnga_error("array patches cannot overlap ", 0L);
}
}
if(atype != btype ) pnga_error("array type mismatch ", 0L);
/* check if patch indices and dims match */
for(i=0; i<andim; i++)
if(alo[i] <= 0 || ahi[i] > adims[i])
pnga_error("g_a indices out of range ", 0L);
for(i=0; i<bndim; i++)
if(blo[i] <= 0 || bhi[i] > bdims[i])
pnga_error("g_b indices out of range ", 0L);
/* check if numbers of elements in two patches match each other */
atotal = 1; btotal = 1;
for(i=0; i<andim; i++) atotal *= (ahi[i] - alo[i] + 1);
for(i=0; i<bndim; i++) btotal *= (bhi[i] - blo[i] + 1);
if(atotal != btotal)
pnga_error("capacities two of patches do not match ", 0L);
/* additional restrictions that apply if one or both arrays use
block-cyclic data distributions */
num_blocks_a = pnga_total_blocks(g_a);
num_blocks_b = pnga_total_blocks(g_b);
if (num_blocks_a >= 0 || num_blocks_b >= 0) {
if (!(*trans == 'n' || *trans == 'N')) {
pnga_error("Transpose option not supported for block-cyclic data", 0L);
}
if (!snga_test_shape(alo, ahi, blo, bhi, andim, bndim)) {
pnga_error("Change in shape not supported for block-cyclic data", 0L);
}
}
if (num_blocks_a < 0 && num_blocks_b <0) {
/* now find out cordinates of a patch of g_a that I own */
if (use_put) {
pnga_distribution(g_a, me_a, los, his);
} else {
pnga_distribution(g_b, me_b, los, his);
}
/* copy my share of data */
if (use_put) {
has_intersection = pnga_patch_intersect(alo, ahi, los, his, andim);
} else {
has_intersection = pnga_patch_intersect(blo, bhi, los, his, bndim);
}
if(has_intersection){
if (use_put) {
pnga_access_ptr(g_a, los, his, &src_data_ptr, ld);
} else {
pnga_access_ptr(g_b, los, his, &src_data_ptr, ld);
}
/* calculate the number of elements in the patch that I own */
nelem = 1; for(i=0; i<andim; i++) nelem *= (his[i] - los[i] + 1);
for(i=0; i<andim; i++) ald[i] = ahi[i] - alo[i] + 1;
for(i=0; i<bndim; i++) bld[i] = bhi[i] - blo[i] + 1;
base = 0; factor = 1;
for(i=0; i<andim; i++) {
base += los[i] * factor;
factor *= ld[i];
}
/*** straight copy possible if there's no reshaping or transpose ***/
if((*trans == 'n' || *trans == 'N') &&
snga_test_shape(alo, ahi, blo, bhi, andim, bndim)) {
/* find source[lo:hi] --> destination[lo:hi] */
if (use_put) {
snga_dest_indices(andim, los, alo, ald, bndim, lod, blo, bld);
snga_dest_indices(andim, his, alo, ald, bndim, hid, blo, bld);
pnga_put(g_b, lod, hid, src_data_ptr, ld);
pnga_release(g_a, los, his);
} else {
snga_dest_indices(bndim, los, blo, bld, andim, lod, alo, ald);
snga_dest_indices(bndim, his, blo, bld, andim, hid, alo, ald);
pnga_get(g_a, lod, hid, src_data_ptr, ld);
pnga_release(g_b, los, his);
}
/*** due to generality of this transformation scatter is required ***/
} else{
if (use_put) {
tmp_ptr = ga_malloc(nelem, atype, "v");
src_idx_ptr = (Integer*) ga_malloc((andim*nelem), MT_F_INT, "si");
dst_idx_ptr = (Integer*) ga_malloc((bndim*nelem), MT_F_INT, "di");
/* calculate the destination indices */
/* given los and his, find indices for each elements
* bvalue: starting index in each dimension
* bunit: stride in each dimension
*/
for (i=0; i<andim; i++) {
bvalue[i] = los[i];
if (i == 0) bunit[i] = 1;
else bunit[i] = bunit[i-1] * (his[i-1] - los[i-1] + 1);
}
/* source indices */
for (i=0; i<nelem; i++) {
for (j=0; j<andim; j++){
src_idx_ptr[i*andim+j] = bvalue[j];
/* if the next element is the first element in
* one dimension, increment the index by 1
*/
if (((i+1) % bunit[j]) == 0) bvalue[j]++;
/* if the index becomes larger than the upper
* bound in one dimension, reset it.
*/
if(bvalue[j] > his[j]) bvalue[j] = los[j];
}
}
/* index factor: reshaping without transpose */
factor_idx1[0] = 1;
for (j=1; j<andim; j++)
factor_idx1[j] = factor_idx1[j-1] * ald[j-1];
/* index factor: reshaping with transpose */
factor_idx2[andim-1] = 1;
for (j=(andim-1)-1; j>=0; j--)
factor_idx2[j] = factor_idx2[j+1] * ald[j+1];
/* data factor */
factor_data[0] = 1;
for (j=1; j<andim; j++)
factor_data[j] = factor_data[j-1] * ld[j-1];
/* destination indices */
for(i=0; i<nelem; i++) {
/* linearize the n-dimensional indices to one dimension */
idx = 0;
if (*trans == 'n' || *trans == 'N')
for (j=0; j<andim; j++)
idx += (src_idx_ptr[i*andim+j] - alo[j]) *
factor_idx1[j];
else
/* if the patch needs to be transposed, reverse
* the indices: (i, j, ...) -> (..., j, i)
*/
for (j=(andim-1); j>=0; j--)
idx += (src_idx_ptr[i*andim+j] - alo[j]) *
factor_idx2[j];
/* convert the one dimensional index to n-dimensional
* indices of destination
*/
for (j=0; j<bndim; j++) {
dst_idx_ptr[i*bndim+j] = idx % bld[j] + blo[j];
idx /= bld[j];
}
/* move the data block to create a new block */
/* linearize the data indices */
idx = 0;
for (j=0; j<andim; j++)
idx += (src_idx_ptr[i*andim+j]) * factor_data[j];
/* adjust the position
* base: starting address of the first element */
idx -= base;
/* move the element to the temporary location */
switch(atype) {
case C_DBL: ((double*)tmp_ptr)[i] =
((double*)src_data_ptr)[idx];
break;
case C_INT:
((int *)tmp_ptr)[i] = ((int *)src_data_ptr)[idx];
break;
case C_DCPL:((DoubleComplex *)tmp_ptr)[i] =
((DoubleComplex *)src_data_ptr)[idx];
break;
case C_SCPL:((SingleComplex *)tmp_ptr)[i] =
((SingleComplex *)src_data_ptr)[idx];
break;
case C_FLOAT: ((float *)tmp_ptr)[i] =
((float *)src_data_ptr)[idx];
break;
case C_LONG: ((long *)tmp_ptr)[i] =
((long *)src_data_ptr)[idx];
break;
case C_LONGLONG: ((long long *)tmp_ptr)[i] =
((long long *)src_data_ptr)[idx];
}
}
pnga_release(g_a, los, his);
pnga_scatter(g_b, tmp_ptr, dst_idx_ptr, 0, nelem);
ga_free(dst_idx_ptr);
ga_free(src_idx_ptr);
ga_free(tmp_ptr);
} else {
tmp_ptr = ga_malloc(nelem, atype, "v");
src_idx_ptr = (Integer*) ga_malloc((bndim*nelem), MT_F_INT, "si");
dst_idx_ptr = (Integer*) ga_malloc((andim*nelem), MT_F_INT, "di");
/* calculate the destination indices */
/* given los and his, find indices for each elements
* bvalue: starting index in each dimension
* bunit: stride in each dimension
*/
for (i=0; i<andim; i++) {
bvalue[i] = los[i];
if (i == 0) bunit[i] = 1;
else bunit[i] = bunit[i-1] * (his[i-1] - los[i-1] + 1);
}
/* destination indices */
for (i=0; i<nelem; i++) {
for (j=0; j<bndim; j++){
src_idx_ptr[i*bndim+j] = bvalue[j];
/* if the next element is the first element in
* one dimension, increment the index by 1
*/
if (((i+1) % bunit[j]) == 0) bvalue[j]++;
/* if the index becomes larger than the upper
* bound in one dimension, reset it.
*/
if(bvalue[j] > his[j]) bvalue[j] = los[j];
}
}
/* index factor: reshaping without transpose */
factor_idx1[0] = 1;
for (j=1; j<bndim; j++)
factor_idx1[j] = factor_idx1[j-1] * bld[j-1];
/* index factor: reshaping with transpose */
factor_idx2[bndim-1] = 1;
for (j=(bndim-1)-1; j>=0; j--)
factor_idx2[j] = factor_idx2[j+1] * bld[j+1];
/* data factor */
factor_data[0] = 1;
for (j=1; j<bndim; j++)
factor_data[j] = factor_data[j-1] * ld[j-1];
/* destination indices */
for(i=0; i<nelem; i++) {
/* linearize the n-dimensional indices to one dimension */
idx = 0;
if (*trans == 'n' || *trans == 'N')
for (j=0; j<andim; j++)
idx += (src_idx_ptr[i*bndim+j] - blo[j]) *
factor_idx1[j];
else
/* if the patch needs to be transposed, reverse
* the indices: (i, j, ...) -> (..., j, i)
*/
for (j=(andim-1); j>=0; j--)
idx += (src_idx_ptr[i*bndim+j] - blo[j]) *
factor_idx2[j];
/* convert the one dimensional index to n-dimensional
* indices of destination
*/
for (j=0; j<andim; j++) {
dst_idx_ptr[i*bndim+j] = idx % ald[j] + alo[j];
idx /= ald[j];
}
/* move the data block to create a new block */
/* linearize the data indices */
idx = 0;
for (j=0; j<bndim; j++)
idx += (src_idx_ptr[i*bndim+j]) * factor_data[j];
/* adjust the position
* base: starting address of the first element */
idx -= base;
/* move the element to the temporary location */
switch(atype) {
case C_DBL: ((double*)tmp_ptr)[i] =
((double*)src_data_ptr)[idx];
break;
case C_INT:
((int *)tmp_ptr)[i] = ((int *)src_data_ptr)[idx];
break;
case C_DCPL:((DoubleComplex *)tmp_ptr)[i] =
((DoubleComplex *)src_data_ptr)[idx];
break;
case C_SCPL:((SingleComplex *)tmp_ptr)[i] =
((SingleComplex *)src_data_ptr)[idx];
break;
case C_FLOAT: ((float *)tmp_ptr)[i] =
((float *)src_data_ptr)[idx];
break;
case C_LONG: ((long *)tmp_ptr)[i] =
((long *)src_data_ptr)[idx];
break;
case C_LONGLONG: ((long long *)tmp_ptr)[i] =
((long long *)src_data_ptr)[idx];
}
}
pnga_release(g_b, los, his);
pnga_gather(g_a, tmp_ptr, dst_idx_ptr, 0, nelem);
ga_free(dst_idx_ptr);
ga_free(src_idx_ptr);
ga_free(tmp_ptr);
}
}
}
} else {
Integer offset, last, jtot;
for (i=0; i<andim; i++) {
ald[i] = ahi[i] - alo[i] + 1;
}
for (i=0; i<bndim; i++) {
bld[i] = bhi[i] - blo[i] + 1;
}
if (use_put) {
/* Array a is block-cyclic distributed */
if (num_blocks_a >= 0) {
/* Uses simple block-cyclic data distribution */
if (!pnga_uses_proc_grid(g_a)) {
for (i = me_a; i < num_blocks_a; i += anproc) {
pnga_distribution(g_a, i, los, his);
/* make temporory copies of los, his since ngai_patch_intersection
destroys original versions */
for (j=0; j < andim; j++) {
lod[j] = los[j];
hid[j] = his[j];
}
if (pnga_patch_intersect(alo,ahi,los,his,andim)) {
pnga_access_block_ptr(g_a, i, &src_data_ptr, ld);
offset = 0;
last = andim - 1;
jtot = 1;
for (j=0; j<last; j++) {
offset += (los[j]-lod[j])*jtot;
jtot *= ld[j];
}
offset += (los[last]-lod[last])*jtot;
switch(atype) {
case C_DBL:
src_data_ptr = (void*)((double*)(src_data_ptr) + offset);
break;
case C_INT:
src_data_ptr = (void*)((int*)(src_data_ptr) + offset);
break;
case C_DCPL:
src_data_ptr = (void*)((DoubleComplex*)(src_data_ptr) + offset);
break;
case C_SCPL:
src_data_ptr = (void*)((SingleComplex*)(src_data_ptr) + offset);
break;
case C_FLOAT:
src_data_ptr = (void*)((float*)(src_data_ptr) + offset);
break;
case C_LONG:
src_data_ptr = (void*)((long*)(src_data_ptr) + offset);
break;
case C_LONGLONG:
src_data_ptr = (void*)((long long*)(src_data_ptr) + offset);
break;
default:
break;
}
snga_dest_indices(andim, los, alo, ald, bndim, lod, blo, bld);
snga_dest_indices(andim, his, alo, ald, bndim, hid, blo, bld);
pnga_put(g_b, lod, hid, src_data_ptr, ld);
pnga_release_block(g_a, i);
}
}
} else {
/* Uses scalapack block-cyclic data distribution */
Integer proc_index[MAXDIM], index[MAXDIM];
Integer topology[MAXDIM];
Integer blocks[MAXDIM], block_dims[MAXDIM];
pnga_get_proc_index(g_a, me_a, proc_index);
pnga_get_proc_index(g_a, me_a, index);
pnga_get_block_info(g_a, blocks, block_dims);
pnga_get_proc_grid(g_a, topology);
while (index[andim-1] < blocks[andim-1]) {
/* find bounding coordinates of block */
/*chk = 1;*/
for (i = 0; i < andim; i++) {
los[i] = index[i]*block_dims[i]+1;
his[i] = (index[i] + 1)*block_dims[i];
if (his[i] > adims[i]) his[i] = adims[i];
/*if (his[i] < los[i]) chk = 0;*/
}
/* make temporory copies of los, his since ngai_patch_intersection
destroys original versions */
for (j=0; j < andim; j++) {
lod[j] = los[j];
hid[j] = his[j];
}
if (pnga_patch_intersect(alo,ahi,los,his,andim)) {
pnga_access_block_grid_ptr(g_a, index, &src_data_ptr, ld);
offset = 0;
last = andim - 1;
jtot = 1;
for (j=0; j<last; j++) {
offset += (los[j]-lod[j])*jtot;
jtot *= ld[j];
}
offset += (los[last]-lod[last])*jtot;
switch(atype) {
case C_DBL:
src_data_ptr = (void*)((double*)(src_data_ptr) + offset);
break;
case C_INT:
src_data_ptr = (void*)((int*)(src_data_ptr) + offset);
break;
case C_DCPL:
src_data_ptr = (void*)((DoubleComplex*)(src_data_ptr) + offset);
break;
case C_SCPL:
src_data_ptr = (void*)((SingleComplex*)(src_data_ptr) + offset);
break;
case C_FLOAT:
src_data_ptr = (void*)((float*)(src_data_ptr) + offset);
break;
case C_LONG:
src_data_ptr = (void*)((long*)(src_data_ptr) + offset);
break;
case C_LONGLONG:
src_data_ptr = (void*)((long long*)(src_data_ptr) + offset);
break;
default:
break;
}
snga_dest_indices(andim, los, alo, ald, bndim, lod, blo, bld);
snga_dest_indices(andim, his, alo, ald, bndim, hid, blo, bld);
pnga_put(g_b, lod, hid, src_data_ptr, ld);
pnga_release_block_grid(g_a, index);
}
/* increment index to get next block on processor */
index[0] += topology[0];
for (i = 0; i < andim; i++) {
if (index[i] >= blocks[i] && i<andim-1) {
index[i] = proc_index[i];
index[i+1] += topology[i+1];
}
}
}
}
} else {
/* Array b is block-cyclic distributed */
pnga_distribution(g_a, me_a, los, his);
if (pnga_patch_intersect(alo,ahi,los,his,andim)) {
pnga_access_ptr(g_a, los, his, &src_data_ptr, ld);
snga_dest_indices(andim, los, alo, ald, bndim, lod, blo, bld);
snga_dest_indices(andim, his, alo, ald, bndim, hid, blo, bld);
pnga_put(g_b, lod, hid, src_data_ptr, ld);
pnga_release(g_a, los, his);
}
}
} else {
/* Array b is block-cyclic distributed */
if (num_blocks_b >= 0) {
/* Uses simple block-cyclic data distribution */
if (!pnga_uses_proc_grid(g_b)) {
for (i = me_b; i < num_blocks_b; i += bnproc) {
pnga_distribution(g_b, i, los, his);
/* make temporory copies of los, his since ngai_patch_intersection
destroys original versions */
for (j=0; j < andim; j++) {
lod[j] = los[j];
hid[j] = his[j];
}
if (pnga_patch_intersect(blo,bhi,los,his,andim)) {
pnga_access_block_ptr(g_b, i, &src_data_ptr, ld);
offset = 0;
last = bndim - 1;
jtot = 1;
for (j=0; j<last; j++) {
offset += (los[j]-lod[j])*jtot;
jtot *= ld[j];
}
offset += (los[last]-lod[last])*jtot;
switch(atype) {
case C_DBL:
src_data_ptr = (void*)((double*)(src_data_ptr) + offset);
break;
case C_INT:
src_data_ptr = (void*)((int*)(src_data_ptr) + offset);
break;
case C_DCPL:
src_data_ptr = (void*)((DoubleComplex*)(src_data_ptr) + offset);
break;
case C_SCPL:
src_data_ptr = (void*)((SingleComplex*)(src_data_ptr) + offset);
break;
case C_FLOAT:
src_data_ptr = (void*)((float*)(src_data_ptr) + offset);
break;
case C_LONG:
src_data_ptr = (void*)((long*)(src_data_ptr) + offset);
break;
case C_LONGLONG:
src_data_ptr = (void*)((long long*)(src_data_ptr) + offset);
break;
default:
break;
}
snga_dest_indices(bndim, los, blo, bld, andim, lod, alo, ald);
snga_dest_indices(bndim, his, blo, bld, andim, hid, alo, ald);
pnga_get(g_a, lod, hid, src_data_ptr, ld);
pnga_release_block(g_b, i);
}
}
} else {
/* Uses scalapack block-cyclic data distribution */
Integer proc_index[MAXDIM], index[MAXDIM];
Integer topology[MAXDIM];
Integer blocks[MAXDIM], block_dims[MAXDIM];
pnga_get_proc_index(g_b, me_b, proc_index);
pnga_get_proc_index(g_b, me_b, index);
pnga_get_block_info(g_b, blocks, block_dims);
pnga_get_proc_grid(g_b, topology);
while (index[bndim-1] < blocks[bndim-1]) {
/* find bounding coordinates of block */
/*chk = 1;*/
for (i = 0; i < bndim; i++) {
los[i] = index[i]*block_dims[i]+1;
his[i] = (index[i] + 1)*block_dims[i];
if (his[i] > bdims[i]) his[i] = bdims[i];
/*if (his[i] < los[i]) chk = 0;*/
}
/* make temporory copies of los, his since ngai_patch_intersection
destroys original versions */
for (j=0; j < andim; j++) {
lod[j] = los[j];
hid[j] = his[j];
}
if (pnga_patch_intersect(blo,bhi,los,his,andim)) {
pnga_access_block_grid_ptr(g_b, index, &src_data_ptr, ld);
offset = 0;
last = bndim - 1;
jtot = 1;
for (j=0; j<last; j++) {
offset += (los[j]-lod[j])*jtot;
jtot *= ld[j];
}
offset += (los[last]-lod[last])*jtot;
switch(atype) {
case C_DBL:
src_data_ptr = (void*)((double*)(src_data_ptr) + offset);
break;
case C_INT:
src_data_ptr = (void*)((int*)(src_data_ptr) + offset);
break;
case C_DCPL:
src_data_ptr = (void*)((DoubleComplex*)(src_data_ptr) + offset);
break;
case C_SCPL:
src_data_ptr = (void*)((SingleComplex*)(src_data_ptr) + offset);
break;
case C_FLOAT:
src_data_ptr = (void*)((float*)(src_data_ptr) + offset);
break;
case C_LONG:
src_data_ptr = (void*)((long*)(src_data_ptr) + offset);
break;
case C_LONGLONG:
src_data_ptr = (void*)((long long*)(src_data_ptr) + offset);
break;
default:
break;
}
snga_dest_indices(bndim, los, blo, bld, andim, lod, alo, ald);
snga_dest_indices(bndim, his, blo, bld, andim, hid, alo, ald);
pnga_get(g_a, lod, hid, src_data_ptr, ld);
pnga_release_block_grid(g_b, index);
}
/* increment index to get next block on processor */
index[0] += topology[0];
for (i = 0; i < bndim; i++) {
if (index[i] >= blocks[i] && i<bndim-1) {
index[i] = proc_index[i];
index[i+1] += topology[i+1];
}
}
}
}
} else {
/* Array a is block-cyclic distributed */
pnga_distribution(g_b, me_b, los, his);
if (pnga_patch_intersect(blo,bhi,los,his,bndim)) {
pnga_access_ptr(g_b, los, his, &src_data_ptr, ld);
snga_dest_indices(bndim, los, blo, bld, andim, lod, alo, ald);
snga_dest_indices(bndim, his, blo, bld, andim, hid, alo, ald);
pnga_get(g_a, lod, hid, src_data_ptr, ld);
pnga_release(g_b, los, his);
}
}
}
}
GA_POP_NAME;
/* ARMCI_AllFence(); */
if(local_sync_end) {
if (anproc <= bnproc) {
pnga_pgroup_sync(a_grp);
} else if (a_grp == pnga_pgroup_get_world() &&
b_grp == pnga_pgroup_get_world()) {
pnga_sync();
} else {
pnga_pgroup_sync(b_grp);
}
}
}
static void snga_dot_local_patch(Integer atype, Integer andim, Integer *loA,
Integer *hiA, Integer *ldA, void *A_ptr, void *B_ptr,
int *alen, void *retval)
{
int isum;
double dsum;
DoubleComplex zsum;
SingleComplex csum;
float fsum;
long lsum;
long long llsum;
Integer i, j, n1dim, idx;
Integer bvalue[MAXDIM], bunit[MAXDIM], baseldA[MAXDIM];
isum = 0; dsum = 0.; zsum.real = 0.; zsum.imag = 0.; fsum = 0;lsum=0;llsum=0;
csum.real = 0.; csum.imag = 0.;
/* number of n-element of the first dimension */
n1dim = 1; for(i=1; i<andim; i++) n1dim *= (hiA[i] - loA[i] + 1);
/* calculate the destination indices */
bvalue[0] = 0; bvalue[1] = 0; bunit[0] = 1; bunit[1] = 1;
/* baseldA[0] = ldA[0]
* baseldA[1] = ldA[0] * ldA[1]
* baseldA[2] = ldA[0] * ldA[1] * ldA[2] .....
*/
baseldA[0] = ldA[0]; baseldA[1] = baseldA[0] *ldA[1];
for(i=2; i<andim; i++) {
bvalue[i] = 0;
bunit[i] = bunit[i-1] * (hiA[i-1] - loA[i-1] + 1);
baseldA[i] = baseldA[i-1] * ldA[i];
}
/* compute "local" contribution to the dot product */
switch (atype){
case C_INT:
for(i=0; i<n1dim; i++) {
idx = 0;
for(j=1; j<andim; j++) {
idx += bvalue[j] * baseldA[j-1];
if(((i+1) % bunit[j]) == 0) bvalue[j]++;
if(bvalue[j] > (hiA[j]-loA[j])) bvalue[j] = 0;
}
for(j=0; j<(hiA[0]-loA[0]+1); j++)
isum += ((int *)A_ptr)[idx+j] *
((int *)B_ptr)[idx+j];
}
*(int*)retval += isum;
break;
case C_DCPL:
for(i=0; i<n1dim; i++) {
idx = 0;
for(j=1; j<andim; j++) {
idx += bvalue[j] * baseldA[j-1];
if(((i+1) % bunit[j]) == 0) bvalue[j]++;
if(bvalue[j] > (hiA[j]-loA[j])) bvalue[j] = 0;
}
for(j=0; j<(hiA[0]-loA[0]+1); j++) {
DoubleComplex a = ((DoubleComplex *)A_ptr)[idx+j];
DoubleComplex b = ((DoubleComplex *)B_ptr)[idx+j];
zsum.real += a.real*b.real - b.imag * a.imag;
zsum.imag += a.imag*b.real + b.imag * a.real;
}
}
((double*)retval)[0] += zsum.real;
((double*)retval)[1] += zsum.imag;
break;
case C_SCPL:
for(i=0; i<n1dim; i++) {
idx = 0;
for(j=1; j<andim; j++) {
idx += bvalue[j] * baseldA[j-1];
if(((i+1) % bunit[j]) == 0) bvalue[j]++;
if(bvalue[j] > (hiA[j]-loA[j])) bvalue[j] = 0;
}
for(j=0; j<(hiA[0]-loA[0]+1); j++) {
SingleComplex a = ((SingleComplex *)A_ptr)[idx+j];
SingleComplex b = ((SingleComplex *)B_ptr)[idx+j];
csum.real += a.real*b.real - b.imag * a.imag;
csum.imag += a.imag*b.real + b.imag * a.real;
}
}
((float*)retval)[0] += csum.real;
((float*)retval)[1] += csum.imag;
break;
case C_DBL:
for(i=0; i<n1dim; i++) {
idx = 0;
for(j=1; j<andim; j++) {
idx += bvalue[j] * baseldA[j-1];
if(((i+1) % bunit[j]) == 0) bvalue[j]++;
if(bvalue[j] > (hiA[j]-loA[j])) bvalue[j] = 0;
}
for(j=0; j<(hiA[0]-loA[0]+1); j++)
dsum += ((double*)A_ptr)[idx+j] *
((double*)B_ptr)[idx+j];
}
*(double*)retval += dsum;
break;
case C_FLOAT:
for(i=0; i<n1dim; i++) {
idx = 0;
for(j=1; j<andim; j++) {
idx += bvalue[j] * baseldA[j-1];
if(((i+1) % bunit[j]) == 0) bvalue[j]++;
if(bvalue[j] > (hiA[j]-loA[j])) bvalue[j] = 0;
}
for(j=0; j<(hiA[0]-loA[0]+1); j++)
fsum += ((float *)A_ptr)[idx+j] *
((float *)B_ptr)[idx+j];
}
*(float*)retval += fsum;
break;
case C_LONG:
for(i=0; i<n1dim; i++) {
idx = 0;
for(j=1; j<andim; j++) {
idx += bvalue[j] * baseldA[j-1];
if(((i+1) % bunit[j]) == 0) bvalue[j]++;
if(bvalue[j] > (hiA[j]-loA[j])) bvalue[j] = 0;
}
for(j=0; j<(hiA[0]-loA[0]+1); j++)
lsum += ((long *)A_ptr)[idx+j] *
((long *)B_ptr)[idx+j];
}
*(long*)retval += lsum;
break;
case C_LONGLONG:
for(i=0; i<n1dim; i++) {
idx = 0;
for(j=1; j<andim; j++) {
idx += bvalue[j] * baseldA[j-1];
if(((i+1) % bunit[j]) == 0) bvalue[j]++;
if(bvalue[j] > (hiA[j]-loA[j])) bvalue[j] = 0;
}
for(j=0; j<(hiA[0]-loA[0]+1); j++)
llsum += ((long long *)A_ptr)[idx+j] *
((long long *)B_ptr)[idx+j];
}
*(long long*)retval += llsum;
break;
default:
pnga_error("snga_dot_local_patch: type not supported",atype);
}
}
/*\ generic dot product routine
\*/
#if HAVE_SYS_WEAK_ALIAS_PRAGMA
# pragma weak wnga_dot_patch = pnga_dot_patch
#endif
void pnga_dot_patch(Integer g_a, char *t_a, Integer *alo, Integer *ahi, Integer g_b, char *t_b, Integer *blo, Integer *bhi, void *retval)
{
Integer i=0, j=0;
Integer compatible=0;
Integer atype=0, btype=0, andim=0, adims[MAXDIM], bndim=0, bdims[MAXDIM];
Integer loA[MAXDIM], hiA[MAXDIM], ldA[MAXDIM];
Integer loB[MAXDIM], hiB[MAXDIM], ldB[MAXDIM];
Integer g_A = g_a, g_B = g_b;
void *A_ptr=NULL, *B_ptr=NULL;
Integer ctype=0;