-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathcontract.cc
More file actions
1156 lines (1064 loc) · 32.3 KB
/
contract.cc
File metadata and controls
1156 lines (1064 loc) · 32.3 KB
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
//
// Copyright 2018 The Simons Foundation, Inc. - All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//TODO: replace unordered_map with a simpler container (small_map? or jump directly to location?)
#include <unordered_map>
#include <future>
#include "itensor/util/multalloc.h"
#include "itensor/util/cputime.h"
#include "itensor/detail/algs.h"
#include "itensor/detail/gcounter.h"
#include "itensor/tensor/mat.h"
#include "itensor/tensor/contract.h"
#include "itensor/tensor/slicemat.h"
#include "itensor/tensor/sliceten.h"
#include "itensor/indexset.h"
#include "itensor/global.h"
using std::vector;
namespace itensor {
template<typename T>
void
printv(const vector<T>& t)
{
print("{ ");
for(const auto& i : t) print(i," ");
println("}");
}
template<typename T,typename F>
void
printv(const vector<T>& t,
const F& f)
{
print("{ ");
for(const auto& i : t)
{
f(i);
print(" ");
}
println("}");
}
template<typename T, size_t size>
void
printv(const std::array<T,size>& t)
{
print("{ ");
for(const auto& i : t) print(i," ");
println("}");
}
template<typename T>
void
printv(const autovector<T>& t)
{
print("{ ");
for(auto i = t.mini(); i <= t.maxi(); ++i)
{
print(t[i]," ");
}
println("}");
}
#define PRI(a) print(#a,": "); printv(a);
#define PRIL(a,l) print(#a,": "); printv(a,l);
//template<typename T>
//long
//find_index(vector<T> const& v,
// T const& t)
// {
// using size_type = typename vector<T>::size_type;
// for(size_type i = 0; i < v.size(); ++i)
// if(v[i] == t) return i;
// return -1;
// }
//
//template<typename T, size_t MaxSize>
//long
//find_index(const VarArray<T,MaxSize>& v,
// const T& t)
// {
// using size_type = typename VarArray<T,MaxSize>::size_type;
// for(size_type i = 0; i < v.size(); ++i)
// if(v[i] == t) return i;
// return -1;
// }
//
//template<typename T, size_t MaxSize>
//long
//find_index(const InfArray<T,MaxSize>& v,
// const T& t)
// {
// using size_type = typename InfArray<T,MaxSize>::size_type;
// for(size_type i = 0; i < v.size(); ++i)
// if(v[i] == t) return i;
// return -1;
// }
//
// small_map has an interface similar to std::map
// but can only contain N elements and uses linear search
//
template<typename A, typename B, int N = 30>
class small_map
{
public:
std::array<std::pair<A,B>,N> d;
int nd{0};
B&
operator[](const A& a)
{
for(int i = 0; i < nd; ++i)
{
if(d[i].first == a) return d[i].second;
}
if(++nd >= N) error("couldnt use small_map, nd too big");
d[nd-1] = std::make_pair(a,B{});
return d[nd-1].second;
}
};
// struct analyzing index pattern for C = A * B
struct CProps
{
Labels ai,
bi,
ci;
int nactiveA = 0,
nactiveB = 0,
nactiveC = 0;
private:
Labels AtoB_,
AtoC_,
BtoC_;
bool permuteA_ = false,
permuteB_ = false,
permuteC_ = false;
public:
using Dimension = size_t;
Dimension dleft = 1,
dmid = 1,
dright = 1;
int ncont = 0,
Acstart,
Bcstart,
Austart,
Bustart;
Permutation PA,
PB,
PC;
bool ctrans = false;
Range newArange,
newBrange,
newCrange;
CProps(Labels const& ai_,
Labels const& bi_,
Labels const& ci_)
:
ai(ai_),bi(bi_),ci(ci_),
Acstart(ai_.size()),
Bcstart(bi_.size()),
Austart(ai_.size()),
Bustart(bi_.size())
{ }
CProps(CProps const&) = delete;
CProps& operator=(CProps const&) = delete;
bool
contractedA(int i) const { return AtoC_[i] < 0; }
bool
contractedB(int i) const { return BtoC_[i] < 0; }
int
AtoB(int i) const { return AtoB_[i]; }
int
AtoC(int i) const { return AtoC_[i]; }
int
BtoC(int i) const { return BtoC_[i]; }
bool
permuteA() const { return permuteA_; }
bool
permuteB() const { return permuteB_; }
bool
permuteC() const { return permuteC_; }
bool
Atrans() const { return contractedA(0); }
bool
Btrans() const { return !contractedB(0); }
bool
Ctrans() const { return ctrans; }
private:
bool
checkACsameord() const
{
if(size_t(Austart) >= ai.size()) return true;
auto aCind = AtoC(Austart);
using size_type = decltype(ai.size());
for(size_type i = 0; i < ai.size(); ++i)
if(!contractedA(i))
{
if(AtoC(i) != aCind) return false;
++aCind;
}
return true;
}
bool
checkBCsameord() const
{
if(size_t(Bustart) >= bi.size()) return true;
auto bCind = BtoC(Bustart);
using size_type = decltype(bi.size());
for(size_type i = 0; i < bi.size(); ++i)
if(!contractedB(i))
{
if(BtoC(i) != bCind) return false;
++bCind;
}
return true;
}
public:
template<typename R, typename V1, typename V2>
void
compute(TenRefc<R,V1> A,
TenRefc<R,V2> B,
TenRefc<R,common_type<V1,V2>> C)
{
// Optimizations TODO
//
// o Add "automatic C" mode where index order of C can be
// unspecified, and will be chosen so as not to require
// permuting C at the end.
//
// o If trailing extent(j)==1 dimensions at end of A, B, or C indices
// (as often the case for ITensors with m==1 indices),
// have CProps resize ai, bi, and ci accordingly to avoid
// looping over these.
//
computePerms();
//Use PC.size() as a check to see if we've already run this
if(PC.size() != 0) return;
int ra = ai.size(),
rb = bi.size(),
rc = ci.size();
PC = Permutation(rc);
dleft = 1;
dmid = 1;
dright = 1;
int c = 0;
for(int i = 0; i < ra; ++i)
if(!contractedA(i))
{
dleft *= A.extent(i);
PC.setFromTo(c,AtoC(i));
++c;
}
else
{
dmid *= A.extent(i);
}
for(int j = 0; j < rb; ++j)
if(!contractedB(j))
{
dright *= B.extent(j);
PC.setFromTo(c,BtoC(j));
++c;
}
if(!isTrivial(PC))
{
permuteC_ = true;
if(checkBCsameord() && checkACsameord())
{
//Can avoid permuting C by
//computing Bt*At = Ct
ctrans = true;
permuteC_ = false;
}
}
//Check if A can be treated as a matrix without permuting
permuteA_ = false;
if(!(contractedA(0) || contractedA(ra-1)))
{
//If contracted indices are not all at front or back,
//will have to permute A
permuteA_ = true;
}
else
{
//Contracted ind start at front or back, check if contiguous
for(int i = 0; i < ncont; ++i)
if(!contractedA(Acstart+i))
{
//Contracted indices not contiguous, must permute
permuteA_ = true;
break;
}
}
// Check if B is matrix-like
permuteB_ = false;
if(!(contractedB(0) || contractedB(rb-1)))
{
//If contracted indices are not all at front or back,
//will have to permute B
permuteB_ = true;
}
else
{
for(int i = 0; i < ncont; ++i)
if(!contractedB(Bcstart+i))
{
//Contracted inds not contiguous, permute
permuteB_ = true;
break;
}
}
if(!permuteA_ && !permuteB_)
{
//Check if contracted inds. in same order
for(int i = 0; i < ncont; ++i)
if(AtoB(Acstart+i) != (Bcstart+i))
{
//If not in same order,
//must permute one of A or B
//so permute the smaller one
if(dleft < dright) permuteA_ = true;
else permuteB_ = true;
break;
}
}
if(permuteC_ && !(permuteA_ && permuteB_))
{
auto PCost = [](Real d) { return d*d; };
//Could avoid permuting C if
//permute both A and B, worth it?
auto pCcost = PCost(dleft*dright);
Real extra_pABcost = 0;
if(!permuteA_) extra_pABcost += PCost(dleft*dmid);
if(!permuteB_) extra_pABcost += PCost(dmid*dright);
if(extra_pABcost < pCcost)
{
//printfln("dleft=%d, dmid=%d, dright=%d",dleft,dmid,dright);
//if(Global::debug1()) printfln("Permuting %s %s (%d) instead of permuting C (%d)",permuteA_?"":"A",permuteB_?"":"B",extra_pABcost,pCcost);
permuteA_ = true;
permuteB_ = true;
permuteC_ = false;
}
}
//if(Global::debug1()) printfln("At line %d: permute A,B,C = %s,%s,%s",__LINE__,permuteA_,permuteB_,permuteC_);
if(permuteA_)
{
PA = Permutation(ra);
//Permute contracted indices to the front,
//in the same order as on B
int newi = 0;
auto bind = Bcstart;
for(int i = 0; i < ncont; ++i)
{
while(!contractedB(bind)) ++bind;
auto j = find_index(ai,bi[bind]);
PA.setFromTo(j,newi++);
++bind;
}
//Reset p.AtoC:
std::fill(AtoC_.begin(),AtoC_.end(),-1);
//Permute uncontracted indices to
//appear in same order as on C
for(int k = 0; k < rc; ++k)
{
auto j = find_index(ai,ci[k]);
if(j != -1)
{
AtoC_[newi] = k;
PA.setFromTo(j,newi);
++newi;
}
if(newi == ra) break;
}
//Also update Austart,Acstart
Acstart = ra;
Austart = ra;
for(decltype(ra) i = 0; i < ra; ++i)
{
if(contractedA(i))
Acstart = std::min(i,Acstart);
else
Austart = std::min(i,Austart);
}
newArange = permuteExtents(A.range(),PA);
}
if(permuteB_)
{
PB = Permutation(rb);
int newi = 0;
if(permuteA_)
{
//A's contracted indices already set to
//be in same order as B above, so just
//permute contracted indices to the front
//keeping relative order
for(int i = Bcstart; newi < ncont; ++newi)
{
while(!contractedB(i)) ++i;
PB.setFromTo(i++,newi);
}
}
else
{
//Permute contracted indices to the
//front and in same order as on A
auto aind = Acstart;
for(int i = 0; i < ncont; ++i)
{
while(!contractedA(aind)) ++aind;
int j = find_index(bi,ai[aind]);
PB.setFromTo(j,newi++);
++aind;
}
}
//Reset p.BtoC:
std::fill(BtoC_.begin(),BtoC_.end(),-1);
//Permute uncontracted indices to
//appear in same order as on C
for(int k = 0; k < rc; ++k)
{
auto j = find_index(bi,ci[k]);
if(j != -1)
{
BtoC_[newi] = k;
PB.setFromTo(j,newi);
++newi;
}
if(newi == rb) break;
}
Bcstart = rb;
Bustart = rb;
for(int i = 0; i < rb; ++i)
{
if(contractedB(i))
Bcstart = std::min(i,Bcstart);
else
Bustart = std::min(i,Bustart);
}
newBrange = permuteExtents(B.range(),PB);
}
if(permuteA_ || permuteB_)
{
//Recompute PC
int c = 0;
for(int i = 0; i < ra; ++i)
if(!contractedA(i))
{
PC.setFromTo(c,AtoC_[i]);
++c;
}
for(int j = 0; j < rb; ++j)
if(!contractedB(j))
{
PC.setFromTo(c,BtoC_[j]);
++c;
}
ctrans = false;
if(isTrivial(PC))
{
permuteC_ = false;
}
else
{
permuteC_ = true;
//Here we already know since pc_triv = false that
//at best indices from B precede those from A (on result C)
//so if both sets remain in same order on C
//just need to transpose C, not permute it
if(checkBCsameord() && checkACsameord())
{
ctrans = true;
permuteC_ = false;
}
}
}
//PRI(AtoC_)
//PRI(BtoC_)
//Print(PC);
if(permuteC_)
{
auto Rb = RangeBuilder(rc);
if(!permuteA_)
{
for(decltype(ra) i = 0; i < ra; ++i)
if(!contractedA(i))
Rb.nextIndex(A.extent(i));
}
else
{
for(decltype(ra) i = 0; i < ra; ++i)
if(!contractedA(i))
Rb.nextIndex(newArange.extent(i));
}
if(!permuteB_)
{
for(decltype(rb) j = 0; j < rb; ++j)
if(!contractedB(j))
Rb.nextIndex(B.extent(j));
}
else
{
for(decltype(rb) j = 0; j < rb; ++j)
if(!contractedB(j))
Rb.nextIndex(newBrange.extent(j));
}
newCrange = Rb.build();
}
}
void
computePerms()
{
//Use !AtoB.empty() as a check to see if we've already run this
if(!AtoB_.empty()) return;
int na = ai.size(),
nb = bi.size(),
nc = ci.size();
AtoB_ = Labels(na,-1);
AtoC_ = Labels(na,-1);
BtoC_ = Labels(nb,-1);
for(int i = 0; i < na; ++i)
{
for(int j = 0; j < nb; ++j)
if(ai[i] == bi[j])
{
++ncont;
if(i < Acstart) Acstart = i;
if(j < Bcstart) Bcstart = j;
AtoB_[i] = j;
break;
}
}
for(int i = 0; i < na; ++i)
{
for(int k = 0; k < nc; ++k)
if(ai[i] == ci[k])
{
if(i < Austart) Austart = i;
AtoC_[i] = k;
break;
}
}
for(int j = 0; j < nb; ++j)
{
for(int k = 0; k < nc; ++k)
if(bi[j] == ci[k])
{
if(j < Bustart) Bustart = j;
BtoC_[j] = k;
break;
}
}
//PRI(AtoB_)
//PRI(AtoC_)
//PRI(BtoC_)
}
void
computeNactive()
{
// Out of A, B and C (C = A*B), each index appears in two tensors.
// An active index appears as one of the first two indices in each of the two
// tensor in which it appears. More specifically:
// the first index of a tensor is active if its pair is also a first index, or if its
// pair is a second index and that tensor's first index is active.
// indval is the number of times an index appears among the first two of each tensor
// Good indices have indval == 2, bad ones have indval == 1
if(ai.size() < 2 || bi.size() < 2 || ci.size() < 2)
{
nactiveA = nactiveB = nactiveC = 0;
return;
}
small_map<int,int> indval;
for(int i = 0; i <= 1; ++i)
{
++indval[ai[i]];
++indval[bi[i]];
++indval[ci[i]];
}
for(int elim = 1; elim <= 3; ++elim) // bad guys at position 0 kill the index at 1
{
if(indval[ai[0]] == 1) indval[ai[1]] = 1;
if(indval[bi[0]] == 1) indval[bi[1]] = 1;
if(indval[ci[0]] == 1) indval[ci[1]] = 1;
}
nactiveA = (indval[ai[0]] == 1 ? 0 : (indval[ai[1]] == 1 ? 1 : 2));
nactiveB = (indval[bi[0]] == 1 ? 0 : (indval[bi[1]] == 1 ? 1 : 2));
nactiveC = (indval[ci[0]] == 1 ? 0 : (indval[ci[1]] == 1 ? 1 : 2));
}
};
struct ABoffC
{
MatrixRefc mA,
mB;
MatrixRef mC;
int offC;
ABoffC(MatrixRefc& mA_,
MatrixRefc& mB_,
MatrixRef& mC_,
int offC_)
:
mA(mA_),
mB(mB_),
mC(mC_),
offC(offC_)
{ }
void
execute() const { multAdd(mA,mB,mC); }
};
class CABqueue
{
std::unordered_map<int,vector<ABoffC>> subtask;
public:
CABqueue() { }
void
addtask(MatrixRefc& mA,
MatrixRefc& mB,
MatrixRef& mC,
int offC)
{
subtask[offC].emplace_back(mA,mB,mC,offC);
}
void
run(int numthread)
{
//////////
////Analyze tasks:
//println("number of distinct offCs is ",subtask.size());
//int ttasks = 0;
//for(auto& st : subtask) ttasks += st.second.size();
//println("number of distinct tasks is ",ttasks);
//size_t maxj = 0;
//for(const auto& st : subtask)
// maxj = std::max(st.second.size(),maxj);
//println("max subtask size is ",maxj);
//////////
//Loop over threads in a round-robin fashion.
//Assign all tasks with the same memory
//destination (offC) to the same thread.
vector<vector<ABoffC>> threadtask(numthread);
int ss = 0;
for(auto& t : subtask)
{
auto& st_tasks = t.second;
auto& tt = threadtask[ss];
std::move(st_tasks.begin(),st_tasks.end(),std::inserter(tt,tt.end()));
ss = (ss+1)%numthread;
}
//"Package" thread tasks into std::future objects
//which begin running once they are created
vector<std::future<void>> futs(numthread);
assert(threadtask.size()==futs.size());
for(size_t i = 0; i < futs.size(); ++i)
{
auto& tt = threadtask[i];
//printfln("task size for thread %d is %d",i,tt.size());
futs[i] = std::async(std::launch::async,
[&tt]()
{
for(const auto& task : tt)
task.execute();
}
);
}
//Wait for futures to complete
for(auto& ft : futs)
{
ft.wait();
}
}
};
template<typename range_t, typename VA, typename VB>
void
contract(CProps const& p,
TenRefc<range_t,VA> A,
TenRefc<range_t,VB> B,
TenRef<range_t,common_type<VA,VB>> C,
Real alpha = 1.,
Real beta = 0.)
{
using VC = common_type<VA,VB>;
auto Apsize = p.permuteA() ? dim(p.newArange) : 0ul;
auto Bpsize = p.permuteB() ? dim(p.newBrange) : 0ul;
auto Cpsize = p.permuteC() ? dim(p.newCrange) : 0ul;
auto Abufsize = isCplx(A) ? 2ul*Apsize : Apsize;
auto Bbufsize = isCplx(B) ? 2ul*Bpsize : Bpsize;
auto Cbufsize = isCplx(C) ? 2ul*Cpsize : Cpsize;
auto d = vector_no_init<Real>(Abufsize+Bbufsize+Cbufsize);
auto ab = MAKE_SAFE_PTR(d.data(),d.size());
auto bb = ab+Abufsize;
auto cb = bb+Bbufsize;
MatRefc<VA> aref;
if(p.permuteA())
{
auto aptr = SAFE_REINTERPRET(VA,ab);
auto tref = makeTenRef(SAFE_PTR_GET(aptr,Apsize),Apsize,&p.newArange);
tref &= permute(A,p.PA);
aref = transpose(makeMatRefc(tref.store(),p.dmid,p.dleft));
}
else
{
if(p.Atrans())
{
aref = transpose(makeMatRefc(A.store(),p.dmid,p.dleft));
}
else
{
aref = makeMatRefc(A.store(),p.dleft,p.dmid);
}
}
MatRefc<VB> bref;
if(p.permuteB())
{
auto bptr = SAFE_REINTERPRET(VB,bb);
auto tref = makeTenRef(SAFE_PTR_GET(bptr,Bpsize),Bpsize,&p.newBrange);
tref &= permute(B,p.PB);
bref = makeMatRefc(tref.store(),p.dmid,p.dright);
}
else
{
if(p.Btrans())
{
bref = transpose(makeMatRefc(B.store(),p.dright,p.dmid));
}
else
{
bref = makeMatRefc(B.store(),p.dmid,p.dright);
}
}
MatRef<VC> cref;
TenRef<Range,VC> newC;
if(p.permuteC())
{
auto cptr = SAFE_REINTERPRET(VC,cb);
newC = makeTenRef(SAFE_PTR_GET(cptr,Cpsize),Cpsize,&p.newCrange);
cref = makeMatRef(newC.store(),nrows(aref),ncols(bref));
}
else
{
if(p.Ctrans())
{
cref = transpose(makeMatRef(C.store(),ncols(bref),nrows(aref)));
}
else
{
cref = makeMatRef(C.store(),nrows(aref),ncols(bref));
}
}
gemm(aref,bref,cref,alpha,beta);
if(p.permuteC())
{
#ifdef DEBUG
if(isTrivial(p.PC)) Error("Calling permute in contract with a trivial permutation");
#endif
C &= permute(newC,p.PC);
}
}
template<typename R, typename T1, typename T2>
void
contractScalar(T1 a,
TenRefc<R,T2> B, Labels const& bi,
TenRef<R,common_type<T1,T2>> C, Labels const& ci,
Real alpha,
Real beta)
{
using T3 = common_type<T1,T2>;
auto fac = alpha*a;
auto PB = permute(B,calcPerm(bi,ci));
if(beta == 0)
transform(PB,C,[fac](T2 b, T3& c){ c = fac*b; });
else
transform(PB,C,[fac,beta](T2 b, T3& c){ c = fac*b+beta*c; });
}
template<typename RangeT, typename VA, typename VB>
void
contract(TenRefc<RangeT,VA> A, Labels const& ai,
TenRefc<RangeT,VB> B, Labels const& bi,
TenRef<RangeT,common_type<VA,VB>> C,
Labels const& ci,
Real alpha,
Real beta)
{
if(ai.empty())
{
contractScalar(*A.data(),B,bi,C,ci,alpha,beta);
}
else if(bi.empty())
{
contractScalar(*B.data(),A,ai,C,ci,alpha,beta);
}
else
{
CProps props(ai,bi,ci);
props.compute(A,B,C);
contract(props,A,B,C,alpha,beta);
}
}
//Explicit template instantiations:
template void
contract(TenRefc<Range,Real>, Labels const&,
TenRefc<Range,Real>, Labels const&,
TenRef<Range,Real> , Labels const&,
Real,Real);
template void
contract(TenRefc<Range,Cplx>, Labels const&,
TenRefc<Range,Real>, Labels const&,
TenRef<Range,Cplx> , Labels const&,
Real,Real);
template void
contract(TenRefc<Range,Real>, Labels const&,
TenRefc<Range,Cplx>, Labels const&,
TenRef<Range,Cplx> , Labels const&,
Real,Real);
template void
contract(TenRefc<Range,Cplx>, Labels const&,
TenRefc<Range,Cplx>, Labels const&,
TenRef<Range,Cplx> , Labels const&,
Real,Real);
template void
contract(TenRefc<IndexSet,Real>, Labels const&,
TenRefc<IndexSet,Real>, Labels const&,
TenRef<IndexSet,Real> , Labels const&,
Real,Real);
template void
contract(TenRefc<IndexSet,Cplx>, Labels const&,
TenRefc<IndexSet,Real>, Labels const&,
TenRef<IndexSet,Cplx> , Labels const&,
Real,Real);
template void
contract(TenRefc<IndexSet,Real>, Labels const&,
TenRefc<IndexSet,Cplx>, Labels const&,
TenRef<IndexSet,Cplx> , Labels const&,
Real,Real);
template void
contract(TenRefc<IndexSet,Cplx>, Labels const&,
TenRefc<IndexSet,Cplx>, Labels const&,
TenRef<IndexSet,Cplx> , Labels const&,
Real,Real);
struct MultInfo
{
bool tA = false,
tB = false,
Bfirst = false;
MultInfo() {}
};
MultInfo static
computeMultInfo(Labels const& ai,
Labels const& bi,
Labels const& ci)
{
MultInfo I;
if(ai[1] == ci[1])
{
if(ai[0] == bi[1]) // Bik Akj = Cij, mC += mB * mA
{
I.Bfirst = true;
//println("=======Case 1==========");
}
else //ai[0] == bi[0]) Bki Akj = C_ij, mC += mBt * mA
{
I.tB = true;
I.Bfirst = true;
//println("=======Case 2==========");
}
}
else if(ai[1] == ci[0])
{
if(ai[0] == bi[1]) // Aki Bjk = Cij, mCt += mAt * mBt;
{
I.tA = true;
I.tB = true;
//println("=======Case 3==========");
}
else //ai[0] == bi[0]) Aki Bkj = Cij, mC += mAt * mB
{
I.tA = true;
//println("=======Case 4==========");
}
}
else if(ai[1] == bi[1])
{
if(ai[0] == ci[1]) // Bik Ajk = Cij, mC += mB * mAt
{
I.Bfirst = true;
I.tA = true;
//println("=======Case 5==========");
}
else //ai[0] == ci[0]) Aik Bjk = Cij, mC += mA * mBt
{
I.tB = true;
//println("=======Case 6==========");
}
}
else if(ai[1] == bi[0])
{
if(ai[0] == ci[1]) // Bki Ajk = Cij, mC += mBt * mAt
{
I.Bfirst = true;
I.tA = true;
I.tB = true;
//println("=======Case 7==========");
}
else //ai[0] == ci[0], Aik Bkj = Cij, mC += mA * mB
{
//println("=======Case 8==========");
}
}
return I;
}
template<typename RangeT>
void
contractloop(TenRefc<RangeT> A, Labels const& ai,
TenRefc<RangeT> B, Labels const& bi,
TenRef<RangeT> C, Labels const& ci,
Args const& args)
{
if(ai.empty() || bi.empty())
{
contract(A,ai,B,bi,C,ci);
return;
}
CProps p(ai,bi,ci);
p.computeNactive();
//printfln("nactive A, B, C are %d %d %d",p.nactiveA,p.nactiveB,p.nactiveC);
if(p.nactiveA != 2 || p.nactiveB != 2 || p.nactiveC != 2)
{