-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCudaPMERecip.cu
2601 lines (2229 loc) · 76.6 KB
/
CudaPMERecip.cu
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 <typeinfo>
#include <iostream>
#include <cassert>
#include <math.h>
#include "gpu_utils.h"
#include "cuda_utils.h"
#include "reduce.h"
#include "CudaPMERecip.h"
#ifdef USE_FBFFT
#include "fbfft/FBFFT.cuh"
#include "fbfft/FBFFTCommon.cuh"
#define fbfftCheck(stmt) do { \
facebook::cuda::fbfft::FBFFTParameters::ErrorCode err = stmt; \
if (err != facebook::cuda::fbfft::FBFFTParameters::Success) { \
printf("Error running %s in file %s, function %s\n", #stmt,__FILE__,__FUNCTION__); \
if (err == facebook::cuda::fbfft::FBFFTParameters::UnsupportedSize) \
printf("Error code: UnsupportedSize\n"); \
if (err == facebook::cuda::fbfft::FBFFTParameters::UnsupportedDimension) \
printf("Error code: UnsupportedDimension\n"); \
exit(1); \
} \
} while(0)
#endif
static const double pi = 3.14159265358979323846;
//
// CudaPMERecip class
//
// AT = Accumulation Type
// CT = Calculation Type (real)
// CT2 = Calculation Type (complex)
//
// (c) Antti-Pekka Hynninen, 2013, [email protected]
//
// In real space:
// Each instance of CudaPMERecip is responsible for grid region (x0..x1) x (y0..y1) x (z0..z1)
// Note that usually x0=0, x1=nfftx-1
//
// The generic version can only be used for float at the moment
template <typename T>
__forceinline__ __device__ void write_grid(const float val, const int ind,
T* data) {
atomicAdd(&data[ind], (T)val);
}
// Template specialization for 64bit integer = "long long int"
template <>
__forceinline__ __device__ void write_grid <long long int> (const float val,
const int ind,
long long int* data) {
unsigned long long int qintp = llitoulli(lliroundf(FORCE_SCALE*val));
atomicAdd((unsigned long long int *)&data[ind], qintp);
}
// Template specialization for 32bit integer = "int"
template <>
__forceinline__ __device__ void write_grid <int> (const float val,
const int ind,
int* data) {
unsigned int qintp = itoui(iroundf(FORCE_SCALE_I*val));
atomicAdd((unsigned int *)&data[ind], qintp);
}
/*
//
// Temporary kernels that change the data layout
//
__global__ void change_gridp(const int ncoord, const gridp_t *gridp,
int *ixtbl, int *iytbl, int *iztbl, float *charge) {
unsigned int pos = blockIdx.x*blockDim.x + threadIdx.x;
if (pos < ncoord) {
gridp_t gridpval = gridp[pos];
int x = gridpval.x;
int y = gridpval.y;
int z = gridpval.z;
float q = gridpval.q;
ixtbl[pos] = x;
iytbl[pos] = y;
iztbl[pos] = z;
charge[pos] = q;
}
}
*/
/*
__global__ void change_theta(const int ncoord, const float3 *theta,
float4 *thetax, float4 *thetay, float4 *thetaz) {
unsigned int pos = blockIdx.x*blockDim.x + threadIdx.x;
if (pos < ncoord) {
thetax[pos].x = theta[pos*4].x;
thetax[pos].y = theta[pos*4+1].x;
thetax[pos].z = theta[pos*4+2].x;
thetax[pos].w = theta[pos*4+3].x;
thetay[pos].x = theta[pos*4].y;
thetay[pos].y = theta[pos*4+1].y;
thetay[pos].z = theta[pos*4+2].y;
thetay[pos].w = theta[pos*4+3].y;
thetaz[pos].x = theta[pos*4].z;
thetaz[pos].y = theta[pos*4+1].z;
thetaz[pos].z = theta[pos*4+2].z;
thetaz[pos].w = theta[pos*4+3].z;
}
}
*/
//
// Calculate theta and dtheta for general order bspline
//
template <typename T, typename T3, int order>
__forceinline__ __device__ void calc_theta_dtheta(T wx, T wy, T wz, T3 *theta, T3 *dtheta) {
theta[order-1].x = ((T)0);
theta[order-1].y = ((T)0);
theta[order-1].z = ((T)0);
theta[1].x = wx;
theta[1].y = wy;
theta[1].z = wz;
theta[0].x = ((T)1) - wx;
theta[0].y = ((T)1) - wy;
theta[0].z = ((T)1) - wz;
#pragma unroll
for (int k=3;k <= order-1;k++) {
T div = ((T)1) / (T)(k-1);
theta[k-1].x = div*wx*theta[k-2].x;
theta[k-1].y = div*wy*theta[k-2].y;
theta[k-1].z = div*wz*theta[k-2].z;
#pragma unroll
for (int j=1;j <= k-2;j++) {
theta[k-j-1].x = div*((wx + j)*theta[k-j-2].x + (k-j-wx)*theta[k-j-1].x);
theta[k-j-1].y = div*((wy + j)*theta[k-j-2].y + (k-j-wy)*theta[k-j-1].y);
theta[k-j-1].z = div*((wz + j)*theta[k-j-2].z + (k-j-wz)*theta[k-j-1].z);
}
theta[0].x = div*(((T)1) - wx)*theta[0].x;
theta[0].y = div*(((T)1) - wy)*theta[0].y;
theta[0].z = div*(((T)1) - wz)*theta[0].z;
}
//--- perform standard b-spline differentiation
dtheta[0].x = -theta[0].x;
dtheta[0].y = -theta[0].y;
dtheta[0].z = -theta[0].z;
#pragma unroll
for (int j=2;j <= order;j++) {
dtheta[j-1].x = theta[j-2].x - theta[j-1].x;
dtheta[j-1].y = theta[j-2].y - theta[j-1].y;
dtheta[j-1].z = theta[j-2].z - theta[j-1].z;
}
//--- one more recursion
T div = ((T)1) / (T)(order-1);
theta[order-1].x = div*wx*theta[order-2].x;
theta[order-1].y = div*wy*theta[order-2].y;
theta[order-1].z = div*wz*theta[order-2].z;
#pragma unroll
for (int j=1;j <= order-2;j++) {
theta[order-j-1].x = div*((wx + j)*theta[order-j-2].x + (order-j-wx)*theta[order-j-1].x);
theta[order-j-1].y = div*((wy + j)*theta[order-j-2].y + (order-j-wy)*theta[order-j-1].y);
theta[order-j-1].z = div*((wz + j)*theta[order-j-2].z + (order-j-wz)*theta[order-j-1].z);
}
theta[0].x = div*(((T)1) - wx)*theta[0].x;
theta[0].y = div*(((T)1) - wy)*theta[0].y;
theta[0].z = div*(((T)1) - wz)*theta[0].z;
}
//
// General version for any order
//
template <typename T, int order>
__forceinline__ __device__ void calc_one_theta(const T w, T *theta) {
theta[order-1] = ((T)0);
theta[1] = w;
theta[0] = ((T)1) - w;
#pragma unroll
for (int k=3;k <= order-1;k++) {
T div = ((T)1) / (T)(k-1);
theta[k-1] = div*w*theta[k-2];
#pragma unroll
for (int j=1;j <= k-2;j++) {
theta[k-j-1] = div*((w+j)*theta[k-j-2] + (k-j-w)*theta[k-j-1]);
}
theta[0] = div*(((T)1) - w)*theta[0];
}
//--- one more recursion
T div = ((T)1) / (T)(order-1);
theta[order-1] = div*w*theta[order-2];
#pragma unroll
for (int j=1;j <= order-2;j++) {
theta[order-j-1] = div*((w+j)*theta[order-j-2] + (order-j-w)*theta[order-j-1]);
}
theta[0] = div*(((T)1) - w)*theta[0];
}
#ifdef USE_NEW_SPREAD
__global__ void
place_charges_ortho(const float4 *xyzq, const int ncoord,
const float recip11, const float recip22, const float recip33,
const int nfftx, const int nffty, const int nfftz,
const int xsize, const int ysize,
int* coordIndGrid, int* coordIndOverflow, unsigned int* numCoordIndOverflow) {
const int i = threadIdx.x + blockIdx.x*blockDim.x;
if (i < ncoord) {
float4 xyzqi = xyzq[i];
float x = xyzqi.x;
float y = xyzqi.y;
float z = xyzqi.z;
float q = xyzqi.w;
float w;
w = x*recip11 + 2.0f;
float frx = (float)(nfftx*(w - (floorf(w + 0.5f) - 0.5f)));
w = y*recip22 + 2.0f;
float fry = (float)(nffty*(w - (floorf(w + 0.5f) - 0.5f)));
w = z*recip33 + 2.0f;
float frz = (float)(nfftz*(w - (floorf(w + 0.5f) - 0.5f)));
int frxi = (int)frx;
int fryi = (int)fry;
int frzi = (int)frz;
// Get position on the grid
int ind = frxi + xsize*(fryi + ysize*frzi);
// Store i+1 if old value is 0
int old = atomicCAS(&coordIndGrid[ind], 0, i+1);
// If already occupied, store into overflow
if (old != 0) {
int p = atomicInc(numCoordIndOverflow, ncoord+1);
coordIndOverflow[p] = i;
}
}
}
//
// threadIdx.x = x
// blockIdx.y = y
// blockIdx.z = z
//
template <typename AT, int order>
__global__ void
spread_charge_ortho(const float4* __restrict__ xyzq, const int ncoord,
const float recip11, const float recip22, const float recip33,
const int nfftx, const int nffty, const int nfftz,
const int xsize, const int ysize,
const int* coordIndGrid, AT* data) {
__shared__ float sh_thetax[64*order];
__shared__ float sh_thetay[64*order];
__shared__ float sh_thetaz[64*order];
// data element this thread is writing
AT dataval = (AT)0;
// Loop over y-z plane and compute bsplines
for (int tz=0;tz < order;tz++) {
int iz = blockIdx.z - tz;
if (iz < 0) iz += nfftz;
for (int ty=0;ty < order;ty++) {
int iy = blockIdx.y - ty;
if (iy < 0) iy += nffty;
int ind = threadIdx.x + xsize*(iy + ysize*iz);
int pos = (threadIdx.x < nfftx) ? coordIndGrid[ind] : 0;
__syncthreads();
if (pos == 0) {
#pragma unroll
for (int i=0;i < order;i++) {
sh_thetax[threadIdx.x*order + i] = 0.0f;
sh_thetay[threadIdx.x*order + i] = 0.0f;
sh_thetaz[threadIdx.x*order + i] = 0.0f;
}
} else {
float4 xyzqi = xyzq[pos-1];
float x = xyzqi.x;
float y = xyzqi.y;
float z = xyzqi.z;
float q = xyzqi.w;
float w;
w = x*recip11 + 2.0f;
float frx = (float)(nfftx*(w - (floorf(w + 0.5f) - 0.5f)));
w = y*recip22 + 2.0f;
float fry = (float)(nffty*(w - (floorf(w + 0.5f) - 0.5f)));
w = z*recip33 + 2.0f;
float frz = (float)(nfftz*(w - (floorf(w + 0.5f) - 0.5f)));
int frxi = (int)frx;
int fryi = (int)fry;
int frzi = (int)frz;
int tmp = frxi + xsize*(fryi + ysize*frzi);
float wx = frx - (float)frxi;
float wy = fry - (float)fryi;
float wz = frz - (float)frzi;
float theta[order];
calc_one_theta<float, order>(wx, theta);
#pragma unroll
for (int i=0;i < order;i++) sh_thetax[threadIdx.x*order + i] = q*theta[i];
calc_one_theta<float, order>(wy, theta);
#pragma unroll
for (int i=0;i < order;i++) sh_thetay[threadIdx.x*order + i] = theta[i];
calc_one_theta<float, order>(wz, theta);
#pragma unroll
for (int i=0;i < order;i++) sh_thetaz[threadIdx.x*order + i] = theta[i];
}
__syncthreads();
// Loop over x line
if (threadIdx.x < nfftx) {
for (int tx=0;tx < order;tx++) {
int ix = threadIdx.x - tx;
if (ix < 0) ix += nfftx;
dataval += roundCTtoAT<AT, float>(sh_thetax[ix*order + tx]*sh_thetay[ix*order + ty]*sh_thetaz[ix*order + tz]);
}
}
}
}
// Write data to global memory
if (threadIdx.x < nfftx)
data[threadIdx.x + xsize*(blockIdx.y + ysize*blockIdx.z)] = dataval;
}
template <typename AT, int order>
__global__ void
spread_overflow_charge_ortho(const float4* __restrict__ xyzq, const int ncoord,
const float recip11, const float recip22, const float recip33,
const int nfftx, const int nffty, const int nfftz,
const int xsize, const int ysize,
const int* coordIndOverflow, const unsigned int* numCoordIndOverflow,
AT* data) {
// Shared memory use:
// order = 4: 1920 bytes
// order = 6: 2688 bytes
// order = 8: 3456 bytes
__shared__ int sh_ix[32];
__shared__ int sh_iy[32];
__shared__ int sh_iz[32];
__shared__ float sh_thetax[order*32];
__shared__ float sh_thetay[order*32];
__shared__ float sh_thetaz[order*32];
const int ncoordOverflow = *numCoordIndOverflow;
// pos0 = beginning of position for this block
int pos0 = blockIdx.x*blockDim.x;
while (pos0 < ncoordOverflow) {
// Process atoms pos to pos_end-1
int pos = pos0 + threadIdx.x;
int pos_end = min(pos0 + blockDim.x, ncoordOverflow);
__syncthreads();
if (pos < pos_end && threadIdx.y == 0) {
int i = coordIndOverflow[pos];
float4 xyzqi = xyzq[i];
float x = xyzqi.x;
float y = xyzqi.y;
float z = xyzqi.z;
float q = xyzqi.w;
float w;
w = x*recip11 + 2.0f;
float frx = (float)(nfftx*(w - (floorf(w + 0.5f) - 0.5f)));
w = y*recip22 + 2.0f;
float fry = (float)(nffty*(w - (floorf(w + 0.5f) - 0.5f)));
w = z*recip33 + 2.0f;
float frz = (float)(nfftz*(w - (floorf(w + 0.5f) - 0.5f)));
int frxi = (int)frx;
int fryi = (int)fry;
int frzi = (int)frz;
sh_ix[threadIdx.x] = frxi;
sh_iy[threadIdx.x] = fryi;
sh_iz[threadIdx.x] = frzi;
float wx = frx - (float)frxi;
float wy = fry - (float)fryi;
float wz = frz - (float)frzi;
float theta[order];
calc_one_theta<float, order>(wx, theta);
#pragma unroll
for (int i=0;i < order;i++) sh_thetax[threadIdx.x*order + i] = q*theta[i];
calc_one_theta<float, order>(wy, theta);
#pragma unroll
for (int i=0;i < order;i++) sh_thetay[threadIdx.x*order + i] = theta[i];
calc_one_theta<float, order>(wz, theta);
#pragma unroll
for (int i=0;i < order;i++) sh_thetaz[threadIdx.x*order + i] = theta[i];
}
__syncthreads();
// Grid point location, values of (ix0, iy0, iz0) are in range 0..order-1
// NOTE: Only tid=0...order*order*order-1 do any computation
const int order3 = ((order*order*order-1)/warpSize + 1)*warpSize;
const int tid = (threadIdx.x + threadIdx.y*blockDim.x) % order3; // 0...order3-1
const int x0 = tid % order;
const int y0 = (tid / order) % order;
const int z0 = tid / (order*order);
// Loop over atoms pos..pos_end-1
int iadd = blockDim.x*blockDim.y/order3;
int i = (threadIdx.x + threadIdx.y*blockDim.x)/order3;
int iend = pos_end - pos0;
for (;i < iend;i += iadd) {
int x = sh_ix[i] + x0;
int y = sh_iy[i] + y0;
int z = sh_iz[i] + z0;
if (x >= nfftx) x -= nfftx;
if (y >= nffty) y -= nffty;
if (z >= nfftz) z -= nfftz;
// Get position on the grid
int ind = x + xsize*(y + ysize*(z));
// Here we unroll the 6x6x6 loop with 216 threads.
// NOTE: We use 7*32=224 threads to do this
// Calculate interpolated charge value and store it to global memory
if (tid < order*order*order)
write_grid<AT>(sh_thetax[i*order+x0]*sh_thetay[i*order+y0]*sh_thetaz[i*order+z0], ind, data);
}
pos0 += blockDim.x*gridDim.x;
}
}
#endif // USE_NEW_SPREAD
//
// Spreads the charge on the grid. Calculates theta and dtheta on the fly
// blockDim.x = Number of atoms each block loads
// blockDim.y*blockDim.x/order3 = Number of atoms we spread at once
//
template <typename AT, int order>
__global__ void
spread_charge_ortho(const float4 *xyzq, const int ncoord,
const float recip11, const float recip22, const float recip33,
const int nfftx, const int nffty, const int nfftz,
const int xsize, const int ysize,
AT* data) {
// Shared memory use:
// order = 4: 1920 bytes
// order = 6: 2688 bytes
// order = 8: 3456 bytes
__shared__ int sh_ix[32];
__shared__ int sh_iy[32];
__shared__ int sh_iz[32];
__shared__ float sh_thetax[order*32];
__shared__ float sh_thetay[order*32];
__shared__ float sh_thetaz[order*32];
// Process atoms pos to pos_end-1
const unsigned int pos = blockIdx.x*blockDim.x + threadIdx.x;
const unsigned int pos_end = min((blockIdx.x+1)*blockDim.x, ncoord);
if (pos < pos_end && threadIdx.y == 0) {
float4 xyzqi = xyzq[pos];
float x = xyzqi.x;
float y = xyzqi.y;
float z = xyzqi.z;
float q = xyzqi.w;
float w;
w = x*recip11 + 2.0f;
float frx = (float)(nfftx*(w - (floorf(w + 0.5f) - 0.5f)));
w = y*recip22 + 2.0f;
float fry = (float)(nffty*(w - (floorf(w + 0.5f) - 0.5f)));
w = z*recip33 + 2.0f;
float frz = (float)(nfftz*(w - (floorf(w + 0.5f) - 0.5f)));
int frxi = (int)frx;
int fryi = (int)fry;
int frzi = (int)frz;
sh_ix[threadIdx.x] = frxi;
sh_iy[threadIdx.x] = fryi;
sh_iz[threadIdx.x] = frzi;
float wx = frx - (float)frxi;
float wy = fry - (float)fryi;
float wz = frz - (float)frzi;
float theta[order];
calc_one_theta<float, order>(wx, theta);
#pragma unroll
for (int i=0;i < order;i++) sh_thetax[threadIdx.x*order + i] = q*theta[i];
calc_one_theta<float, order>(wy, theta);
#pragma unroll
for (int i=0;i < order;i++) sh_thetay[threadIdx.x*order + i] = theta[i];
calc_one_theta<float, order>(wz, theta);
#pragma unroll
for (int i=0;i < order;i++) sh_thetaz[threadIdx.x*order + i] = theta[i];
}
__syncthreads();
// Grid point location, values of (ix0, iy0, iz0) are in range 0..order-1
// NOTE: Only tid=0...order*order*order-1 do any computation
const int order3 = ((order*order*order-1)/warpSize + 1)*warpSize;
const int tid = (threadIdx.x + threadIdx.y*blockDim.x) % order3; // 0...order3-1
const int x0 = tid % order;
const int y0 = (tid / order) % order;
const int z0 = tid / (order*order);
// Loop over atoms pos..pos_end-1
int iadd = blockDim.x*blockDim.y/order3;
int i = (threadIdx.x + threadIdx.y*blockDim.x)/order3;
int iend = pos_end - blockIdx.x*blockDim.x;
for (;i < iend;i += iadd) {
int x = sh_ix[i] + x0;
int y = sh_iy[i] + y0;
int z = sh_iz[i] + z0;
if (x >= nfftx) x -= nfftx;
if (y >= nffty) y -= nffty;
if (z >= nfftz) z -= nfftz;
// Get position on the grid
int ind = x + xsize*(y + ysize*(z));
// Here we unroll the 6x6x6 loop with 216 threads.
// NOTE: We use 7*32=224 threads to do this
// Calculate interpolated charge value and store it to global memory
if (tid < order*order*order)
write_grid<AT>(sh_thetax[i*order+x0]*sh_thetay[i*order+y0]*sh_thetaz[i*order+z0], ind, data);
}
}
// Local structure for scalar_sum -function for energy and virial reductions
struct RecipVirial_t {
double energy;
double virial[6];
};
//
// Performs scalar sum on data(nfft1, nfft2, nfft3)
// T = float or double
// T2 = float2 or double2
//
template <typename T, typename T2, bool calc_energy_virial>
__global__ void scalar_sum_ortho_kernel(const int nfft1, const int nfft2, const int nfft3,
const int size1, const int size2, const int size3,
const int nf1, const int nf2, const int nf3,
const T recip11, const T recip22, const T recip33,
const T* prefac1, const T* prefac2, const T* prefac3,
const T fac, const T piv_inv,
const bool global_base, T2* data,
double* __restrict__ energy_recip,
Virial_t* __restrict__ virial) {
extern __shared__ T sh_prefac[];
// Create pointers to shared memory
T* sh_prefac1 = (T *)&sh_prefac[0];
T* sh_prefac2 = (T *)&sh_prefac[nfft1];
T* sh_prefac3 = (T *)&sh_prefac[nfft1 + nfft2];
// Calculate start position (k1, k2, k3) for each thread
unsigned int tid = blockIdx.x*blockDim.x + threadIdx.x;
int k3 = tid/(size1*size2);
tid -= k3*size1*size2;
int k2 = tid/size1;
int k1 = tid - k2*size1;
// Calculate increments (k1_inc, k2_inc, k3_inc)
int tot_inc = blockDim.x*gridDim.x;
int k3_inc = tot_inc/(size1*size2);
tot_inc -= k3_inc*size1*size2;
int k2_inc = tot_inc/size1;
int k1_inc = tot_inc - k2_inc*size1;
// Set data[0] = 0 for the global (0,0,0)
if (global_base && (blockIdx.x + threadIdx.x == 0)) {
T2 zero;
zero.x = (T)0;
zero.y = (T)0;
data[0] = zero;
// Increment position
k1 += k1_inc;
if (k1 >= size1) {
k1 -= size1;
k2++;
}
k2 += k2_inc;
if (k2 >= size2) {
k2 -= size2;
k3++;
}
k3 += k3_inc;
}
// Load prefac data into shared memory
int pos = threadIdx.x;
while (pos < nfft1) {
sh_prefac1[pos] = prefac1[pos];
pos += blockDim.x;
}
pos = threadIdx.x;
while (pos < nfft2) {
sh_prefac2[pos] = prefac2[pos];
pos += blockDim.x;
}
pos = threadIdx.x;
while (pos < nfft3) {
sh_prefac3[pos] = prefac3[pos];
pos += blockDim.x;
}
__syncthreads();
double energy = 0.0;
double virial0 = 0.0;
double virial1 = 0.0;
double virial2 = 0.0;
double virial3 = 0.0;
double virial4 = 0.0;
double virial5 = 0.0;
while (k3 < size3) {
int pos = k1 + (k2 + k3*size2)*size1;
T2 q = data[pos];
int m1 = k1;
int m2 = k2;
int m3 = k3;
if (k1 >= nf1) m1 -= nfft1;
if (k2 >= nf2) m2 -= nfft2;
if (k3 >= nf3) m3 -= nfft3;
T mhat1 = recip11*m1;
T mhat2 = recip22*m2;
T mhat3 = recip33*m3;
T msq = mhat1*mhat1 + mhat2*mhat2 + mhat3*mhat3;
T msq_inv = (T)1.0/msq;
// NOTE: check if it's faster to pre-calculate exp()
T eterm = exp(-fac*msq)*piv_inv*sh_prefac1[k1]*sh_prefac2[k2]*sh_prefac3[k3]*msq_inv;
if (calc_energy_virial) {
T tmp1 = eterm*(q.x*q.x + q.y*q.y);
T vterm = ((T)2)*(fac + msq_inv);
T tmp2 = tmp1*vterm;
energy += (double)tmp1;
virial0 += (double)(tmp1*(vterm*mhat1*mhat1 - ((T)1)));
virial1 += (double)(tmp2*mhat1*mhat2);
virial2 += (double)(tmp2*mhat1*mhat3);
virial3 += (double)(tmp1*(vterm*mhat2*mhat2 - ((T)1)));
virial4 += (double)(tmp2*mhat2*mhat3);
virial5 += (double)(tmp1*(vterm*mhat3*mhat3 - ((T)1)));
// The following is put into a separate if {} -block to avoid divergence within warp and
// save registers
if (k1 >= 1 && k1 < nfft1) {
int k1s = nfft1 - (k1+1) + 1;
int k2s = ((nfft2-(k2+1)+1) % nfft2);
int k3s = ((nfft3-(k3+1)+1) % nfft3);
int m1s = k1s;
int m2s = k2s;
int m3s = k3s;
if (k1s >= nf1) m1s -= nfft1;
if (k2s >= nf2) m2s -= nfft2;
if (k3s >= nf3) m3s -= nfft3;
T mhat1s = recip11*m1s;
T mhat2s = recip22*m2s;
T mhat3s = recip33*m3s;
T msqs = mhat1s*mhat1s + mhat2s*mhat2s + mhat3s*mhat3s;
T msqs_inv = ((T)1)/msqs;
T eterms = exp(-fac*msqs)*piv_inv*sh_prefac1[k1s]*sh_prefac2[k2s]*sh_prefac3[k3s]*msqs_inv;
T tmp1s = eterms*(q.x*q.x + q.y*q.y);
T vterms = ((T)2)*(fac + msqs_inv);
T tmp2s = tmp1s*vterms;
energy += (double)tmp1s;
virial0 += (double)(tmp1s*(vterms*mhat1s*mhat1s - ((T)1)));
virial1 += (double)(tmp2s*mhat1s*mhat2s);
virial2 += (double)(tmp2s*mhat1s*mhat3s);
virial3 += (double)(tmp1s*(vterms*mhat2s*mhat2s - ((T)1)));
virial4 += (double)(tmp2s*mhat2s*mhat3s);
virial5 += (double)(tmp1s*(vterms*mhat3s*mhat3s - ((T)1)));
}
}
q.x *= eterm;
q.y *= eterm;
data[pos] = q;
// Increment position
k1 += k1_inc;
if (k1 >= size1) {
k1 -= size1;
k2++;
}
k2 += k2_inc;
if (k2 >= size2) {
k2 -= size2;
k3++;
}
k3 += k3_inc;
}
// Reduce energy and virial
if (calc_energy_virial) {
#if __CUDA_ARCH__ < 300
// Requires blockDim.x*sizeof(RecipVirial_t) amount of shared memory
volatile RecipVirial_t* sh_ev = (RecipVirial_t *)sh_prefac;
// NOTE: this __syncthreads() is needed because we're using a single shared memory buffer
__syncthreads();
sh_ev[threadIdx.x].energy = energy;
sh_ev[threadIdx.x].virial[0] = virial0;
sh_ev[threadIdx.x].virial[1] = virial1;
sh_ev[threadIdx.x].virial[2] = virial2;
sh_ev[threadIdx.x].virial[3] = virial3;
sh_ev[threadIdx.x].virial[4] = virial4;
sh_ev[threadIdx.x].virial[5] = virial5;
__syncthreads();
#endif
#if __CUDA_ARCH__ < 300
for (int d=1;d < blockDim.x;d *= 2) {
int t = threadIdx.x + d;
double energy_val = (t < blockDim.x) ? sh_ev[t].energy : 0.0;
double virial0_val = (t < blockDim.x) ? sh_ev[t].virial[0] : 0.0;
double virial1_val = (t < blockDim.x) ? sh_ev[t].virial[1] : 0.0;
double virial2_val = (t < blockDim.x) ? sh_ev[t].virial[2] : 0.0;
double virial3_val = (t < blockDim.x) ? sh_ev[t].virial[3] : 0.0;
double virial4_val = (t < blockDim.x) ? sh_ev[t].virial[4] : 0.0;
double virial5_val = (t < blockDim.x) ? sh_ev[t].virial[5] : 0.0;
__syncthreads();
sh_ev[threadIdx.x].energy += energy_val;
sh_ev[threadIdx.x].virial[0] += virial0_val;
sh_ev[threadIdx.x].virial[1] += virial1_val;
sh_ev[threadIdx.x].virial[2] += virial2_val;
sh_ev[threadIdx.x].virial[3] += virial3_val;
sh_ev[threadIdx.x].virial[4] += virial4_val;
sh_ev[threadIdx.x].virial[5] += virial5_val;
__syncthreads();
}
#else
const int tid = threadIdx.x & (warpsize-1);
const int base = (threadIdx.x/warpsize);
volatile RecipVirial_t* sh_ev = (RecipVirial_t *)sh_prefac;
// Reduce within warps
for (int d=warpsize/2;d >= 1;d /= 2) {
energy += __hiloint2double(__shfl(__double2hiint(energy), tid+d),
__shfl(__double2loint(energy), tid+d));
virial0 += __hiloint2double(__shfl(__double2hiint(virial0), tid+d),
__shfl(__double2loint(virial0), tid+d));
virial1 += __hiloint2double(__shfl(__double2hiint(virial1), tid+d),
__shfl(__double2loint(virial1), tid+d));
virial2 += __hiloint2double(__shfl(__double2hiint(virial2), tid+d),
__shfl(__double2loint(virial2), tid+d));
virial3 += __hiloint2double(__shfl(__double2hiint(virial3), tid+d),
__shfl(__double2loint(virial3), tid+d));
virial4 += __hiloint2double(__shfl(__double2hiint(virial4), tid+d),
__shfl(__double2loint(virial4), tid+d));
virial5 += __hiloint2double(__shfl(__double2hiint(virial5), tid+d),
__shfl(__double2loint(virial5), tid+d));
}
// Reduce between warps
// NOTE: this __syncthreads() is needed because we're using a single shared memory buffer
__syncthreads();
if (tid == 0) {
sh_ev[base].energy = energy;
sh_ev[base].virial[0] = virial0;
sh_ev[base].virial[1] = virial1;
sh_ev[base].virial[2] = virial2;
sh_ev[base].virial[3] = virial3;
sh_ev[base].virial[4] = virial4;
sh_ev[base].virial[5] = virial5;
}
__syncthreads();
if (base == 0) {
energy = (tid < blockDim.x/warpsize) ? sh_ev[tid].energy : 0.0;
virial0 = (tid < blockDim.x/warpsize) ? sh_ev[tid].virial[0] : 0.0;
virial1 = (tid < blockDim.x/warpsize) ? sh_ev[tid].virial[1] : 0.0;
virial2 = (tid < blockDim.x/warpsize) ? sh_ev[tid].virial[2] : 0.0;
virial3 = (tid < blockDim.x/warpsize) ? sh_ev[tid].virial[3] : 0.0;
virial4 = (tid < blockDim.x/warpsize) ? sh_ev[tid].virial[4] : 0.0;
virial5 = (tid < blockDim.x/warpsize) ? sh_ev[tid].virial[5] : 0.0;
for (int d=warpsize/2;d >= 1;d /= 2) {
energy += __hiloint2double(__shfl(__double2hiint(energy), tid+d),
__shfl(__double2loint(energy), tid+d));
virial0 += __hiloint2double(__shfl(__double2hiint(virial0), tid+d),
__shfl(__double2loint(virial0), tid+d));
virial1 += __hiloint2double(__shfl(__double2hiint(virial1), tid+d),
__shfl(__double2loint(virial1), tid+d));
virial2 += __hiloint2double(__shfl(__double2hiint(virial2), tid+d),
__shfl(__double2loint(virial2), tid+d));
virial3 += __hiloint2double(__shfl(__double2hiint(virial3), tid+d),
__shfl(__double2loint(virial3), tid+d));
virial4 += __hiloint2double(__shfl(__double2hiint(virial4), tid+d),
__shfl(__double2loint(virial4), tid+d));
virial5 += __hiloint2double(__shfl(__double2hiint(virial5), tid+d),
__shfl(__double2loint(virial5), tid+d));
}
}
#endif
if (threadIdx.x == 0) {
#if __CUDA_ARCH__ < 300
energy = sh_ev[0].energy;
virial0 = sh_ev[0].virial[0];
virial1 = sh_ev[0].virial[1];
virial2 = sh_ev[0].virial[2];
virial3 = sh_ev[0].virial[3];
virial4 = sh_ev[0].virial[4];
virial5 = sh_ev[0].virial[5];
#endif
atomicAdd(energy_recip, energy*half_ccelec);
virial0 *= -half_ccelec;
virial1 *= -half_ccelec;
virial2 *= -half_ccelec;
virial3 *= -half_ccelec;
virial4 *= -half_ccelec;
virial5 *= -half_ccelec;
atomicAdd(&virial->virmat[0], virial0);
atomicAdd(&virial->virmat[1], virial1);
atomicAdd(&virial->virmat[2], virial2);
atomicAdd(&virial->virmat[3], virial1);
atomicAdd(&virial->virmat[4], virial3);
atomicAdd(&virial->virmat[5], virial4);
atomicAdd(&virial->virmat[6], virial2);
atomicAdd(&virial->virmat[7], virial4);
atomicAdd(&virial->virmat[8], virial5);
}
}
/*
// Set data[0] = 0 for the global (0,0,0)
if (global_base && (blockIdx.x + threadIdx.x == 0)) {
T2 zero;
zero.x = (T)0;
zero.y = (T)0;
data[0] = zero;
}
*/
}
#ifndef USE_TEXTURE_OBJECTS
texture<float, 1, cudaReadModeElementType> gridTexRef;
#endif
// Per atom data structure for the gather_force -kernels
template <typename T, int order>
struct gather_t {
int ix;
int iy;
int iz;
T charge;
T thetax[order];
T thetay[order];
T thetaz[order];
T dthetax[order];
T dthetay[order];
T dthetaz[order];
float f1;
float f2;
float f3;
};
template <typename T>
__forceinline__ __device__ void write_force_atomic(const float fx,
const float fy,
const float fz,
const int ind,
const int stride,
const int stride2,
T* force) {
// The generic version can not be used for anything
}
template <typename T>
__forceinline__ __device__ void write_force(const float fx,
const float fy,
const float fz,
const int ind,
const int stride,
const int stride2,
T* force) {
// The generic version can not be used for anything
}
// Template specialization for 64bit integer = "long long int"
template <>
__forceinline__ __device__ void write_force_atomic <long long int> (const float fx,
const float fy,
const float fz,
const int ind,
const int stride,
const int stride2,
long long int* force) {
unsigned long long int fx_ulli = llitoulli(lliroundf(FORCE_SCALE*fx));
unsigned long long int fy_ulli = llitoulli(lliroundf(FORCE_SCALE*fy));
unsigned long long int fz_ulli = llitoulli(lliroundf(FORCE_SCALE*fz));
atomicAdd((unsigned long long int *)&force[ind ], fx_ulli);
atomicAdd((unsigned long long int *)&force[ind + stride ], fy_ulli);
atomicAdd((unsigned long long int *)&force[ind + stride2], fz_ulli);
}
// Template specialization for 64bit integer = "long long int"
template <>
__forceinline__ __device__ void write_force <long long int> (const float fx,
const float fy,
const float fz,
const int ind,
const int stride,
const int stride2,
long long int* force) {
unsigned long long int fx_ulli = llitoulli(lliroundf(FORCE_SCALE*fx));
unsigned long long int fy_ulli = llitoulli(lliroundf(FORCE_SCALE*fy));
unsigned long long int fz_ulli = llitoulli(lliroundf(FORCE_SCALE*fz));
unsigned long long int *force_ulli = (unsigned long long int *)force;
force_ulli[ind ] += fx_ulli;
force_ulli[ind + stride ] += fy_ulli;
force_ulli[ind + stride2] += fz_ulli;
}
//-----------------------------------------------------------------------------------------
// Generic version can not be used
template <typename T> __forceinline__ __device__
void gather_force_store(const float fx, const float fy, const float fz,
const int stride, const int pos,
T* force) {
}
// Template specialization for "long long int"
template <> __forceinline__ __device__
void gather_force_store<long long int>(const float fx, const float fy, const float fz,
const int stride, const int pos,
long long int* force) {
// Add into strided "long long int" array
long long int fx_lli = lliroundf(fx*FORCE_SCALE);
long long int fy_lli = lliroundf(fy*FORCE_SCALE);
long long int fz_lli = lliroundf(fz*FORCE_SCALE);
write_force<long long int>(fx_lli, fy_lli, fz_lli, pos, stride, force);