-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticles.cpp
1322 lines (1059 loc) · 44.7 KB
/
particles.cpp
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.h"
#include <iostream>
#include <string>
#include <cmath>
#include "timer.h"
/**
* @brief Gather particle data
*
* @tparam quant Quantiy to gather
* @param part Particle data
* @param d_data Output data
*/
template < part::quant quant >
void gather_quant(
ParticleData part,
float * const __restrict__ d_data,
sycl::queue & q )
{
auto tile_nx = part.nx;
auto ntiles = part.ntiles;
// 8×1 work items per group
sycl::range<2> local{ 8, 1 };
// ntiles.x × ntiles.y groups
sycl::range<2> global{ ntiles.x, ntiles.y };
q.submit([&](sycl::handler &h) {
h.parallel_for(
sycl::nd_range{ global * local, local },
[=](sycl::nd_item<2> it) {
const int2 tile_idx = make_int2( it.get_group(0), it.get_group(1));
const int tile_id = tile_idx.y * part.ntiles.x + tile_idx.x;
const auto tile_off = part.offset[ tile_id ];
const auto tile_np = part.np[ tile_id ];
int2 * const __restrict__ ix = & part.ix[ tile_off ];
float2 const * __restrict__ const x = & part.x[ tile_off ];
float3 const * __restrict__ const u = & part.u[ tile_off ];
for( int idx = it.get_local_id(0); idx < tile_np; idx += it.get_local_range(0) ) {
float val;
if ( quant == part::x ) val = (it.get_group(0) * tile_nx.x + ix[idx].x) + (0.5f + x[idx].x);
if ( quant == part::y ) val = (it.get_group(1) * 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[ tile_off + idx ] = val;
}
});
});
q.wait();
};
/**
* @brief Gather data from a specific particle quantity in a device buffer
*
* @param quant Quantity to gather
* @param d_data Output data buffer, assumed to have size >= np
*/
void Particles::gather( part::quant quant, float * const __restrict__ d_data )
{
// Gather data on device
switch (quant) {
case part::x :
gather_quant<part::x>( *this, d_data, queue );
break;
case part::y:
gather_quant<part::y>( *this, d_data, queue );
break;
case part::ux:
gather_quant<part::ux>( *this, d_data, queue );
break;
case part::uy:
gather_quant<part::uy>( *this, d_data, queue );
break;
case part::uz:
gather_quant<part::uz>( *this, d_data, queue );
break;
}
}
/**
* @brief Gather particle data, scaling values
*
* @note Data (val) will be returned as `scale.x * val + scale.y`
*
* @tparam quant Quantiy to gather
* @param part Particle data
* @param d_data Scaled output data
* @param scale Scale factor for data
*/
template < part::quant quant >
void gather_quant(
ParticleData part,
float * const __restrict__ d_data,
const float2 scale,
sycl::queue & q )
{
auto tile_nx = part.nx;
// 8×1 work items per group
sycl::range<2> local{ 8, 1 };
// ntiles.x × ntiles.y groups
sycl::range<2> global{ part.ntiles.x, part.ntiles.y };
q.submit([&](sycl::handler &h) {
h.parallel_for(
sycl::nd_range{ global * local, local },
[=](sycl::nd_item<2> it) {
const int2 tile_idx = make_int2( it.get_group(0), it.get_group(1));
const int tile_id = tile_idx.y * part.ntiles.x + tile_idx.x;
const auto tile_off = part.offset[ tile_id ];
const auto tile_np = part.np[ tile_id ];
int2 * const __restrict__ ix = &part.ix[ tile_off ];
float2 const * __restrict__ const x = &part.x[ tile_off ];
float3 const * __restrict__ const u = &part.u[ tile_off ];
for( int idx = it.get_local_id(0); idx < tile_np; idx += it.get_local_range(0) ) {
float val;
if ( quant == part::x ) val = (it.get_group(0) * tile_nx.x + ix[idx].x) + (0.5f + x[idx].x);
if ( quant == part::y ) val = (it.get_group(1) * 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[ tile_off + idx ] = ops::fma( scale.x, val, scale.y );
}
});
});
q.wait();
}
/**
* @brief Gather data from a specific particle quantity in a device buffer, scaling values
*
* @param quant Quantity to gather
* @param d_data Output data buffer, assumed to have size >= np
* @param scale Scale factor for data
*/
void Particles::gather( part::quant quant, float * const __restrict__ d_data, const float2 scale )
{
// Gather data on device
switch (quant) {
case part::x :
gather_quant<part::x>( *this, d_data, scale, queue );
break;
case part::y:
gather_quant<part::y>( *this, d_data, scale, queue );
break;
case part::ux:
gather_quant<part::ux>( *this, d_data, scale, queue );
break;
case part::uy:
gather_quant<part::uy>( *this, d_data, scale, queue );
break;
case part::uz:
gather_quant<part::uz>( *this, d_data, scale, queue );
break;
}
}
/**
* @brief Save particle data to disk
*
* @param metadata 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 &metadata, zdf::iteration &iter, std::string path ) {
uint32_t np = np_total();
metadata.np = np;
// Open file
zdf::file part_file;
zdf::open_part_file( part_file, metadata, iter, path+"/"+metadata.name );
// Gather and save each quantity
float *d_data = nullptr;
float *h_data = nullptr;
if( np > 0 ) {
d_data = device::malloc<float>( np, queue );
h_data = host::malloc<float>( np, queue );
}
if ( np > 0 ) {
gather( part::quant::x, d_data );
device::memcpy_tohost( h_data, d_data, np, queue );
}
zdf::add_quant_part_file( part_file, "x", h_data, np );
if ( np > 0 ) {
gather( part::quant::y, d_data );
device::memcpy_tohost( h_data, d_data, np, queue );
}
zdf::add_quant_part_file( part_file, "y", h_data, np );
if ( np > 0 ) {
gather( part::quant::ux, d_data );
device::memcpy_tohost( h_data, d_data, np, queue );
}
zdf::add_quant_part_file( part_file, "ux", h_data, np );
if ( np > 0 ) {
gather( part::quant::uy, d_data );
device::memcpy_tohost( h_data, d_data, np, queue );
}
zdf::add_quant_part_file( part_file, "uy", h_data, np );
if ( np > 0 ) {
gather( part::quant::uz, d_data );
device::memcpy_tohost( h_data, d_data, np, queue );
}
zdf::add_quant_part_file( part_file, "uz", h_data, np );
// Close the file
zdf::close_file( part_file );
// Cleanup
if ( np > 0 ) {
device::free( d_data, queue );
host::free( h_data, queue );
}
}
/**
* @brief Check which particles have left the tile and determine new number
* of particles per tile.
*
* @warning This kernel expects that sort.new_np has been zeroed before being
* called.
*
* @param part (in) Particle data
* @param sort (out) Sort data (new number of particles per tile, indices
* particles leaving the tile, etc.)
* @param periodic (in) Correct for periodic boundaries
*/
void bnd_check(
ParticleData part, ParticleSortData sort,
int2 const periodic,
sycl::queue & q )
{
int2 ntiles = make_int2( part.ntiles.x, part.ntiles.y );
int2 lim = make_int2( part.nx.x, part.nx.y );
// 256×1 work items per group
sycl::range<2> local{ 256, 1 };
// ntiles.x × ntiles.y groups
sycl::range<2> global{ part.ntiles.x, part.ntiles.y };
q.submit([&](sycl::handler &h) {
/// @brief [shared] Number of particles moving in each direction
auto _npt = sycl::local_accessor< int, 1 > ( 9, h );
/// @brief [shared] Number of particle leaving tile
auto _nout = sycl::local_accessor< int, 1 > ( 1, h );
h.parallel_for(
sycl::nd_range{ global * local, local },
[=](sycl::nd_item<2> it) {
const int2 tile_idx = make_int2( it.get_group(0), it.get_group(1));
const int tile_id = tile_idx.y * ntiles.x + tile_idx.x;
const auto offset = part.offset[ tile_id ];
const auto np = part.np[ tile_id ];
int2 * __restrict__ ix = &part.ix[ offset ];
/// @brief Indices of particles leaving tile
int * __restrict__ idx = &sort.idx[ offset ];
for( auto i = 0; i < 9; ++i ) _npt[i] = 0;
_nout[0] = 0;
it.barrier();
// Count particles according to their motion
// Store indices of particles leaving tile
for( int i = it.get_local_id(0); i < np; i += it.get_local_range(0) ) {
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 ) {
device::local::atomicAdd( &_npt[ (ycross+1) * 3 + (xcross+1) ], 1 );
idx[ device::local::atomicAdd( &_nout[0], 1 ) ] = i;
}
}
it.barrier();
if (it.get_local_id(0) == 0 ){
// Particles remaining on the tile
_npt[4] = np - _nout[0];
// Store number of particles leaving tile
sort.nidx[ tile_id ] = _nout[0];
}
it.barrier();
for( int i = it.get_local_id(0); i < 9; i += it.get_local_range(0) ) {
// Store number of particles leaving tile in each direction
sort.npt[ 9*tile_id + i ] = _npt[i];
// Add number of particles to target neighboring node
// Find target node
int target_tx = tile_idx.x + ( i % 3 - 1 );
int target_ty = tile_idx.y + ( i / 3 - 1 );
// Correct for periodic boundaries
if ( periodic.x ) {
if ( target_tx < 0 ) target_tx += ntiles.x;
if ( target_tx >= ntiles.x ) target_tx -= ntiles.x;
}
if ( periodic.y ) {
if ( target_ty < 0 ) target_ty += ntiles.y;
if ( target_ty >= ntiles.y ) target_ty -= ntiles.y;
}
if ( ( target_tx >= 0 ) && ( target_tx < ntiles.x ) &&
( target_ty >= 0 ) && ( target_ty < ntiles.y ) ) {
int target_tid = target_ty * ntiles.x + target_tx;
device::global::atomicAdd( & sort.new_np[ target_tid ], _npt[i] );
}
}
});
});
q.wait();
}
/**
* @brief Recalculates particle tile offset
*
* @note The number of particles in each tile is set to 0
*
* @param tmp (out) Particle buffer
* @param new_np (in) New number of particles per tile.
*/
void update_tile_info(
ParticleData tmp,
const int * __restrict__ new_np,
sycl::queue & q )
{
const int ntiles = tmp.ntiles.x * tmp.ntiles.y;
const int max_num_sub_groups = q.get_device().get_info<sycl::info::device::max_num_sub_groups>();
unsigned group_size = ( ntiles < 256 ) ? ntiles : 256;
sycl::range<1> local{ group_size };
q.submit([&](sycl::handler &h) {
/// @brief [shared] Sum of previous group
auto group_prev = sycl::local_accessor< int, 1 > ( 1, h );
/// @brief [shared] Sum of previous sub-group
auto sg_prev = sycl::local_accessor< int, 1 > ( 1, h );
/// @brief [shared] Temporary results from each sub group
auto _tmp = sycl::local_accessor< int, 1 > ( max_num_sub_groups, h );
h.parallel_for(
sycl::nd_range{ local, local },
[=](sycl::nd_item<1> it) {
group_prev[0] = 0;
auto sg = it.get_sub_group();
for( int i = it.get_local_id(0); i < ntiles; i += it.get_local_range(0) ) {
auto s = new_np[i];
auto v = device::subgroup::exscan_add( sg, s );
if ( sg.get_local_id() == sg.get_local_range() - 1 )
_tmp[ sg.get_group_linear_id() ] = v + s;
it.barrier();
// Only 1 sub-group does this
if ( sg.get_group_linear_id() == 0 ) {
sg_prev[0] = group_prev[0];
for( auto j = 0; j < sg.get_group_linear_range(); j += sg.get_local_linear_range() ) {
int t = _tmp[ j + sg.get_local_id() ];
int e = device::subgroup::exscan_add( sg, t ) + sg_prev[0];
_tmp[ j + sg.get_local_id() ] = e;
if ( sg.get_local_id() == sg.get_local_linear_range() - 1 )
sg_prev[0] = e + t;
}
}
it.barrier();
// Add in contribution from previous threads
v += _tmp[ sg.get_group_linear_id() ];
tmp.offset[i] = v;
tmp.np[i] = 0;
if ( it.get_local_id(0) == it.get_local_range(0)-1 ) {
group_prev[0] = v+s;
}
it.barrier();
}
});
});
q.wait();
}
/**
* @brief Recalculates particle tile offset, leaving room for additional particles
*
* @note The number of particles in each tile is set to 0
*
* @param tmp (out) Particle buffer
* @param new_np (in/out) New number of particles per tile. Set to 0 after calculation.
* @param extra (in) Additional incoming particles
* @return uint32_t (out) Total number of particles (including additional ones)
*/
uint32_t update_tile_info(
ParticleData tmp,
const int * __restrict__ new_np,
const int * __restrict__ extra,
device::Var<uint32_t> tmp_dev_np,
sycl::queue & q )
{
const int ntiles = tmp.ntiles.x * tmp.ntiles.y;
const int max_num_sub_groups = q.get_device().get_info<sycl::info::device::max_num_sub_groups>();
unsigned group_size = ( ntiles < 256 ) ? ntiles : 256;
sycl::range<1> local{ group_size };
q.submit([&](sycl::handler &h) {
auto dev_np = tmp_dev_np.ptr();
/// @brief [shared] Sum of previous group
auto _prev = sycl::local_accessor< int, 1 > ( 1, h );
/// @brief [shared] Temporary results from each sub group
auto _tmp = sycl::local_accessor< int, 1 > ( max_num_sub_groups, h );
h.parallel_for(
sycl::nd_range{ local, local },
[=](sycl::nd_item<1> it) {
_prev[0] = 0;
auto sg = it.get_sub_group();
for( int i = it.get_local_id(0); i < ntiles; i += it.get_local_range(0) ) {
auto s = new_np[i] + extra[i];
auto v = device::subgroup::exscan_add( sg, s );
if ( sg.get_local_id() == sg.get_local_range() - 1 )
_tmp[ sg.get_group_linear_id() ] = v + s;
it.barrier();
// Only 1 warp does this
if ( sg.get_group_linear_id() == 0 ) {
auto t = _tmp[ sg.get_local_id() ];
t = device::subgroup::exscan_add(sg, t);
_tmp[ sg.get_local_id() ] = t + _prev[0];
}
it.barrier();
// Add in contribution from previous threads
v += _tmp[ sg.get_group_linear_id() ];
tmp.offset[i] = v;
tmp.np[i] = 0;
if (( it.get_local_id(0) == it.get_local_range(0)-1 ) || (i+1 == ntiles)) {
_prev[0] = v+s;
}
it.barrier();
}
if ( it.get_global_id(0) == 0 ) {
dev_np[0] = _prev[0];
}
});
});
q.wait();
return tmp_dev_np.get();
}
/**
* @brief Copy outgoing particles to temporary buffer
*
* @note Particles leaving the tile are copied to a temporary particle buffer
* into the tile that will hold the data after the sort and that is
* currently empty.
*
* If particles are copyed from the middle of the buffer, a particle will
* be copied from the end of the buffer to fill the hole.
*
* If the tile data position/limits in the main buffer will change,
* particles that stay in the tile but are now in invalid positions will
* be shifted.
*
* @param part Particle data
* @param tmp Temporary particle buffer (has new offsets)
* @param sort Sort data (new number of particles per tile, indices of
* particles leaving the tile, etc.)
* @param periodic Correct for periodic boundaries
*/
void copy_out(
ParticleData part,
ParticleData tmp,
const ParticleSortData sort,
const int2 periodic,
sycl::queue & q )
{
const int2 ntiles = make_int2( part.ntiles.x, part.ntiles.y );
const int2 lim = make_int2( part.nx.x, part.nx.y );
// 8×1 work items per group
sycl::range<2> local{ 256, 1 };
// ntiles.x × ntiles.y groups
sycl::range<2> global{ part.ntiles.x, part.ntiles.y };
q.submit([&](sycl::handler &h) {
/// @brief [shared] offsets in target buffer
auto _dir_offset = sycl::local_accessor< int, 1 > ( 9, h );
/// @brief [shared] index of particle used to fill hole
auto _c = sycl::local_accessor< int, 1 > ( 1, h );
h.parallel_for(
sycl::nd_range{ global * local, local },
[=](sycl::nd_item<2> it) {
const int2 tile_idx = make_int2( it.get_group(0), it.get_group(1) );
const int tile_id = tile_idx.y * part.ntiles.x + tile_idx.x;
int const old_offset = part.offset[ tile_id ];
int * __restrict__ npt = &sort.npt[ 9*tile_id ];
int2 * __restrict__ ix = &part.ix[ old_offset ];
float2 * __restrict__ x = &part.x[ old_offset ];
float3 * __restrict__ u = &part.u[ old_offset ];
int * __restrict__ idx = &sort.idx[ old_offset ];
uint32_t const nidx = sort.nidx[ tile_id ];
int const new_offset = tmp.offset[ tile_id ];
int const new_np = sort.new_np[ tile_id ];
// The _dir_offset variable holds 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.ix;
float2* __restrict__ tmp_x = tmp.x;
float3* __restrict__ tmp_u = tmp.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 ( it.get_local_id(0) == 0 ) {
_dir_offset[4] = new_offset + device::global::atomicAdd( & tmp.np[ tile_id ], nshift );
}
it.barrier();
// Find offsets on new buffer
for( int i = it.get_local_id(0); i < 9; i += it.get_local_range(0) ) {
if ( i != 4 ) {
// Find target node
int target_tx = tile_idx.x + i % 3 - 1;
int target_ty = tile_idx.y + i / 3 - 1;
bool valid = true;
// Correct for periodic boundaries
if ( periodic.x ) {
if ( target_tx < 0 ) target_tx += ntiles.x;
if ( target_tx >= ntiles.x ) target_tx -= ntiles.x;
} else {
valid &= ( target_tx >= 0 ) && ( target_tx < ntiles.x );
}
if ( periodic.y ) {
if ( target_ty < 0 ) target_ty += ntiles.y;
if ( target_ty >= ntiles.y ) target_ty -= ntiles.y;
} else {
valid &= ( target_ty >= 0 ) && ( target_ty < ntiles.y );
}
if ( valid ) {
// If valid neighbour tile reserve space on tmp. array
int target_tid = target_ty * ntiles.x + target_tx;
_dir_offset[i] = tmp.offset[ target_tid ] +
device::global::atomicAdd( &tmp.np[ target_tid ], npt[ i ] );
} else {
// Otherwise mark offset as invalid
_dir_offset[i] = -1;
}
}
}
_c[0] = n0;
it.barrier();
// Copy particles moving away from tile and fill holes
for( int i = it.get_local_id(0); i < nidx; i += it.get_local_range(0) ) {
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 );
const int dir = (ycross+1) * 3 + (xcross+1);
// Check if particle crossed into a valid neighbor
if ( _dir_offset[dir] >= 0 ) {
// _dir_offset[] includes the offset in the global tmp particle buffer
int l = device::local::atomicAdd( & _dir_offset[dir], 1 );
nix.x -= xcross * lim.x;
nix.y -= ycross * lim.y;
tmp_ix[ l ] = nix;
tmp_x[ l ] = nx;
tmp_u[ l ] = nu;
}
// Fill hole if needed
if ( k < n0 ) {
int c, invalid;
do {
c = device::local::atomicAdd( &_c[0], 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 ];
}
}
it.barrier();
// At this point all particles up to n0 are correct
// Copy particles that need to be shifted
// We've reserved space for nshift particles earlier
const int new_idx = _dir_offset[4];
if ( new_offset >= old_offset ) {
// Copy from beggining of buffer
for( int i = it.get_local_id(0); i < nshift; i += it.get_local_range(0) ) {
tmp_ix[ new_idx + i ] = ix[ i ];
tmp_x[ new_idx + i ] = x [ i ];
tmp_u[ new_idx + i ] = u [ i ];
}
} else {
// Copy from end of buffer
const int old_idx = n0 - nshift;
for( int i = it.get_local_id(0); i < nshift; i += it.get_local_range(0) ) {
tmp_ix[ new_idx + i ] = ix[ old_idx + i ];
tmp_x[ new_idx + i ] = x [ old_idx + i ];
tmp_u[ new_idx + i ] = u [ old_idx + i ];
}
}
// Store current number of local particles
// These are already in the correct position in global buffer
if ( it.get_local_id(0) == 0 ) {
part.np[ tile_id ] = n0 - nshift;
}
});
});
q.wait();
}
/**
* @brief Copy incoming particles to main buffer. Buffer will be fully sorted after
* this step
*
* @param part Main particle data
* @param tmp Temporary particle data
*/
void copy_in(
ParticleData part,
ParticleData tmp,
sycl::queue & q )
{
// 8×1 work items per group
sycl::range<2> local{ 256, 1 };
// ntiles.x × ntiles.y groups
sycl::range<2> global{ part.ntiles.x, part.ntiles.y };
q.submit([&](sycl::handler &h) {
h.parallel_for(
sycl::nd_range{ global * local, local },
[=](sycl::nd_item<2> it) {
const int2 tile_idx = make_int2( it.get_group(0), it.get_group(1));
const int tile_id = tile_idx.y * part.ntiles.x + tile_idx.x;
const int old_offset = part.offset[ tile_id ];
const int old_np = part.np[ tile_id ];
const int new_offset = tmp.offset[ tile_id ];
const int tmp_np = tmp.np[ tile_id ];
// Notice that we are already working with the new offset
int2 * __restrict__ ix = &part.ix[ new_offset ];
float2 * __restrict__ x = &part.x [ new_offset ];
float3 * __restrict__ u = &part.u [ new_offset ];
int2 * __restrict__ tmp_ix = &tmp.ix[ new_offset ];
float2 * __restrict__ tmp_x = &tmp.x [ new_offset ];
float3 * __restrict__ tmp_u = &tmp.u [ new_offset ];
if ( new_offset >= old_offset ) {
// Add particles to the end of the buffer
for( int i = it.get_local_id(0); i < tmp_np; i += it.get_local_range(0) ) {
ix[ old_np + i ] = tmp_ix[ i ];
x[ old_np + i ] = tmp_x[ i ];
u[ old_np + i ] = tmp_u[ i ];
}
} else {
// Add particles to the beggining of buffer
int np0 = old_offset - new_offset;
if ( np0 > tmp_np ) np0 = tmp_np;
for( int i = it.get_local_id(0); i < np0; i += it.get_local_range(0) ) {
ix[ i ] = tmp_ix[ i ];
x[ i ] = tmp_x[ i ];
u[ i ] = tmp_u[ i ];
}
// If any particles left, add particles to the end of the buffer
for( int i = np0 + it.get_local_id(0); i < tmp_np; i += it.get_local_range(0) ) {
ix[ old_np + i ] = tmp_ix[ i ];
x[ old_np + i ] = tmp_x[ i ];
u[ old_np + i ] = tmp_u[ i ];
}
}
it.barrier();
// Store the new offset and number of particles
if ( it.get_local_id(0) == 0 ) {
part.np[ tile_id ] = old_np + tmp_np;
part.offset[ tile_id ] = new_offset;
}
});
});
q.wait();
}
/**
* @brief Copies copy all particles to correct tiles in another buffer
*
* @note Requires that new buffer (`tmp`) already has the correct offset
* values, and number of particles set to 0.
*
* @param part Particle data
* @param tmp Temporary particle buffer (has new offsets)
* @param sort Sort data (indices of particles leaving the tile, etc.)
* @param periodic Correct for periodic boundaries
*/
void copy_sorted(
ParticleData part,
ParticleData tmp,
const ParticleSortData sort,
const int2 periodic,
sycl::queue & q )
{
const int2 ntiles = make_int2( part.ntiles.x, part.ntiles.y );
const int2 lim = make_int2( part.nx.x, part.nx.y );
// 8×1 work items per group
sycl::range<2> local{ 8, 1 };
// ntiles.x × ntiles.y groups
sycl::range<2> global{ part.ntiles.x, part.ntiles.y };
q.submit([&](sycl::handler &h) {
/// @brief [shared] offsets in target buffer
auto _dir_offset = sycl::local_accessor< int, 1 > ( 9, h );
/// @brief [shared] index of particle used to fill hole
auto _c = sycl::local_accessor< int, 1 > ( 1, h );
h.parallel_for(
sycl::nd_range{ global * local, local },
[=](sycl::nd_item<2> it) {
const int2 tile_idx = make_int2( it.get_group(0), it.get_group(1));
const int tile_id = tile_idx.y * part.ntiles.x + tile_idx.x;
int const old_offset = part.offset[ tile_id ];
int * __restrict__ npt = &sort.npt[ 9*tile_id ];
int2 * __restrict__ ix = &part.ix[ old_offset ];
float2 * __restrict__ x = &part.x[ old_offset ];
float3 * __restrict__ u = &part.u[ old_offset ];
int * __restrict__ idx = &sort.idx[ old_offset ];
uint32_t const nidx = sort.nidx[ tile_id ];
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.ix;
float2* __restrict__ tmp_x = tmp.x;
float3* __restrict__ tmp_u = tmp.u;
// Find offsets on new buffer
for( int i = it.get_local_id(0); i < 9; i += it.get_local_range(0) ) {
int tx = it.get_group(0);
int ty = it.get_group(1);
// Find target node
int target_tx = tx + i % 3 - 1;
int target_ty = ty + i / 3 - 1;
bool valid = true;
// Correct for periodic boundaries
if ( periodic.x ) {
if ( target_tx < 0 ) target_tx += ntiles.x;
if ( target_tx >= ntiles.x ) target_tx -= ntiles.x;
} else {
valid &= ( target_tx >= 0 ) && ( target_tx < ntiles.x );
}
if ( periodic.y ) {
if ( target_ty < 0 ) target_ty += ntiles.y;
if ( target_ty >= ntiles.y ) target_ty -= ntiles.y;
} else {
valid &= ( target_ty >= 0 ) && ( target_ty < ntiles.y );
}
if ( valid ) {
// If valid neighbour tile reserve space on tmp. array
int target_tid = target_ty * ntiles.x + target_tx;
_dir_offset[i] = tmp.offset[ target_tid ] +
device::global::atomicAdd( & tmp.np[ target_tid ], npt[ i ] );
} else {
// Otherwise mark offset as invalid
_dir_offset[i] = -1;
}
}
const int n0 = npt[4];
_c[0] = n0;
it.barrier();
// Copy particles moving away from tile and fill holes
for( int i = it.get_local_id(0); i < nidx; i += it.get_local_range(0) ) {
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 );
const int dir = (ycross+1) * 3 + (xcross+1);
// Check if particle crossed into a valid neighbor
if ( _dir_offset[dir] >= 0 ) {
// _dir_offset[] includes the offset in the global tmp particle buffer
int l = device::local::atomicAdd( & _dir_offset[dir], 1 );
nix.x -= xcross * lim.x;
nix.y -= ycross * lim.y;
tmp_ix[ l ] = nix;
tmp_x[ l ] = nx;
tmp_u[ l ] = nu;
}
// Fill hole if needed
if ( k < n0 ) {
int c, invalid;
do {
c = device::local::atomicAdd( &_c[0], 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 ];
}
}
it.barrier();
// Copy particles staying in tile
const int start = _dir_offset[4];
for( int i = it.get_local_id(0); i < nidx; i += it.get_local_range(0) ) {
tmp_ix[ start + i ] = ix[i];
tmp_x [ start + i ] = x[i];
tmp_u [ start + i ] = u[i];
}
});
});
q.wait();
}
#if 0
/**
* @brief Moves particles to the correct tiles
*
* @note Particles are only expected to have moved no more than 1 tile
* in each direction
*
* @param tmp Temporary particle buffer
* @param sort Temporary sort index
* @param extra Additional space to add to each tile. Leaves room for
* particles to be injected later.
*/
void Particles::tile_sort( Particles & tmp, ParticleSort & sort, const int * __restrict__ extra ) {
// Reset sort data
sort.reset();
// Get new number of particles per tile
bnd_check ( *this, sort, periodic );
// Get new offsets (prefix scan of np)
if ( extra ) {
// Includes extra values in offset calculations
// Used to reserve space in particle buffer for later injection
update_tile_info ( tmp, sort.new_np, extra );