-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCudaNeighborListSort.cu
1433 lines (1250 loc) · 47.2 KB
/
CudaNeighborListSort.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 <stdio.h>
#include <iostream>
#include <cassert>
#include "gpu_utils.h"
#include "cuda_utils.h"
//#define BUCKET_SORT_IN_USE
#include "CudaNeighborListSort.h"
// IF defined, uses strict (Factor = 1.0f) memory reallocation. Used for debuggin memory problems.
#define STRICT_MEMORY_REALLOC
//
// Sort atoms into z-columns
//
// col_natom[0..ncellx*ncelly-1] = number of atoms in each column
// atom_icol[istart..iend] = column index for atoms
//
__global__ void calc_z_column_index_kernel(const int izoneStart, const int izoneEnd,
const ZoneParam_t* __restrict__ ZoneParam,
const float4* __restrict__ xyzq,
int* __restrict__ col_natom,
int* __restrict__ atom_icol,
int3* __restrict__ col_xy_zone) {
// Atom index
const int i = threadIdx.x + blockIdx.x*blockDim.x;
int ind0 = 0;
int istart = 0;
int iend = 0;
for (int izone=izoneStart;izone <= izoneEnd;izone++) {
istart = iend;
iend = istart + ZoneParam[izone].ncoord;
if (i >= istart && i < iend) {
float4 xyzq_val = xyzq[i];
float x = xyzq_val.x;
float y = xyzq_val.y;
float3 min_xyz = ZoneParam[izone].min_xyz;
int ncellx = ZoneParam[izone].ncellx;
int ncelly = ZoneParam[izone].ncelly;
int ix = min((int)((x - min_xyz.x)*ZoneParam[izone].inv_celldx), ncellx-1);
int iy = min((int)((y - min_xyz.y)*ZoneParam[izone].inv_celldy), ncelly-1);
int ind = ind0 + ix + iy*ncellx;
atomicAdd(&col_natom[ind], 1);
atom_icol[i] = ind;
int3 col_xy_zone_val;
col_xy_zone_val.x = ix;
col_xy_zone_val.y = iy;
col_xy_zone_val.z = izone;
col_xy_zone[ind] = col_xy_zone_val;
break;
}
ind0 += ZoneParam[izone].ncellx*ZoneParam[izone].ncelly;
}
}
//
// For each zone, calculates the maximum number of atoms per z-column
// This information is used in the sorting of z-columns
//
// Thread block per zone
//
__global__ void calcZoneMaxZColNatomKernel(const int izoneStart,
const ZoneParam_t* __restrict__ ZoneParam,
const int* __restrict__ col_natom,
int* __restrict__ zoneMaxZColNatom) {
// Shared memory
// Requires: blockDim.x*sizeof(int)
extern __shared__ int shZoneMaxZColNatom[];
// Number of columns in this zone
const int ncol = ZoneParam[izoneStart+blockIdx.x].ncellx*ZoneParam[izoneStart+blockIdx.x].ncelly;
// Starting column index in this zone
const int col0 = ZoneParam[izoneStart+blockIdx.x].zone_col - ZoneParam[izoneStart].zone_col;
// Result, only used by threadIdx.x = 0
int max_val = 0;
for (int base=0;base < ncol;base+=blockDim.x) {
// Load blockDim.x number of values into shared memory
shZoneMaxZColNatom[threadIdx.x] = (base + threadIdx.x < ncol) ? col_natom[col0 + base + threadIdx.x] : 0;
__syncthreads();
// Find maximum of blockDim.x number of values
for (int d=1;d < blockDim.x;d*=2) {
int val = (threadIdx.x >= d) ? shZoneMaxZColNatom[threadIdx.x-d] : 0;
__syncthreads();
shZoneMaxZColNatom[threadIdx.x] = max(shZoneMaxZColNatom[threadIdx.x], val);
__syncthreads();
}
// Compare maximum found to the global maximum
if (threadIdx.x == 0) max_val = max(max_val, shZoneMaxZColNatom[blockDim.x-1]);
}
// Write result into global memory
if (threadIdx.x == 0) {
zoneMaxZColNatom[blockIdx.x] = max_val;
}
}
//
// Computes z column position using parallel exclusive prefix sum
// Also computes the cell_patom, col_ncellz, col_cell, and ncell
//
// NOTE: Must have nblock = 1, we loop over buckets to avoid multiple kernel calls
//
__global__ void calc_z_column_pos_kernel(const int tilesize, const int ncol_tot,
const int atomStart, const int cellStart,
const int3* __restrict__ col_xy_zone,
int* __restrict__ col_natom,
int* __restrict__ col_patom,
int* __restrict__ cell_patom,
int* __restrict__ col_ncellz,
int4* __restrict__ cell_xyz_zone,
int* __restrict__ col_cell,
NlistParam_t* __restrict__ d_NlistParam) {
// Shared memory
// Requires: blockDim.x*sizeof(int2)
// This shared memory buffer is used for computing positions (cumulative sums) of columns, size blockDim.x*sizeof(int2)
extern __shared__ int2 sh_pos[];
if (threadIdx.x == 0) {
col_patom[0] = 0;
}
int2 offset = make_int2(0, 0);
for (int base=0;base < ncol_tot;base += blockDim.x) {
// i = column index
int i = base + threadIdx.x;
int2 tmpval;
tmpval.x = (i < ncol_tot) ? col_natom[i] : 0; // Number of atoms in this column
tmpval.y = (i < ncol_tot) ? (tmpval.x - 1)/tilesize + 1 : 0; // Number of z-cells in this column (NOTE: this needs to be 0 when tmpval.x = 0)
if (i < ncol_tot) col_ncellz[i] = tmpval.y; // Set col_ncellz[icol]
sh_pos[threadIdx.x] = tmpval;
if (i < ncol_tot) col_natom[i] = 0;
__syncthreads();
for (int d=1;d < blockDim.x; d *= 2) {
int2 posd = (threadIdx.x >= d) ? sh_pos[threadIdx.x-d] : make_int2(0, 0);
__syncthreads();
int2 pos0 = sh_pos[threadIdx.x];
pos0.x += posd.x;
pos0.y += posd.y;
sh_pos[threadIdx.x] = pos0;
__syncthreads();
}
if (i < ncol_tot) {
// shpos[threadIdx.x].x = cumulative sum of number of atoms in this column
// shpos[threadIdx.x].y = cumulative sum of number of z-cells in this column
// Write col_patom in global memory
int2 val1 = sh_pos[threadIdx.x];
val1.x += offset.x;
val1.y += offset.y;
col_patom[i+1] = val1.x;
// Write cell_patom in global memory
// OPTIMIZATION NOTE: Is this looping too slow? Should we move this into a separate kernel?
int2 val0 = (threadIdx.x > 0) ? sh_pos[threadIdx.x - 1] : make_int2(0, 0);
val0.x += offset.x;
val0.y += offset.y;
int icell0 = val0.y;
int icell1 = val1.y;
int iatom = val0.x + atomStart;
// Write col_cell
col_cell[i] = icell0 + cellStart;
// col_xy_zone[icol].x = x coordinate for each column
// col_xy_zone[icol].y = y coordinate for each column
// col_xy_zone[icol].z = zone for each column
int4 cell_xyz_zone_val;
int3 col_xy_zone_val = col_xy_zone[i];
cell_xyz_zone_val.x = col_xy_zone_val.x; // icellx
cell_xyz_zone_val.y = col_xy_zone_val.y; // icelly
cell_xyz_zone_val.z = 0; // icellz (set in the loop below)
cell_xyz_zone_val.w = col_xy_zone_val.z; // izone
for (int icell=icell0;icell < icell1;icell++,iatom+=tilesize,cell_xyz_zone_val.z++) {
cell_patom[icell] = iatom;
cell_xyz_zone[icell] = cell_xyz_zone_val;
}
}
// Add the last value to the offset for the next block
int2 lastval = sh_pos[blockDim.x-1];
offset.x += lastval.x;
offset.y += lastval.y;
// Sync threads so that the next iteration can start writing in shared memory
__syncthreads();
}
if (threadIdx.x == 0) {
// Cap off cell_patom
cell_patom[offset.y] = offset.x + atomStart;
// Write ncell into global GPU buffer
d_NlistParam->ncell = offset.y;
// Clear nexcl
d_NlistParam->nexcl = 0;
// Zero n_ientry and n_tile -counters in preparation for neighbor list build
d_NlistParam->n_ientry = 0;
d_NlistParam->n_tile = 0;
}
// Set zone_cell = starting cell for each zone
//if (threadIdx.x < maxNumZone) {
// int icol = d_ZoneParam[threadIdx.x].zone_col;
// d_NlistParam.zone_cell[threadIdx.x] = (icol < ncol_tot) ? col_cell[icol] : d_NlistParam.ncell;
//}
}
/*
//
// Calculates ncellz_max[izone].
//
// blockDim.x = max number of columns over all zones
// Each thread block calculates one zone (blockIdx.x = izone)
//
__global__ void calc_ncellz_max_kernel(const int* __restrict__ col_ncellz) {
// Shared memory
// Requires: blockDim.x*sizeof(int)
extern __shared__ int sh_col_ncellz[];
// ncol[izone] gives the cumulative sum of ncellx[izone]*ncelly[izone]
// NOTE: we can use zone_col & ncol_tot to replace ncol
int start = d_NlistParam.ncol[blockIdx.x];
int end = d_NlistParam.ncol[blockIdx.x+1] - 1;
int ncellz_max = 0;
for (;start <= end;start += blockDim.x) {
// Load col_ncellz into shared memory
int pos = start + threadIdx.x;
int col_ncellz_val = 0;
if (pos <= end) col_ncellz_val = col_ncellz[pos];
sh_col_ncellz[threadIdx.x] = col_ncellz_val;
__syncthreads();
// Reduce
int n = end - start;
for (int d=1;d < n;d *= 2) {
int t = threadIdx.x + d;
int val = (t < n) ? sh_col_ncellz[t] : 0;
__syncthreads();
sh_col_ncellz[threadIdx.x] = max(sh_col_ncellz[threadIdx.x], val);
__syncthreads();
}
// Store into register
if (threadIdx.x == 0) ncellz_max = max(ncellz_max, sh_col_ncellz[0]);
}
// Write into global memory
if (threadIdx.x == 0) d_ZoneParam[blockIdx.x].ncellz_max = ncellz_max;
}
*/
/*
//
// Calculates celldz_min[izone], where izone = blockIdx.x = 0...7
//
__global__ void calc_celldz_min_kernel() {
// Shared memory
// Requires: blockDim.x*sizeof(float)
extern __shared__ float sh_celldz_min[];
// ncol[izone] gives the cumulative sum of ncellx[izone]*ncelly[izone]
int start = d_NlistParam.ncell[blockIdx.x];
int end = d_NlistParam.ncell[blockIdx.x+1] - 1;
float celldz_min = (float)(1.0e20);
for (;start <= end;start += blockDim.x) {
// Load value into shared memory
float celldz_min_val = (float)(1.0e20);
int pos = start + threadIdx.x;
if (pos <= end) celldz_min_val = ;
sh_celldz_min[threadIdx.x] = celldz_min_val;
__synthreads();
// Reduce
int n = end - start;
for (int d=1;d < n;d *= 2) {
int t = threadIdx.x + d;
float val = (t < n) ? sh_celldz_min[t] : (float)(1.0e20);
__syncthreads();
sh_celldz_min[threadIdx.x] = min(sh_celldz_min[threadIdx.x], val);
__syncthreads();
}
// Store into register
if (threadIdx.x == 0) celldz_min = min(celldz_min, sh_celldz_min[0]);
}
// Write into global memory
if (threadIdx.x == 0) d_NlistParam.celldz_min[blockIdx.x] = celldz_min;
}
*/
//
// Finds the min_xyz and max_xyz for zone "izone"
//
__global__ void calc_minmax_xyz_kernel(const int ncoord, const int izone,
const float4* __restrict__ xyzq,
ZoneParam_t* __restrict__ d_ZoneParam) {
// Shared memory
// Requires: 6*blockDim.x*sizeof(float)
extern __shared__ float sh_minmax_xyz[];
volatile float* sh_min_x = &sh_minmax_xyz[0];
volatile float* sh_min_y = &sh_minmax_xyz[blockDim.x];
volatile float* sh_min_z = &sh_minmax_xyz[blockDim.x*2];
volatile float* sh_max_x = &sh_minmax_xyz[blockDim.x*3];
volatile float* sh_max_y = &sh_minmax_xyz[blockDim.x*4];
volatile float* sh_max_z = &sh_minmax_xyz[blockDim.x*5];
// Load data into shared memory
const int i = threadIdx.x + blockIdx.x*blockDim.x;
float4 xyzq_i = xyzq[min(i,ncoord-1)];
float x = xyzq_i.x;
float y = xyzq_i.y;
float z = xyzq_i.z;
sh_min_x[threadIdx.x] = x;
sh_min_y[threadIdx.x] = y;
sh_min_z[threadIdx.x] = z;
sh_max_x[threadIdx.x] = x;
sh_max_y[threadIdx.x] = y;
sh_max_z[threadIdx.x] = z;
__syncthreads();
// Reduce
for (int d=1;d < blockDim.x;d *= 2) {
int t = threadIdx.x + d;
float min_x = (t < blockDim.x) ? sh_min_x[t] : (float)(1.0e20);
float min_y = (t < blockDim.x) ? sh_min_y[t] : (float)(1.0e20);
float min_z = (t < blockDim.x) ? sh_min_z[t] : (float)(1.0e20);
float max_x = (t < blockDim.x) ? sh_max_x[t] : (float)(-1.0e20);
float max_y = (t < blockDim.x) ? sh_max_y[t] : (float)(-1.0e20);
float max_z = (t < blockDim.x) ? sh_max_z[t] : (float)(-1.0e20);
__syncthreads();
sh_min_x[threadIdx.x] = min(sh_min_x[threadIdx.x], min_x);
sh_min_y[threadIdx.x] = min(sh_min_y[threadIdx.x], min_y);
sh_min_z[threadIdx.x] = min(sh_min_z[threadIdx.x], min_z);
sh_max_x[threadIdx.x] = max(sh_max_x[threadIdx.x], max_x);
sh_max_y[threadIdx.x] = max(sh_max_y[threadIdx.x], max_y);
sh_max_z[threadIdx.x] = max(sh_max_z[threadIdx.x], max_z);
__syncthreads();
}
// Store into global memory
if (threadIdx.x == 0) {
atomicMin(&d_ZoneParam[izone].min_xyz.x, sh_min_x[0]);
atomicMin(&d_ZoneParam[izone].min_xyz.y, sh_min_y[0]);
atomicMin(&d_ZoneParam[izone].min_xyz.z, sh_min_z[0]);
atomicMax(&d_ZoneParam[izone].max_xyz.x, sh_max_x[0]);
atomicMax(&d_ZoneParam[izone].max_xyz.y, sh_max_y[0]);
atomicMax(&d_ZoneParam[izone].max_xyz.z, sh_max_z[0]);
}
}
//
// Re-order atoms according to pos. Non-deterministic version (because of atomicAdd())
//
__global__ void reorder_atoms_z_column_kernel(const int ncoord,
const int* atom_icol,
int* col_natom,
const int* col_patom,
const float4* __restrict__ xyzq_in,
float4* __restrict__ xyzq_out,
int* __restrict__ ind_sorted) {
const int i = threadIdx.x + blockIdx.x*blockDim.x;
if (i < ncoord) {
// Column index
int icol = atom_icol[i];
int pos = col_patom[icol];
int n = atomicAdd(&col_natom[icol], 1);
// new position = pos + n
int newpos = pos + n;
ind_sorted[newpos] = i;
xyzq_out[newpos] = xyzq_in[i];
}
}
//
// Reorders loc2glo using ind_sorted and shifts ind_sorted by atomStart
//
__global__ void reorder_loc2glo_kernel(const int ncoord, const int atomStart,
int* __restrict__ ind_sorted,
const int* __restrict__ loc2glo_in,
int* __restrict__ loc2glo_out) {
const int i = threadIdx.x + blockIdx.x*blockDim.x;
if (i < ncoord) {
int j = ind_sorted[i];
ind_sorted[i] = j + atomStart;
loc2glo_out[i] = loc2glo_in[j];
}
}
//
// Builds glo2loc using loc2glo
//
__global__ void build_glo2loc_kernel(const int ncoord, const int atomStart,
const int* __restrict__ loc2glo,
int* __restrict__ glo2loc) {
const int i = threadIdx.x + blockIdx.x*blockDim.x;
if (i < ncoord) {
int ig = loc2glo[i];
glo2loc[ig] = i + atomStart;
}
}
/*
//
// Builds atom_pcell. Single warp takes care of single cell
//
__global__ void build_atom_pcell_kernel(const int* __restrict__ cell_patom,
int* __restrict__ atom_pcell) {
const int icell = (threadIdx.x + blockIdx.x*blockDim.x)/warpsize;
const int wid = threadIdx.x % warpsize;
if (icell < d_NlistParam.ncell) {
int istart = cell_patom[icell];
int iend = cell_patom[icell+1] - 1;
if (istart + wid <= iend) atom_pcell[istart + wid] = icell;
}
}
*/
//
// Sorts atoms according to z coordinate
//
// Uses bitonic sort, see:
// http://www.tools-of-computing.com/tc/CS/Sorts/bitonic_sort.htm
//
// Each thread block sorts a single z column.
//
// Shared memory version (use_shmem = true)
// Length of z-columns is limited by the shared memory size.
// max_n not used
// glo_keyvalBase = NULL
//
// Global memory version (use_shmem = false)
// No limitations on the length of the z-columns
// max_n = maximum number of atoms across all z-columns
// glo_keyvalBase = buffer of size max_n*blockDim.x
//
template <bool use_shmem>
__global__
void bitonicSortZColKernel(const int max_n,
const int* __restrict__ col_patom,
float4* __restrict__ xyzq,
float4* __restrict__ xyzqTmp,
int* __restrict__ indSorted,
keyval_t* __restrict__ glo_keyvalBase) {
// Shared memory:
// Requires max_n*sizeof(keyval_t) for the shared memory version
extern __shared__ keyval_t sh_keyval[];
const int col_patom0 = col_patom[blockIdx.x];
const int n = col_patom[blockIdx.x+1] - col_patom0;
// Get pointer for this column (global memory version only)
keyval_t* __restrict__ glo_keyval;
if (!use_shmem) glo_keyval = &glo_keyvalBase[max_n*blockIdx.x];
// Read keys and values into shared/global memory
// key = z-coordinate
// val = index
for (int i=threadIdx.x;i < n;i+=blockDim.x) {
keyval_t keyval;
keyval.key = xyzq[i + col_patom0].z;
keyval.val = i + col_patom0;
if (use_shmem) {
sh_keyval[i] = keyval;
} else {
glo_keyval[i] = keyval;
}
}
__syncthreads();
for (int k = 2;k < 2*n;k *= 2) {
for (int j = k/2; j > 0;j /= 2) {
for (int i=threadIdx.x;i < n;i+=blockDim.x) {
int ixj = i ^ j;
if (ixj > i && ixj < n) {
// asc = true for ascending order
bool asc = ((i & k) == 0);
for (int kk = k*2;kk < 2*n;kk *= 2)
asc = ((i & kk) == 0 ? !asc : asc);
// Read data
keyval_t keyval1 = (use_shmem) ? sh_keyval[i] : glo_keyval[i];
keyval_t keyval2 = (use_shmem) ? sh_keyval[ixj] : glo_keyval[ixj];
float lo_key = asc ? keyval1.key : keyval2.key;
float hi_key = asc ? keyval2.key : keyval1.key;
if (lo_key > hi_key) {
// keys are in wrong order => exchange
if (use_shmem) {
sh_keyval[i] = keyval2;
sh_keyval[ixj] = keyval1;
} else {
glo_keyval[i] = keyval2;
glo_keyval[ixj] = keyval1;
}
}
}
}
__syncthreads();
}
}
// sh_keyval[threadIdx.x].val gives the mapping:
//
// xyzq_new[threadIdx.x + col_patom0] = xyzq[sh_keyval[threadIdx.x].val]
// loc2glo_new[threadIdx.x + col_patom0] = loc2glo[sh_keyval[threadIdx.x].val]
//
// keys are not needed anymore, store index into that memory location
for (int i=threadIdx.x;i < n;i+=blockDim.x) {
if (use_shmem) {
sh_keyval[i].ind = indSorted[i + col_patom0];
} else {
glo_keyval[i].ind = indSorted[i + col_patom0];
}
}
__syncthreads();
for (int ibase=0;ibase < n;ibase+=blockDim.x) {
int i = ibase + threadIdx.x;
float4 xyzq_val;
int ind_val;
if (i < n) {
int pos = (use_shmem) ? sh_keyval[i].val : glo_keyval[i].val;
ind_val = (use_shmem) ? sh_keyval[pos - col_patom0].ind : glo_keyval[pos - col_patom0].ind;
xyzq_val = xyzq[pos];
}
__syncthreads();
if (i < n) {
int newpos = i + col_patom0;
indSorted[newpos] = ind_val;
xyzqTmp[newpos] = xyzq_val;
}
}
__syncthreads();
for (int i=threadIdx.x;i < n;i+=blockDim.x) {
xyzq[i + col_patom0] = xyzqTmp[i + col_patom0];
}
}
#ifdef BUCKET_SORT_IN_USE
//
// Sort atoms in z-columns using bucket sort
// One block does one z-column
//
// min_z = minimum z-coordinate for this zone
// invBucketWidth = 1/(bucket width)
// numBucket = number of buckets per column
// bucketPos[] = for each bucket, position
// bucketIndex[] = for each atom, bucket index
//
__global__ void bucketSortZColKernel(const int* __restrict__ col_patom,
const float min_z, const float invBucketWidth,
const int numBucket,
int* __restrict__ bucketPosBase,
int* __restrict__ bucketIndexBase,
float4* __restrict__ xyzqBase,
float4* __restrict__ xyzqTmpBase,
int* __restrict__ indSortedBase,
int* __restrict__ indSortedTmpBase) {
// Shared memory, requires: blockDim.x*sizeof(int)
extern __shared__ int shBucketPos[];
// Get atom starting index and number of coordinates
const int atomStart = col_patom[blockIdx.x];
const int ncoord = col_patom[blockIdx.x+1] - atomStart;
// Pointers for this column
int* __restrict__ bucketPos = &bucketPosBase[numBucket*blockIdx.x];
int* __restrict__ bucketIndex = &bucketIndexBase[atomStart];
float4* __restrict__ xyzq = &xyzqBase[atomStart];
float4* __restrict__ xyzqTmp = &xyzqTmpBase[atomStart];
int* __restrict__ indSorted = &indSortedBase[atomStart];
int* __restrict__ indSortedTmp = &indSortedTmpBase[atomStart];
// Before starting, clear bucketPos
for (int i=threadIdx.x;i < numBucket;i+=blockDim.x) bucketPos[i] = 0;
__syncthreads();
// Assign atoms into buckets
for (int i=threadIdx.x;i < ncoord;i+=blockDim.x) {
int ibucket = min((int)((xyzq[i].z - min_z)*invBucketWidth), numBucket-1);
atomicAdd(&bucketPos[ibucket], 1);
bucketIndex[i] = ibucket;
}
__syncthreads();
// Compute position of buckets
int pos0 = 0;
for (int base=0;base < ncoord;base+=blockDim.x) {
// Load bucketPos into shared memory
shBucketPos[threadIdx.x] = (base+threadIdx.x < ncoord) ? bucketPos[base+threadIdx.x] : 0;
__syncthreads();
// Reduce in shared memory, result is inclusive cumsum
for (int d=1;d < blockDim.x;d*=2) {
int val = (threadIdx.x >= d) ? shBucketPos[threadIdx.x-d] : 0;
__syncthreads();
shBucketPos[threadIdx.x] += val;
__syncthreads();
}
// Write result back into global memory and shift to get exclusive cumsum
if (base+threadIdx.x < ncoord)
bucketPos[base+threadIdx.x] = pos0 + (threadIdx.x >= 1) ? shBucketPos[threadIdx.x-1] : 0;
// Broadcast last value to pos0
pos0 = shBucketPos[blockDim.x-1];
}
__syncthreads();
// Re-order according to buckets, result in (xyzqTmp, indSortedTmp)
for (int i=threadIdx.x;i < ncoord;i+=blockDim.x) {
int ibucket = bucketIndex[i];
int pos = atomicAdd(&bucketPos[ibucket], 1);
xyzqTmp[pos] = xyzq[i];
indSortedTmp[pos] = indSorted[i];
}
__syncthreads();
// Sort within buckets
THIS PART NOT IMPLEMENTED!
// Copy final result back to xyzq
for (int i=threadIdx.x;i < ncoord;i+=blockDim.x) {
xyzq[i] = xyzqTmp[i];
indSorted[i] = indSortedTmp[i];
}
}
#endif
//
// Calculates bounding box (bb) and cell z-boundaries (cell_bz)
// NOTE: Each thread calculates one bounding box
//
__global__ void calc_bb_cell_bz_kernel(const int ncell, const int* __restrict__ cell_patom,
const float4* __restrict__ xyzq,
bb_t* __restrict__ bb,
float* __restrict__ cell_bz) {
const int icell = threadIdx.x + blockIdx.x*blockDim.x;
if (icell < ncell) {
int istart = cell_patom[icell];
int iend = cell_patom[icell+1] - 1;
float4 xyzq_val = xyzq[istart];
float minx = xyzq_val.x;
float miny = xyzq_val.y;
float minz = xyzq_val.z;
float maxx = xyzq_val.x;
float maxy = xyzq_val.y;
float maxz = xyzq_val.z;
for (int i=istart+1;i <= iend;i++) {
xyzq_val = xyzq[i];
minx = min(minx, xyzq_val.x);
miny = min(miny, xyzq_val.y);
minz = min(minz, xyzq_val.z);
maxx = max(maxx, xyzq_val.x);
maxy = max(maxy, xyzq_val.y);
maxz = max(maxz, xyzq_val.z);
}
// Set the cell z-boundary equal to the z-coordinate of the last atom
cell_bz[icell] = xyzq_val.z;
bb_t bb_val;
bb_val.x = 0.5f*(minx + maxx);
bb_val.y = 0.5f*(miny + maxy);
bb_val.z = 0.5f*(minz + maxz);
bb_val.wx = 0.5f*(maxx - minx);
bb_val.wy = 0.5f*(maxy - miny);
bb_val.wz = 0.5f*(maxz - minz);
bb[icell] = bb_val;
}
}
//########################################################################################
//########################################################################################
//########################################################################################
//
// Class creator
//
CudaNeighborListSort::CudaNeighborListSort(const int tilesize, const int izoneStart, const int izoneEnd) :
tilesize(tilesize), izoneStart(izoneStart), izoneEnd(izoneEnd) {
col_natom_len = 0;
col_natom = NULL;
col_patom_len = 0;
col_patom = NULL;
atom_icol_len = 0;
atom_icol = NULL;
col_xy_zone_len = 0;
col_xy_zone = NULL;
loc2gloTmp_len = 0;
loc2gloTmp = NULL;
xyzqTmpLen = 0;
xyzqTmp = NULL;
#ifdef BUCKET_SORT_IN_USE
bucketPosLen = 0;
bucketPos = NULL;
bucketIndexLen = 0;
bucketIndex = NULL;
indSortedTmpLen = 0;
indSortedTmp = NULL;
#else
keyvalBufferLen = 0;
keyvalBuffer = NULL;
#endif
test = false;
// Allocate pinned host memory
allocate_host<int>(&h_ncell, 1);
allocate_host<int>(&h_zoneMaxZColNatom, izoneEnd-izoneStart+1);
// Allocate device memory
allocate<int>(&d_zoneMaxZColNatom, izoneEnd-izoneStart+1);
// Create events
cudaCheck(cudaEventCreate(&ncell_copy_event));
cudaCheck(cudaEventCreate(&zoneMaxZColNatom_copy_event));
}
//
// Class destructor
//
CudaNeighborListSort::~CudaNeighborListSort() {
if (col_natom != NULL) deallocate<int>(&col_natom);
if (col_patom != NULL) deallocate<int>(&col_patom);
if (atom_icol != NULL) deallocate<int>(&atom_icol);
if (col_xy_zone != NULL) deallocate<int3>(&col_xy_zone);
if (loc2gloTmp != NULL) deallocate<int>(&loc2gloTmp);
if (xyzqTmp != NULL) deallocate<float4>(&xyzqTmp);
#ifdef BUCKET_SORT_IN_USE
if (bucketPos != NULL) deallocate<int>(&bucketPos);
if (bucketIndex != NULL) deallocate<int>(&bucketIndex);
if (indSortedTmp != NULL) deallocate<int>(&indSortedTmp);
#else
if (keyvalBuffer != NULL) deallocate<keyval_t>(&keyvalBuffer);
#endif
deallocate_host<int>(&h_ncell);
deallocate_host<int>(&h_zoneMaxZColNatom);
deallocate<int>(&d_zoneMaxZColNatom);
cudaCheck(cudaEventDestroy(ncell_copy_event));
cudaCheck(cudaEventDestroy(zoneMaxZColNatom_copy_event));
}
//
// Copies h_ZoneParam (CPU) -> d_ZoneParam (GPU)
//
void CudaNeighborListSort::setZoneParam(ZoneParam_t* h_ZoneParam, ZoneParam_t* d_ZoneParam,
cudaStream_t stream) {
copy_HtoD<ZoneParam_t>(h_ZoneParam+izoneStart, d_ZoneParam+izoneStart, izoneEnd-izoneStart+1, stream);
}
//
// Copies d_ZoneParam (GPU) -> h_ZoneParam (CPU)
//
void CudaNeighborListSort::getZoneParam(ZoneParam_t* h_ZoneParam, ZoneParam_t* d_ZoneParam,
cudaStream_t stream) {
cudaCheck(cudaStreamSynchronize(stream));
copy_DtoH_sync<ZoneParam_t>(d_ZoneParam+izoneStart, h_ZoneParam+izoneStart, izoneEnd-izoneStart+1);
}
//
// Calculate min_xyz and max_xyz
//
void CudaNeighborListSort::calc_min_max_xyz(const int *zone_patom, const float4 *xyzq,
ZoneParam_t *h_ZoneParam, ZoneParam_t *d_ZoneParam,
cudaStream_t stream) {
for (int izone=izoneStart;izone <= izoneEnd;izone++) {
h_ZoneParam[izone].min_xyz.x = (float)1.0e20;
h_ZoneParam[izone].min_xyz.y = (float)1.0e20;
h_ZoneParam[izone].min_xyz.z = (float)1.0e20;
h_ZoneParam[izone].max_xyz.x = (float)(-1.0e20);
h_ZoneParam[izone].max_xyz.y = (float)(-1.0e20);
h_ZoneParam[izone].max_xyz.z = (float)(-1.0e20);
}
setZoneParam(h_ZoneParam, d_ZoneParam, stream);
for (int izone=izoneStart;izone <= izoneEnd;izone++) {
int nstart = zone_patom[izone];
int ncoord_zone = zone_patom[izone+1] - nstart;
if (ncoord_zone > 0) {
int nthread = 512;
int nblock = (ncoord_zone-1)/nthread+1;
int shmem_size = 6*nthread*sizeof(float);
calc_minmax_xyz_kernel<<< nblock, nthread, shmem_size, stream >>>
(ncoord_zone, izone, &xyzq[nstart], d_ZoneParam);
}
}
getZoneParam(h_ZoneParam, d_ZoneParam, stream);
}
//
// Setups sort parameters: NlistParam, ncol_tot, ncell_max
//
// NOTE: ncell_max is an approximate upper bound for the number of cells,
// it is possible to blow this bound, so we should check for it
void CudaNeighborListSort::sort_setup(const int *zone_patom,
ZoneParam_t *h_ZoneParam, ZoneParam_t *d_ZoneParam,
cudaStream_t stream) {
//
// Setup ZoneParam and calculate ncol_tot, ncoord_tot, and ncell_max
//
ncol_tot = 0;
ncoord_tot = 0;
ncell_max = 0;
if (izoneStart == 0) h_ZoneParam[0].zone_col = 0;
for (int izone=izoneStart;izone <= izoneEnd;izone++) {
h_ZoneParam[izone].ncoord = zone_patom[izone+1] - zone_patom[izone];
if (h_ZoneParam[izone].ncoord > 0) {
// NOTE: we increase the cell sizes here by 0.001 to make sure no atoms drop outside cells
float xsize = h_ZoneParam[izone].max_xyz.x - h_ZoneParam[izone].min_xyz.x + 0.001f;
float ysize = h_ZoneParam[izone].max_xyz.y - h_ZoneParam[izone].min_xyz.y + 0.001f;
float zsize = h_ZoneParam[izone].max_xyz.z - h_ZoneParam[izone].min_xyz.z + 0.001f;
float delta = powf(xsize*ysize*zsize*tilesize/(float)h_ZoneParam[izone].ncoord, 1.0f/3.0f);
h_ZoneParam[izone].ncellx = max(1, (int)(xsize/delta));
h_ZoneParam[izone].ncelly = max(1, (int)(ysize/delta));
// Approximation for ncellz = 2 x "uniform distribution of atoms"
h_ZoneParam[izone].ncellz_max = max(1, 2*h_ZoneParam[izone].ncoord/
(h_ZoneParam[izone].ncellx*h_ZoneParam[izone].ncelly*tilesize));
/*
fprintf(stderr,"%d %d %d | %f %f %f | %f %f %f | %f %f %f\n",
h_ZoneParam[izone].ncellx, h_ZoneParam[izone].ncelly, h_ZoneParam[izone].ncellz_max,
h_ZoneParam[izone].min_xyz.x, h_ZoneParam[izone].min_xyz.y, h_ZoneParam[izone].min_xyz.z,
h_ZoneParam[izone].max_xyz.x, h_ZoneParam[izone].max_xyz.y, h_ZoneParam[izone].max_xyz.z,
xsize, ysize, zsize);
*/
h_ZoneParam[izone].celldx = xsize/(float)(h_ZoneParam[izone].ncellx);
h_ZoneParam[izone].celldy = ysize/(float)(h_ZoneParam[izone].ncelly);
h_ZoneParam[izone].celldz_min = zsize/(float)(h_ZoneParam[izone].ncellz_max);
if (test) {
std::cerr << izone << ": " << h_ZoneParam[izone].min_xyz.z
<< " ... " << h_ZoneParam[izone].max_xyz.z << std::endl;
}
} else {
h_ZoneParam[izone].ncellx = 0;
h_ZoneParam[izone].ncelly = 0;
h_ZoneParam[izone].ncellz_max = 0;
h_ZoneParam[izone].celldx = 1.0f;
h_ZoneParam[izone].celldy = 1.0f;
h_ZoneParam[izone].celldz_min = 1.0f;
}
h_ZoneParam[izone].inv_celldx = 1.0f/h_ZoneParam[izone].celldx;
h_ZoneParam[izone].inv_celldy = 1.0f/h_ZoneParam[izone].celldy;
if (izone > 0) {
h_ZoneParam[izone].zone_col = h_ZoneParam[izone-1].zone_col +
h_ZoneParam[izone-1].ncellx*h_ZoneParam[izone-1].ncelly;
}
int ncellxy = h_ZoneParam[izone].ncellx*h_ZoneParam[izone].ncelly;
ncol_tot += ncellxy;
ncoord_tot += h_ZoneParam[izone].ncoord;
ncell_max += ncellxy*h_ZoneParam[izone].ncellz_max;
}
// Copy h_ZoneParam => d_ZoneParam
setZoneParam(h_ZoneParam, d_ZoneParam, stream);
// Wait till setZoneParam finishes
cudaCheck(cudaStreamSynchronize(stream));
}
//
// Allocates / Re-allocates memory for sort
//
void CudaNeighborListSort::sort_realloc() {
#ifdef STRICT_MEMORY_REALLOC
float fac = 1.0f;
#else
float fac = 1.2f;
#endif
reallocate<int>(&col_natom, &col_natom_len, ncol_tot, fac);
reallocate<int>(&col_patom, &col_patom_len, ncol_tot+1, fac);
reallocate<int3>(&col_xy_zone, &col_xy_zone_len, ncol_tot, fac);
reallocate<int>(&atom_icol, &atom_icol_len, ncoord_tot, fac);
reallocate<int>(&loc2gloTmp, &loc2gloTmp_len, ncoord_tot, fac);
reallocate<float4>(&xyzqTmp, &xyzqTmpLen, ncoord_tot, fac);
}
//
// Sorts atoms, core subroutine.
//
void CudaNeighborListSort::sort_core(const int* zone_patom, const int cellStart, const int colStart,
ZoneParam_t* h_ZoneParam, ZoneParam_t* d_ZoneParam,
NlistParam_t* d_NlistParam,
int* cell_patom, int* col_ncellz, int4* cell_xyz_zone,
int* col_cell, int* ind_sorted, const float4 *xyzq, float4 *xyzq_sorted,
cudaStream_t stream) {
int nthread, nblock, shmem_size;
int atomStart = zone_patom[izoneStart];
// Clear col_natom
clear_gpu_array<int>(col_natom, ncol_tot, stream);
//
// Calculate number of atoms in each z-column (col_natom)
// and the column index for each atom (atom_icol)
//
nthread = 512;
nblock = (ncoord_tot-1)/nthread+1;
calc_z_column_index_kernel<<< nblock, nthread, 0, stream >>>
(izoneStart, izoneEnd, d_ZoneParam, xyzq+atomStart,
col_natom, atom_icol, col_xy_zone);
cudaCheck(cudaGetLastError());
//
// For each zone, calculate maximum number of atoms in columns
//
nthread = 0;
for (int izone=izoneStart;izone <= izoneEnd;izone++) {
nthread = max(nthread, h_ZoneParam[izone].ncellx*h_ZoneParam[izone].ncelly);
}
nthread = min(nthread, get_max_nthread());
shmem_size = nthread*sizeof(int);
calcZoneMaxZColNatomKernel<<< izoneEnd-izoneStart+1, nthread, shmem_size, stream>>>
(izoneStart, d_ZoneParam, col_natom, d_zoneMaxZColNatom);
cudaCheck(cudaGetLastError());
copy_DtoH<int>(d_zoneMaxZColNatom, h_zoneMaxZColNatom, izoneEnd-izoneStart+1, stream);
cudaCheck(cudaEventRecord(zoneMaxZColNatom_copy_event, stream));
//
// Calculate positions in z columns
// NOTE: Clears col_natom and sets (col_patom, cell_patom, col_ncellz, d_NlistParam.ncell)
//
nthread = min(((ncol_tot-1)/tilesize+1)*tilesize, get_max_nthread());
shmem_size = nthread*sizeof(int2);
if (shmem_size > get_max_shmem_size()) {
std::cerr << "CudaNeighborListSort::sort_core, Device maximum reached: shmem_size="
<< shmem_size << std::endl;
exit(1);
}
calc_z_column_pos_kernel <<< 1, nthread, shmem_size, stream >>>
(tilesize, ncol_tot, atomStart, cellStart, col_xy_zone, col_natom, col_patom,
cell_patom+cellStart, col_ncellz+colStart, cell_xyz_zone+cellStart,
col_cell+colStart, d_NlistParam);
cudaCheck(cudaGetLastError());
// This copying is done to get value of ncell and col_max_natom
copy_DtoH<int>(&d_NlistParam->ncell, h_ncell, 1, stream);
cudaCheck(cudaEventRecord(ncell_copy_event, stream));
//
// Reorder atoms into z-columns
// NOTE: also sets up startcell_zone[izone]
//
nthread = 512;
nblock = (ncoord_tot-1)/nthread+1;
reorder_atoms_z_column_kernel<<< nblock, nthread, 0, stream >>>
(ncoord_tot, atom_icol, col_natom, col_patom,
xyzq+atomStart, xyzq_sorted+atomStart, ind_sorted+atomStart);
cudaCheck(cudaGetLastError());
// Test z columns
if (test) {
cudaCheck(cudaDeviceSynchronize());
test_z_columns(zone_patom, h_ZoneParam, xyzq+atomStart, xyzq_sorted+atomStart,
col_patom, ind_sorted+atomStart);
}
// Wait until zoneMaxZColNatom is received on host
cudaCheck(cudaEventSynchronize(zoneMaxZColNatom_copy_event));