-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparticles.cu
1789 lines (1422 loc) · 52.9 KB
/
particles.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 "particles.cuh"
#include <iostream>
#include <string>
#include "util.cuh"
#include "timer.cuh"
#include <cooperative_groups.h>
#include <cooperative_groups/reduce.h>
namespace cg=cooperative_groups;
__global__
void _init_tiles_kernel(
t_part_tiles const tiles,
unsigned int const max_np_tile ) {
const int i = blockIdx.y * gridDim.x + blockIdx.x;
tiles.offset[i] = i * max_np_tile;
tiles.np[i] = 0;
tiles.np2[i] = 0;
}
/**
* @brief Construct a new Particle Buffer:: Particle Buffer object
*
* @param ntiles Number of tiles (x,y)
* @param nx Tile size
* @param max_np_tile Maximum number of particles per tile
*/
__host__
Particles::Particles(uint2 const ntiles, uint2 const nx, unsigned int const max_np_tile ) :
ntiles( ntiles ), nx( nx ), max_np_tile( max_np_tile ), periodic( make_int2(1,1) )
{
size_t size = ntiles.x * ntiles.y * max_np_tile;
malloc_dev( data.ix, size );
malloc_dev( data.x, size );
malloc_dev( data.u, size );
malloc_dev( idx, size );
// Allocate tile information array on device
malloc_dev( tiles.np, ntiles.x * ntiles.y );
malloc_dev( tiles.offset, ntiles.x * ntiles.y );
malloc_dev( tiles.np2, ntiles.x * ntiles.y );
malloc_dev( tiles.offset2, ntiles.x * ntiles.y );
malloc_dev( tiles.nidx, ntiles.x * ntiles.y );
malloc_dev( tiles.npt, 9 * ntiles.x * ntiles.y );
// The buffer is initially empty
device::zero( tiles.np, ntiles.x * ntiles.y );
device::zero( tiles.offset, ntiles.x * ntiles.y );
// device::zero( tiles.offset2, ntiles.x * ntiles.y );
// device::zero( tiles.np2, ntiles.x * ntiles.y );
};
__global__
/**
* @brief CUDA Kernel for getting total number of particles
*
* Note that the kernel does not reset the output total value
*
* @param d_tiles Tile information
* @param ntiles total number of tiles
* @param total (out) total number of particles
*/
void _np_kernel(
int const * const __restrict__ d_tile_np,
unsigned int const ntiles, unsigned int * const __restrict__ total) {
auto group = cg::this_thread_block();
auto warp = cg::tiled_partition<32>(group);
unsigned int np = 0;
for( int i = group.thread_rank(); i < ntiles; i += group.num_threads() )
np += d_tile_np[i];
np = cg::reduce( warp, np, cg::plus<unsigned int>());
if ( warp.thread_rank() == 0 ) atomicAdd( total, np );
}
__host__
/**
* @brief Gets total number of particles on device
*
* @return unsigned long long Total number of particles
*/
unsigned int Particles::np() {
_dev_tmp_uint = 0;
auto size = ntiles.x*ntiles.y;
auto block = ( size < 1024 ) ? size : 1024 ;
auto grid = (size-1)/block + 1;
_np_kernel <<< grid, block >>> ( tiles.np, size, _dev_tmp_uint.ptr() );
return _dev_tmp_uint.get();
}
__global__
void _np_max_tile( int const * const __restrict__ d_tile_np,
unsigned int const ntiles, unsigned int * const __restrict__ max) {
auto group = cg::this_thread_block();
auto warp = cg::tiled_partition<32>(group);
unsigned int v = 0;
for( int i = group.thread_rank(); i < ntiles; i += group.num_threads() ) {
int tile_np = d_tile_np[i];
if ( tile_np > v ) v = tile_np;
}
v = cg::reduce( warp, v, cg::greater<unsigned int>());
if ( warp.thread_rank() == 0 ) atomicMax( max, v );
}
__host__
/**
* @brief Gets maximum number of particles per tile
*
* @return unsigned int
*/
unsigned int Particles::np_max_tile() {
_dev_tmp_uint = 0;
auto size = ntiles.x*ntiles.y;
auto block = ( size < 1024 ) ? size : 1024 ;
auto grid = (size-1)/block + 1;
_np_max_tile <<< grid, block >>> ( tiles.np, size, _dev_tmp_uint.ptr() );
return _dev_tmp_uint.get();
}
__global__
void _np_min_tile( int const * const __restrict__ d_tiles_np,
unsigned int const ntiles, unsigned int * const __restrict__ max) {
auto group = cg::this_thread_block();
auto warp = cg::tiled_partition<32>(group);
unsigned int v = 0;
for( int i = group.thread_rank(); i < ntiles; i += group.num_threads() ) {
int tile_np = d_tiles_np[i];
if ( tile_np > v ) v = tile_np;
}
v = cg::reduce( warp, v, cg::less<unsigned int>());
if ( warp.thread_rank() == 0 ) atomicMin( max, v );
}
__host__
/**
* @brief Gets minimum number of particles per tile
*
* @return unsigned int
*/
unsigned int Particles::np_min_tile() {
_dev_tmp_uint = 0;
auto size = ntiles.x*ntiles.y;
auto block = ( size < 1024 ) ? size : 1024 ;
auto grid = (size-1)/block + 1;
_np_max_tile <<< grid, block >>> ( tiles.np, size, _dev_tmp_uint.ptr() );
return _dev_tmp_uint.get();
}
__global__
void _np_exscan_kernel(
unsigned int * const __restrict__ idx,
int const * const __restrict__ d_tiles_np, unsigned int const ntiles,
unsigned int * const __restrict__ total) {
__shared__ unsigned int tmp[ 32 ];
__shared__ unsigned int prev;
auto block = cg::this_thread_block();
auto warp = cg::tiled_partition<32>(block);
prev = 0;
for( unsigned int i = block.thread_rank(); i < ntiles; i += block.num_threads() ) {
unsigned int s = d_tiles_np[i];
unsigned int v = cg::exclusive_scan( warp, s, cg::plus<unsigned int>());
if ( warp.thread_rank() == warp.num_threads() - 1 ) tmp[ warp.meta_group_rank() ] = v + s;
block.sync();
if ( warp.meta_group_rank() == 0 ) {
auto t = tmp[ warp.thread_rank() ];
t = cg::exclusive_scan( warp, t, cg::plus<unsigned int>());
tmp[ warp.thread_rank() ] = t + prev;
}
block.sync();
v += tmp[ warp.meta_group_rank() ];
idx[i] = v;
if ((block.thread_rank() == block.num_threads() - 1) || ( i + 1 == ntiles ) )
prev = v + s;
block.sync();
}
if ( block.thread_rank() == 0 ) *total = prev;
}
/**
* @brief Exclusive scan of number of particles per tile
*
* This is used for compacting operations
*
* @param d_offset Output array on device, must be of size ntiles.x * ntiles.y
* @return unsigned int Total number of particles
*/
unsigned int Particles::np_exscan( unsigned int * __restrict__ d_offset ) {
auto size = ntiles.x*ntiles.y;
auto block = ( size < 1024 ) ? size : 1024 ;
_dev_tmp_uint = 0;
_np_exscan_kernel <<< 1, block >>> ( d_offset, tiles.np, size, _dev_tmp_uint.ptr() );
return _dev_tmp_uint.get();
}
/**
* @brief CUDA kernel for gathering particle data
*
* @tparam quant Quantiy to gather
* @param d_ix Particle data (cells)
* @param d_x Particle data (positions)
* @param d_u Particle data (generalized velocity)
* @param d_tiles Particle tile information
* @param tile_nx Size of tile grid
* @param d_out_offset Output array offsets
* @param d_data Output data
*/
template < part::quant quant >
__global__
void _gather_quant(
t_part_data const data,
t_part_tiles const tiles,
uint2 const tile_nx,
unsigned int const * const __restrict__ d_out_offset,
float * const __restrict__ d_data )
{
const int tid = blockIdx.y * gridDim.x + blockIdx.x;
const int offset = tiles.offset[tid];
const int np = tiles.np[tid];
int2 __restrict__ const * const ix = &data.ix[ offset ];
float2 __restrict__ const * const x = &data.x[ offset ];
float3 __restrict__ const * const u = &data.u[ offset ];
unsigned int const out_offset = d_out_offset[ tid ];
for( int idx = threadIdx.x; idx < np; idx += blockDim.x ) {
float val;
if ( quant == part::x ) val = (blockIdx.x * tile_nx.x + ix[idx].x) + (0.5f + x[idx].x);
if ( quant == part::y ) val = (blockIdx.y * tile_nx.y + ix[idx].y) + (0.5f + x[idx].y);
if ( quant == part::ux ) val = u[idx].x;
if ( quant == part::uy ) val = u[idx].y;
if ( quant == part::uz ) val = u[idx].z;
d_data[ out_offset + idx ] = val;
}
};
__host__
/**
* @brief Gather data from a specific particle quantity in a device buffer
*
* @param quant Quantity to gather
* @param h_data Output data host buffer, assumed to have size >= np
* @param np Number of particles
* @param d_data_offset Data offset in output array for each tile
*/
void Particles::gather( part::quant quant, float * const __restrict__ h_data,
float * const __restrict__ d_data,
unsigned int const np, unsigned int const * const __restrict__ d_out_offset ) {
if ( np > 0 ) {
dim3 grid( ntiles.x, ntiles.y );
dim3 block( 1024 );
// Gather data on device
switch (quant) {
case part::x :
_gather_quant<part::x> <<<grid,block>>>( data, tiles, nx, d_out_offset, d_data );
break;
case part::y:
_gather_quant<part::y> <<<grid,block>>>( data, tiles, nx, d_out_offset, d_data );
break;
case part::ux:
_gather_quant<part::ux> <<<grid,block>>>( data, tiles, nx, d_out_offset, d_data );
break;
case part::uy:
_gather_quant<part::uy> <<<grid,block>>>( data, tiles, nx, d_out_offset, d_data );
break;
case part::uz:
_gather_quant<part::uz> <<<grid,block>>>( data, tiles, nx, d_out_offset, d_data );
break;
}
// Copy to host
devhost_memcpy( h_data, d_data, np );
}
}
__host__
/**
* @brief Gather data from a specific particle quantity in a device buffer
*
* This version will first do an exscan on the number of particles per tile to
* determine the data offset on the outout buffer for each tile and call the
* above version.
*
* @param quant Quantity to gather
* @param h_data Output data host buffer, assumed to have size >= np
*/
void Particles::gather( part::quant quant, float * const __restrict__ h_data ) {
unsigned int * d_out_offset;
malloc_dev( d_out_offset, ntiles.x * ntiles.y );
unsigned int np = np_exscan( d_out_offset );
if ( np > 0 ) {
float * d_data;
malloc_dev( d_data, np );
gather( quant, h_data, d_data, np, d_out_offset );
free_dev( d_data );
}
free_dev( d_out_offset );
}
__host__
/**
* @brief Save particle data to disk
*
* @param info Particle metadata (name, labels, units, etc.). Information is used to set file name
* @param iter Iteration metadata
* @param path Path where to save the file
*/
void Particles::save( zdf::part_info &info, zdf::iteration &iter, std::string path ) {
// Get number of particles and data offsets
unsigned int * d_out_offset;
malloc_dev( d_out_offset, ntiles.x * ntiles.y );
unsigned int np = np_exscan( d_out_offset );
info.np = np;
// Open file
zdf::file part_file;
zdf::open_part_file( part_file, info, iter, path+"/"+info.name );
// Gather and save each quantity
float *h_data = nullptr, *d_data = nullptr;
if( np > 0 ) {
malloc_host( h_data, np );
malloc_dev( d_data, np );
}
gather( part::quant::x, h_data, d_data, np, d_out_offset );
zdf::add_quant_part_file( part_file, "x", h_data, np );
gather( part::quant::y, h_data, d_data, np, d_out_offset );
zdf::add_quant_part_file( part_file, "y", h_data, np );
gather( part::quant::ux, h_data, d_data, np, d_out_offset );
zdf::add_quant_part_file( part_file, "ux", h_data, np );
gather( part::quant::uy, h_data, d_data, np, d_out_offset );
zdf::add_quant_part_file( part_file, "uy", h_data, np );
gather( part::quant::uz, h_data, d_data, np, d_out_offset );
zdf::add_quant_part_file( part_file, "uz", h_data, np );
// Close the file
zdf::close_file( part_file );
// Cleanup
if ( np > 0 ) {
free_dev( d_data );
free_host( h_data );
}
free_dev( d_out_offset );
}
__host__
/**
* @brief Moves particles to the correct tiles
*
* Note that particles are only expected to have moved no more than 1 tile
* in each direction
*
*/
void Particles::tile_sort() {
// Create temporary buffer
Particles tmp( ntiles, nx, max_np_tile );
tile_sort( tmp, false );
}
/**
* Low memory sorter - mk 2
*
*/
__global__
/**
* @brief CUDA Kernel for checking which particles have left the tile
* and determine new number of particles per tile.
*
* This kernel expects that new_tiles.np has been zeroed before being
* called.
*
* Outputs:
* 1. new_tiles.np : new number of particles per tile after sort
* 2. tiles.nidx : total number of particles exiting the tile
* 3. tiles.npt : number of particles going into each direction
* 3. d_idx : indices of particles leaving each tile
*
* @param lim Tile size
* @param tiles Tile structure data
* @param data Particle data
* @param new_tiles (out) New tile structure data
* @param d_idx (out) Indexes of particles leaving tile
*/
void _mk2_bnd_check( int2 const lim,
t_part_tiles const tiles, t_part_data const data,
t_part_tiles const new_tiles, int * __restrict__ d_idx )
{
auto block = cg::this_thread_block();
auto warp = cg::tiled_partition<32>(block);
const int tid = blockIdx.y * gridDim.x + blockIdx.x;
unsigned int const np = tiles.np[ tid ];
unsigned int const offset = tiles.offset[ tid ];
int2 * __restrict__ ix = &data.ix[ offset ];
int * __restrict__ idx = &d_idx[ offset ];
__shared__ int _npt[9];
for( int i = block.thread_rank(); i < 9; i+= block.num_threads() )
_npt[i] = 0;
__shared__ int _nout;
_nout = 0;
block.sync();
// Count particles according to their motion
// Store indices of particles leaving tile
for( int i = block.thread_rank(); i < np; i+= block.num_threads() ) {
int2 ipos = ix[i];
int xcross = ( ipos.x >= lim.x ) - ( ipos.x < 0 );
int ycross = ( ipos.y >= lim.y ) - ( ipos.y < 0 );
if ( xcross || ycross ) {
atomicAdd( &_npt[ (ycross+1) * 3 + (xcross+1) ], 1 );
idx[ atomicAdd( &_nout, 1 ) ] = i;
}
}
block.sync();
if ( block.thread_rank() == 0 ) {
_npt[4] = np - _nout;
}
block.sync();
for( int i = block.thread_rank(); i < 9; i+= block.num_threads() ) {
// Find target node
int tx = blockIdx.x + i % 3 - 1;
int ty = blockIdx.y + i / 3 - 1;
// Correct for periodic boundaries
if ( tx < 0 ) tx += gridDim.x;
if ( tx >= gridDim.x ) tx -= gridDim.x;
if ( ty < 0 ) ty += gridDim.y;
if ( ty >= gridDim.y ) ty -= gridDim.y;
atomicAdd( & new_tiles.np[ ty * gridDim.x + tx ], _npt[i] );
}
if ( block.thread_rank() == 0 ) {
for( int i = 0; i < 9; i++ ) tiles.npt[ 9*tid + i ] = _npt[i];
tiles.nidx[ tid ] = _nout;
}
}
__global__
/**
* @brief CUDA kernel for recalculating particle tile offset
*
* Inputs:
* 1. tiles.np values
*
* Outputs:
* 1. tiles.offset new values (prefix scan of tiles.np)
* 2. tiles.np2 is also set to offset
*
* @param tiles Tile structure data
* @param ntiles Total number of tiles
*/
void _mk2_update_offset( t_part_tiles const tiles, const unsigned int ntiles ) {
// 32 is the current maximum number of warps
__shared__ int tmp[ 32 ];
__shared__ int prev;
auto block = cg::this_thread_block();
auto warp = cg::tiled_partition<32>(block);
// Contribution from previous warp
prev = 0;
for( unsigned int i = block.thread_rank(); i < ntiles; i += block.num_threads() ) {
auto s = tiles.np[i];
auto v = cg::exclusive_scan( warp, s, cg::plus<int>());
if ( warp.thread_rank() == warp.num_threads() - 1 ) tmp[ warp.meta_group_rank() ] = v + s;
block.sync();
// Only 1 warp does this
if ( warp.meta_group_rank() == 0 ) {
auto t = tmp[ warp.thread_rank() ];
t = cg::exclusive_scan( warp, t, cg::plus<int>());
tmp[ warp.thread_rank() ] = t + prev;
}
block.sync();
// Add in contribution from previous threads
v += tmp[ warp.meta_group_rank() ];
tiles.offset[i] = v;
// Also store offset on np2
tiles.np2[i] = v;
if ((block.thread_rank() == block.num_threads() - 1) || ( i + 1 == ntiles ) )
prev = v + s;
block.sync();
}
}
__global__
void _mk2_copy_sort( int2 const lim,
t_part_tiles const tiles, t_part_data const data, int * __restrict__ d_idx,
t_part_tiles const new_tiles, t_part_data const new_data )
{
auto block = cg::this_thread_block();
auto warp = cg::tiled_partition<32>(block);
const int tid = blockIdx.y * gridDim.x + blockIdx.x;
unsigned int const offset = tiles.offset[ tid ];
int* __restrict__ npt = &tiles.npt[ 9*tid ];
int2 * __restrict__ ix = &data.ix[ offset ];
float2 * __restrict__ x = &data.x[ offset ];
float3 * __restrict__ u = &data.u[ offset ];
int * __restrict__ idx = &d_idx[ offset ];
unsigned int const nidx = tiles.nidx[ tid ];
int2* __restrict__ new_ix = new_data.ix;
float2* __restrict__ new_x = new_data.x;
float3* __restrict__ new_u = new_data.u;
__shared__ int _dir_offset[9];
__shared__ int _c;
// Find offsets on new buffer
for( int i = block.thread_rank(); i < 9; i+= block.num_threads() ) {
// Find target node
int tx = blockIdx.x + i % 3 - 1;
int ty = blockIdx.y + i / 3 - 1;
// Correct for periodic boundaries
if ( tx < 0 ) tx += gridDim.x;
if ( tx >= gridDim.x ) tx -= gridDim.x;
if ( ty < 0 ) ty += gridDim.y;
if ( ty >= gridDim.y ) ty -= gridDim.y;
int tid2 = ty * gridDim.x + tx;
_dir_offset[i] = atomicAdd( & new_tiles.np2[ tid2 ], npt[ i ] );
}
const int n0 = npt[4];
_c = n0;
block.sync();
for( int i = block.thread_rank(); i < nidx; i+= block.num_threads() ) {
int k = idx[i];
int2 nix = ix[k];
float2 nx = x[k];
float3 nu = u[k];
int xcross = ( nix.x >= lim.x ) - ( nix.x < 0 );
int ycross = ( nix.y >= lim.y ) - ( nix.y < 0 );
nix.x -= xcross * lim.x;
nix.y -= ycross * lim.y;
int l = atomicAdd( & _dir_offset[(ycross + 1) * 3 + (xcross + 1)], 1 );
new_ix[ l ] = nix;
new_x[ l ] = nx;
new_u[ l ] = nu;
// Fill hole if needed
if ( k < n0 ) {
int c, invalid;
do {
c = atomicAdd( &_c, 1 );
invalid = ( ix[c].x < 0 ) || ( ix[c].x >= lim.x) ||
( ix[c].y < 0 ) || ( ix[c].y >= lim.y);
} while (invalid);
ix[ k ] = ix[ c ];
x [ k ] = x [ c ];
u [ k ] = u [ c ];
}
}
const int start = _dir_offset[4];
block.sync();
for( int i = block.thread_rank(); i < n0; i+= block.num_threads() ) {
new_ix[ start + i ] = ix[i];
}
for( int i = block.thread_rank(); i < n0; i+= block.num_threads() ) {
new_x[ start + i ] = x[i];
}
// Since float3 does not give coallesced access we copy the u data as float
float * __restrict__ u0 = (float *) u;
float * __restrict__ u1 = (float *) &new_u[ start ];
for( int i = block.thread_rank(); i < 3 * n0; i+= block.num_threads() ) {
u1[i] = u0[i];
}
}
__host__
void Particles::tile_sort_mk2( Particles &tmp ) {
dim3 grid( ntiles.x, ntiles.y );
dim3 block( 1024 );
int2 lim;
lim.x = nx.x;
lim.y = nx.y;
// Get new number of particles per tile
device::zero( tmp.tiles.np, ntiles.x * ntiles.y );
_mk2_bnd_check<<< grid, block >>> (
lim, tiles, data, tmp.tiles, idx
);
// Get new offsets (prefix scan of np)
_mk2_update_offset<<< 1, 1024 >>> (
tmp.tiles, ntiles.x * ntiles.y
);
_mk2_copy_sort <<< grid, block >>> (
lim, tiles, data, idx,
tmp.tiles, tmp.data
);
// Swap pointers with tmp. class
swap( data.ix, tmp.data.ix );
swap( data.x, tmp.data.x );
swap( data.u, tmp.data.u );
swap( tiles.np, tmp.tiles.np );
swap( tiles.offset, tmp.tiles.offset );
// validate( "After tile_sort_lowmem ");
}
/**
* Low memory sorter - mk 3
*
*/
__global__
/**
* @brief CUDA Kernel for checking which particles have left the tile
* and determine new number of particles per tile.
*
* This kernel expects that new_tiles.np has been zeroed before being
* called.
*
* Outputs:
* 1. new_tiles.np : new number of particles per tile after sort
* 2. tiles.nidx : total number of particles exiting the tile
* 3. tiles.npt : number of particles going into each direction
* 3. d_idx : indices of particles leaving each tile
*
* @param lim Tile size
* @param tiles Tile structure data
* @param data Particle data
* @param new_tiles (out) New tile structure data
* @param d_idx (out) Indexes of particles leaving tile
*/
void _mk3_bnd_check( int2 const lim,
t_part_tiles const tiles, t_part_data const data,
t_part_tiles const new_tiles, int * __restrict__ d_idx,
int2 const periodic )
{
auto block = cg::this_thread_block();
auto warp = cg::tiled_partition<32>(block);
const int tid = blockIdx.y * gridDim.x + blockIdx.x;
unsigned int const np = tiles.np[ tid ];
unsigned int const offset = tiles.offset[ tid ];
int * __restrict__ npt = &tiles.npt[ 9*tid ];
int2 * __restrict__ ix = &data.ix[ offset ];
int * __restrict__ idx = &d_idx[ offset ];
// Number of particles moving in each direction
__shared__ int _npt[9];
for( int i = block.thread_rank(); i < 9; i+= block.num_threads() )
_npt[i] = 0;
__shared__ int _nout;
_nout = 0;
block.sync();
// Count particles according to their motion
// Store indices of particles leaving tile
for( int i = block.thread_rank(); i < np; i+= block.num_threads() ) {
int2 ipos = ix[i];
int xcross = ( ipos.x >= lim.x ) - ( ipos.x < 0 );
int ycross = ( ipos.y >= lim.y ) - ( ipos.y < 0 );
if ( xcross || ycross ) {
atomicAdd( &_npt[ (ycross+1) * 3 + (xcross+1) ], 1 );
idx[ atomicAdd( &_nout, 1 ) ] = i;
}
}
block.sync();
if ( block.thread_rank() == 0 ) {
_npt[4] = np - _nout;
}
block.sync();
for( int i = block.thread_rank(); i < 9; i+= block.num_threads() ) {
// Find target node
int tx = blockIdx.x + i % 3 - 1;
int ty = blockIdx.y + i / 3 - 1;
// Correct for periodic boundaries
if ( periodic.x ) {
if ( tx < 0 ) tx += gridDim.x;
if ( tx >= gridDim.x ) tx -= gridDim.x;
}
if ( periodic.y ) {
if ( ty < 0 ) ty += gridDim.y;
if ( ty >= gridDim.y ) ty -= gridDim.y;
}
if ( ( tx >= 0 ) && ( tx < gridDim.x ) &&
( ty >= 0 ) && ( ty < gridDim.y ) ) {
int tid2 = ty * gridDim.x + tx;
atomicAdd( & new_tiles.np[ tid2 ], _npt[i] );
}
}
if ( block.thread_rank() == 0 ) {
for( int i = 0; i < 9; i++ ) npt[ i ] = _npt[i];
tiles.nidx[ tid ] = _nout;
}
}
__global__
/**
* @brief CUDA kernel for recalculating particle tile offset
*
* Inputs:
* 1. tiles.np values
*
* Outputs:
* 1. tiles.offset new values (prefix scan of tiles.np)
* 2. tiles.offset2 is also set to offset
*
* @param tiles Tile structure data
* @param ntiles Total number of tiles
*/
void _mk3_update_offset( t_part_tiles const tiles, const unsigned int ntiles ) {
// 32 is the current maximum number of warps
__shared__ int tmp[ 32 ];
__shared__ int prev;
auto block = cg::this_thread_block();
auto warp = cg::tiled_partition<32>(block);
// Contribution from previous warp
prev = 0;
for( unsigned int i = block.thread_rank(); i < ntiles; i += block.num_threads() ) {
auto s = tiles.np[i];
auto v = cg::exclusive_scan( warp, s, cg::plus<int>());
if ( warp.thread_rank() == warp.num_threads() - 1 ) tmp[ warp.meta_group_rank() ] = v + s;
block.sync();
// Only 1 warp does this
if ( warp.meta_group_rank() == 0 ) {
auto t = tmp[ warp.thread_rank() ];
t = cg::exclusive_scan( warp, t, cg::plus<int>());
tmp[ warp.thread_rank() ] = t + prev;
}
block.sync();
// Add in contribution from previous threads
v += tmp[ warp.meta_group_rank() ];
tiles.offset[i] = v;
// Also store offset on np2
tiles.offset2[i] = v;
if ((block.thread_rank() == block.num_threads() - 1) || ( i + 1 == ntiles ) )
prev = v + s;
block.sync();
}
}
__global__
/**
* @brief CUDA kernel for recalculating particle tile offset
*
* Inputs:
* 1. tiles.np values
* 2. tiles.np2 values
*
* Outputs:
* 1. tiles.offset new values (prefix scan of tiles.np + tiles.np2)
* 2. tiles.offset2 is also set to offset
*
* @param tiles Tile structure data
* @param ntiles Total number of tiles
*/
void _mk3_update_offset_np2( t_part_tiles const tiles, const unsigned int ntiles ) {
// 32 is the current maximum number of warps
__shared__ int tmp[ 32 ];
__shared__ int prev;
auto block = cg::this_thread_block();
auto warp = cg::tiled_partition<32>(block);
// Contribution from previous warp
prev = 0;
for( unsigned int i = block.thread_rank(); i < ntiles; i += block.num_threads() ) {
auto s = tiles.np[i] + tiles.np2[i];
auto v = cg::exclusive_scan( warp, s, cg::plus<int>());
if ( warp.thread_rank() == warp.num_threads() - 1 ) tmp[ warp.meta_group_rank() ] = v + s;
block.sync();
// Only 1 warp does this
if ( warp.meta_group_rank() == 0 ) {
auto t = tmp[ warp.thread_rank() ];
t = cg::exclusive_scan( warp, t, cg::plus<int>());
tmp[ warp.thread_rank() ] = t + prev;
}
block.sync();
// Add in contribution from previous threads
v += tmp[ warp.meta_group_rank() ];
tiles.offset[i] = v;
// Also store offset on offset2
tiles.offset2[i] = v;
if ((block.thread_rank() == block.num_threads() - 1) || ( i + 1 == ntiles ) )
prev = v + s;
block.sync();
}
// Store total number of particles
// if ( block.thread_rank() == 0 ) *reduction = prev;
}
__global__
/**
* @brief CUDA kernel for copying particles to temp. buffer
*
* @param lim Tile size
* @param tiles Tile structure data
* @param data Particle data
* @param d_idx Indices of particles leaving the node
* @param tmp_tiles Temporary tile structure data
* @param tmp_data Temporary particle data
*/
void _mk3_copy_out( int2 const lim,
t_part_tiles const tiles, t_part_data const data, int * __restrict__ d_idx,
t_part_tiles const tmp_tiles, t_part_data const tmp_data,
int2 const periodic )
{
auto block = cg::this_thread_block();
auto warp = cg::tiled_partition<32>(block);
const int tid = blockIdx.y * gridDim.x + blockIdx.x;
int const old_offset = tiles.offset[ tid ];
int * __restrict__ npt = &tiles.npt[ 9*tid ];
int2 * __restrict__ ix = &data.ix[ old_offset ];
float2 * __restrict__ x = &data.x[ old_offset ];
float3 * __restrict__ u = &data.u[ old_offset ];
int * __restrict__ idx = &d_idx[ old_offset ];
unsigned int const nidx = tiles.nidx[ tid ];
int const new_offset = tmp_tiles.offset[ tid ];
int const new_np = tmp_tiles.np[ tid ];
__shared__ int _dir_offset[9];
// The _dir_offset variables hold the offset for each of the 9 target
// tiles so the tmp_* variables just point to the beggining of the buffers
int2* __restrict__ tmp_ix = tmp_data.ix;
float2* __restrict__ tmp_x = tmp_data.x;
float3* __restrict__ tmp_u = tmp_data.u;
// Number of particles staying in tile
const int n0 = npt[4];
// Number of particles staying in the tile that need to be copied to temp memory
// because tile position in memory has shifted
int nshift;
if ( new_offset >= old_offset ) {
// Buffer has shifted right, copy particles left behind to end of buffer
nshift = new_offset - old_offset;
} else {
// Buffer has shifted left, attempt to fill initial space with particles
// coming from other tiles, use additional particles from end of buffer
// if needed
nshift = (old_offset + n0) - (new_offset + new_np);
if ( nshift < 0 ) nshift = 0;
}
// At most n0 particles will be shifted
if ( nshift > n0 ) nshift = n0;
// Reserve space in the tmp array
if( block.thread_rank() == 0 ) {
_dir_offset[4] = atomicAdd( & tmp_tiles.offset2[ tid ], nshift );
}
block.sync();
// Find offsets on new buffer
for( int i = block.thread_rank(); i < 9; i+= block.num_threads() ) {
if ( i != 4 ) {
// Find target node
int tx = blockIdx.x + i % 3 - 1;
int ty = blockIdx.y + i / 3 - 1;
bool valid = true;
// Correct for periodic boundaries
if ( periodic.x ) {
if ( tx < 0 ) tx += gridDim.x;
if ( tx >= gridDim.x ) tx -= gridDim.x;
} else {