-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathcoarse_op_kernel.cuh
More file actions
2007 lines (1738 loc) · 78.9 KB
/
coarse_op_kernel.cuh
File metadata and controls
2007 lines (1738 loc) · 78.9 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
#pragma once
#include <color_spinor_field_order.h>
#include <gauge_field_order.h>
#include <clover_field_order.h>
#include <multigrid_helper.cuh>
#include <index_helper.cuh>
#include <gamma.cuh>
#include <linalg.cuh>
#include <matrix_tile.cuh>
#include <target_device.h>
#include <kernel.h>
#include <shared_memory_cache_helper.h>
namespace quda {
/** This is the storage type used when computing the coarse link
variables: by using integers we have deterministic atomics */
using storeType = int;
/** This is the arg struct used for all multigrid coarse-grid
construction. The same instance is reused for different
kernels */
template <bool from_coarse_, typename Float_, int fineSpin_, int coarseSpin_, int fineColor_, int coarseColor_, typename coarseGauge,
typename coarseGaugeAtomic, typename fineGauge, typename fineSpinorAV_, typename fineSpinorUV_,
typename fineSpinorV_, typename fineClover>
struct CalculateYArg : kernel_param<> {
using Float = Float_; /** Float Precision of the computation */
using fineSpinorV = fineSpinorV_; /** Type of the fine grid spinor field */
using fineSpinorUV = fineSpinorUV_; /** Type of the temporary that stores the fine-link * spinor field product */
using fineSpinorAV = fineSpinorAV_; /** Type of the temporary that stores the clover/kd-inv * spinor field product */
static constexpr int fineSpin = fineSpin_; /** Number of spins on the fine grid */
static constexpr int coarseSpin = coarseSpin_; /** Number of spins on the coarse grid */
static constexpr int fineColor = fineColor_; /** Number of colors on the fine grid */
static constexpr int coarseColor = coarseColor_; /** Number of colors on the coarse grid */
static constexpr int fineDof = fineSpin * fineColor;
static constexpr int coarseDof = coarseSpin * coarseColor;
static constexpr bool from_coarse = from_coarse_; /** Whether the fine grid is itself a coarse grid */
static constexpr bool is_mma_compatible = coarseGauge::is_mma_compatible; /** Whether tensor-core acceleration is applicable */
static constexpr bool from_kd_op = fineSpin == 1 && fineSpinorV::nSpin != fineSpinorAV::nSpin; /** Whether we're coarsening the KD operator or not */
coarseGauge Y; /** Computed coarse link field */
coarseGauge X; /** Computed coarse clover field */
coarseGaugeAtomic Y_atomic; /** Y atomic accessor used for computation before conversion to final format */
coarseGaugeAtomic X_atomic; /** X atomic accessor used for computation before conversion to final format */
fineSpinorUV UV; /** Temporary that stores the fine-link * spinor field product */
fineSpinorAV AV; /** Temporary that stores the clover * spinor field product */
const fineGauge U; /** Fine grid link field */
const fineGauge L; /** Fine grid long link field for asqtad/hisq op */
const fineGauge K; /** Fine grid Kahler-Dirac inverse */
const fineSpinorV V; /** Fine grid spinor field */
const fineClover C; /** Fine grid clover field */
const fineClover Cinv; /** Fine grid clover field */
int_fastdiv x_size[QUDA_MAX_DIM]; /** Dimensions of fine grid */
int xc_size[QUDA_MAX_DIM]; /** Dimensions of coarse grid */
int_fastdiv geo_bs[QUDA_MAX_DIM]; /** Geometric block dimensions */
const int spin_bs; /** Spin block size */
const spin_mapper<fineSpin,coarseSpin> spin_map; /** Helper that maps fine spin to coarse spin */
int comm_dim[QUDA_MAX_DIM]; /** Node parition array */
Float kappa; /** kappa value */
Float mass; /** mass value */
Float mu; /** mu value */
Float mu_factor; /** multiplicative factor for mu applied when mu is added to the operator */
Float rescale; /** rescaling factor used when rescaling the Y links if the maximum increases */
const int fineVolumeCB; /** Fine grid volume */
const int coarseVolumeCB; /** Coarse grid volume */
const int *fine_to_coarse; /** Pointer to the fine-to-coarse look-up table */
const int *coarse_to_fine; /** Pointer to the coarse-to-fine look-up table */
const bool bidirectional; /** Whether the operator we are coarsening requires bi-directional coarsening */
/** To increase L2 locality we can schedule the geometry to grid.y
and the coarse colors to grid.x. This will increase the
potential for L2 reuse since a given wave of thread blocks
will be for different coarse color but the same coarse grid
point which will have common loads. */
bool coarse_color_wave = false;
/** Enable this for shared-memory atomics instead of global
atomics. Doing so means that all (modulo the parity) of the
coarsening for a coarse degree of freedom is handled by a
single thread block. For computeVUV only at present. */
bool shared_atomic = false;
/** With parity_flip enabled we make parity the slowest running
dimension in the y-thread axis, and coarse color runs faster.
This improves read locality at the expense of write
locality */
bool parity_flip = false;
int_fastdiv aggregates_per_block = 1; /** number of aggregates per thread block */
int_fastdiv grid_z; /** this is the coarseColor grid that is wrapped into the x grid when coarse_color_wave is enabled */
int_fastdiv coarse_color_grid_z; /** constant we ned to divide by */
static constexpr bool compute_max = false;
Float *max_h = nullptr; /** scalar that stores the maximum element on the host */
Float *max_d = nullptr; /** scalar that stores the maximum elenent on the device */
Float *max = nullptr; /** points to either max_h or max_d, for host or device, respectively */
int dim; /** which dimension are we working on */
QudaDirection dir; /** which direction are working on */
int dim_index; /** which direction / dimension we are working on */
bool twist; /** whether we are doing twisted or non-twisted */
bool kd_dagger; /** whether we're applying the dagger of the KD inverse field or not */
static constexpr int tile_height_uv = fineColor % 4 == 0 ? 4 : fineColor % 3 == 0 ? 3 : fineColor % 2 ? 2 : 1; /** tile height used for computeUV */
static constexpr int tile_width_uv = coarseColor % 2 == 0 ? 2 : 1; /** tile width used for computeUV */
using uvTileType = TileSize<fineColor, coarseColor, fineColor, tile_height_uv, tile_width_uv, 1>; /** tile type used for computeUV */
uvTileType uvTile; /** tile instance used for computeUV */
// tile used for computeVUV - for fine Wilson grids best to use 4, else use max of 3
static constexpr int tile_height_vuv = (coarseColor % 4 == 0 && fineSpin == 4) ? 4 : coarseColor % 3 == 0 ? 3 : 2; /** tile height used for computeVUV */
static constexpr int tile_width_vuv = coarseColor % 2 == 0 ? 2 : 1; /** tile width used for computeVUV */
using vuvTileType = TileSize<coarseColor, coarseColor, fineColor, tile_height_vuv, tile_width_vuv, 1>; /** tile type used for computeVUV */
vuvTileType vuvTile; /** tile instance used for computeUV */
// max colors per block is 8, rounded up to whole multiples of tile size
static constexpr int max_color_height_per_block = coarseColor < 8 ? coarseColor : ((8 + tile_height_vuv - 1) / tile_height_vuv) * tile_height_vuv;
static constexpr int max_color_width_per_block = coarseColor < 8 ? coarseColor : ((8 + tile_width_vuv - 1) / tile_width_vuv) * tile_width_vuv;
static constexpr int max_height_tiles_per_block = max_color_height_per_block / tile_height_vuv;
static constexpr int max_width_tiles_per_block = max_color_width_per_block / tile_width_vuv;
static_assert(max_color_height_per_block % tile_height_vuv == 0, "max_color_height_per_block must be divisible by tile height");
static_assert(max_color_width_per_block % tile_width_vuv == 0, "max_color_width_per_block must be divisible by tile width");
/**
@brief Constructor for CalculateYArg
@param[out] Y Accessor to the coarse gauge field
@param[out] X Accessor to the coarse clover field
@param[in,out] Y Atomic accessor to the coarse gauge field
@param[in,out] X Atomic accessor to the coarse clover field
@param[in,out] UV Accessor to the uv temp packed colorspinor field
@param[in,out] AV Accessor to the av temp packed colorspinor field
@param[in] U Accessor to the fine gauge field
@param[in] L Accessor to the fine long-link field
@param[in] K Accessor to the Kahler-Dirac inverse field
@param[in] V Accessor to the packed nullspace colorspinor field
@param[in] C Accessor to the fine clover field
@param[in] Cinv Accessor to the fine inverse clover field
@param[in] kappa Kappa parameter from the fine operator
@param[in] mass Mass parameter from the fine operator
@param[in] mu Twisted mass parameter from the fine operator
@param[in] mu_factor Additional twisted mass parameter for coarse-grid stability
@param[in] x_size_ Fine-grid geometric dimensions
@param[in] xc_size_ Coarse-grid geometric dimensions
@param[in] fine_to_coarse Pointer to fine-to-coarse look-up table (memory space is same compute)
@param[in] coarse_to_fine Pointer to coarse-to-fine look-up table (memory space is same compute)
@param[in] bidirectional Whether the operator we are coarsening requires bi-directional coarsening
*/
CalculateYArg(coarseGauge &Y, coarseGauge &X,
coarseGaugeAtomic &Y_atomic, coarseGaugeAtomic &X_atomic,
fineSpinorUV &UV, fineSpinorAV &AV, const fineGauge &U, const fineGauge &L, const fineGauge &K, const fineSpinorV &V,
const fineClover &C, const fineClover &Cinv, const ColorSpinorField &v, double kappa, double mass, double mu, double mu_factor,
const int *x_size_, const int *xc_size_, int spin_bs_,
const int *fine_to_coarse, const int *coarse_to_fine, bool bidirectional)
: Y(Y), X(X), Y_atomic(Y_atomic), X_atomic(X_atomic),
UV(UV), AV(AV), U(U), L(L), K(K), V(V), C(C), Cinv(Cinv), spin_bs(spin_bs_), spin_map(),
kappa(static_cast<Float>(kappa)), mass(static_cast<Float>(mass)), mu(static_cast<Float>(mu)), mu_factor(static_cast<Float>(mu_factor)),
fineVolumeCB(v.VolumeCB()), coarseVolumeCB(X.VolumeCB()),
fine_to_coarse(fine_to_coarse), coarse_to_fine(coarse_to_fine),
bidirectional(bidirectional)
{
if (v.GammaBasis() != QUDA_DEGRAND_ROSSI_GAMMA_BASIS)
errorQuda("Gamma basis %d not supported", v.GammaBasis());
for (int i=0; i<QUDA_MAX_DIM; i++) {
x_size[i] = i < 4 ? x_size_[i] : 1;
xc_size[i] = i < 4 ? xc_size_[i] : 1;
geo_bs[i] = x_size[i] / xc_size[i];
comm_dim[i] = i < 4 ? comm_dim_partitioned(i) : 1;
}
}
};
/** This is a wrapper class that derives from the kernel argument
struct, and is used to enable the maximum element computation */
template <typename T> struct ArgMax : public T {
static constexpr bool compute_max = true;
ArgMax(const T& t) : T(t) { }
};
/**
@brief Helper for computing if the present site is within the
forwards halo region
@return If we within the halo
@param[in] coord Grid coordinates
@param[in] dim Dimension of the shift
@param[in] nFace Depth of the halo
@param[in] arg Kernel argument
*/
template <typename Arg> constexpr bool isHalo(const int coord[], int dim, int nFace, const Arg &arg)
{
switch (dim) {
case 0: return arg.comm_dim[0] && coord[0] + nFace >= arg.x_size[0];
case 1: return arg.comm_dim[1] && coord[1] + nFace >= arg.x_size[1];
case 2: return arg.comm_dim[2] && coord[2] + nFace >= arg.x_size[2];
case 3: return arg.comm_dim[3] && coord[3] + nFace >= arg.x_size[3];
}
return false;
}
/**
@brief Helper for computing if the fine site we are on
corresponds to the coarse operator diagaonl
@return If we are on the coarse diagonal
@param[in] coord Fine grid coordinates
@param[in] coord_coarse Coarse grid coordinates
@param[in] dim Dimension
@param[in] arg Kernel argument
*/
template <typename Arg> constexpr bool isCoarseDiagonal(const int coord[], const int coord_coarse[], int dim, int nFace, const Arg &arg)
{
switch (dim) {
case 0: return ((coord[0] + nFace) % arg.x_size[0]) / arg.geo_bs[0] == coord_coarse[0];
case 1: return ((coord[1] + nFace) % arg.x_size[1]) / arg.geo_bs[1] == coord_coarse[1];
case 2: return ((coord[2] + nFace) % arg.x_size[2]) / arg.geo_bs[2] == coord_coarse[2];
case 3: return ((coord[3] + nFace) % arg.x_size[3]) / arg.geo_bs[3] == coord_coarse[3];
}
return false;
}
/**
@brief Calculates the matrix UV^{s,c'}_mu(x) = \sum_c U^{c}_mu(x) * V^{s,c}_mu(x+mu) for Wilson-type fermions
Where: mu = dim, s = fine spin, c' = coarse color, c = fine color
or, if dir == QUDA_IN_PLACE, UV^{s,c'}(x) = \sum_c C^{c}_mu(x) * V^{s,c}_mu(x+mu)
@return The maximum element of the result (if Arg::compute_max == true)
@param[in] arg Kernel argumnt
@param[in] Gacc Input gauge vector, always arg.U for nSpin == 4 (Wilson fermions) so we ignore it
@param[in] Wacc Input vector accessor
@param[in] parity Parity index
@param[in] x_cb Checkerboard index
@param[in] i0 Color color row index (coarse)
@param[in] j0 Color column index (fine)
*/
template <int nFace, typename gType, typename Wtype, typename Arg>
__device__ __host__ inline std::enable_if_t<!Arg::from_coarse && Arg::fineSpin == 4, typename Arg::Float>
computeUV(const Arg &arg, const gType&, const Wtype &Wacc, int parity, int x_cb, int i0, int j0)
{
constexpr int uvSpin = Arg::fineSpin;
using real = typename Arg::Float;
using complex = complex<real>;
using TileType = typename Arg::uvTileType;
auto &tile = arg.uvTile;
using Ctype = decltype(make_tile_C<complex, false>(tile));
Ctype UV[uvSpin];
int coord[4];
getCoords(coord, x_cb, arg.x_size, parity);
if ( isHalo(coord, arg.dim, nFace, arg) ) {
int ghost_idx = ghostFaceIndex<1>(coord, arg.x_size, arg.dim, nFace);
#pragma unroll
for (int k = 0; k < TileType::k; k += TileType::K) { // Fine Color columns of gauge field
auto U = make_tile_A<complex, false>(tile);
U.load(arg.U, arg.dim, parity, x_cb, i0, k);
#pragma unroll
for (int s = 0; s < Arg::fineSpin; s++) { //Fine Spin
auto W = make_tile_B<complex, true>(tile);
W.loadCS(Wacc, arg.dim, 1, 1 - parity, ghost_idx, s, k, j0);
UV[s].mma_nn(U, W);
} // Fine color columns
} // Fine spin (tensor)
} else {
int y_cb = linkIndexHop(coord, arg.x_size, arg.dim, nFace);
#pragma unroll
for (int k = 0; k < TileType::k; k += TileType::K) { // Fine Color columns of gauge field
auto U = make_tile_A<complex, false>(tile);
U.load(arg.U, arg.dim, parity, x_cb, i0, k);
#pragma unroll
for (int s = 0; s < Arg::fineSpin; s++) { //Fine Spin
auto W = make_tile_B<complex, false>(tile);
W.loadCS(Wacc, 0, 0, 1 - parity, y_cb, s, k, j0);
UV[s].mma_nn(U, W);
} //Fine color columns
} // Fine Spin
}
real uv_max = static_cast<real>(0.0);
#pragma unroll
for (int s = 0; s < uvSpin; s++) {
if constexpr (Arg::compute_max) {
uv_max = fmax(UV[s].abs_max(), uv_max);
} else {
UV[s].saveCS(arg.UV, 0, 0, parity, x_cb, s, i0, j0);
}
}
return uv_max;
} // computeUV
/**
@brief Calculates the matrix UV^{s,c'}_mu(x) = \sum_c U^{c}_mu(x) * V^{s,c}_mu(x+mu) for non-KD staggered operators
Where: mu = dim, s = fine spin, c' = coarse color, c = fine color
or, if dir == QUDA_IN_PLACE, UV^{s,c'}(x) = \sum_c C^{c}_mu(x) * V^{s,c}_mu(x+mu)
@return The maximum element of the result (if Arg::compute_max == true)
@param[in] arg Kernel argumnt
@param[in] Gacc Input gauge vector
@param[in] Wacc Input vector accessor
@param[in] parity Parity index
@param[in] x_cb Checkerboard index
@param[in] i0 Color color row index (coarse)
@param[in] j0 Color column index (fine)
*/
template <int nFace, typename gType, typename Wtype, typename Arg>
__device__ __host__ inline std::enable_if_t<!Arg::from_coarse && Arg::fineSpin == 1 && !Arg::from_kd_op, typename Arg::Float>
computeUV(const Arg &arg, const gType& Gacc, const Wtype &Wacc, int parity, int x_cb, int i0, int j0)
{
constexpr int uvSpin = Arg::fineSpinorUV::nSpin;
using real = typename Arg::Float;
using complex = complex<real>;
using TileType = typename Arg::uvTileType;
auto &tile = arg.uvTile;
using Ctype = decltype(make_tile_C<complex, false>(tile));
Ctype UV[uvSpin];
int coord[4];
getCoords(coord, x_cb, arg.x_size, parity);
if ( isHalo(coord, arg.dim, nFace, arg) ) {
int ghost_idx = (nFace == 1) ? ghostFaceIndex<1>(coord, arg.x_size, arg.dim, 1) : ghostFaceIndexStaggered<1>(coord, arg.x_size, arg.dim, 3);
auto W = make_tile_B<complex, true>(tile);
#pragma unroll
for (int k = 0; k < TileType::k; k += TileType::K) { // Fine Color columns of the gauge field
auto U = make_tile_A<complex, false>(tile);
U.load(Gacc, arg.dim, parity, x_cb, i0, k);
// loading from V == AV, which has nSpin == 1
W.loadCS(Wacc, arg.dim, 1, 1 - parity, ghost_idx, 0, k, j0);
UV[0].mma_nn(U, W);
} // fine color columns
} else {
int y_cb = linkIndexHop(coord, arg.x_size, arg.dim, nFace);
auto W = make_tile_B<complex, false>(tile);
#pragma unroll
for (int k = 0; k < TileType::k; k += TileType::K) { // Fine Color columns of the gauge field
auto U = make_tile_A<complex, false>(tile);
U.load(Gacc, arg.dim, parity, x_cb, i0, k);
// loading from V == AV, which has nSpin == 1
W.loadCS(Wacc, 0, 0, 1 - parity, y_cb, 0, k, j0);
UV[0].mma_nn(U, W);
} // fine color columns
}
real uv_max = static_cast<real>(0.0);
#pragma unroll
for (int s = 0; s < uvSpin; s++) {
if constexpr (Arg::compute_max) {
uv_max = fmax(UV[s].abs_max(), uv_max);
} else {
UV[s].saveCS(arg.UV, 0, 0, parity, x_cb, s, i0, j0);
}
}
return uv_max;
} // computeUV
/**
@brief Calculates the matrix UV^{s,c'}_mu(x) = \sum_c U^{c}_mu(x) * V^{s,c}_mu(x+mu) for KD staggered operators
Where: mu = dim, s = fine spin, c' = coarse color, c = fine color
or, if dir == QUDA_IN_PLACE, UV^{s,c'}(x) = \sum_c C^{c}_mu(x) * V^{s,c}_mu(x+mu)
@return The maximum element of the result (if Arg::compute_max == true)
@param[in] arg Kernel argumnt
@param[in] Gacc Input gauge vector
@param[in] Wacc Input vector accessor
@param[in] parity Parity index
@param[in] x_cb Checkerboard index
@param[in] i0 Color color row index (coarse)
@param[in] j0 Color column index (fine)
*/
template <int nFace, typename gType, typename Wtype, typename Arg>
__device__ __host__ inline std::enable_if_t<!Arg::from_coarse && Arg::fineSpin == 1 && Arg::from_kd_op, typename Arg::Float>
computeUV(const Arg &arg, const gType& Gacc, const Wtype &Wacc, int parity, int x_cb, int i0, int j0)
{
constexpr int uvSpin = Arg::fineSpinorUV::nSpin;
using real = typename Arg::Float;
using complex = complex<real>;
using TileType = typename Arg::uvTileType;
auto &tile = arg.uvTile;
using Ctype = decltype(make_tile_C<complex, false>(tile));
Ctype UV[uvSpin];
int coord[4];
getCoords(coord, x_cb, arg.x_size, parity);
if ( isHalo(coord, arg.dim, nFace, arg) ) {
int ghost_idx = (nFace == 1) ? ghostFaceIndex<1>(coord, arg.x_size, arg.dim, 1) : ghostFaceIndexStaggered<1>(coord, arg.x_size, arg.dim, 3);
auto W = make_tile_B<complex, true>(tile);
// Need to keep track of if we're loading from V or AV
if (arg.dir == QUDA_FORWARDS) {
// loading from V, only need to load from "one" spin
#pragma unroll
for (int k = 0; k < TileType::k; k += TileType::K) { // Fine Color columns of gauge field
auto U = make_tile_A<complex, false>(tile);
U.load(Gacc, arg.dim, parity, x_cb, i0, k);
W.loadCS(Wacc, arg.dim, 1, 1 - parity, ghost_idx, 0, k, j0);
// store to a different component of UV depending on if we're gathering
// from even, odd
if (parity == 0) UV[1].mma_nn(U, W);
else UV[0].mma_nn(U, W);
}
} else if (arg.dir == QUDA_BACKWARDS) {
// loading from AV, need to be mindful of if we're loading from a "from even" or "from odd" site
#pragma unroll
for (int k = 0; k < TileType::k; k += TileType::K) { // Fine Color columns of gauge field
auto U = make_tile_A<complex, false>(tile);
U.load(Gacc, arg.dim, parity, x_cb, i0, k);
#pragma unroll
for (int s = 0; s < Arg::fineSpinorAV::nSpin; s++) {
W.loadCS(Wacc, arg.dim, 1, 1 - parity, ghost_idx, s, k, j0);
UV[s].mma_nn(U, W);
}
}
}
} else {
int y_cb = linkIndexHop(coord, arg.x_size, arg.dim, nFace);
auto W = make_tile_B<complex, false>(tile);
// KD op, need to keep track of if we're loading from V or AV
if (arg.dir == QUDA_FORWARDS) {
// loading from V, only need to load from "one" spin
#pragma unroll
for (int k = 0; k < TileType::k; k += TileType::K) { // Fine Color columns of gauge field
auto U = make_tile_A<complex, false>(tile);
U.load(Gacc, arg.dim, parity, x_cb, i0, k);
W.loadCS(Wacc, 0, 0, 1 - parity, y_cb, 0, k, j0);
// store to a different component of UV depending on if we're gathering
// from even, odd
if (parity == 0) UV[1].mma_nn(U, W);
else UV[0].mma_nn(U, W);
}
} else if (arg.dir == QUDA_BACKWARDS) {
// loading from AV, need to be mindful of if we're loading from a "from even" or "from odd" site
#pragma unroll
for (int k = 0; k < TileType::k; k += TileType::K) { // Fine Color columns of gauge field
auto U = make_tile_A<complex, false>(tile);
U.load(Gacc, arg.dim, parity, x_cb, i0, k);
#pragma unroll
for (int s = 0; s < Arg::fineSpinorAV::nSpin; s++) {
W.loadCS(Wacc, 0, 0, 1 - parity, y_cb, s, k, j0);
UV[s].mma_nn(U, W);
}
}
}
}
real uv_max = static_cast<real>(0.0);
if (arg.dir == QUDA_FORWARDS) {
if constexpr (Arg::compute_max) {
uv_max = parity ? UV[0].abs_max() : UV[1].abs_max();
} else {
if (parity == 0)
UV[1].saveCS(arg.UV, 0, 0, parity, x_cb, 1, i0, j0);
else
UV[0].saveCS(arg.UV, 0, 0, parity, x_cb, 0, i0, j0);
}
} else {
#pragma unroll
for (int s = 0; s < uvSpin; s++) {
if constexpr (Arg::compute_max) {
uv_max = fmax(UV[s].abs_max(), uv_max);
} else {
UV[s].saveCS(arg.UV, 0, 0, parity, x_cb, s, i0, j0);
}
}
}
return uv_max;
} // computeUV
/**
@brief Calculates the matrix UV^{s,c'}_mu(x) = \sum_c U^{c}_mu(x) * V^{s,c}_mu(x+mu) for coarse operators
Where: mu = dim, s = fine spin, c' = coarse color, c = fine color
or, if dir == QUDA_IN_PLACE, UV^{s,c'}(x) = \sum_c C^{c}_mu(x) * V^{s,c}_mu(x+mu)
@return The maximum element of the result (if Arg::compute_max == true)
@param[in] arg Kernel argumnt
@param[in] Gacc Input gauge vector, always arg.U for nSpin == 4 (Wilson fermions) so we ignore it
@param[in] Wacc Input vector accessor
@param[in] parity Parity index
@param[in] x_cb Checkerboard index
@param[in] i0 Color color row index (coarse)
@param[in] j0 Color column index (fine)
*/
template <int nFace, typename Gtype, typename Wtype, typename Arg>
__device__ __host__ inline std::enable_if_t<Arg::from_coarse, typename Arg::Float>
computeUV(const Arg &arg, const Gtype&, const Wtype &Wacc, int parity, int x_cb, int i0, int j0)
{
constexpr int uvSpin = Arg::fineSpinorUV::nSpin;
using real = typename Arg::Float;
using complex = complex<real>;
using TileType = typename Arg::uvTileType;
auto &tile = arg.uvTile;
using Ctype = decltype(make_tile_C<complex, false>(tile));
Ctype UV[uvSpin];
int coord[4];
getCoords(coord, x_cb, arg.x_size, parity);
if (arg.dir == QUDA_IN_PLACE) {
#pragma unroll
for (int k = 0; k < TileType::k; k += TileType::K) { // Fine Color columns of coarse clover field
#pragma unroll
for (int s_col = 0; s_col < Arg::fineSpin; s_col++) {
auto W = make_tile_B<complex, false>(tile);
W.loadCS(Wacc, 0, 0, parity, x_cb, s_col, k, j0);
#pragma unroll
for (int s = 0; s < Arg::fineSpin; s++) { //Fine Spin
auto C = make_tile_A<complex, false>(tile);
C.load(arg.C, 0, parity, x_cb, s, s_col, i0, k);
UV[s_col * Arg::fineSpin + s].mma_nn(C, W);
} // which chiral block
} //Fine Spin
} // Fine color columns
} else if ( isHalo(coord, arg.dim, nFace, arg) ) {
int ghost_idx = ghostFaceIndex<1>(coord, arg.x_size, arg.dim, nFace);
#pragma unroll
for (int k = 0; k < TileType::k; k += TileType::K) { // Fine Color columns of gauge field
#pragma unroll
for (int s_col=0; s_col<Arg::fineSpin; s_col++) {
auto W = make_tile_B<complex, true>(tile);
W.loadCS(Wacc, arg.dim, 1, 1 - parity, ghost_idx, s_col, k, j0);
#pragma unroll
for (int s = 0; s < Arg::fineSpin; s++) { //Fine Spin
// on coarse lattice, if forwards then use forwards links
auto U = make_tile_A<complex, false>(tile);
U.load(arg.U, arg.dim + (arg.dir == QUDA_FORWARDS ? 4 : 0), parity, x_cb, s, s_col, i0, k);
UV[s_col * Arg::fineSpin + s].mma_nn(U, W);
} // which chiral block
} //Fine color columns
} // Fine Spin
} else {
int y_cb = linkIndexHop(coord, arg.x_size, arg.dim, nFace);
#pragma unroll
for (int k = 0; k < TileType::k; k += TileType::K) { // Fine Color columns of gauge field
#pragma unroll
for (int s_col = 0; s_col < Arg::fineSpin; s_col++) {
auto W = make_tile_B<complex, false>(tile);
W.loadCS(Wacc, 0, 0, 1 - parity, y_cb, s_col, k, j0);
#pragma unroll
for (int s = 0; s < Arg::fineSpin; s++) { //Fine Spin
// on coarse lattice, if forwards then use forwards links
auto U = make_tile_A<complex, false>(tile);
U.load(arg.U, arg.dim + (arg.dir == QUDA_FORWARDS ? 4 : 0), parity, x_cb, s, s_col, i0, k);
UV[s_col * Arg::fineSpin + s].mma_nn(U, W);
} // which chiral block
} //Fine Spin
} // Fine color columns
}
real uv_max = static_cast<real>(0.0);
#pragma unroll
for (int s = 0; s < uvSpin; s++) {
if constexpr (Arg::compute_max) {
uv_max = fmax(UV[s].abs_max(), uv_max);
} else {
UV[s].saveCS(arg.UV, 0, 0, parity, x_cb, s, i0, j0);
}
}
return uv_max;
} // computeUV
/**
Functor for the UV computation.
If Arg::compute_max is true, then we do not save the result, and
instead merely compute the maximum element. This is used for
setting the scale for fixed point.
*/
template <typename Arg> struct compute_uv {
using real = typename Arg::Float;
static constexpr int nFace = 1;
const Arg &arg;
static constexpr const char *filename() { return KERNEL_FILE; }
constexpr compute_uv(const Arg &arg) : arg(arg) { }
/**
3-d parallelism
@param[in] x_cb e/o fine-grid spacetime
@param[in] ic_parity parity * output color column
@param[in] jc output color row
*/
__device__ __host__ void operator()(int x_cb, int ic_parity, int jc)
{
int ic = ic_parity % arg.uvTile.M_tiles;
int parity = ic_parity / arg.uvTile.M_tiles;
real max;
if (arg.dir == QUDA_FORWARDS || arg.dir == QUDA_IN_PLACE) // only for preconditioned clover is V != AV, will need extra logic for staggered KD
max = computeUV<nFace>(arg, arg.U, arg.V, parity, x_cb, ic * arg.uvTile.M, jc * arg.uvTile.N);
else
max = computeUV<nFace>(arg, arg.U, arg.AV, parity, x_cb, ic * arg.uvTile.M, jc * arg.uvTile.N);
if (Arg::compute_max) atomic_fetch_abs_max(arg.max, max);
}
};
/**
Functor for the LV computation.
If Arg::compute_max is true, then we do not save the result, and
instead merely compute the maximum element. This is used for
setting the scale for fixed point.
*/
template <typename Arg> struct compute_lv {
using real = typename Arg::Float;
static constexpr int nFace = 3;
static_assert(Arg::fineSpin == 1, "compute_lv is only defined for the staggered dslash");
const Arg &arg;
static constexpr const char *filename() { return KERNEL_FILE; }
constexpr compute_lv(const Arg &arg) : arg(arg) { }
/**
3-d parallelism
@param[in] x_cb e/o fine-grid spacetime
@param[in] ic_parity parity * output color column
@param[in] jc output color row
*/
__device__ __host__ void operator()(int x_cb, int ic_parity, int jc)
{
int ic = ic_parity % arg.uvTile.M_tiles;
int parity = ic_parity / arg.uvTile.M_tiles;
real max;
if (arg.dir == QUDA_FORWARDS || arg.dir == QUDA_IN_PLACE) // only for preconditioned clover is V != AV, will need extra logic for staggered KD
max = computeUV<nFace>(arg, arg.L, arg.V, parity, x_cb, ic * arg.uvTile.M, jc * arg.uvTile.N);
else
max = computeUV<nFace>(arg, arg.L, arg.AV, parity, x_cb, ic * arg.uvTile.M, jc * arg.uvTile.N);
if (Arg::compute_max) atomic_fetch_abs_max(arg.max, max);
}
};
/**
Calculates the matrix A V^{s,c'}(x) = \sum_c A^{c}(x) * V^{s,c}(x)
Where: s = fine spin, c' = coarse color, c = fine color
If Arg::compute_max is true, then we do not save the result, and
instead merely compute the maximum element. This is used for
setting the scale for fixed point.
*/
template <typename Arg> struct compute_av {
using real = typename Arg::Float;
const Arg &arg;
static constexpr const char *filename() { return KERNEL_FILE; }
constexpr compute_av(const Arg &arg) : arg(arg) { }
/**
3-d parallelism
@param[in] x_cb e/o fine-grid spacetime
@param[in] ch_parity chirality * parity
@param[in] ic_c output color
*/
__device__ __host__ inline void operator()(int x_cb, int ch_parity, int ic_c)
{
int ch = ch_parity % 2;
int parity = ch_parity / 2;
constexpr int N = Arg::fineSpin * Arg::fineColor / 2;
HMatrix<real, N> A;
#pragma unroll
for (int i = 0; i < N; i++) {
int s_i = i / Arg::fineColor;
int c_i = i % Arg::fineColor;
#pragma unroll
for (int j = 0; j <= i; j++) {
int s_j = j / Arg::fineColor;
int c_j = j % Arg::fineColor;
#ifndef DYNAMIC_CLOVER
A(i, j) = arg.Cinv(parity, x_cb, ch, s_i, s_j, c_i, c_j);
#else
A(i, j) = arg.C(parity, x_cb, ch, s_i, s_j, c_i, c_j);
#endif
}
}
ColorSpinor<real, Arg::fineColor, Arg::fineSpin / 2> V;
#pragma unroll
for (int s = 0; s < Arg::fineSpin / 2; s++) {
#pragma unroll
for (int c = 0; c < Arg::fineColor; c++) { V(s, c) = arg.V(parity, x_cb, 2 * ch + s, c, ic_c); }
}
#ifndef DYNAMIC_CLOVER
auto AV = A * V;
#else
// solve for the matrix
linalg::Cholesky<HMatrix, clover::cholesky_t<real>, N> cholesky(A);
auto AV = cholesky.solve(V);
#endif
if (!Arg::compute_max) {
#pragma unroll
for (int s = 0; s < Arg::fineSpin / 2; s++) {
#pragma unroll
for (int ic = 0; ic < Arg::fineColor; ic++) { arg.AV(parity, x_cb, 2 * ch + s, ic, ic_c) = AV(s, ic); }
}
} else {
real max = static_cast<real>(0.0);
#pragma unroll
for (int s = 0; s < Arg::fineSpin / 2; s++) {
#pragma unroll
for (int ic = 0; ic < Arg::fineColor; ic++) {
auto abs_max = fmax(abs(AV(s, ic).real()), abs(AV(s, ic).imag()));
max = fmax(abs_max, max);
}
}
atomic_fetch_abs_max(arg.max, max);
}
}
};
/**
Calculates the matrix A V^{s,c'}(x) = \sum_c A^{c}(x) * V^{s,c}(x) for twisted-mass fermions
Where: s = fine spin, c' = coarse color, c = fine color
*/
template <typename Arg> struct compute_tmav {
using real = typename Arg::Float;
const Arg &arg;
static constexpr const char *filename() { return KERNEL_FILE; }
constexpr compute_tmav(const Arg &arg) : arg(arg) { }
/**
3-d parallelism
@param[in] x_cb e/o fine-grid spacetime
@param[in] parity parity index
@param[in] v output color
*/
__device__ __host__ inline void operator()(int x_cb, int parity, int v)
{
complex<real> fp(1./(1.+arg.mu*arg.mu),-arg.mu/(1.+arg.mu*arg.mu));
complex<real> fm(1./(1.+arg.mu*arg.mu),+arg.mu/(1.+arg.mu*arg.mu));
#pragma unroll
for (int s = 0; s < Arg::fineSpin/2; s++) {
#pragma unroll
for (int c = 0; c < Arg::fineColor; c++) {
arg.AV(parity,x_cb,s,c,v) = arg.V(parity,x_cb,s,c,v) * fp;
}
}
#pragma unroll
for (int s = Arg::fineSpin/2; s < Arg::fineSpin; s++) {
#pragma unroll
for (int c = 0; c < Arg::fineColor; c++) {
arg.AV(parity,x_cb,s,c,v) = arg.V(parity,x_cb,s,c,v) * fm;
}
}
}
};
/**
Calculates the matrix A V^{s,c'}(x) = \sum_c A^{c}(x) * V^{s,c}(x) for twisted-clover fermions
Where: s = fine spin, c' = coarse color, c = fine color
If Arg::compute_max is true, then we do not save the result, and
instead merely compute the maximum element. This is used for
setting the scale for fixed point.
*/
template <typename Arg> struct compute_tmcav {
using real = typename Arg::Float;
const Arg &arg;
static constexpr const char *filename() { return KERNEL_FILE; }
constexpr compute_tmcav(const Arg &arg) : arg(arg) { }
/**
3-d parallelism
@param[in] x_cb e/o fine-grid spacetime
@param[in] ch_parity chirality * parity
@param[in] c_row output color column
*/
__device__ __host__ inline void operator()(int x_cb, int ch_parity, int ic_c)
{
int ch = ch_parity % 2;
int parity = ch_parity / 2;
constexpr int N = Arg::fineSpin * Arg::fineColor / 2;
HMatrix<real, N> A;
#pragma unroll
for (int i = 0; i < N; i++) {
int s_i = i / Arg::fineColor;
int c_i = i % Arg::fineColor;
#pragma unroll
for (int j = 0; j <= i; j++) {
int s_j = j / Arg::fineColor;
int c_j = j % Arg::fineColor;
A(i, j) = arg.C(parity, x_cb, ch, s_i, s_j, c_i, c_j);
}
}
complex<real> mu(0., arg.mu);
if (ch == 0) mu *= static_cast<real>(-1.0);
ColorSpinor<real, Arg::fineColor, Arg::fineSpin / 2> V;
#pragma unroll
for (int s = 0; s < Arg::fineSpin / 2; s++) {
#pragma unroll
for (int c = 0; c < Arg::fineColor; c++) {
V(s, c) = arg.V(parity, x_cb, 2 * ch + s, c, ic_c);
}
}
// first apply the clover matrix directly, including mu
auto UV = A * V;
UV += mu * V;
// Then we calculate AV = Cinv UV, so [AV = (C^2 + mu^2)^{-1} (Clover -/+ i mu)·Vector]
// for in twisted-clover fermions, Cinv keeps (C^2 + mu^2)^{-1}
if (!clover::dynamic_inverse()) {
// load in the clover inverse matrix
HMatrix<real, N> Ainv;
#pragma unroll
for (int i = 0; i < N; i++) {
int s_i = i / Arg::fineColor;
int c_i = i % Arg::fineColor;
#pragma unroll
for (int j = 0; j <= i; j++) {
int s_j = j / Arg::fineColor;
int c_j = j % Arg::fineColor;
Ainv(i, j) = arg.Cinv(parity, x_cb, ch, s_i, s_j, c_i, c_j);
}
}
auto AV = Ainv * UV;
if (!Arg::compute_max) {
#pragma unroll
for (int s = 0; s < Arg::fineSpin / 2; s++)
#pragma unroll
for (int c = 0; c < Arg::fineColor; c++)
arg.AV(parity, x_cb, 2 * ch + s, c, ic_c) = AV(s, c);
} else {
real max = static_cast<real>(0.0);
#pragma unroll
for (int s = 0; s < Arg::fineSpin / 2; s++) {
#pragma unroll
for (int c = 0; c < Arg::fineColor; c++) {
auto abs_max = fmax(abs(AV(s, c).real()), abs(AV(s, c).imag()));
max = fmax(abs_max, max);
}
}
atomic_fetch_abs_max(arg.max, max);
}
} else {
// compute the clover inverse matrix with the already loaded clover matrix
A = A.square();
A += arg.mu * arg.mu;
linalg::Cholesky<HMatrix, clover::cholesky_t<real>, N> cholesky(A);
const auto AV = cholesky.solve(UV);
if (!Arg::compute_max) {
#pragma unroll
for (int s = 0; s < Arg::fineSpin / 2; s++)
#pragma unroll
for (int c = 0; c < Arg::fineColor; c++)
arg.AV(parity, x_cb, 2 * ch + s, c, ic_c) = AV(s, c);
} else {
real max = static_cast<real>(0.0);
#pragma unroll
for (int s = 0; s < Arg::fineSpin / 2; s++) {
#pragma unroll
for (int c = 0; c < Arg::fineColor; c++) {
auto abs_max = fmax(abs(AV(s, c).real()), abs(AV(s, c).imag()));
max = fmax(abs_max, max);
}
}
atomic_fetch_abs_max(arg.max, max);
}
}
}
};
/**
Calculates the matrix A V^{s,c'}(x) = \sum_{c,d} K^{c}_{d}(x) * V^{s,c}(x+d)
Where: s = fine spin, c' = coarse color, c = fine color, d = hypercube corner
See staggered_kd_apply_xinv_kernel.cuh for reference
If Arg::compute_max is true, then we do not save the result, and
instead merely compute the maximum element. This is used for
setting the scale for fixed point.
*/
template <typename Arg> struct compute_kv {
using real = typename Arg::Float;
using Vector = ColorSpinor<real, Arg::fineColor, 1>;
using Link = Matrix<complex<float>, Arg::fineColor>;
const Arg &arg;
static constexpr const char *filename() { return KERNEL_FILE; }
constexpr compute_kv(const Arg &arg) : arg(arg) { }
/**
3-d parallelism
@param[in] x_cb e/o fine-grid spacetime
@param[in] ch_parity [chirality (clover), source parity (staggered)] and parity
@param[in] c_row output color column
*/
__device__ __host__ inline void operator()(int x_cb, int ch_parity, int ic_c)
{
const int nbr_parity = ch_parity % 2;
const int parity = ch_parity / 2;
// Get coordinates
constexpr auto nDim = 4;
Coord<nDim> coord;
coord.x_cb = x_cb;
coord.X = getCoordsCB(coord, x_cb, arg.x_size, arg.x_size[0] / 2, parity);
// Get location of unit corner of hypercube
int x_c[nDim];
#pragma unroll
for (int d = 0; d < nDim; d++)
x_c[d] = 2 * (coord[d] / 2);
Vector out;
// only needed for dagger
// global parity == parity w/in the KD block
int my_corner = 8 * parity + 4 * (coord[3] % 2) + 2 * (coord[2] % 2) + (coord[1] % 2);
// Begin accumulating into the output vector
// start at 0 -> store in spin 0 (even source)
// start at 8 -> store in spin 1 (odd source)
int nbr_corner = (nbr_parity == 0) ? 0 : 8;
#pragma unroll
for (int nbr_t = 0; nbr_t < 2; nbr_t++) {
#pragma unroll
for (int nbr_z = 0; nbr_z < 2; nbr_z++) {
#pragma unroll
for (int nbr_y = 0; nbr_y < 2; nbr_y++) {
const int offset[nDim] = { (nbr_parity + nbr_t + nbr_z + nbr_y) & 1, nbr_y, nbr_z, nbr_t };
const int nbr_x_cb = linkIndexShift(x_c, offset, arg.x_size);
// Load Xinv link
Link Xinv;
#pragma unroll
for (int ic_f = 0; ic_f < Arg::fineColor; ic_f++) {
#pragma unroll
for (int jc_f = 0; jc_f < Arg::fineColor; jc_f++) {
Xinv(ic_f, jc_f) = arg.kd_dagger ? arg.K(my_corner, nbr_parity, nbr_x_cb, 0, 0, ic_f, jc_f) : arg.K(nbr_corner, parity, x_cb, 0, 0, ic_f, jc_f);
}
}
// Load spinor
Vector in;
#pragma unroll
for (int jc_f = 0; jc_f < Arg::fineColor; jc_f++) {