-
Notifications
You must be signed in to change notification settings - Fork 1
/
contraction.cc
executable file
·2953 lines (2368 loc) · 91.4 KB
/
contraction.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "contraction.h"
#define CCHECK 0
#define RRANK 7
#define DEBUG_TRR 0
#define DEBUG_TDEBUG_IP 0
#define DEBUG_TCM 0
#define DEBUG_T 0
#define DEBUG_I 0
#define DEBUG_T1 0
#define DEBUG_TR 0
#define DEBUG_TTRR 0
#define DISPLAY_TIME 0
#define COST_EVAL 1
namespace RRR{
using namespace std;
Contraction::Contraction(Grid* &g)
{
grid = g;//new Grid(g->grid_dims,g->pgrid);
rank = grid->rank;
num_procs = grid->nprocs;
grid_dims = grid->grid_dims;
serial = grid_dims;
pgrid = new int[grid_dims + 1];
my_address = new int[grid_dims + 1];
memcpy(pgrid, grid->pgrid, grid_dims * sizeof(int));
memcpy(my_address, grid->proc_addr, grid_dims * sizeof(int));
pgrid[grid_dims] = 1;
my_address[grid_dims] = 0;
//debugging
receiver_grid_buffer = new int[num_procs*num_procs];
sender_grid_buffer = new int[num_procs*num_procs];
}
Contraction::Contraction(Tensor* &a, Tensor* &b, Tensor* &c, Grid* &g)
{
grid = new Grid(g->grid_dims,g->pgrid);
rank = grid->rank;
num_procs = grid->nprocs;
//debugging
//receiver_grid_buffer = new int[num_procs*num_procs];
//sender_grid_buffer = new int[num_procs*num_procs];
A = a;
B = b;
C = c;
//transpose flags for A and B for local dgemm
t_A = 0;
t_B = 0;
//stores mapping information regarding transposing blocks of A and B and C before dgemm can be called
p_map_A = new int[A->dims];
p_map_B = new int[B->dims];
p_map_C = new int[C->dims];
//stores transpose mapping information regarding blocks of C after local dgemm is called
inv_p_map_C = new int[C->dims];
inverse_block_range_C = new int[C->dims];
//holds information on if indicies of C are from A or from B
is_C_indx_ext_A_or_ext_B = new int[C->dims];
local_num_dgemm = 0;
total_num_dgemm = 0;
num_self_sends = 0;
grid_dims = grid->grid_dims;
serial = grid_dims;
pgrid = new int[grid_dims + 1];
my_address = new int[grid_dims + 1];
memcpy(pgrid, grid->pgrid, grid_dims * sizeof(int));
memcpy(my_address, grid->proc_addr, grid_dims * sizeof(int));
pgrid[grid_dims] = 1;
my_address[grid_dims] = 0;
//debugging
receiver_grid_buffer = new int[num_procs*num_procs];
sender_grid_buffer = new int[num_procs*num_procs];
}
//Changes the grid if a redistribution is required for contraction
void Contraction::change_grid(Grid* &g)
{
grid = new Grid(g->grid_dims,g->pgrid);
rank = grid->rank;
num_procs = grid->nprocs;
grid_dims = grid->grid_dims;
serial = grid_dims;
pgrid = new int[grid_dims + 1];
my_address = new int[grid_dims + 1];
memcpy(pgrid, grid->pgrid, grid_dims * sizeof(int));
memcpy(my_address, grid->proc_addr, grid_dims * sizeof(int));
pgrid[grid_dims] = 1;
my_address[grid_dims] = 0;
}
Contraction::~Contraction()
{
delete[] p_map_A;
delete[] p_map_B;
delete[] p_map_C;
delete[] inv_p_map_C;
delete[] is_C_indx_ext_A_or_ext_B;
delete[] pgrid;
delete[] my_address;
delete[] A_to_C_map;
delete[] B_to_C_map;
}
//returns a permutation map that combines the affect of
//applying map1 and map2 consecutively
void Contraction::compose_p_maps(Tensor* &T, int* &map1, int* &map2, int* &composed_map)
{
for(int i = 0; i<T->dims; i++)
{
composed_map[i] = map2[map1[i]];
}
}
//generates permutation maps based on symmetry for bounced blocks
void Contraction::get_symm_permutation(Tensor* &T, int* &tile_address, int* &sym_permutation)
{
for(int i =0; i<T->dims; i++)
{
sym_permutation[i] = i;
}
for(int i =0; i<T->dims; i++)
{
for(int j = i+1; j<T->dims; j++)
{
//if one component of the tile address is less than the other
//and the pair of tile address belong to the same symmetry group
//then such tile address must have been created by flipping the
//indices in the original tile
if((tile_address[i] < tile_address[j])
&& T->SG_index_map_permanent[i] == T->SG_index_map_permanent[j]
&& T->SG_index_map_permanent[i] <= 1)
{
sym_permutation[i] = j;
sym_permutation[j] = i;
break;
}
}
}
}
//generates permutation maps for A, B and C for doing transpose before local dgemm can be performed
//also generates the size of external and internal indices
void Contraction::generate_permutation_map(Tensor* &A, Tensor* &B, Tensor* &C, vector<pair<int,int>> &contr_list)
{
//-----------------------Find if the external indices are mixed in C--------//
//stores if the first index of C is from A or from B
/* int first = is_C_indx_ext_A_or_ext_B[0];
bool mixed = false;
//has encountered stores if external indices from A or B or both have been encountered
//0 is for external index from A and 1 is for external index from B
bool has_encountered[2] = {false, false};
for(int i = 0; i<C->dims; i++)
{
//if both external indices from A and B have been encountered and the index currently encountered
//is same as the first index then it must mean that the indices are mixed.
//C has to be transposed
if(has_encountered[0] && has_encountered[1] && is_C_indx_ext_A_or_ext_B[i] == first)
{
mixed = true;
break;
}
//set the index encountered at i to be true
has_encountered[is_C_indx_ext_A_or_ext_B[i] - 1] = true;
}*/
//----------------------------------------------------------------------//
int num_external_A = 0;
int num_external_B = 0;
int num_contr_indx= 0;
//contraction size for A and B
int nk_a = 1;
int nk_b = 1;
/*This is a really dumb implementation of permutation map generator.
It works but will not generate the most efficient transposes. It always
generates transpsosed blocks of the form [e1,e2,e3,k1,k2,k3]*/
for(int i = 0; i<A->dims; i++)
{
//if i is an external index then map it to the number of external indicies discovered so far
if(A->cntr_map[i] == 0)
{
//computes the size of the external index in B
n_a = n_a * A->block_range[i];
p_map_A[i] = num_external_A;
//map Cs corresponding external index to same as that of A
p_map_C[A_to_C_map[i]] = num_external_A;
//stores the block range of the transposed block of C for inverse mapping
inverse_block_range_C[num_external_A] = C->block_range[A_to_C_map[i]];
//stores the inverse mapping from transposed block of C to original block of C
inv_p_map_C[num_external_A] = A_to_C_map[i];
num_external_A++;
}else{
nk_a = nk_a*A->block_range[i];
}
}
for(int i = 0; i<B->dims; i++)
{
if(B->cntr_map[i] == 0)
{
//computes the size of the external index in B
n_b = n_b * B->block_range[i];
p_map_B[i] = num_external_B;
//map Cs corresponding external index to same as that of B
//offsetted by the total number of external indices of A
p_map_C[B_to_C_map[i]] = num_external_A + num_external_B;
//stores the block range of the transposed block of C for inverse mapping
inverse_block_range_C[num_external_A + num_external_B] = C->block_range[B_to_C_map[i]];
//stores the inverse mapping from transposed block of C to original block of C
inv_p_map_C[num_external_A + num_external_B] = B_to_C_map[i];
num_external_B++;
}
else
{
nk_b = nk_b * B->block_range[i];
}
}
if(DEBUG_T1 && rank == RRANK){
if(nk_a != nk_b) {
A->printInfo();
B->printInfo();
cout<<"nk_a is "<<nk_a<<" and nk_b is "<<nk_b<<endl<<endl;
}
}
//makes sure the size of the contracting indices are equal in A and B
assert(nk_a == nk_b);
n_k = nk_a;
for (std::vector<pair<int,int>>::iterator it = contr_list.begin(); it != contr_list.end(); it++)
{
p_map_A[(*it).first] = num_external_A + num_contr_indx;
p_map_B[(*it).second] = num_external_B + num_contr_indx;;
num_contr_indx++;
}
}
// Asserts validity of specified contraction
void Contraction::assert_contr_validity(Tensor* &A, Tensor* &B)
{
// Check if all external indices (non-contracting indices) are orthogonal
for(int i=0; i < A->dims; i++)
{
int dim_A = A->index_dimension_map[i];
if(dim_A != serial)
{
int index_B = -1;
for(int j=0; j < B->dims; j++)
{
if(B->index_dimension_map[j] == dim_A && B->index_dimension_map[j] != A->dims)
{
index_B = j;
}
}
//index_B is -1 noly if there is an index of A is overlapping
//with index of B. We want the assertion to be false only
//if external indices overlap. Internal indices can be handled
//with reduction
if(index_B != -1 && !(A->cntr_map[i] == 1 && B->cntr_map[index_B] ==1))
{
assert(A->cntr_map[i] != B->cntr_map[index_B]);
}
}
}
//When there is no reduction
// Check if same contracting index in different tensors does not overlap
for(int i=0; i<A->dims; i++)
{
for(int j=0; j<B->dims; j++)
{
if(A->cntr_map[i] == B->cntr_map[j] && A->index_dimension_map[i] != serial && B->index_dimension_map[j] != serial)
{
if(A->contr_dim_str[i].compare(B->contr_dim_str[j]) == 0)
{
assert(A->index_dimension_map[i] != B->index_dimension_map[j]);
}
}
}
}
// Find out number of contracting indices in each tensor, and check if they are equal
int num_contr_ind_A = 0, num_contr_ind_B = 0;
for(int i=0; i< A->get_dims(); i++)
if(A->cntr_map[i] > 0) num_contr_ind_A++;
for(int i=0; i< B->get_dims(); i++)
if(B->cntr_map[i] > 0) num_contr_ind_B++;
assert(num_contr_ind_A == num_contr_ind_B);
}
//stores the address of C corresponding to address of A and B in C_address
void Contraction::get_C_address(int* &A_address,int dims_a, int* &B_address, int dims_b, int* &C_address, int dims_c)
{
C_address = new int[dims_c];
//finds the common indicies between A and C
for(int it_a = 0; it_a<dims_a; it_a++)
{
int map_A = A_to_C_map[it_a];
if( map_A != -1)
{
C_address[map_A] = A_address[it_a];
}
}
//finds the common indicies between B and C
for(int it_b = 0; it_b<dims_b; it_b++)
{
int map_B = B_to_C_map[it_b];
if( map_B != -1)
{
C_address[map_B] = B_address[it_b];
}
}
}
void Contraction::get_C_addresses(Tensor* &T_A, Tensor* &T_B, Tensor* &T_C, int* &C_addresses, map<int,int>* &address_to_location)
{
address_to_location = new map<int,int>();
C_addresses = new int[T_C->dims * T_C->num_actual_tiles];
int* A_addresses = T_A->tile_address;
int* B_addresses = T_B->tile_address;
int A_num_tiles = T_A->num_actual_tiles;
int B_num_tiles = T_B->num_actual_tiles;
//stores the permuted address of A and B
int* A_perm_address = new int[T_A->dims * A_num_tiles];
int* B_perm_address = new int[T_B->dims * B_num_tiles];
/*Created permuted addresses of A and B to create the permuted
* addresses of C*/
int* source = A_addresses;
int* dest = A_perm_address;
for(int i_a = 0; i_a<A_num_tiles; i_a++)
{
for(int d = 0; d< T_A->dims; d++)
{
dest[p_map_A[d]] = source[d];
}
source += T_A->dims;
dest += T_A->dims;
}
source = B_addresses;
dest = B_perm_address;
for(int i_b = 0; i_b<B_num_tiles; i_b++)
{
for(int d = 0; d< T_B->dims; d++)
{
dest[p_map_B[d]] = source[d];
}
source += T_B->dims;
dest += T_B->dims;
}
/*Create permuted address of C. Unpermute the address to find
* where theses address are stored in the C tensor. If the
* address is found then store it in the address map. Also
* fill up Cs addresses along the way*/
int* C_perm_address = new int[T_C->dims * sizeof(int)];;
int* C_address = new int[T_C->dims * sizeof(int)];
//Use grid_C to find rank of a given address of C within the C_grid
int C_dims = T_C->dims;
int* C_grid = new int[C_dims];
for(int d = 0; d< C_dims; d++)
{
C_grid[p_map_C[d]] = T_C->get_vgrid()[d];
}
Block_Grid* grid_C = new Block_Grid(C_dims, C_grid);
int* source_A = A_perm_address;
int* source_B = B_perm_address;
dest = C_addresses;
/*Create addresses of C based on addresses of A then looks for
* it in C tensor. Creates a map of these address to where
* they are located. The addresses are converted to address
* rank within the C tensor. These ranks are used as keys for
* the map*/
for(int i_a = 0; i_a<A_num_tiles; i_a++)
{
source_B = B_perm_address;
for(int i_b = 0; i_b<B_num_tiles; i_b++)
{
for(int j = 0; j<(A->dims-num_cntr_indices); j++)
{
C_perm_address[j] = source_A[j];
}
for(int j = 0; j<(B->dims-num_cntr_indices); j++)
{
C_perm_address[j + A->dims-num_cntr_indices] = source_B[j];
}
for(int d = 0; d< T_C->dims; d++)
{
C_address[inv_p_map_C[d]] = C_perm_address[d];
}
int c_rank = grid_C->get_block_rank(C_perm_address);
/*if this address has not been put in the map already
* then find the location of this address and also
* copy it to C_addresses*/
if(address_to_location->find(c_rank) == address_to_location->end())
{
int C_location = T_C->getTile(C_address);
//address not found. Something went wrong Shieeetttt!
assert(C_location != -1);
//copy the address
memcpy(dest, C_perm_address, T_C->dims * sizeof(int));
dest+= T_C->dims;
//address was found
(*address_to_location)[c_rank] = C_location;
}
source_B += T_B->dims;
}
source_A += T_A->dims;
}
}
// Extracts blocks from the serialized input tensor that are required
// for computing blocks in C along a dimension that is serialized in the
// input tensor but distributed in C
int Contraction::get_blks_from_serialized(int num_dim, string* &ext_dim, int* &ext_grid_dim, Tensor* &T, double* &blocks, int* &addresses)
{
// Find the external dimension number in T
int* ext_T = new int[num_dim];
list<int>* sym_list = new list<int>[num_dim];
for(int j=0; j<num_dim; j++)
{
for(int i=0; i<T->dims; i++)
{
if(ext_dim[j].compare(T->contr_dim_str[i]) == 0)
{
ext_T[j] = i;
// Once the external dimension is found in T, check if it is symmetric.
// If yes, find the dimensions symmetric to it
sym_list[j] = list<int>();
if(T->SG_index_map[i] < 2) // this is a symmetric dimension
{
for(int k=0; k < T->dims; k++)
{
if(k != i && T->SG_index_map[k] == T->SG_index_map[i])
{
sym_list[j].push_back(k);
}
}
}
}
}
}
// Find the external dimension number in C
int* ext_C = new int[num_dim];
for(int j=0; j<num_dim; j++)
{
for(int i=0; i<C->dims; i++)
{
if(ext_dim[j].compare(C->contr_dim_str[i]) == 0)
{
ext_C[j] = i;
}
}
}
// The grid dimension where this external dimension of C will be distributed
ext_grid_dim = new int[num_dim];
for(int i=0; i<num_dim; i++)
{
ext_grid_dim[i] = C->index_dimension_map[ext_C[i]];
}
delete[] ext_C;
return deserialize(T, blocks, addresses, num_dim, ext_T, ext_grid_dim, sym_list);
}
int Contraction::deserialize(Tensor* &T, double* &blocks, int* &addresses, int num_dim, int* &ext_T, int* &ext_grid_dim, list<int>* &sym_list)
{
// Find number of blocks to retain and mark them for retention
bool* retain = new bool[T->num_actual_tiles];
// If the block is retained due to symmetry, save the dimension symmetric with the deserializing dimesion
int* retain_sym = new int[T->num_actual_tiles];
// Mark blocks for duplication in this flag array, store the duplicate block's address
int** duplicate_addr = new int*[T->num_actual_tiles];
// Maintain tile number for duplication
list<int> duplicates = list<int>();
for(int i=0; i < T->num_actual_tiles; i++)
{
retain[i] = true;
retain_sym[i] = -1;
}
int retain_count = 0;
for(int i=0; i < T->num_actual_tiles; i++)
{
int* addr = T->tile_address + i * T->dims;
// Check if this block is meant to be retained
for(int j=0; j<num_dim; j++)
{
// Do not retain the block if it should not belong here after deserialization
if(addr[ext_T[j]] % pgrid[ext_grid_dim[j]] != my_address[ext_grid_dim[j]])
{
retain[i] = false;
break;
}
}
// Check if this block should be retained due to symmetry
for(int j=0; j<num_dim; j++)
{
for(list<int>::iterator it = sym_list[j].begin(); it != sym_list[j].end(); it++)
{
if(addr[*it] % pgrid[ext_grid_dim[j]] == my_address[ext_grid_dim[j]])
{
// If already marked retained, that means the block should be
// retained due to its current address as well as its symmetric address
// make copy of this block with permuted address
if(retain[i] == true && addr[*it] != addr[ext_T[j]])
{
// Only implementing for 2D tensors for now
duplicate_addr[i] = new int[T->dims];
duplicate_addr[i][*it] = addr[*it];
duplicate_addr[i][ext_T[j]] = addr[ext_T[j]];
duplicates.push_back(i);
}
// Mark for retention due to symmetry
retain[i] = true;
retain_sym[i] = *it;
break;
}
}
}
if(retain[i] == true)
{
retain_count++;
}
}
retain_count += duplicates.size();
// Copy blocks to retain to a new buffer
blocks = new double[T->block_size * retain_count];
addresses = new int[T->dims * retain_count];
int block_offset = 0, addr_offset = 0;
for(int i=0; i < T->num_actual_tiles; i++)
{
if(retain[i] == true)
{
// Copy memory block
memcpy(blocks + block_offset, T->tensor_tiles + i * T->block_size, T->block_size * sizeof(double));
block_offset += T->block_size;
// Copy block address, and permute it as per the symmetry, if this block was retained due to symmetry
int* addr = addresses + addr_offset;
memcpy(addr, T->tile_address + i * T->dims, T->dims * sizeof(int));
if(retain_sym[i] != -1) // If this block was retained due to symmetry, permute its address
{
int dim = -1;
for(int j=0; j<num_dim; j++) // Find the deserializing dimension
{
if(T->SG_index_map[ext_T[j]] == T->SG_index_map[retain_sym[i]])
{
dim = j;
}
}
assert(dim != -1);
// Swap the indices such that the retained block has correct address
swap(&addr[dim], &addr[retain_sym[i]]);
}
addr_offset += T->dims;
}
}
// Copy blocks to be duplicated
for(list<int>::iterator it = duplicates.begin(); it != duplicates.end(); it++)
{
int i = *it; // tile number
memcpy(blocks + block_offset, T->tensor_tiles + i * T->block_size, T->block_size * sizeof(double));
block_offset += T->block_size;
memcpy(addresses + addr_offset, duplicate_addr[i], T->dims * sizeof(int));
addr_offset += T->dims;
}
// Free memory
delete[] sym_list;
delete[] retain;
delete[] retain_sym;
delete[] duplicate_addr;
return retain_count;
}
// Deserialize the 2D input tensor T if the other input tensor S is 4D and the output is 2D
Tensor* Contraction::deserialize_422(Tensor* &T, Tensor* &S)
{
double* blocks;
int* addresses;
int num_dim = 2;
int *des_tensor_dims = new int[2];
int *des_grid_dims = new int[2];
list<int>* sym_list = new list<int>[num_dim];
// Search for contracting dimensions in 4D tensor and find what dimension of
// physical grid they are distributed.
// Distribute corresponding contracting dimensions of the 2D input tensor
// along the same physical dimensions
for(int i=0; i < T->dims; i++)
{
des_tensor_dims[i] = i;
for(int j=0; j < S->dims; j++)
{
if(T->contr_dim_str[i].compare(S->contr_dim_str[j]) == 0)
{
des_grid_dims[i] = S->index_dimension_map[j];
}
}
}
if(T->SG_index_map[0] == T->SG_index_map[1] && T->SG_index_map[0] < 2)
{
sym_list[0] = list<int>();
sym_list[1] = list<int>();
sym_list[0].push_back(1);
sym_list[1].push_back(0);
}
int num_blocks = deserialize(T, blocks, addresses, num_dim, des_tensor_dims, des_grid_dims, sym_list);
return T->generate_deserialized_tensor(num_dim, des_tensor_dims, des_grid_dims, blocks, addresses, num_blocks);
}
// Collect data at instigator
// Processors send to instigator and instigator collects the data
int Contraction::instigate_collection(Tensor* &X,
int contr_dim,
int contr_idx,
double* &blocks,
int * &block_addrs)
{
if (DEBUG_I && rank == RRANK) cout<<"Rank "<<rank<<" Instigate send with contr_dim "<<contr_dim<<" and contr indx "<<contr_idx<<endl<<fflush;
int send_addr_count, send_data_count;
timer1 -= MPI_Wtime();
send_to_instigator_rect(X, contr_dim, contr_idx, send_addr_count, send_data_count);
timer1 += MPI_Wtime();
if (DEBUG_I && rank == RRANK) cout<<"Rank "<<rank<< " Instigate receive with contr_dim "<<contr_dim<<" and contr indx "<<contr_idx<<endl<<fflush;
int r = recv_at_instigator_rect(X, contr_dim, contr_idx, blocks, block_addrs, send_addr_count, send_data_count);
if (DEBUG_I && rank == RRANK) cout<<"Rank "<<rank<< " Returned from Instigate receive with contr_dim "<<contr_dim<<" and contr indx "<<contr_idx<<endl<<fflush;
return r;
}
// Send data to instigator
void Contraction::send_to_instigator(
Tensor* &X,
int contr_dim,
int contr_idx,
int &count_addr_sends,
int &count_data_sends
)
{
// Check if this node is responsible for sending bounce data
int** receivers;
int* matching_indices;
count_addr_sends = 0;
count_data_sends = 0;
// Find receivers for this processor
num_receivers = X->get_receivers(contr_dim, contr_idx, receivers, matching_indices);
if(num_receivers == 0)
return;
// Get receivers' ranks
grid->get_proc_ranks(num_receivers, receivers, current_receivers);
//if(rank == 1) cout << rank << " num_recvrs = " << num_receivers << " first = " << current_receivers[0] << endl << fflush;
// Since ranks are available, delete the memory allocated for receiver addresses
for(int i=0; i < num_receivers; i++)
{
delete[] receivers[i];
}
delete[] receivers;
//count the number of self sends
num_self_sends = 0;
for(int j = 0; j< num_receivers; j++)
{
if(current_receivers[j] == rank)
{
num_self_sends++;
}
else
{
count_addr_sends++;
}
}
//if(rank == rank) cout << rank << " num_self_sends = " << num_self_sends << endl << fflush;
// Allocate MPI_Requests so that we can wait on them later
send_req_addr = new MPI_Request[count_addr_sends];
send_req_data = new MPI_Request[count_addr_sends];
// Allocate memory for data that is already present in this processor
if(num_self_sends != 0)
{
self_adr_sizes = new int[num_self_sends];
self_buffers = new double*[num_self_sends];
self_addresses = new int*[num_self_sends];
self_data_sizes = new int[num_self_sends];
}
else
{
self_adr_sizes = 0;
self_buffers = 0;
self_addresses = 0;
self_data_sizes = 0;
}
num_self_sends = 0;
count_addr_sends = 0;
int addr_tag = 1;
int data_tag = 2;
for(int i=0; i < num_receivers; i++)
{
double* blocks;
int* block_addrs;
int data_size, addr_size;
MPI_Comm comm = MPI_COMM_WORLD;
//if(rank == 0) cout<<"matching index of reciever "<<i<<"is "<<matching_indices[i]<<endl;
//if(rank==0)X->print_all_tile_addr();
// Retrieve data to be sent
int num_tiles = X->getTiles(matching_indices[i], contr_idx, blocks, block_addrs);
//if(rank == rank) cout<<rank<< " num tiles from reciever "<<i<<" is "<<num_tiles<<endl;
// Permute virtual block address to match the contraction dimension
if(matching_indices[i] < contr_dim) // matching index comes before contracting dimension
{
for(int j = 0; j< num_tiles; j++)
{
for(int d = matching_indices[i]; d<contr_dim; d++)
{
block_addrs[j * grid_dims + d] = block_addrs[j * grid_dims + d + 1] ;
}
block_addrs[j * grid_dims + contr_dim] = contr_idx;
}
}
if(matching_indices[i] > contr_dim) // matching index comes after contracting dimension
{
for(int j = 0; j< num_tiles; j++)
{
int temp = block_addrs[ j*grid_dims + matching_indices[i]];
for(int d = matching_indices[i]; d>contr_dim; d--)
{
block_addrs[j * grid_dims + d] = block_addrs[j * grid_dims + d - 1] ;
}
block_addrs[j * grid_dims + contr_dim] = temp;
}
}
// Compute size of addresses
addr_size = num_tiles * X->get_dims() ;
// Size of data
data_size = num_tiles * X->block_size;
// Find receiver rank
int receiver = current_receivers[i];//get_proc_rank(receivers[i], grid_dims, pgrid);
//cout << rank << " receiver = "<<receiver<< endl;
// Send addresses and blocks if receivers are not this processor (sender)
if(receiver != rank)
{
// Send addresses
//cout << grid->get_proc_addr_str(rank) << " sending addr_size " << addr_size << " to " << grid->get_proc_addr_str(receiver) << endl;
//cout << rank << " sending addr_size " << addr_size << " to " << receiver << endl;
MPI_Isend(block_addrs, addr_size, MPI_INT, receiver, addr_tag, comm, &send_req_addr[count_addr_sends]);
count_addr_sends++;
// Send Data
if(addr_size)
{
//cout << grid->get_proc_addr_str(rank) << " sending data_size " << data_size << " to " << grid->get_proc_addr_str(receiver) << endl;
//cout << rank << " sending data_size " << data_size << " to " << receiver << endl;
MPI_Isend(blocks, data_size, MPI_DOUBLE, receiver, data_tag, comm, &send_req_data[count_data_sends]);
count_data_sends++;
}
}
else // if receiver is this processor (sender)
{
self_buffers[num_self_sends] = blocks;
self_addresses[num_self_sends] = block_addrs;
self_data_sizes[num_self_sends] = data_size;
self_adr_sizes[num_self_sends] = addr_size;
//if(rank==0){ printGetTiles(blocks, block_addrs, X->block_size, self_adr_sizes[num_self_sends]*sizeof(int)/X->dims, X->dims); }
num_self_sends++;
}
}
delete[] matching_indices;
}
// Receive data at instigator
int Contraction::recv_at_instigator (
Tensor* &X,
int contr_dim,
int contr_idx,
double* &blocks, // out
int* &block_addrs, // out
int send_addr_count,
int send_data_count)
{
// Find the processor dimension along which the contracting dim of tensor is distributed
int pindex = X->index_dimension_map[contr_dim];
// If this processor is not an instigator,
// then wait for all the sends from this processor to finish
if(contr_idx % pgrid[pindex] != my_address[pindex])
{
// Wait for address sends
MPI_Status* statuses_addr = new MPI_Status[send_addr_count];
MPI_Waitall(send_addr_count, send_req_addr, statuses_addr);
// Wait for data blocks sends
MPI_Status* statuses_data = new MPI_Status[send_data_count];
MPI_Waitall(send_data_count, send_req_data, statuses_data);
// Free memory
if(send_addr_count > 0)
{
delete[] send_req_addr;
delete[] statuses_addr;
}
if(send_data_count > 0)
{
delete[] send_req_data;
delete[] statuses_data;
}
return 0;
}
// Find all the processors to get the data from
int** bouncers;
int* senders;
num_senders = X->get_bouncers(contr_dim, bouncers);
grid->get_proc_ranks(num_senders, bouncers, senders);
current_senders = senders;
// Free memory allocated for bouncer addresses, now that ranks are stored
for(int i=0; i<num_senders; i++)
{
delete[] bouncers[i];
}
delete[] bouncers;
// Sizes of addresses and blocks to be received
int addr_size[num_senders];
int data_size[num_senders];
memset(addr_size, num_senders, 0);
memset(data_size, num_senders, 0);
// Temporarily hold data and addresses from each senders at locations pointed by these
int** temp_block_addrs = new int*[num_senders];
double** temp_blocks = new double*[num_senders];
MPI_Comm comm = MPI_COMM_WORLD;
MPI_Status status;
int tag;
// Probe the address messages sent and find out the number of blocks that are coming
for(int i=0; i<num_senders; i++)
{
if(senders[i] != rank)
{
//if(rank == rank) cout << rank << " Probing for msg from " << senders[i] << endl;
MPI_Probe(senders[i], 1, MPI_COMM_WORLD, &status);
MPI_Get_count(&status, MPI_INT, &addr_size[i]);
// Allocate memory for blocks and addresses using this count
if(addr_size[i] != MPI_UNDEFINED)
{
data_size[i] = (addr_size[i]/X->dims)*X->block_size;
temp_block_addrs[i] = new int[addr_size[i]];
temp_blocks[i] = new double[data_size[i]];
}
}
}
// Receive
for(int i=0; i<num_senders; i++)
{
MPI_Comm comm = MPI_COMM_WORLD;
if(senders[i] != rank)
{
// Receive addresses
if(addr_size[i] != MPI_UNDEFINED)
{
tag = 1;
//cout << grid->get_proc_addr_str(rank) << " receiving addr_size " << addr_size[i] << " to " << grid->get_proc_addr_str(senders[i]) << endl;
//cout << rank << " receiving addr_size " << addr_size[i] << " from " << senders[i] << endl;