su2hmc
Loading...
Searching...
No Matches
cuclover.cu
Go to the documentation of this file.
1
8#include <clover.h>
9
10//CUDA Device code
11namespace Device{
23 template <typename T>
24 __device__ void cuByGenLeft(T a[nc],const unsigned short gen){
25 T tmp = a[0];
26 switch(gen){
28 case(0):
29 a[0] = T(-a[1].imag(), -a[1].real());
30 a[1] = T( tmp.imag(), tmp.real());
31 break;
33 case(1):
34 a[0] = -conj(a[1]);
35 a[1] = conj(tmp);
36 break;
38 case(2):
39 a[0] = T(-a[0].imag(), a[0].real());
40 a[1] = T(-a[1].imag(), a[1].real());
41 break;
42 }
43 return;
44 }
45
55 template <typename T>
56 __device__ void cuByGenRight(T a[nc],const unsigned short gen){
57 T tmp = a[0];
58 switch(gen){
60 case(0):
61 a[0] = T(-a[1].imag(), a[1].real());
62 a[1] = T(-tmp.imag(), tmp.real());
63 break;
65 case(1):
66 a[0]=-a[1]; a[1]=tmp;
67 break;
69 case(2):
70 a[0] = T(-a[0].imag(), a[0].real());
71 a[1] = T( a[1].imag(), -a[1].real());
72 break;
73 }
74 return;
75 }
76
90 template <typename T>
91 __device__ void Half_Leaf(complex<T> Leaves[nc], const complex<T> * __restrict__ u11t,
92 const complex<T> * __restrict__ u12t, complex<T> a[nc], const unsigned int * __restrict__ iu,
93 const unsigned int * __restrict__ id, const unsigned int i, const unsigned short mu, const unsigned short nu,
94 const unsigned short leaf){
95
96 unsigned int ind; unsigned int double_ind=id[nu*kvol+ind];
97 switch(leaf){
98 case(0):
100 a[0]=u11t[i+kvolHalo*mu]; a[1]=u12t[i+kvolHalo*mu];
101 ind = iu[mu*kvol+i];
102
104 Leaves[0]=a[0]*u11t[ind+kvolHalo*nu]-a[1]*conj(u12t[ind+kvolHalo*nu]);
105 Leaves[1]=a[0]*u12t[ind+kvolHalo*nu]+a[1]*conj(u11t[ind+kvolHalo*nu]);
106 break;
107 case(1):
109 ind = id[mu*kvol+i];
110 a[0]=u11t[i+kvolHalo*nu]; a[1]=u12t[i+kvolHalo*nu];
111 //Awkward index...
112 double_ind=iu[nu*kvol+ind];
114 Leaves[0]=a[0]*conj(u11t[double_ind+kvolHalo*mu])+a[1]*conj(u12t[double_ind+kvolHalo*mu]);
115 Leaves[1]=-a[0]*u12t[double_ind+kvolHalo*mu]+a[1]*u11t[double_ind+kvolHalo*mu];
116 break;
117 case(2):
119 ind = id[nu*kvol+i];
120 //Daggered. So Conj what goes into a[0] and negate what goes into a[1]
121 a[0]=conj(u11t[ind+kvolHalo*nu]); a[1]=-u12t[ind+kvolHalo*nu];
122
124 Leaves[0]=a[0]*u11t[ind+kvolHalo*mu]-a[1]*conj(u12t[ind+kvolHalo*mu]);
125 //Don't forget negatiion of second term was handled earlier!
126 Leaves[1]=a[0]*u12t[ind+kvolHalo*mu]+a[1]*conj(u11t[ind+kvolHalo*mu]);
127 break;
128 case(3):
130 ind = id[i+kvol*mu];
131 //Daggered. So Conj what goes into a[0] and negate what goes into a[1]
132 a[0]=conj(u11t[ind+kvolHalo*mu]); a[1]=-u12t[ind+kvolHalo*mu];
133 //Another awkward index
134 double_ind=id[nu*kvol+ind];
135
137 Leaves[0]=a[0]*conj(u11t[double_ind+kvolHalo*nu])+a[1]*conj(u12t[double_ind+kvolHalo*nu]);
138 Leaves[1]=-a[0]*u12t[double_ind+kvolHalo*nu]+a[1]*u11t[double_ind+kvolHalo*nu];
139 break;
140 }
141 return;
142 }
143
154 template <typename T>
155 __device__ void Leaf(const complex<T> * __restrict__ u11t, const complex<T> * __restrict__ u12t,
156 complex<T> Leaves[nc], const unsigned int * __restrict__ iu, const unsigned int * __restrict__ id,
157 const unsigned int i,const unsigned short mu, const unsigned short nu,const unsigned short leaf){
158 complex<T> a[nc];
159 Half_Leaf(Leaves,u11t,u12t,a,iu,id,i,mu,nu,leaf);
160 unsigned int ind,double_ind;
161 switch(leaf){
162 case(0)://Need braces for strict C++ standard compliance as a variable is declared in the case
163 {
164 ind = iu[nu*kvol+i];
166 a[0]=Leaves[0]*conj(u11t[ind+kvolHalo*mu])+Leaves[1]*conj(u12t[ind+kvolHalo*mu]);
167 a[1]=-Leaves[0]*u12t[ind+kvolHalo*mu]+Leaves[1]*u11t[ind+kvolHalo*mu];
168
170 Leaves[0]=a[0]*conj(u11t[i+kvolHalo*nu])+a[1]*conj(u12t[i+kvolHalo*nu]);
171 Leaves[1]=-a[0]*u12t[i+kvolHalo*nu]+a[1]*u11t[i+kvolHalo*nu];
172
173 }
174 break;
175 case(1):
177 ind = id[mu*kvol+i];
178
180 a[0]=Leaves[0]*conj(u11t[ind+kvolHalo*nu])+Leaves[1]*conj(u12t[ind+kvolHalo*nu]);
181 a[1]=-Leaves[0]*u12t[ind+kvolHalo*nu]+Leaves[1]*u11t[ind+kvolHalo*nu];
182
184 Leaves[0]=a[0]*u11t[ind+kvolHalo*mu]-a[1]*conj(u12t[ind+kvolHalo*mu]);
185 Leaves[1]=a[0]*u12t[ind+kvolHalo*mu]+a[1]*conj(u11t[ind+kvolHalo*mu]);
186 break;
187 case(2):
189 ind = id[nu*kvol+i];
190 double_ind=iu[mu*kvol+ind];
192 a[0]=Leaves[0]*u11t[double_ind+kvolHalo*nu]-Leaves[1]*conj(u12t[double_ind+kvolHalo*nu]);
193 a[1]=Leaves[0]*u12t[double_ind+kvolHalo*nu]+Leaves[1]*conj(u11t[double_ind+kvolHalo*nu]);
194
196 Leaves[0]=a[0]*conj(u11t[i+kvolHalo*mu])+a[1]*conj(u12t[i+kvolHalo*mu]);
197 Leaves[1]=-a[0]*u12t[i+kvolHalo*mu]+a[1]*u11t[i+kvolHalo*mu];
198
199 break;
200 case(3)://Need braces for strict C++ standard compliance as a variable is declared in the case
202 ind = id[nu*kvol+i];
203 double_ind=id[mu*kvol+ind];
204
206 a[0]=Leaves[0]*u11t[double_ind+kvolHalo*mu]-Leaves[1]*conj(u12t[double_ind+kvolHalo*mu]);
207 a[1]=Leaves[0]*u12t[double_ind+kvolHalo*mu]+Leaves[1]*conj(u11t[double_ind+kvolHalo*mu]);
208
210 Leaves[0]=a[0]*u11t[ind+kvolHalo*nu]-a[1]*conj(u12t[ind+kvolHalo*nu]);
211 Leaves[1]=a[0]*u12t[ind+kvolHalo*nu]+a[1]*conj(u11t[ind+kvolHalo*nu]);
212 break;
213 }
214 return;
215 }
216
217
226 template <typename T>
227 __device__ void cuGLeft(T out[4],const T G[2], const T X[4]){
228 out[0]=G[0]*X[0]+G[1]*X[2];
229 out[1]=G[0]*X[1]+G[1]*X[3];
230 out[2]=-conj(G[1])*X[0]+conj(G[0])*X[2];
231 out[3]=-conj(G[1])*X[1]+conj(G[0])*X[3];
232 return;
233 }
234
242 template <typename T>
243 __device__ void cuGRight(T out[4],const T G[2], const T X[4]){
244 out[0]=G[0]*X[0]-conj(G[1])*X[1];
245 out[1]=G[1]*X[0]+conj(G[0])*X[1];
246 out[2]=G[0]*X[2]-conj(G[1])*X[3];
247 out[3]=G[1]*X[2]+conj(G[0])*X[3];
248 return;
249 }
250
259 template <typename T>
260 __device__ void cuGSandwich(T out[4],T tmp[4], const T Gl[2], const T X[4],const T Gr[2]){
261 cuGRight(tmp,Gr,X);
262 cuGLeft(out,Gl,tmp);
263 return;
264 }
265
275 __device__ void GetBilinear(Complex_f Z[nc*nc], Bilinear_a Xmn,unsigned int ind){
276 Z[0]=Xmn.diag[ind]; Z[3]=Xmn.diag[ind+kvolHalo];
277 Z[1]=Xmn.offd[ind]; Z[2]=conj(Z[1]);
278 return;
279 }
280}
281
283namespace Kernels{
284 using namespace Device;
296 template <typename T>
297 __global__ __launch_bounds__(__BSIZE__) void Half_Leaves(complex<T> *hLeaves0,complex<T> *hLeaves1,
298 const complex<T> * __restrict__ u11t, const complex<T> * __restrict__ u12t,
299 const unsigned int * __restrict__ iu,const unsigned int * __restrict__ id,
300 const __grid_constant__ unsigned short mu,const __grid_constant__ unsigned short nu){
301
302 const volatile int gsize = gridDim.x*gridDim.y*gridDim.z;
303 const volatile int bsize = blockDim.x*blockDim.y*blockDim.z;
304 const volatile int blockId = blockIdx.x+ blockIdx.y * gridDim.x+ gridDim.x * gridDim.y * blockIdx.z;
305 const volatile int bthreadId= (threadIdx.z * blockDim.y+ threadIdx.y)* blockDim.x+ threadIdx.x;
306 const volatile int gthreadId= blockId * bsize+bthreadId;
307
308 complex<T> Leaves[nc], a[nc];
309 for(unsigned short leaf=0;leaf<ndim;leaf++)
310 for(unsigned int i=gthreadId;i<kvol;i+=gsize*bsize){
311 Half_Leaf(Leaves,u11t,u12t,a,iu,id,i,mu,nu,leaf);
312 hLeaves0[i+kvol*leaf]=Leaves[0]; hLeaves1[i+kvol*leaf]=Leaves[1];
313 }
314 return;
315 }
316
328 template <typename T>
329 __global__ __launch_bounds__(__BSIZE__) void Full_Clover(complex<T> * __restrict__ clover1, complex<T> * __restrict__ clover2,\
330 const complex<T> * __restrict__ u11t, const complex<T> * __restrict__ u12t,
331 const unsigned int * __restrict__ iu, const unsigned int * __restrict__ id, const __grid_constant__ int mu,
332 const __grid_constant__ int nu){
333 const volatile int gsize = gridDim.x*gridDim.y*gridDim.z;
334 const volatile int bsize = blockDim.x*blockDim.y*blockDim.z;
335 const volatile int blockId = blockIdx.x+ blockIdx.y * gridDim.x+ gridDim.x * gridDim.y * blockIdx.z;
336 const volatile int bthreadId= (threadIdx.z * blockDim.y+ threadIdx.y)* blockDim.x+ threadIdx.x;
337 const volatile int gthreadId= blockId * bsize+bthreadId;
338
339 complex<T> Leaves[2];
340 for(unsigned int i=gthreadId;i<kvol;i+=gsize*bsize){
341 clover1[i]=0;clover2[i]=0;
342 for(unsigned short leaf=0;leaf<ndim;leaf++)
343 {
344 //Pointer arithemetic on the leaves.
345 Leaf(u11t,u12t,Leaves,iu,id,i,mu,nu,leaf);
346 clover1[i]+=Leaves[0]; clover2[i]+=Leaves[1];
347 }
350
354 clover1[i]=clover1[i].imag(); clover1[i]*=(1.0f/4.0f);
355 // clover1[i]=clover1[1].imag()/4.0f;
356
358 clover2[i]+=clover2[i]; clover2[i]*=(-I_f/8.0f);
359 }
360 return;
361 }
362
376 template <typename T>
377 __global__ __launch_bounds__(__BSIZE__) void cuCalcXmunu(Bilinear_a Xmunu, const complex<T> * __restrict__ X1,
378 const complex<T> * __restrict__ X2, const __grid_constant__ complex<T> sigval_G[24],
379 const __grid_constant__ unsigned short sigin_G[24], const __grid_constant__ unsigned short clov){
380 const char funcname[] = "Xmunu";
381 const unsigned int gsize = gridDim.x*gridDim.y*gridDim.z;
382 const unsigned int bsize = blockDim.x*blockDim.y*blockDim.z;
383 const unsigned int blockId = blockIdx.x+ blockIdx.y * gridDim.x+ gridDim.x * gridDim.y * blockIdx.z;
384 const unsigned int bthreadId= (threadIdx.z * blockDim.y+ threadIdx.y)* blockDim.x+ threadIdx.x;
385 const unsigned int gthreadId= blockId * bsize+bthreadId;
386
387 __shared__ complex<T> sigval[24]; __shared__ unsigned short sigin[24];
388#pragma unroll
389 for(unsigned short i=gthreadId%__BSIZE__;i<24;i+=bsize){
390 sigval[i]=sigval_G[i];
391 sigin[i]=sigin_G[i];
392 }
393 __syncthreads();
394 //Get sign and index of @f$\sigma_{\mu\nu}@f correct
395 for(unsigned int i=gthreadId;i<kvol;i+=gsize*bsize){
396 //Buffer. Four registers
397 Bilinear Xmn;
398 Xmn.diag[0]=Xmn.diag[1]=0;Xmn.offd=0;
399 for(unsigned short idirac=0; idirac<ndirac*nc; idirac+=nc){
400 const unsigned short sind = sigin[clov*ndirac+(idirac>>1)]<<1;
401 const complex<T> sig = sigval[clov*ndirac+(idirac>>1)];
402 for(unsigned short c1=0;c1<nc;c1++){
403 //Spinors (rows) So we only load from memory once.
404 const complex<T> X1s = X1[i+kvolHalo*(sind+c1)];
405 const complex<T> X2s = X2[i+kvolHalo*(sind+c1)];
406 for(unsigned short c2=0;c2<nc;c2++){
407 //The second off diagonal term is the conjugate of the first
408 if(c1==1&&c2==0)
409 continue;
410 //Conjugated spinor (columns).
411 const complex<T> X1c = conj(X1[i+kvolHalo*(idirac+c2)]);
412 const complex<T> X2c = conj(X2[i+kvolHalo*(idirac+c2)]);
413 if(c1==c2)
414 Xmn.diag[c1]+=(T)(sig*(X2s*X1c+X1s*X2c)).real();
415 else
416 Xmn.offd+=sig*(X2s*X1c+X1s*X2c);
417 }
418 }
419 }
420 //Write the diagonals
421 for(unsigned short c=0;c<nc;c++)
422 Xmunu.diag[i+kvolHalo*c]=Xmn.diag[c];
423 //And the off diagonal terms
424 Xmunu.offd[i]=Xmn.offd;
425 }
426 return;
427 }
428
443 template <typename T>
444 __global__ __launch_bounds__(__BSIZE__) void Clov_Force(double * __restrict__ dSdpi, const complex<T> * __restrict__ u11t,
445 const complex<T> * __restrict__ u12t, const Bilinear_a Xmn, const unsigned int * __restrict__ iu,
446 const unsigned int * __restrict__ id, const __grid_constant__ float akappa,
447 const __grid_constant__ unsigned short mu, const __grid_constant__ unsigned short nu){
448
449 const unsigned int gsize = gridDim.x*gridDim.y*gridDim.z;
450 const unsigned int bsize = blockDim.x*blockDim.y*blockDim.z;
451 const unsigned int blockId = blockIdx.x+ blockIdx.y * gridDim.x+ gridDim.x * gridDim.y * blockIdx.z;
452 const unsigned int bthreadId= (threadIdx.z * blockDim.y+ threadIdx.y)* blockDim.x+ threadIdx.x;
453 const unsigned int gthreadId= blockId * bsize+bthreadId;
454 //And get the @f$X_{\mu\nu}@f$ values
455 //Loop over @f$\mu@f$ and @f$\nu@f$,
456 for(unsigned int i=gthreadId;i<kvol;i+=gsize*bsize){
457 //Buffer for intermediate force calculation. One for each generator.
458 //This is where it gets messy. Using HiRep/OpenQCD labelling for different intermediate values
459 //But recycling to reduce register pressure on GPU
460 //First up, W0, W1 and W6 match their Documentation values
461 complex<T> W0[2]; complex<T> W1[2]; complex<T> W6[2];
462 //Get the correct site. Originally ind and stood for up and down. Then I realised only one was needed
463 //at a time and am too lazy to change it everywhere.
464 unsigned int ind = id[i+kvol*nu];
465 //Gauge field @f$U_\nu\left(i-\hat{\nu}\right)
466 W1[0]=u11t[ind+kvolHalo*nu]; W1[1]=u12t[ind+kvolHalo*nu];
467
468 //@f$Z_2=X_{\mu\nu}\left(i-\hat{\nu}\right)@f$
469 complex<T> Z[nc*nc];
470 Device::GetBilinear(Z,Xmn,ind);
471
472 //W0 is @f$U^\dagger_\mu@f(x-\hat{nu}\right)@f$
473 W0[0]=conj(u11t[ind+kvolHalo*mu]); W0[1]=-u12t[ind+kvolHalo*mu];
474
475 //Need a temporary Z buffers for the intermediate result
476 complex<T> Zbuff1[nc*nc];complex<T> Zbuff2[nc*nc];
477 Device::cuGSandwich(Zbuff1,Zbuff2,W0,Z,W1);
478
479 //@f$W_6=W_0 W_1@f$
480 W6[0]=W0[0]*W1[0]-W0[1]*conj(W1[1]); W6[1]=W0[0]*W1[1]+W0[1]*conj(W1[0]);
481
482 //Z3 is the @f$X_{\mu\nu}\left(x+\hat{\mu}-\hat{\nu}\right)@f$. Store in Z
483 ind=iu[ind+kvol*mu];
484 Device::GetBilinear(Z,Xmn,ind);
485
486 //Need a second Zbuffer for another intermediate result.
487 Device::cuGRight(Zbuff2,W6,Z);
488 //Sum the two results into Zbuff1. Then scale by -W5
489#pragma unroll
490 for(unsigned short c=0;c<nc*nc;c++)
491 Zbuff1[c]+=Zbuff2[c];
492 //W5 is @f$U^\dagger_\nu\left(x+\hat{\mu}-\hat{\nu}\right)@f$
493 complex<T> W5[2];
494 W5[0]=conj(u11t[ind+kvolHalo*nu]); W5[1]=-u12t[ind+kvolHalo*nu];
495 //Now multiply by @f$W_5@f$ from the left into Zbuff2
496 Device::cuGLeft(Zbuff2,W5,Zbuff1);
497
498 //Intermediate results from the four parts of the sum.
499 complex<T> F_int[4];
500#pragma unroll
501 for(unsigned short c=0;c<nc*nc;c++)
502 //Negative as it is @f$-W_5@f$
503 F_int[c]=-Zbuff2[c];
504
505 //Now we repeat for the last term in the sum. Recycling along the way.
506 //First store @f$W_2=U_\nu\left(x+\hat{\mu}\right)@f$ into W0.
507 ind=iu[i+kvol*mu];
508 W0[0]=u11t[ind+kvolHalo*nu]; W0[1]=u12t[ind+kvolHalo*nu];
509 //@f$W_3=U^\dagger_\mu\left(x+\hat{\nu}\right). Storing it in W1
510 ind=iu[i+kvol*nu];
511 W1[0]=conj(u11t[ind+kvolHalo*mu]); W1[1]=-u12t[ind+kvolHalo*mu];
512 //@f$Z_4=X_{\mu\nu}\left(x+\hat{\mu}+\hat{\nu}\right)@f$. Storing in Z
513 ind=iu[ind+kvol*mu];
514 Device::GetBilinear(Z,Xmn,ind);
515 //Calculate and write into Zbuff1
516 Device::cuGSandwich(Zbuff1,Zbuff2,W0,Z,W1);
517
518 //@f$W_7=W_0 W_1@f$
519 complex<T> W7[2];
520 W7[0]=W0[0]*W1[0]-W0[1]*conj(W1[1]); W7[1]=W0[0]*W1[1]+W0[1]*conj(W1[0]);
521 //@f$Z_5=X_{\mu\nu}\left(x+\hat{\nu}\right)@f$
522 ind=iu[i+kvol*nu];
523 Device::GetBilinear(Z,Xmn,ind);
524 //And calculate the second term
525 Device::cuGLeft(Zbuff2,W7,Z);
526 //Sum the two results into Zbuff1.
527#pragma unroll
528 for(unsigned short c=0;c<nc*nc;c++)
529 Zbuff1[c]+=Zbuff2[c];
530 //W4 is @f$U^\dagger_\nu\left(x\right)@f$
531 complex<T> W4[2];
532 W4[0]=conj(u11t[i+kvolHalo*nu]); W4[1]=-u12t[i+kvolHalo*nu];
533 //Now multiply by @f$W_4@f$ from the right into Zbuff2
534 Device::cuGRight(Zbuff2,W4,Zbuff1);
535
536 //Intermediate results from the four parts of the sum.
537#pragma unroll
538 for(unsigned short c=0;c<nc*nc;c++)
539 F_int[c]+=Zbuff2[c];
540 //The last thing we need is @f$W_8=W_7W_4-W_5W_6@f$. Do it in parts and store intermediates in W0 and W1
541 W0[0]=W7[0]*W4[0]-W7[1]*conj(W4[1]); W0[1]=W7[0]*W4[1]+W7[1]*conj(W4[0]);
542 W1[0]=W5[0]*W6[0]-W5[1]*conj(W6[1]); W1[1]=W5[0]*W6[1]+W5[1]*conj(W6[0]);
543 //Store W8 in W0
544 W0[0]-=W1[0]; W0[1]-=W1[1];
545
546 //Now load @f$@Z_0=X_{\mu\nu}(x)@f$
547 Device::GetBilinear(Z,Xmn,i);
548 Device::cuGLeft(Zbuff1,W0,Z);
549 //And sum intermediate
550#pragma unroll
551 for(unsigned short c=0;c<nc*nc;c++)
552 F_int[c]+=Zbuff1[c];
553
554 //Now load @f$@Z_1=X_{\mu\nu}(x)@f$
555 ind=iu[i+kvol*mu];
556 Device::GetBilinear(Z,Xmn,ind);
557 Device::cuGRight(Zbuff1,W0,Z);
558 //And sum intermediate
559#pragma unroll
560 for(unsigned short c=0;c<nc*nc;c++){
561 F_int[c]+=Zbuff1[c];
562 F_int[c]*=-I_f;
563 }
564
565 //Excellent. Now we just need to multiply by the derivative term
566 W0[0]=u11t[i+kvolHalo*mu]; W0[1]=u12t[i+kvolHalo*mu];
567 for(unsigned short gen=0;gen<nadj;gen++){
568 W1[0]=W0[0]; W1[1]=W0[1];
569 cuByGenLeft(W1,gen);
570 Device::cuGLeft(Zbuff1,W1,F_int);
571 //Sum of the real part of the trace.
572 float dSdpis=creal(Zbuff1[0])+creal(Zbuff1[3]);
573 //tmp lets us control the number of registers explictly
574 if(mu<nu)
575 dSdpi[i+kvol*(gen*ndim+mu)] -=akappa*dSdpis/4.0f;
576 else
577 dSdpi[i+kvol*(gen*ndim+mu)] +=akappa*dSdpis/4.0f;
578
579 }
580 }
581 return;
582 }
583
584 //Clover multiplication
599 template <typename T>
600 __global__ __launch_bounds__(__BSIZE__) void ByClover(complex<T> * phi,const complex<T> * __restrict__ r,
601 const complex<T> * __restrict__ clover1,
602 const complex<T> * __restrict__ clover2,const __grid_constant__ complex<T> sigval_G[24],
603 const __grid_constant__ float akappa,
604 const __grid_constant__ unsigned short sigin_G[24],const __grid_constant__ bool dag){
605 const unsigned int gsize = gridDim.x*gridDim.y*gridDim.z;
606 const unsigned int bsize = blockDim.x*blockDim.y*blockDim.z;
607 const unsigned int blockId = blockIdx.x+ blockIdx.y * gridDim.x+ gridDim.x * gridDim.y * blockIdx.z;
608 const unsigned int bthreadId= (threadIdx.z * blockDim.y+ threadIdx.y)* blockDim.x+ threadIdx.x;
609 const unsigned int gthreadId= blockId * bsize+bthreadId;
610 __shared__ complex<T> sigval[24]; __shared__ unsigned short sigin[24];
611#pragma unroll
612 for(unsigned short i=gthreadId%__BSIZE__;i<24;i+=bsize){
613 sigval[i]=sigval_G[i];
614 sigin[i]=sigin_G[i];
615 }
616 __syncthreads();
617
618 for(unsigned int i=gthreadId;i<kvol;i+=bsize*gsize){
619 //Prefetched r and Phi array
620 complex<T> phi_s[ngorkov][nc];
621#pragma unroll
622 for(unsigned short igorkov=0; igorkov<ngorkov; igorkov++)
623 for(unsigned short c=0; c<nc; c++){
624 phi_s[igorkov][c]=0;
625 }
626 complex<T> r_s[nc];
627 complex<T> clov_s[nc];
628 for(unsigned short clov=0;clov<6;clov++){
629 clov_s[0]=clover1[clov*kvol+i]; clov_s[1]=clover2[clov*kvol+i];
630#pragma unroll
631 for(unsigned short igorkov=0; igorkov<ngorkov; igorkov++){
632 //Mod 4 done bitwise. In general n mod 2^m = n & (2^m-1)
633 const unsigned short idirac = igorkov&3;
634 const unsigned short sind = (igorkov<4) ? sigin[clov*ndirac+idirac] : sigin[clov*ndirac+idirac]+4;
635#pragma unroll
636 for(unsigned short c=0; c<nc; c++)
637 r_s[c]= r[i+kvolHalo*(sind*nc+c)];
639 phi_s[igorkov][0]+=sigval[clov*ndirac+idirac]*(creal(clov_s[0])*r_s[0]+clov_s[1]*r_s[1]);
640 //Clover is in the Lie Algebra, not Lie group. So signs are correct here.
641 phi_s[igorkov][1]+=sigval[clov*ndirac+idirac]*(conj(clov_s[1])*r_s[0]-creal(clov_s[0])*r_s[1]);
642 }
643 }
644#pragma unroll
645 for(unsigned short igorkov=0; igorkov<ngorkov; igorkov++)
646 for(unsigned short c=0; c<nc; c++){
647 //dag is just to do with the output layout and if it has a halo
648 if(dag)
649 phi[i+kvol*(nc*igorkov+c)]+=akappa*phi_s[igorkov][c];
650 else
651 phi[i+kvolHalo*(nc*igorkov+c)]+=akappa*phi_s[igorkov][c];
652 }
653 }
654 return;
655 }
656
671 template <typename T>
672 __global__ __launch_bounds__(__BSIZE__) void HbyClover(complex<T> * phi,const complex<T> * __restrict__ r,
673 const complex<T> * __restrict__ clover1,const complex<T> * __restrict__ clover2,
674 const __grid_constant__ complex<T> sigval[24], const __grid_constant__ float akappa,
675 const __grid_constant__ unsigned short sigin[24],const __grid_constant__ bool dag){
676 const unsigned int gsize = gridDim.x*gridDim.y*gridDim.z;
677 const unsigned int bsize = blockDim.x*blockDim.y*blockDim.z;
678 const unsigned int blockId = blockIdx.x+ blockIdx.y * gridDim.x+ gridDim.x * gridDim.y * blockIdx.z;
679 const unsigned int bthreadId= (threadIdx.z * blockDim.y+ threadIdx.y)* blockDim.x+ threadIdx.x;
680 const unsigned int gthreadId= blockId * bsize+bthreadId;
681
682 for(unsigned int i=gthreadId;i<kvol;i+=bsize*gsize){
683 //Prefetched r and Phi array
684 complex<T> phi_s[ndirac*nc];
685#pragma unroll
686 for(unsigned short idirac=0; idirac<ndirac*nc; idirac+=nc)
687#pragma unroll
688 for(unsigned short c=0; c<nc; c++){
689 phi_s[idirac+c]=0;
690 }
691#pragma unroll
692 for(unsigned short clov=0;clov<6;clov++){
693 const T clov_a=creal(clover1[clov*kvol+i]);
694 const complex<T> clov_b=clover2[clov*kvol+i];
695 // clov_s[0]=; clov_s[1]=k$;
696#pragma unroll
697 for(unsigned short idirac=0; idirac<ndirac*nc; idirac+=nc){
698 const complex<T> sig=sigval[clov*ndirac+(idirac>>1)];
699 const unsigned int sind = i+kvolHalo*(sigin[clov*ndirac+(idirac>>1)] << (nc-1));
700 const complex<T> r_s[nc]={r[sind],r[sind+kvolHalo]};
702 phi_s[idirac+0]+=sig*(clov_a*r_s[0]+clov_b*r_s[1]);
703 //Clover is in the Lie Algebra, not Lie group. So signs are correct here.
704 phi_s[idirac+1]+=sig*(conj(clov_b)*r_s[0]-clov_a*r_s[1]);
705 }
706 }
707#pragma unroll
708 for(unsigned short idirac=0; idirac<ndirac*nc; idirac++)
709 //dag is just to do with the output layout and if it has a halo
710 if(dag)
711 phi[i+kvol*(idirac)]+=akappa*phi_s[idirac];
712 else
713 phi[i+kvolHalo*(idirac)]+=akappa*phi_s[idirac];
714 }
715 return;
716 }
717}
718using namespace Kernels;
719
720//Calling Wrappers
721//This gets called by C so cannot be templated...
722int cuClover(Complex_f *clover[nc],Complex_f *ut[nc], unsigned int *iu, unsigned int *id){
723 const char funcname[]="cuClover";
724#ifdef _DEBUG
725 cudaMallocManaged((void **)&clover[0],6*kvol*sizeof(Complex_f),cudaMemAttachGlobal);
726 cudaMallocManaged((void **)&clover[1],6*kvol*sizeof(Complex_f),cudaMemAttachGlobal);
727#else
728 cudaMallocAsync((void **)&clover[0],6*kvol*sizeof(Complex_f),streams[0]);
729 cudaMallocAsync((void **)&clover[1],6*kvol*sizeof(Complex_f),streams[1]);
730#endif
731 for(unsigned short mu=0;mu<ndim-1;mu++)
732 for(unsigned short nu=mu+1;nu<ndim;nu++)
733 if(mu!=nu){
734 //Clover index
735 unsigned short clov = (mu==0) ? nu-1 :mu+nu;
736 //Allocate clover memory
737 //Note that the clover is completely local, so doesn't need a halo for MPI
738 Full_Clover<<<dimGrid,dimBlock,0,streams[clov]>>>(clover[0]+clov*kvol,clover[1]+clov*kvol,\
739 ut[0],ut[1],iu,id,mu,nu);
740 }
742 return 0;
743}
744void cuByClover(Complex *phi, Complex *r, Complex *clover[nc],Complex *sigval,const float akappa, unsigned short *sigin, bool dag){
745 Kernels::ByClover<<<dimGrid,dimBlock>>>(phi,r,clover[0],clover[1],sigval,akappa,sigin,dag);
746}
747void cuHbyClover(Complex *phi, Complex *r, Complex *clover[nc],Complex *sigval, const float akappa, unsigned short *sigin, bool dag){
748 Kernels::HbyClover<<<dimGrid,dimBlock>>>(phi,r,clover[0],clover[1],sigval,akappa,sigin,dag);
749}
750void cuByClover_f(Complex_f *phi, Complex_f *r, Complex_f *clover[nc],Complex_f *sigval, const float akappa, unsigned short *sigin,bool dag){
751 Kernels::ByClover<<<dimGrid,dimBlock>>>(phi,r,clover[0],clover[1],sigval,akappa,sigin,dag);
752}
753void cuHbyClover_f(Complex_f *phi, Complex_f *r, Complex_f *clover[nc],Complex_f *sigval, const float akappa, unsigned short *sigin,bool dag){
754 Kernels::HbyClover<<<dimGrid,dimBlock>>>(phi,r,clover[0],clover[1],sigval,akappa,sigin,dag);
755}
756
757void cuCalcXmunu(Bilinear_a Xmunu, Complex_f *X1, Complex_f *X2, const Complex_f *sigval, const unsigned short *sigin,\
758 const unsigned short mu, const unsigned short nu){
759 //Get sign and index of @f$\sigma_{\mu\nu}@f correct
760 unsigned short clov = (mu==0) ? nu-1 : mu+nu;
761 Kernels::cuCalcXmunu<<<dimGrid,dimBlock,0,streams[clov]>>>(Xmunu,X1,X2,sigval,sigin,clov);
762 return;
763}
764int cuClov_Force(double *dSdpi, Complex_f *ut[nc], Complex_f *X1, Complex_f *X2, const Complex_f *sigval,\
765 const unsigned short *sigin, const unsigned int *iu, const unsigned int *id, const float akappa){
766 const char funcname[]="Clov_Force";
767
768 const unsigned short nclov = (ndim-1)*(ndim-2);
769 //Too many pointers here but not bothered doing it correctly. Overhead is basically zero.
770 Bilinear_a Xmn[nclov];
771 //Allocate half-leaf memory. We will have one stream for each direction
772 for(unsigned short mu=0;mu<ndim-1;mu++)
773 for(unsigned short nu=mu;nu<ndim;nu++)
774 if(mu!=nu){
775 //Get sign and index of @f$\sigma_{\mu\nu}@f correct
776 unsigned short clov = (mu==0) ? nu-1 : mu+nu;
777 //Allocate and evaluate @f$X_{\mu\nu}@f$ terms
778 cudaMallocAsync((void **)&Xmn[clov].diag,nc*kvol*sizeof(float),streams[clov]);
779 cudaMallocAsync((void **)&Xmn[clov].offd,nc*kvol*sizeof(complex<float>),streams[clov]);
780 Kernels::cuCalcXmunu<<<dimGrid,dimBlock,0,streams[clov]>>>(Xmn[clov],X1,X2,sigval,sigin,clov);
781 }
783
784 for(unsigned short mu=0;mu<ndim;mu++)
785 for(unsigned short nu=0;nu<ndim;nu++)
786 if(mu!=nu){
787 unsigned short clov=0;
788 if(mu<nu)
789 clov = (mu==0) ? nu-1 : mu+nu;
790 else
791 clov = (nu==0) ? mu-1 : mu+nu;
792 //Compute force for @f$\mu\nu@f$ and @f$\nu\mu@f$
793 Kernels::Clov_Force<<<dimGrid,dimBlock,0,streams[mu]>>>(dSdpi,ut[0],ut[1],Xmn[clov],iu,id,akappa,mu,nu);
794
795 }
797 for(unsigned short clov=0;clov<nclov;clov++){
798 cudaFreeAsync(Xmn[clov].diag,NULL); cudaFreeAsync(Xmn[clov].offd,NULL);
799 }
801 return 0;
802}
Routines needed for Clover improved wilson fermions.
__device__ void GetBilinear(Complex_f Z[nc *nc], Bilinear_a Xmn, unsigned int ind)
Loads the compacted bilinear form into a complex valued matrix.
Definition cuclover.cu:275
__device__ void cuByGenLeft(T a[nc], const unsigned short gen)
Multiply leaf (or part of one) by generator from left.
Definition cuclover.cu:24
void cuCalcXmunu(Bilinear_a Xmunu, Complex_f *X1, Complex_f *X2, const Complex_f *sigval, const unsigned short *sigin, const unsigned short mu, const unsigned short nu)
CUDA wrapper for CalcXmunu. Only called during testing to be honest.
Definition cuclover.cu:757
__global__ void Clov_Force(double *dSdpi, const complex< T > *u11t, const complex< T > *u12t, const Bilinear_a Xmn, const unsigned int *iu, const unsigned int *id, const __grid_constant__ float akappa, const __grid_constant__ unsigned short mu, const __grid_constant__ unsigned short nu)
cuGets the clover contribution to the force
Definition cuclover.cu:444
__device__ void cuByGenRight(T a[nc], const unsigned short gen)
Multiply leaf (or part of one) by generator from right.
Definition cuclover.cu:56
__device__ void cuGSandwich(T out[4], T tmp[4], const T Gl[2], const T X[4], const T Gr[2])
Multiplies by a gauge field from the left and the right.
Definition cuclover.cu:260
int cuClov_Force(double *dSdpi, Complex_f *ut[nc], Complex_f *X1, Complex_f *X2, const Complex_f *sigval, const unsigned short *sigin, const unsigned int *iu, const unsigned int *id, const float akappa)
CUDA wrapper for Clover_Force.
Definition cuclover.cu:764
__global__ void cuCalcXmunu(Bilinear_a Xmunu, const complex< T > *X1, const complex< T > *X2, const __grid_constant__ complex< T > sigval_G[24], const __grid_constant__ unsigned short sigin_G[24], const __grid_constant__ unsigned short clov)
Gets for the clover force.
Definition cuclover.cu:377
__device__ void cuGLeft(T out[4], const T G[2], const T X[4])
Multiplies by a gauge field from the left.
Definition cuclover.cu:227
__device__ void cuGRight(T out[4], const T G[2], const T X[4])
Multiplies by a gauge field from the right.
Definition cuclover.cu:243
void cuHbyClover(Complex *phi, Complex *r, Complex *clover[nc], Complex *sigval, const float akappa, unsigned short *sigin, bool dag)
CUDA wrapper for HbyClover.
Definition cuclover.cu:747
void cuByClover_f(Complex_f *phi, Complex_f *r, Complex_f *clover[nc], Complex_f *sigval, const float akappa, unsigned short *sigin, bool dag)
CUDA wrapper for ByClover_f.
Definition cuclover.cu:750
void cuHbyClover_f(Complex_f *phi, Complex_f *r, Complex_f *clover[nc], Complex_f *sigval, const float akappa, unsigned short *sigin, bool dag)
CUDA wrapper for HbyClover_f.
Definition cuclover.cu:753
void cuByClover(Complex *phi, Complex *r, Complex *clover[nc], Complex *sigval, const float akappa, unsigned short *sigin, bool dag)
CUDA wrapper for ByClover.
Definition cuclover.cu:744
__global__ void HbyClover(complex< T > *phi, const complex< T > *r, const complex< T > *clover1, const complex< T > *clover2, const __grid_constant__ complex< T > sigval[24], const __grid_constant__ float akappa, const __grid_constant__ unsigned short sigin[24], const __grid_constant__ bool dag)
Clover analogue of the Hdslash operation. The H in front is for half, as we only act on the fermions ...
Definition cuclover.cu:672
__global__ void ByClover(complex< T > *phi, const complex< T > *r, const complex< T > *clover1, const complex< T > *clover2, const __grid_constant__ complex< T > sigval_G[24], const __grid_constant__ float akappa, const __grid_constant__ unsigned short sigin_G[24], const __grid_constant__ bool dag)
Clover analogue of the Dslash operation. This version acts on all flavours similar to Dslash and Dsla...
Definition cuclover.cu:600
int cuClover(Complex_f *clover[nc], Complex_f *ut[nc], unsigned int *iu, unsigned int *id)
CUDA wrapper for calculating the clovers in all directions at all sites .
Definition cuclover.cu:722
__global__ void Half_Leaves(complex< T > *hLeaves0, complex< T > *hLeaves1, const complex< T > *u11t, const complex< T > *u12t, const unsigned int *iu, const unsigned int *id, const __grid_constant__ unsigned short mu, const __grid_constant__ unsigned short nu)
Calculates the products of the first two links in a plaquette.
Definition cuclover.cu:297
__global__ void Full_Clover(complex< T > *clover1, complex< T > *clover2, const complex< T > *u11t, const complex< T > *u12t, const unsigned int *iu, const unsigned int *id, const __grid_constant__ int mu, const __grid_constant__ int nu)
Calculates the clovers in all directions at all sites .
Definition cuclover.cu:329
__device__ void Half_Leaf(complex< T > Leaves[nc], const complex< T > *u11t, const complex< T > *u12t, complex< T > a[nc], const unsigned int *iu, const unsigned int *id, const unsigned int i, const unsigned short mu, const unsigned short nu, const unsigned short leaf)
Calculates the first half of the leaf for a clover term. We split it so that the force term can reuse...
Definition cuclover.cu:91
__device__ void Leaf(const complex< T > *u11t, const complex< T > *u12t, complex< T > Leaves[nc], const unsigned int *iu, const unsigned int *id, const unsigned int i, const unsigned short mu, const unsigned short nu, const unsigned short leaf)
Calculates a leaf for a clover term.
Definition cuclover.cu:155
__device__ __forceinline__ T conj(const T &z)
Complex Conjugation.
Definition cusu2hmc.cu:33
CUDA Device code.
Definition cubosonic.cu:13
CUDA Kernels.
Definition cubosonic.cu:48
#define nc
Colours.
Definition sizes.h:186
#define ngorkov
Gor'kov indices.
Definition sizes.h:194
#define nadj
adjacent spatial indices
Definition sizes.h:188
#define kvol
Sublattice volume.
Definition sizes.h:167
#define Complex
Double precision complex number.
Definition sizes.h:68
#define __BSIZE__
Default block size.
Definition sizes.h:48
#define ndirac
Dirac indices.
Definition sizes.h:190
#define cudaDeviceSynchronise()
Get rid of that bastardised yankee English.
Definition sizes.h:57
#define Complex_f
Single precision complex number.
Definition sizes.h:66
#define ndim
Dimensions.
Definition sizes.h:192
#define kvolHalo
Subvolume + halo size.
Definition sizes.h:238
Structure of arrays for Hermitian bilinear in memory.
Definition clover.h:30
Complex_f * offd
Complex valued off-diagonal terms. We only need to store one of these to get the other in .
Definition clover.h:34
float * diag
Real valued diagonal terms.
Definition clover.h:32
Hermitian bilinear on the local stack.
Definition clover.h:40
float diag[2]
Real valued diagonal terms.
Definition clover.h:42
Complex_f offd
Complex valued off-diagonal terms. We only need to store one of these to get the other in .
Definition clover.h:44
cudaStream_t streams[ndirac *ndim *nadj]
An array of concurrent GPU streams to keep it busy.
Definition cusu2hmc.cu:29
#define creal(z)
Extract Real Component using C standard notation.
#define I_f
Define I in single precision.