-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathtest_util.cpp
2882 lines (2461 loc) · 74.6 KB
/
test_util.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
#include <complex>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <short.h>
#if defined(QMP_COMMS)
#include <qmp.h>
#elif defined(MPI_COMMS)
#include <mpi.h>
#endif
#include <wilson_dslash_reference.h>
#include <test_util.h>
#include <dslash_quda.h>
#include "misc.h"
using namespace std;
#define XUP 0
#define YUP 1
#define ZUP 2
#define TUP 3
int Z[4];
int V;
int Vh;
int Vs_x, Vs_y, Vs_z, Vs_t;
int Vsh_x, Vsh_y, Vsh_z, Vsh_t;
int faceVolume[4];
//extended volume, +4
int E1, E1h, E2, E3, E4;
int E[4];
int V_ex, Vh_ex;
int Ls;
int V5;
int V5h;
int mySpinorSiteSize;
extern float fat_link_max;
/**
* For MPI, the default node mapping is lexicographical with t varying fastest.
*/
int gridsize_from_cmdline[4] = {1,1,1,1};
static int lex_rank_from_coords_t(const int *coords, void *fdata)
{
int rank = coords[0];
for (int i = 1; i < 4; i++) {
rank = gridsize_from_cmdline[i] * rank + coords[i];
}
return rank;
}
static int lex_rank_from_coords_x(const int *coords, void *fdata)
{
int rank = coords[3];
for (int i = 2; i >= 0; i--) {
rank = gridsize_from_cmdline[i] * rank + coords[i];
}
return rank;
}
static int rank_order = 0;
void initComms(int argc, char **argv, const int *commDims)
{
#if defined(QMP_COMMS)
QMP_thread_level_t tl;
QMP_init_msg_passing(&argc, &argv, QMP_THREAD_SINGLE, &tl);
// FIXME? - tests crash without this
QMP_declare_logical_topology(commDims, 4);
#elif defined(MPI_COMMS)
#ifdef PTHREADS
int provided;
MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
#else
MPI_Init(&argc, &argv);
#endif
#endif
QudaCommsMap func = rank_order == 0 ? lex_rank_from_coords_t : lex_rank_from_coords_x;
initCommsGridQuda(4, commDims, func, NULL);
initRand();
printfQuda("Rank order is %s major (%s running fastest)\n",
rank_order == 0 ? "column" : "row", rank_order == 0 ? "t" : "x");
#ifdef HAVE_QIO
int partitioned = 0;
for (int i=0; i<4; i++) if (comm_dim(i) > 1) partitioned++;
if (rank_order == 0 && partitioned > 1)
errorQuda("Use of QIO is not supported with column-major process ordering, use row-major instead (--rank-order row)");
#endif
}
void finalizeComms()
{
#if defined(QMP_COMMS)
QMP_finalize_msg_passing();
#elif defined(MPI_COMMS)
MPI_Finalize();
#endif
}
void initRand()
{
int rank = 0;
#if defined(QMP_COMMS)
rank = QMP_get_node_number();
#elif defined(MPI_COMMS)
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
#endif
srand(17*rank + 137);
}
void setDims(int *X) {
V = 1;
for (int d=0; d< 4; d++) {
V *= X[d];
Z[d] = X[d];
faceVolume[d] = 1;
for (int i=0; i<4; i++) {
if (i==d) continue;
faceVolume[d] *= X[i];
}
}
Vh = V/2;
Vs_x = X[1]*X[2]*X[3];
Vs_y = X[0]*X[2]*X[3];
Vs_z = X[0]*X[1]*X[3];
Vs_t = X[0]*X[1]*X[2];
Vsh_x = Vs_x/2;
Vsh_y = Vs_y/2;
Vsh_z = Vs_z/2;
Vsh_t = Vs_t/2;
E1=X[0]+4; E2=X[1]+4; E3=X[2]+4; E4=X[3]+4;
E1h=E1/2;
E[0] = E1;
E[1] = E2;
E[2] = E3;
E[3] = E4;
V_ex = E1*E2*E3*E4;
Vh_ex = V_ex/2;
}
void dw_setDims(int *X, const int L5)
{
V = 1;
for (int d=0; d< 4; d++)
{
V *= X[d];
Z[d] = X[d];
faceVolume[d] = 1;
for (int i=0; i<4; i++) {
if (i==d) continue;
faceVolume[d] *= X[i];
}
}
Vh = V/2;
Ls = L5;
V5 = V*Ls;
V5h = Vh*Ls;
Vs_t = Z[0]*Z[1]*Z[2]*Ls;//?
Vsh_t = Vs_t/2; //?
}
void setSpinorSiteSize(int n)
{
mySpinorSiteSize = n;
}
template <typename Float>
static void printVector(Float *v) {
printfQuda("{(%f %f) (%f %f) (%f %f)}\n", v[0], v[1], v[2], v[3], v[4], v[5]);
}
// X indexes the lattice site
void printSpinorElement(void *spinor, int X, QudaPrecision precision) {
if (precision == QUDA_DOUBLE_PRECISION)
for (int s=0; s<4; s++) printVector((double*)spinor+X*24+s*6);
else
for (int s=0; s<4; s++) printVector((float*)spinor+X*24+s*6);
}
// X indexes the full lattice
void printGaugeElement(void *gauge, int X, QudaPrecision precision) {
if (getOddBit(X) == 0) {
if (precision == QUDA_DOUBLE_PRECISION)
for (int m=0; m<3; m++) printVector((double*)gauge +(X/2)*gaugeSiteSize + m*3*2);
else
for (int m=0; m<3; m++) printVector((float*)gauge +(X/2)*gaugeSiteSize + m*3*2);
} else {
if (precision == QUDA_DOUBLE_PRECISION)
for (int m = 0; m < 3; m++) printVector((double*)gauge + (X/2+Vh)*gaugeSiteSize + m*3*2);
else
for (int m = 0; m < 3; m++) printVector((float*)gauge + (X/2+Vh)*gaugeSiteSize + m*3*2);
}
}
// returns 0 or 1 if the full lattice index X is even or odd
int getOddBit(int Y) {
int x4 = Y/(Z[2]*Z[1]*Z[0]);
int x3 = (Y/(Z[1]*Z[0])) % Z[2];
int x2 = (Y/Z[0]) % Z[1];
int x1 = Y % Z[0];
return (x4+x3+x2+x1) % 2;
}
// a+=b
template <typename Float>
inline void complexAddTo(Float *a, Float *b) {
a[0] += b[0];
a[1] += b[1];
}
// a = b*c
template <typename Float>
inline void complexProduct(Float *a, Float *b, Float *c) {
a[0] = b[0]*c[0] - b[1]*c[1];
a[1] = b[0]*c[1] + b[1]*c[0];
}
// a = conj(b)*conj(c)
template <typename Float>
inline void complexConjugateProduct(Float *a, Float *b, Float *c) {
a[0] = b[0]*c[0] - b[1]*c[1];
a[1] = -b[0]*c[1] - b[1]*c[0];
}
// a = conj(b)*c
template <typename Float>
inline void complexDotProduct(Float *a, Float *b, Float *c) {
a[0] = b[0]*c[0] + b[1]*c[1];
a[1] = b[0]*c[1] - b[1]*c[0];
}
// a += b*c
template <typename Float>
inline void accumulateComplexProduct(Float *a, Float *b, Float *c, Float sign) {
a[0] += sign*(b[0]*c[0] - b[1]*c[1]);
a[1] += sign*(b[0]*c[1] + b[1]*c[0]);
}
// a += conj(b)*c)
template <typename Float>
inline void accumulateComplexDotProduct(Float *a, Float *b, Float *c) {
a[0] += b[0]*c[0] + b[1]*c[1];
a[1] += b[0]*c[1] - b[1]*c[0];
}
template <typename Float>
inline void accumulateConjugateProduct(Float *a, Float *b, Float *c, int sign) {
a[0] += sign * (b[0]*c[0] - b[1]*c[1]);
a[1] -= sign * (b[0]*c[1] + b[1]*c[0]);
}
template <typename Float>
inline void su3Construct12(Float *mat) {
Float *w = mat+12;
w[0] = 0.0;
w[1] = 0.0;
w[2] = 0.0;
w[3] = 0.0;
w[4] = 0.0;
w[5] = 0.0;
}
// Stabilized Bunk and Sommer
template <typename Float>
inline void su3Construct8(Float *mat) {
mat[0] = atan2(mat[1], mat[0]);
mat[1] = atan2(mat[13], mat[12]);
for (int i=8; i<18; i++) mat[i] = 0.0;
}
void su3_construct(void *mat, QudaReconstructType reconstruct, QudaPrecision precision) {
if (reconstruct == QUDA_RECONSTRUCT_12) {
if (precision == QUDA_DOUBLE_PRECISION) su3Construct12((double*)mat);
else su3Construct12((float*)mat);
} else {
if (precision == QUDA_DOUBLE_PRECISION) su3Construct8((double*)mat);
else su3Construct8((float*)mat);
}
}
// given first two rows (u,v) of SU(3) matrix mat, reconstruct the third row
// as the cross product of the conjugate vectors: w = u* x v*
//
// 48 flops
template <typename Float>
static void su3Reconstruct12(Float *mat, int dir, int ga_idx, QudaGaugeParam *param) {
Float *u = &mat[0*(3*2)];
Float *v = &mat[1*(3*2)];
Float *w = &mat[2*(3*2)];
w[0] = 0.0; w[1] = 0.0; w[2] = 0.0; w[3] = 0.0; w[4] = 0.0; w[5] = 0.0;
accumulateConjugateProduct(w+0*(2), u+1*(2), v+2*(2), +1);
accumulateConjugateProduct(w+0*(2), u+2*(2), v+1*(2), -1);
accumulateConjugateProduct(w+1*(2), u+2*(2), v+0*(2), +1);
accumulateConjugateProduct(w+1*(2), u+0*(2), v+2*(2), -1);
accumulateConjugateProduct(w+2*(2), u+0*(2), v+1*(2), +1);
accumulateConjugateProduct(w+2*(2), u+1*(2), v+0*(2), -1);
Float u0 = (dir < 3 ? param->anisotropy :
(ga_idx >= (Z[3]-1)*Z[0]*Z[1]*Z[2]/2 ? param->t_boundary : 1));
w[0]*=u0; w[1]*=u0; w[2]*=u0; w[3]*=u0; w[4]*=u0; w[5]*=u0;
}
template <typename Float>
static void su3Reconstruct8(Float *mat, int dir, int ga_idx, QudaGaugeParam *param) {
// First reconstruct first row
Float row_sum = 0.0;
row_sum += mat[2]*mat[2];
row_sum += mat[3]*mat[3];
row_sum += mat[4]*mat[4];
row_sum += mat[5]*mat[5];
Float u0 = (dir < 3 ? param->anisotropy :
(ga_idx >= (Z[3]-1)*Z[0]*Z[1]*Z[2]/2 ? param->t_boundary : 1));
Float U00_mag = sqrt(1.f/(u0*u0) - row_sum);
mat[14] = mat[0];
mat[15] = mat[1];
mat[0] = U00_mag * cos(mat[14]);
mat[1] = U00_mag * sin(mat[14]);
Float column_sum = 0.0;
for (int i=0; i<2; i++) column_sum += mat[i]*mat[i];
for (int i=6; i<8; i++) column_sum += mat[i]*mat[i];
Float U20_mag = sqrt(1.f/(u0*u0) - column_sum);
mat[12] = U20_mag * cos(mat[15]);
mat[13] = U20_mag * sin(mat[15]);
// First column now restored
// finally reconstruct last elements from SU(2) rotation
Float r_inv2 = 1.0/(u0*row_sum);
// U11
Float A[2];
complexDotProduct(A, mat+0, mat+6);
complexConjugateProduct(mat+8, mat+12, mat+4);
accumulateComplexProduct(mat+8, A, mat+2, u0);
mat[8] *= -r_inv2;
mat[9] *= -r_inv2;
// U12
complexConjugateProduct(mat+10, mat+12, mat+2);
accumulateComplexProduct(mat+10, A, mat+4, -u0);
mat[10] *= r_inv2;
mat[11] *= r_inv2;
// U21
complexDotProduct(A, mat+0, mat+12);
complexConjugateProduct(mat+14, mat+6, mat+4);
accumulateComplexProduct(mat+14, A, mat+2, -u0);
mat[14] *= r_inv2;
mat[15] *= r_inv2;
// U12
complexConjugateProduct(mat+16, mat+6, mat+2);
accumulateComplexProduct(mat+16, A, mat+4, u0);
mat[16] *= -r_inv2;
mat[17] *= -r_inv2;
}
void su3_reconstruct(void *mat, int dir, int ga_idx, QudaReconstructType reconstruct, QudaPrecision precision, QudaGaugeParam *param) {
if (reconstruct == QUDA_RECONSTRUCT_12) {
if (precision == QUDA_DOUBLE_PRECISION) su3Reconstruct12((double*)mat, dir, ga_idx, param);
else su3Reconstruct12((float*)mat, dir, ga_idx, param);
} else {
if (precision == QUDA_DOUBLE_PRECISION) su3Reconstruct8((double*)mat, dir, ga_idx, param);
else su3Reconstruct8((float*)mat, dir, ga_idx, param);
}
}
/*
void su3_construct_8_half(float *mat, short *mat_half) {
su3Construct8(mat);
mat_half[0] = floatToShort(mat[0] / M_PI);
mat_half[1] = floatToShort(mat[1] / M_PI);
for (int i=2; i<18; i++) {
mat_half[i] = floatToShort(mat[i]);
}
}
void su3_reconstruct_8_half(float *mat, short *mat_half, int dir, int ga_idx, QudaGaugeParam *param) {
for (int i=0; i<18; i++) {
mat[i] = shortToFloat(mat_half[i]);
}
mat[0] *= M_PI;
mat[1] *= M_PI;
su3Reconstruct8(mat, dir, ga_idx, param);
}*/
template <typename Float>
static int compareFloats(Float *a, Float *b, int len, double epsilon) {
for (int i = 0; i < len; i++) {
double diff = fabs(a[i] - b[i]);
if (diff > epsilon) {
printfQuda("ERROR: i=%d, a[%d]=%f, b[%d]=%f\n", i, i, a[i], i, b[i]);
return 0;
}
}
return 1;
}
int compare_floats(void *a, void *b, int len, double epsilon, QudaPrecision precision) {
if (precision == QUDA_DOUBLE_PRECISION) return compareFloats((double*)a, (double*)b, len, epsilon);
else return compareFloats((float*)a, (float*)b, len, epsilon);
}
int fullLatticeIndex(int dim[4], int index, int oddBit){
int za = index/(dim[0]>>1);
int zb = za/dim[1];
int x2 = za - zb*dim[1];
int x4 = zb/dim[2];
int x3 = zb - x4*dim[2];
return 2*index + ((x2 + x3 + x4 + oddBit) & 1);
}
// given a "half index" i into either an even or odd half lattice (corresponding
// to oddBit = {0, 1}), returns the corresponding full lattice index.
int fullLatticeIndex(int i, int oddBit) {
/*
int boundaryCrossings = i/(Z[0]/2) + i/(Z[1]*Z[0]/2) + i/(Z[2]*Z[1]*Z[0]/2);
return 2*i + (boundaryCrossings + oddBit) % 2;
*/
int X1 = Z[0];
int X2 = Z[1];
int X3 = Z[2];
//int X4 = Z[3];
int X1h =X1/2;
int sid =i;
int za = sid/X1h;
//int x1h = sid - za*X1h;
int zb = za/X2;
int x2 = za - zb*X2;
int x4 = zb/X3;
int x3 = zb - x4*X3;
int x1odd = (x2 + x3 + x4 + oddBit) & 1;
//int x1 = 2*x1h + x1odd;
int X = 2*sid + x1odd;
return X;
}
// i represents a "half index" into an even or odd "half lattice".
// when oddBit={0,1} the half lattice is {even,odd}.
//
// the displacements, such as dx, refer to the full lattice coordinates.
//
// neighborIndex() takes a "half index", displaces it, and returns the
// new "half index", which can be an index into either the even or odd lattices.
// displacements of magnitude one always interchange odd and even lattices.
//
int neighborIndex(int i, int oddBit, int dx4, int dx3, int dx2, int dx1) {
int Y = fullLatticeIndex(i, oddBit);
int x4 = Y/(Z[2]*Z[1]*Z[0]);
int x3 = (Y/(Z[1]*Z[0])) % Z[2];
int x2 = (Y/Z[0]) % Z[1];
int x1 = Y % Z[0];
// assert (oddBit == (x+y+z+t)%2);
x4 = (x4+dx4+Z[3]) % Z[3];
x3 = (x3+dx3+Z[2]) % Z[2];
x2 = (x2+dx2+Z[1]) % Z[1];
x1 = (x1+dx1+Z[0]) % Z[0];
return (x4*(Z[2]*Z[1]*Z[0]) + x3*(Z[1]*Z[0]) + x2*(Z[0]) + x1) / 2;
}
int neighborIndex(int dim[4], int index, int oddBit, int dx[4]){
const int fullIndex = fullLatticeIndex(dim, index, oddBit);
int x[4];
x[3] = fullIndex/(dim[2]*dim[1]*dim[0]);
x[2] = (fullIndex/(dim[1]*dim[0])) % dim[2];
x[1] = (fullIndex/dim[0]) % dim[1];
x[0] = fullIndex % dim[0];
for(int dir=0; dir<4; ++dir)
x[dir] = (x[dir]+dx[dir]+dim[dir]) % dim[dir];
return (((x[3]*dim[2] + x[2])*dim[1] + x[1])*dim[0] + x[0])/2;
}
int
neighborIndex_mg(int i, int oddBit, int dx4, int dx3, int dx2, int dx1)
{
int ret;
int Y = fullLatticeIndex(i, oddBit);
int x4 = Y/(Z[2]*Z[1]*Z[0]);
int x3 = (Y/(Z[1]*Z[0])) % Z[2];
int x2 = (Y/Z[0]) % Z[1];
int x1 = Y % Z[0];
int ghost_x4 = x4+ dx4;
// assert (oddBit == (x+y+z+t)%2);
x4 = (x4+dx4+Z[3]) % Z[3];
x3 = (x3+dx3+Z[2]) % Z[2];
x2 = (x2+dx2+Z[1]) % Z[1];
x1 = (x1+dx1+Z[0]) % Z[0];
if ( (ghost_x4 >= 0 && ghost_x4 < Z[3]) || !comm_dim_partitioned(3)){
ret = (x4*(Z[2]*Z[1]*Z[0]) + x3*(Z[1]*Z[0]) + x2*(Z[0]) + x1) / 2;
}else{
ret = (x3*(Z[1]*Z[0]) + x2*(Z[0]) + x1) / 2;
}
return ret;
}
/*
* This is a computation of neighbor using the full index and the displacement in each direction
*
*/
int
neighborIndexFullLattice(int i, int dx4, int dx3, int dx2, int dx1)
{
int oddBit = 0;
int half_idx = i;
if (i >= Vh){
oddBit =1;
half_idx = i - Vh;
}
int nbr_half_idx = neighborIndex(half_idx, oddBit, dx4,dx3,dx2,dx1);
int oddBitChanged = (dx4+dx3+dx2+dx1)%2;
if (oddBitChanged){
oddBit = 1 - oddBit;
}
int ret = nbr_half_idx;
if (oddBit){
ret = Vh + nbr_half_idx;
}
return ret;
}
int
neighborIndexFullLattice(int dim[4], int index, int dx[4])
{
const int volume = dim[0]*dim[1]*dim[2]*dim[3];
const int halfVolume = volume/2;
int oddBit = 0;
int halfIndex = index;
if(index >= halfVolume){
oddBit = 1;
halfIndex = index - halfVolume;
}
int neighborHalfIndex = neighborIndex(dim, halfIndex, oddBit, dx);
int oddBitChanged = (dx[0]+dx[1]+dx[2]+dx[3])%2;
if(oddBitChanged){
oddBit = 1 - oddBit;
}
return neighborHalfIndex + oddBit*halfVolume;
}
int
neighborIndexFullLattice_mg(int i, int dx4, int dx3, int dx2, int dx1)
{
int ret;
int oddBit = 0;
int half_idx = i;
if (i >= Vh){
oddBit =1;
half_idx = i - Vh;
}
int Y = fullLatticeIndex(half_idx, oddBit);
int x4 = Y/(Z[2]*Z[1]*Z[0]);
int x3 = (Y/(Z[1]*Z[0])) % Z[2];
int x2 = (Y/Z[0]) % Z[1];
int x1 = Y % Z[0];
int ghost_x4 = x4+ dx4;
x4 = (x4+dx4+Z[3]) % Z[3];
x3 = (x3+dx3+Z[2]) % Z[2];
x2 = (x2+dx2+Z[1]) % Z[1];
x1 = (x1+dx1+Z[0]) % Z[0];
if ( ghost_x4 >= 0 && ghost_x4 < Z[3]){
ret = (x4*(Z[2]*Z[1]*Z[0]) + x3*(Z[1]*Z[0]) + x2*(Z[0]) + x1) / 2;
}else{
ret = (x3*(Z[1]*Z[0]) + x2*(Z[0]) + x1) / 2;
return ret;
}
int oddBitChanged = (dx4+dx3+dx2+dx1)%2;
if (oddBitChanged){
oddBit = 1 - oddBit;
}
if (oddBit){
ret += Vh;
}
return ret;
}
// 4d checkerboard.
// given a "half index" i into either an even or odd half lattice (corresponding
// to oddBit = {0, 1}), returns the corresponding full lattice index.
// Cf. GPGPU code in dslash_core_ante.h.
// There, i is the thread index.
int fullLatticeIndex_4d(int i, int oddBit) {
if (i >= Vh || i < 0) {printf("i out of range in fullLatticeIndex_4d"); exit(-1);}
/*
int boundaryCrossings = i/(Z[0]/2) + i/(Z[1]*Z[0]/2) + i/(Z[2]*Z[1]*Z[0]/2);
return 2*i + (boundaryCrossings + oddBit) % 2;
*/
int X1 = Z[0];
int X2 = Z[1];
int X3 = Z[2];
//int X4 = Z[3];
int X1h =X1/2;
int sid =i;
int za = sid/X1h;
//int x1h = sid - za*X1h;
int zb = za/X2;
int x2 = za - zb*X2;
int x4 = zb/X3;
int x3 = zb - x4*X3;
int x1odd = (x2 + x3 + x4 + oddBit) & 1;
//int x1 = 2*x1h + x1odd;
int X = 2*sid + x1odd;
return X;
}
// 5d checkerboard.
// given a "half index" i into either an even or odd half lattice (corresponding
// to oddBit = {0, 1}), returns the corresponding full lattice index.
// Cf. GPGPU code in dslash_core_ante.h.
// There, i is the thread index sid.
// This function is used by neighborIndex_5d in dslash_reference.cpp.
//ok
int fullLatticeIndex_5d(int i, int oddBit) {
int boundaryCrossings = i/(Z[0]/2) + i/(Z[1]*Z[0]/2) + i/(Z[2]*Z[1]*Z[0]/2) + i/(Z[3]*Z[2]*Z[1]*Z[0]/2);
return 2*i + (boundaryCrossings + oddBit) % 2;
}
int fullLatticeIndex_5d_4dpc(int i, int oddBit) {
int boundaryCrossings = i/(Z[0]/2) + i/(Z[1]*Z[0]/2) + i/(Z[2]*Z[1]*Z[0]/2);
return 2*i + (boundaryCrossings + oddBit) % 2;
}
int
x4_from_full_index(int i)
{
int oddBit = 0;
int half_idx = i;
if (i >= Vh){
oddBit =1;
half_idx = i - Vh;
}
int Y = fullLatticeIndex(half_idx, oddBit);
int x4 = Y/(Z[2]*Z[1]*Z[0]);
return x4;
}
template <typename Float>
static void applyGaugeFieldScaling(Float **gauge, int Vh, QudaGaugeParam *param) {
// Apply spatial scaling factor (u0) to spatial links
for (int d = 0; d < 3; d++) {
for (int i = 0; i < gaugeSiteSize*Vh*2; i++) {
gauge[d][i] /= param->anisotropy;
}
}
// only apply T-boundary at edge nodes
#ifdef MULTI_GPU
bool last_node_in_t = (commCoords(3) == commDim(3)-1) ? true : false;
#else
bool last_node_in_t = true;
#endif
// Apply boundary conditions to temporal links
if (param->t_boundary == QUDA_ANTI_PERIODIC_T && last_node_in_t) {
for (int j = (Z[0]/2)*Z[1]*Z[2]*(Z[3]-1); j < Vh; j++) {
for (int i = 0; i < gaugeSiteSize; i++) {
gauge[3][j*gaugeSiteSize+i] *= -1.0;
gauge[3][(Vh+j)*gaugeSiteSize+i] *= -1.0;
}
}
}
if (param->gauge_fix) {
// set all gauge links (except for the last Z[0]*Z[1]*Z[2]/2) to the identity,
// to simulate fixing to the temporal gauge.
int iMax = ( last_node_in_t ? (Z[0]/2)*Z[1]*Z[2]*(Z[3]-1) : Vh );
int dir = 3; // time direction only
Float *even = gauge[dir];
Float *odd = gauge[dir]+Vh*gaugeSiteSize;
for (int i = 0; i< iMax; i++) {
for (int m = 0; m < 3; m++) {
for (int n = 0; n < 3; n++) {
even[i*(3*3*2) + m*(3*2) + n*(2) + 0] = (m==n) ? 1 : 0;
even[i*(3*3*2) + m*(3*2) + n*(2) + 1] = 0.0;
odd [i*(3*3*2) + m*(3*2) + n*(2) + 0] = (m==n) ? 1 : 0;
odd [i*(3*3*2) + m*(3*2) + n*(2) + 1] = 0.0;
}
}
}
}
}
template <typename Float>
void applyGaugeFieldScaling_long(Float **gauge, int Vh, QudaGaugeParam *param, QudaDslashType dslash_type)
{
int X1h=param->X[0]/2;
int X1 =param->X[0];
int X2 =param->X[1];
int X3 =param->X[2];
int X4 =param->X[3];
// rescale long links by the appropriate coefficient
if (dslash_type == QUDA_ASQTAD_DSLASH) {
for(int d=0; d<4; d++){
for(int i=0; i < V*gaugeSiteSize; i++){
gauge[d][i] /= (-24*param->tadpole_coeff*param->tadpole_coeff);
}
}
}
// apply the staggered phases
for (int d = 0; d < 3; d++) {
//even
for (int i = 0; i < Vh; i++) {
int index = fullLatticeIndex(i, 0);
int i4 = index /(X3*X2*X1);
int i3 = (index - i4*(X3*X2*X1))/(X2*X1);
int i2 = (index - i4*(X3*X2*X1) - i3*(X2*X1))/X1;
int i1 = index - i4*(X3*X2*X1) - i3*(X2*X1) - i2*X1;
int sign = 1;
if (d == 0) {
if (i4 % 2 == 1){
sign= -1;
}
}
if (d == 1){
if ((i4+i1) % 2 == 1){
sign= -1;
}
}
if (d == 2){
if ( (i4+i1+i2) % 2 == 1){
sign= -1;
}
}
for (int j=0; j < 18; j++) {
gauge[d][i*gaugeSiteSize + j] *= sign;
}
}
//odd
for (int i = 0; i < Vh; i++) {
int index = fullLatticeIndex(i, 1);
int i4 = index /(X3*X2*X1);
int i3 = (index - i4*(X3*X2*X1))/(X2*X1);
int i2 = (index - i4*(X3*X2*X1) - i3*(X2*X1))/X1;
int i1 = index - i4*(X3*X2*X1) - i3*(X2*X1) - i2*X1;
int sign = 1;
if (d == 0) {
if (i4 % 2 == 1){
sign = -1;
}
}
if (d == 1){
if ((i4+i1) % 2 == 1){
sign = -1;
}
}
if (d == 2){
if ( (i4+i1+i2) % 2 == 1){
sign = -1;
}
}
for (int j=0; j<18; j++){
gauge[d][(Vh+i)*gaugeSiteSize + j] *= sign;
}
}
}
// Apply boundary conditions to temporal links
if (param->t_boundary == QUDA_ANTI_PERIODIC_T) {
for (int j = 0; j < Vh; j++) {
int sign =1;
if (dslash_type == QUDA_ASQTAD_DSLASH) {
if (j >= (X4-3)*X1h*X2*X3 ){
sign = -1;
}
} else {
if (j >= (X4-1)*X1h*X2*X3 ){
sign = -1;
}
}
for (int i=0; i<18; i++) {
gauge[3][j*gaugeSiteSize + i] *= sign;
gauge[3][(Vh+j)*gaugeSiteSize + i] *= sign;
}
}
}
}
template <typename Float>
static void constructUnitGaugeField(Float **res, QudaGaugeParam *param) {
Float *resOdd[4], *resEven[4];
for (int dir = 0; dir < 4; dir++) {
resEven[dir] = res[dir];
resOdd[dir] = res[dir]+Vh*gaugeSiteSize;
}
for (int dir = 0; dir < 4; dir++) {
for (int i = 0; i < Vh; i++) {
for (int m = 0; m < 3; m++) {
for (int n = 0; n < 3; n++) {
resEven[dir][i*(3*3*2) + m*(3*2) + n*(2) + 0] = (m==n) ? 1 : 0;
resEven[dir][i*(3*3*2) + m*(3*2) + n*(2) + 1] = 0.0;
resOdd[dir][i*(3*3*2) + m*(3*2) + n*(2) + 0] = (m==n) ? 1 : 0;
resOdd[dir][i*(3*3*2) + m*(3*2) + n*(2) + 1] = 0.0;
}
}
}
}
applyGaugeFieldScaling(res, Vh, param);
}
// normalize the vector a
template <typename Float>
static void normalize(complex<Float> *a, int len) {
double sum = 0.0;
for (int i=0; i<len; i++) sum += norm(a[i]);
for (int i=0; i<len; i++) a[i] /= sqrt(sum);
}
// orthogonalize vector b to vector a
template <typename Float>
static void orthogonalize(complex<Float> *a, complex<Float> *b, int len) {
complex<double> dot = 0.0;
for (int i=0; i<len; i++) dot += conj(a[i])*b[i];
for (int i=0; i<len; i++) b[i] -= (complex<Float>)dot*a[i];
}
template <typename Float>
static void constructGaugeField(Float **res, QudaGaugeParam *param, QudaDslashType dslash_type=QUDA_WILSON_DSLASH) {
Float *resOdd[4], *resEven[4];
for (int dir = 0; dir < 4; dir++) {
resEven[dir] = res[dir];
resOdd[dir] = res[dir]+Vh*gaugeSiteSize;
}
for (int dir = 0; dir < 4; dir++) {
for (int i = 0; i < Vh; i++) {
for (int m = 1; m < 3; m++) { // last 2 rows
for (int n = 0; n < 3; n++) { // 3 columns
resEven[dir][i*(3*3*2) + m*(3*2) + n*(2) + 0] = rand() / (Float)RAND_MAX;
resEven[dir][i*(3*3*2) + m*(3*2) + n*(2) + 1] = rand() / (Float)RAND_MAX;
resOdd[dir][i*(3*3*2) + m*(3*2) + n*(2) + 0] = rand() / (Float)RAND_MAX;
resOdd[dir][i*(3*3*2) + m*(3*2) + n*(2) + 1] = rand() / (Float)RAND_MAX;
}
}
normalize((complex<Float>*)(resEven[dir] + (i*3+1)*3*2), 3);
orthogonalize((complex<Float>*)(resEven[dir] + (i*3+1)*3*2), (complex<Float>*)(resEven[dir] + (i*3+2)*3*2), 3);
normalize((complex<Float>*)(resEven[dir] + (i*3 + 2)*3*2), 3);
normalize((complex<Float>*)(resOdd[dir] + (i*3+1)*3*2), 3);
orthogonalize((complex<Float>*)(resOdd[dir] + (i*3+1)*3*2), (complex<Float>*)(resOdd[dir] + (i*3+2)*3*2), 3);
normalize((complex<Float>*)(resOdd[dir] + (i*3 + 2)*3*2), 3);
{
Float *w = resEven[dir]+(i*3+0)*3*2;
Float *u = resEven[dir]+(i*3+1)*3*2;
Float *v = resEven[dir]+(i*3+2)*3*2;
for (int n = 0; n < 6; n++) w[n] = 0.0;
accumulateConjugateProduct(w+0*(2), u+1*(2), v+2*(2), +1);
accumulateConjugateProduct(w+0*(2), u+2*(2), v+1*(2), -1);
accumulateConjugateProduct(w+1*(2), u+2*(2), v+0*(2), +1);
accumulateConjugateProduct(w+1*(2), u+0*(2), v+2*(2), -1);
accumulateConjugateProduct(w+2*(2), u+0*(2), v+1*(2), +1);
accumulateConjugateProduct(w+2*(2), u+1*(2), v+0*(2), -1);
}
{
Float *w = resOdd[dir]+(i*3+0)*3*2;
Float *u = resOdd[dir]+(i*3+1)*3*2;
Float *v = resOdd[dir]+(i*3+2)*3*2;
for (int n = 0; n < 6; n++) w[n] = 0.0;
accumulateConjugateProduct(w+0*(2), u+1*(2), v+2*(2), +1);
accumulateConjugateProduct(w+0*(2), u+2*(2), v+1*(2), -1);
accumulateConjugateProduct(w+1*(2), u+2*(2), v+0*(2), +1);
accumulateConjugateProduct(w+1*(2), u+0*(2), v+2*(2), -1);
accumulateConjugateProduct(w+2*(2), u+0*(2), v+1*(2), +1);
accumulateConjugateProduct(w+2*(2), u+1*(2), v+0*(2), -1);
}
}
}
if (param->type == QUDA_WILSON_LINKS){
applyGaugeFieldScaling(res, Vh, param);
} else if (param->type == QUDA_ASQTAD_LONG_LINKS){
applyGaugeFieldScaling_long(res, Vh, param, dslash_type);
} else if (param->type == QUDA_ASQTAD_FAT_LINKS){
for (int dir = 0; dir < 4; dir++){
for (int i = 0; i < Vh; i++) {
for (int m = 0; m < 3; m++) { // last 2 rows
for (int n = 0; n < 3; n++) { // 3 columns
resEven[dir][i*(3*3*2) + m*(3*2) + n*(2) + 0] =1.0* rand() / (Float)RAND_MAX;
resEven[dir][i*(3*3*2) + m*(3*2) + n*(2) + 1] = 2.0* rand() / (Float)RAND_MAX;
resOdd[dir][i*(3*3*2) + m*(3*2) + n*(2) + 0] = 3.0*rand() / (Float)RAND_MAX;
resOdd[dir][i*(3*3*2) + m*(3*2) + n*(2) + 1] = 4.0*rand() / (Float)RAND_MAX;
}
}
}
}
}
}
template <typename Float>
void constructUnitaryGaugeField(Float **res)
{
Float *resOdd[4], *resEven[4];
for (int dir = 0; dir < 4; dir++) {
resEven[dir] = res[dir];
resOdd[dir] = res[dir]+Vh*gaugeSiteSize;