Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_ParticleUtil.H
Go to the documentation of this file.
1#ifndef AMREX_PARTICLEUTIL_H_
2#define AMREX_PARTICLEUTIL_H_
3#include <AMReX_Config.H>
4
5#include <AMReX_IntVect.H>
6#include <AMReX_Box.H>
7#include <AMReX_Gpu.H>
8#include <AMReX_Print.H>
10#include <AMReX_Math.H>
11#include <AMReX_MFIter.H>
12#include <AMReX_ParGDB.H>
13#include <AMReX_ParticleTile.H>
16#include <AMReX_TypeTraits.H>
17#include <AMReX_Scan.H>
18
19#include <limits>
20#include <type_traits>
21
22namespace amrex {
23
34template <class Iterator>
36int
37numParticlesOutOfRange (Iterator const& pti, int nGrow)
38{
39 return numParticlesOutOfRange(pti,
40 IntVect(AMREX_D_DECL(nGrow, nGrow, nGrow)));
41}
42
53template <class Iterator>
54requires (IsParticleIterator<Iterator>::value)
55int
56numParticlesOutOfRange (Iterator const& pti, IntVect nGrow)
57{
58 const auto& tile = pti.GetParticleTile();
59 const auto np = tile.numParticles();
60 const auto& ptd = tile.getConstParticleTileData();
61 const auto& geom = pti.Geom(pti.GetLevel());
62
63 const auto& domain = geom.Domain();
64 const auto& plo = geom.ProbLoArray();
65 const auto& dxi = geom.InvCellSizeArray();
66
67 Box box = pti.tilebox();
68 box.grow(nGrow);
69
70 ReduceOps<ReduceOpSum> reduce_op;
71 ReduceData<int> reduce_data(reduce_op);
72 using ReduceTuple = typename decltype(reduce_data)::Type;
73
74 reduce_op.eval(np, reduce_data,
75 [=] AMREX_GPU_DEVICE (int i) -> ReduceTuple
76 {
77 auto p = ptd[i];
78 if (!p.id().is_valid()) { return false; }
79 using AssignorType = typename Iterator::CellAssignor;
80 AssignorType assignor;
81 IntVect iv = assignor(p, plo, dxi, domain);
82 return !box.contains(iv);
83 });
84 int hv = amrex::get<0>(reduce_data.value(reduce_op));
85 return hv;
86}
87
100template <class PC>
101requires (IsParticleContainer<PC>::value)
102Long
103numParticlesOutOfRange (PC const& pc, int nGrow)
104{
105 return numParticlesOutOfRange(pc, 0, pc.finestLevel(), nGrow);
106}
107
120template <class PC>
121requires (IsParticleContainer<PC>::value)
122Long
123numParticlesOutOfRange (PC const& pc, IntVect nGrow)
124{
125 return numParticlesOutOfRange(pc, 0, pc.finestLevel(), nGrow);
126}
127
142template <class PC>
143requires (IsParticleContainer<PC>::value)
144Long
145numParticlesOutOfRange (PC const& pc, int lev_min, int lev_max, int nGrow)
146{
147 BL_PROFILE("numParticlesOutOfRange()");
148
149 return numParticlesOutOfRange(pc, lev_min, lev_max,
150 IntVect(AMREX_D_DECL(nGrow, nGrow, nGrow)));
151}
152
167template <class PC>
168requires (IsParticleContainer<PC>::value)
169Long
170numParticlesOutOfRange (PC const& pc, int lev_min, int lev_max, IntVect nGrow)
171{
172 BL_PROFILE("numParticlesOutOfRange()");
173
174 using ParIter = typename PC::ParConstIterType;
175 Long num_wrong = 0;
176 for (int lev = lev_min; lev <= lev_max; ++lev)
177 {
178#ifdef AMREX_USE_OMP
179#pragma omp parallel if (Gpu::notInLaunchRegion() && !system::regtest_reduction) reduction(+:num_wrong)
180#endif
181 for(ParIter pti(pc, lev); pti.isValid(); ++pti)
182 {
183 num_wrong += numParticlesOutOfRange(pti, nGrow);
184 }
185 }
187
188 return num_wrong;
189}
190
192int getTileIndex (const IntVect& iv, const Box& box, const bool a_do_tiling,
193 const IntVect& a_tile_size, Box& tbx)
194{
195 if (a_do_tiling == false) {
196 tbx = box;
197 return 0;
198 } else {
199 //
200 // This function must be consistent with FabArrayBase::buildTileArray function!!!
201 //
202 auto tiling_1d = [](int i, int lo, int hi, int tilesize,
203 int& ntile, int& tileidx, int& tlo, int& thi) {
204 int ncells = hi-lo+1;
205 ntile = amrex::max(ncells/tilesize, 1);
206 int ts_right = ncells/ntile;
207 int ts_left = ts_right+1;
208 int nleft = ncells - ntile*ts_right;
209 int ii = i - lo;
210 int nbndry = nleft*ts_left;
211 if (ii < nbndry) {
212 tileidx = ii / ts_left; // tiles on the left of nbndry have size of ts_left
213 tlo = lo + tileidx * ts_left;
214 thi = tlo + ts_left - 1;
215 } else {
216 tileidx = nleft + (ii-nbndry) / ts_right; // tiles on the right: ts_right
217 tlo = lo + tileidx * ts_right + nleft;
218 thi = tlo + ts_right - 1;
219 }
220 };
221 const IntVect& sml = box.smallEnd();
222 const IntVect& big = box.bigEnd();
223 IntVect ntiles, ivIndex, tilelo, tilehi;
224
225 AMREX_D_TERM(int iv0 = amrex::min(amrex::max(iv[0], sml[0]), big[0]);,
226 int iv1 = amrex::min(amrex::max(iv[1], sml[1]), big[1]);,
227 int iv2 = amrex::min(amrex::max(iv[2], sml[2]), big[2]););
228
229 AMREX_D_TERM(tiling_1d(iv0, sml[0], big[0], a_tile_size[0], ntiles[0], ivIndex[0], tilelo[0], tilehi[0]);,
230 tiling_1d(iv1, sml[1], big[1], a_tile_size[1], ntiles[1], ivIndex[1], tilelo[1], tilehi[1]);,
231 tiling_1d(iv2, sml[2], big[2], a_tile_size[2], ntiles[2], ivIndex[2], tilelo[2], tilehi[2]););
232
233 tbx = Box(tilelo, tilehi);
234
235 return AMREX_D_TERM(ivIndex[0], + ntiles[0]*ivIndex[1], + ntiles[0]*ntiles[1]*ivIndex[2]);
236 }
237}
238
259Box getTileBoxFromIndex (int tile_index, const Box& box, const IntVect& tile_size) noexcept
260{
261 //
262 // This function must be consistent with getTileIndex!!!
263 //
264 IntVect tilelo, tilehi;
265 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
266 const int lo = box.smallEnd(idim);
267 const int ncells = box.bigEnd(idim) - lo + 1;
268 const int ntile = amrex::max(ncells/tile_size[idim], 1);
269 const int itile = tile_index % ntile;
270 tile_index /= ntile;
271 const int ts_right = ncells/ntile;
272 const int ts_left = ts_right+1;
273 const int nleft = ncells - ntile*ts_right;
274 if (itile < nleft) {
275 // tiles on the left of nleft have size of ts_left
276 tilelo[idim] = lo + itile*ts_left;
277 tilehi[idim] = tilelo[idim] + ts_left - 1;
278 } else {
279 // tiles on the right have size of ts_right
280 tilelo[idim] = lo + itile*ts_right + nleft;
281 tilehi[idim] = tilelo[idim] + ts_right - 1;
282 }
283 }
284 return Box(tilelo, tilehi);
285}
286
288int numTilesInBox (const Box& box, const bool a_do_tiling, const IntVect& a_tile_size)
289{
290 if (a_do_tiling == false) {
291 return 1;
292 } else {
293 //
294 // This function must be consistent with FabArrayBase::buildTileArray function!!!
295 //
296 auto tiling_1d = [](int lo, int hi, int tilesize, int& ntile) {
297 int ncells = hi-lo+1;
298 ntile = amrex::max(ncells/tilesize, 1);
299 };
300
301 const IntVect& sml = box.smallEnd();
302 const IntVect& big = box.bigEnd();
303 IntVect ntiles;
304
305 AMREX_D_TERM(tiling_1d(sml[0], big[0], a_tile_size[0], ntiles[0]);,
306 tiling_1d(sml[1], big[1], a_tile_size[1], ntiles[1]);,
307 tiling_1d(sml[2], big[2], a_tile_size[2], ntiles[2]););
308
309 return AMREX_D_TERM(ntiles[0], *=ntiles[1], *=ntiles[2]);
310 }
311}
312
314{
315 BinMapper(const int* off_bins_p,
318 const Dim3* lo_p,
319 const Dim3* hi_p,
320 int* bin_type_array=nullptr)
321 : m_off_bins_p(off_bins_p), m_dxi_p(dxi_p), m_plo_p(plo_p) ,
322 m_lo_p(lo_p) , m_hi_p(hi_p) , m_bin_type_array(bin_type_array) {}
323
324 template <typename T>
326 unsigned int operator() (const T& ptd, int i) const
327 {
328 auto p = ptd[i];
329 int type = (m_bin_type_array) ? m_bin_type_array[i] : 0;
330 int offset = m_off_bins_p[type];
331
332 AMREX_D_TERM(AMREX_ASSERT((p.pos(0)-m_plo_p[type][0])*m_dxi_p[type][0] - m_lo_p[type].x >= 0.0);,
333 AMREX_ASSERT((p.pos(1)-m_plo_p[type][1])*m_dxi_p[type][1] - m_lo_p[type].y >= 0.0);,
334 AMREX_ASSERT((p.pos(2)-m_plo_p[type][2])*m_dxi_p[type][2] - m_lo_p[type].z >= 0.0));
335
336 auto iv = IntVect(AMREX_D_DECL(static_cast<int>(amrex::Math::floor((p.pos(0)-m_plo_p[type][0])*m_dxi_p[type][0])) - m_lo_p[type].x,
337 static_cast<int>(amrex::Math::floor((p.pos(1)-m_plo_p[type][1])*m_dxi_p[type][1])) - m_lo_p[type].y,
338 static_cast<int>(amrex::Math::floor((p.pos(2)-m_plo_p[type][2])*m_dxi_p[type][2])) - m_lo_p[type].z));
339 auto iv3 = iv.dim3();
340 int nx = m_hi_p[type].x-m_lo_p[type].x+1;
341 int ny = m_hi_p[type].y-m_lo_p[type].y+1;
342 int nz = m_hi_p[type].z-m_lo_p[type].z+1;
343 int uix = amrex::min(nx-1,amrex::max(0,iv3.x));
344 int uiy = amrex::min(ny-1,amrex::max(0,iv3.y));
345 int uiz = amrex::min(nz-1,amrex::max(0,iv3.z));
346 return static_cast<unsigned int>( (uiz * ny + uiy) * nx + uix + offset );
347 }
348
349private:
350 const int* m_off_bins_p;
351 const GpuArray<Real,AMREX_SPACEDIM>* m_dxi_p;
352 const GpuArray<Real,AMREX_SPACEDIM>* m_plo_p;
353 const Dim3* m_lo_p;
354 const Dim3* m_hi_p;
355 int* m_bin_type_array;
356};
357
359{
365
366 template <typename ParticleType>
368 unsigned int operator() (const ParticleType& p) const noexcept
369 {
370 Box tbx;
371 auto iv = getParticleCell(p, plo, dxi, domain);
372 auto tid = getTileIndex(iv, box, true, bin_size, tbx);
373 return static_cast<unsigned int>(tid);
374 }
375};
376
390template <typename P>
395{
396 IntVect iv(
397 AMREX_D_DECL(int(amrex::Math::floor((p.pos(0)-plo[0])*dxi[0])),
398 int(amrex::Math::floor((p.pos(1)-plo[1])*dxi[1])),
399 int(amrex::Math::floor((p.pos(2)-plo[2])*dxi[2]))));
400 return iv;
401}
402
417template <typename P>
422 const Box& domain) noexcept
423{
424 IntVect iv = getParticleCell(p, plo, dxi);
425 iv += domain.smallEnd();
426 return iv;
427}
428
429template <typename PTD>
431IntVect getParticleCell (PTD const& ptd, int i,
434 const Box& domain) noexcept
435{
436 IntVect iv(
437 AMREX_D_DECL(int(amrex::Math::floor((ptd.pos(0, i)-plo[0])*dxi[0])),
438 int(amrex::Math::floor((ptd.pos(1, i)-plo[1])*dxi[1])),
439 int(amrex::Math::floor((ptd.pos(2, i)-plo[2])*dxi[2]))));
440 iv += domain.smallEnd();
441 return iv;
442}
443
445{
446
447 template <typename P>
452 const Box& domain) const noexcept
453 {
454 return getParticleCell(p, plo, dxi, domain);
455 }
456};
457
458template <typename P>
460int getParticleGrid (P const& p, amrex::Array4<int> const& mask,
463 const Box& domain) noexcept
464{
465 if (!p.id().is_valid()) { return -1; }
466 IntVect iv = getParticleCell(p, plo, dxi, domain);
467 return mask(iv);
468}
469
470template <typename P>
477 amrex::GpuArray<int,AMREX_SPACEDIM> const& is_per) noexcept
478{
479 bool shifted = false;
480 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim)
481 {
482 if (! is_per[idim]) { continue; }
483 if (p.pos(idim) > rhi[idim]) {
484 while (p.pos(idim) > rhi[idim]) {
485 p.pos(idim) = static_cast<ParticleReal>(p.pos(idim) - (phi[idim] - plo[idim]));
486 }
487 // clamp to avoid precision issues;
488 if (p.pos(idim) < rlo[idim]) {
489 p.pos(idim) = rlo[idim];
490 }
491 shifted = true;
492 }
493 else if (p.pos(idim) < rlo[idim]) {
494 while (p.pos(idim) < rlo[idim]) {
495 p.pos(idim) = static_cast<ParticleReal>(p.pos(idim) + (phi[idim] - plo[idim]));
496 }
497 // clamp to avoid precision issues;
498 if (p.pos(idim) > rhi[idim]) {
499 p.pos(idim) = rhi[idim];
500 }
501 shifted = true;
502 }
503 AMREX_ASSERT( (p.pos(idim) >= rlo[idim] ) && ( p.pos(idim) <= rhi[idim] ));
504 }
505
506 return shifted;
507}
508
521template <typename PTile, typename ParFunc>
522int
523partitionParticles (PTile& ptile, ParFunc const& is_left)
524{
525 const int np = ptile.numParticles();
526 if (np == 0) { return 0; }
527
528 auto ptd = ptile.getParticleTileData();
529
530 const int num_left = Reduce::Sum<int>(np,
531 [=] AMREX_GPU_DEVICE (int i) -> int
532 {
533 return int(is_left(ptd, i));
534 });
535
536 // The ptile will be partitioned into left [0, num_left-1] and right [num_left, np-1].
537 //
538 // Note that currently the number of particles in [0, num_left-1] that should belong to the
539 // right partition is equal to the number of particles in [num_left, np-1] that should belong
540 // in the left partition. We will define num_swaps to be this number. This is the minimum
541 // number of swaps that need to be performed to partition the ptile in place for any algorithm.
542 //
543 // From this it is easy to see that
544 // max_num_swaps = min(size([0, num_left-1]), size([num_left, np-1]))
545 // is an upper bound for num_swaps.
546
547 const int max_num_swaps = std::min(num_left, np - num_left);
548 if (max_num_swaps == 0) { return num_left; }
549
550 Gpu::DeviceVector<int> index_left(max_num_swaps);
551 Gpu::DeviceVector<int> index_right(max_num_swaps);
552 int * const p_index_left = index_left.dataPtr();
553 int * const p_index_right = index_right.dataPtr();
554
555 // The num_swaps particles that are in [0, num_left-1] but should be moved to the right
556 // partition are at the same time the first num_swaps particles for which is_left is false
557 // in all the ptile.
558 // Similarly, the num_swaps particles in [num_left, np-1] that should be moved to the left
559 // partition are the last num_swaps particles of the ptile for which is_left is true.
560 //
561 // The PrefixSum is used to find exactly these particles and store their indexes in
562 // index_left and index_right. Since num_swaps is not known, the first max_num_swaps
563 // particles are stored instead. Here, dst = num_left-1-(i-s) is used to effectively reverse
564 // the PrefixSum to store the last particles for which is_left is true.
565 //
566 // This way, all indexes in index_right are in ascending order, and all indexes in
567 // index_left are in descending order.
568
570 [=] AMREX_GPU_DEVICE (int i) -> int
571 {
572 return int(!is_left(ptd, i));
573 },
574 [=] AMREX_GPU_DEVICE (int i, int const& s)
575 {
576 if (!is_left(ptd, i)) {
577 int dst = s;
578 if (dst < max_num_swaps) {
579 p_index_right[dst] = i;
580 }
581 } else {
582 int dst = num_left-1-(i-s); // avoid integer overflow
583 if (dst < max_num_swaps) {
584 p_index_left[dst] = i;
585 }
586 }
587 },
589
590 // Finally, the particles are swapped. Since max_num_swaps is only an upper bound for num_swaps,
591 // some swaps should not be performed and need to be skipped. This is the case if the index
592 // in index_left[i] is already in the left partition or the index in index_right[i] is already
593 // in the right partition. These two cases coincide for the same i because index_right is in
594 // ascending order and index_left in descending order. This means for both index_left and
595 // index_right the first num_swaps particles need to be swapped, and the particles after that
596 // should be skipped.
597 //
598 // The check right_i < left_i makes sure that the particle going to the right partition is
599 // actually coming from the left partition, which has a lower index than the other particle and
600 // visa-versa.
601 //
602 // Since exactly num_swaps swap operations are performed in the end, which is the smallest
603 // number possible, this algorithm is optimal in the number of swap operations.
604 // This results in good performance in practice if the size of a particle is large enough that
605 // it compensates for the extra kernel launches and evaluations of is_left which this
606 // algorithm needs.
607
608 ParallelForOMP(max_num_swaps,
609 [=] AMREX_GPU_DEVICE (int i)
610 {
611 int left_i = p_index_left[i];
612 int right_i = p_index_right[i];
613 if (right_i < left_i) {
614 swapParticle(ptd, ptd, left_i, right_i);
615 }
616 });
617
618 index_left.free_async();
619 index_right.free_async();
620
621 return num_left;
622}
623
638template <typename PTile, typename ParFunc>
639void
640partitionParticles (PTile& ptile, int num_left, ParFunc const& is_left)
641{
642 const int np = ptile.numParticles();
643 if (np == 0) { return; }
644
645 auto ptd = ptile.getParticleTileData();
646
647 const int max_num_swaps = std::min(num_left, np - num_left);
648 if (max_num_swaps == 0) { return; }
649
650 Gpu::DeviceVector<int> index_left(max_num_swaps);
651 Gpu::DeviceVector<int> index_right(max_num_swaps);
652 int * const p_index_left = index_left.dataPtr();
653 int * const p_index_right = index_right.dataPtr();
654
656 [=] AMREX_GPU_DEVICE (int i) -> int
657 {
658 return int(!is_left(ptd, i));
659 },
660 [=] AMREX_GPU_DEVICE (int i, int const& s)
661 {
662 if (!is_left(ptd, i)) {
663 int dst = s;
664 if (dst < max_num_swaps) {
665 p_index_right[dst] = i;
666 }
667 } else {
668 int dst = num_left-1-(i-s); // avoid integer overflow
669 if (dst < max_num_swaps) {
670 p_index_left[dst] = i;
671 }
672 }
673 },
675
676 ParallelForOMP(max_num_swaps,
677 [=] AMREX_GPU_DEVICE (int i)
678 {
679 int left_i = p_index_left[i];
680 int right_i = p_index_right[i];
681 if (right_i < left_i) {
682 swapParticle(ptd, ptd, left_i, right_i);
683 }
684 });
685
686 index_left.free_async();
687 index_right.free_async();
688}
689
690template <typename PTile>
691void
693{
694 const int new_size = partitionParticles(ptile,
695 [=] AMREX_GPU_DEVICE (auto& ptd, int i) {
696 return ptd.id(i).is_valid();
697 });
698 ptile.resize(new_size);
699}
700
701template <typename PTile, typename PLocator, typename CellAssignor>
702int
703partitionParticlesByDest (PTile& ptile, const PLocator& ploc, CellAssignor const& assignor,
704 const ParticleBufferMap& pmap,
709 const GpuArray<int ,AMREX_SPACEDIM>& is_per,
710 int lev, int gid, int tid,
711 int lev_min, int lev_max, IntVect nGrow, bool remove_negative)
712{
713 auto getPID = pmap.getPIDFunctor();
714 int pid = ParallelContext::MyProcSub();
715
716 Gpu::DeviceVector<uint8_t> particle_stays(ptile.numParticles());
717 uint8_t * const p_particle_stays = particle_stays.dataPtr();
718 auto ptd = ptile.getParticleTileData();
719
720 // the function for determining if a particle stays on this grid is very slow,
721 // so we cache it in particle_stays to avoid evaluating it multiple times.
722 ParallelForOMP(ptile.numParticles(),
723 [=] AMREX_GPU_DEVICE (int i)
724 {
725 int assigned_grid;
726 int assigned_tile;
727 int assigned_lev;
728
729 if (!ptd.id(i).is_valid())
730 {
731 assigned_grid = -1;
732 assigned_tile = -1;
733 assigned_lev = -1;
734 }
735 else
736 {
737 auto p_prime = ptd.getSuperParticle(i);
738 enforcePeriodic(p_prime, plo, phi, rlo, rhi, is_per);
739 auto tup_prime = ploc(p_prime, lev_min, lev_max, nGrow, assignor);
740 assigned_grid = amrex::get<0>(tup_prime);
741 assigned_tile = amrex::get<1>(tup_prime);
742 assigned_lev = amrex::get<2>(tup_prime);
743 if (assigned_grid >= 0)
744 {
745 AMREX_D_TERM(ptd.pos(0, i) = p_prime.pos(0);,
746 ptd.pos(1, i) = p_prime.pos(1);,
747 ptd.pos(2, i) = p_prime.pos(2););
748 }
749 else if (lev_min > 0)
750 {
751 AMREX_D_TERM(p_prime.pos(0) = ptd.pos(0, i);,
752 p_prime.pos(1) = ptd.pos(1, i);,
753 p_prime.pos(2) = ptd.pos(2, i););
754 auto tup = ploc(p_prime, lev_min, lev_max, nGrow, assignor);
755 assigned_grid = amrex::get<0>(tup);
756 assigned_tile = amrex::get<1>(tup);
757 assigned_lev = amrex::get<2>(tup);
758 }
759 }
760
761 p_particle_stays[i] = uint8_t(
762 ((remove_negative == false) && (!ptd.id(i).is_valid())) ||
763 ((assigned_grid == gid) && (assigned_tile == tid) &&
764 (assigned_lev == lev) && (getPID(lev, gid, tid) == pid)));
765 });
766
767 return partitionParticles(ptile,
768 [=] AMREX_GPU_DEVICE (auto& /* ptd */, int i) -> bool {
769 return p_particle_stays[i];
770 });
771}
772
773template <class PC1, class PC2>
774bool SameIteratorsOK (const PC1& pc1, const PC2& pc2) {
775 if (pc1.numLevels() != pc2.numLevels()) {return false;}
776 if (pc1.do_tiling != pc2.do_tiling) {return false;}
777 if (pc1.tile_size != pc2.tile_size) {return false;}
778 for (int lev = 0; lev < pc1.numLevels(); ++lev) {
779 if (pc1.ParticleBoxArray(lev) != pc2.ParticleBoxArray(lev)) {return false;}
780 if (pc1.ParticleDistributionMap(lev) != pc2.ParticleDistributionMap(lev)) {return false;}
781 }
782 return true;
783}
784
785template <class PC>
787 using Iter = typename PC::ParIterType;
788 for (int lev = 0; lev < pc.numLevels(); ++lev) {
789 for (Iter pti(pc, lev); pti.isValid(); ++pti) {
790 pc.DefineAndReturnParticleTile(lev, pti);
791 }
792 }
793}
794
795IntVect computeRefFac (const ParGDBBase* a_gdb, int src_lev, int lev);
796
797Vector<int> computeNeighborProcs (const ParGDBBase* a_gdb, int ngrow);
798
799Vector<int> computeNeighborProcs (const ParGDBBase* a_gdb, IntVect ngrow);
800
802namespace particle_detail
803{
804template <typename C>
805void clearEmptyEntries (C& c)
806{
807 for (auto c_it = c.begin(); c_it != c.end(); /* no ++ */)
808 {
809 if (c_it->second.empty()) { c.erase(c_it++); }
810 else { ++c_it; }
811 }
812}
813
820template <typename PTile>
821bool soaIsSized (PTile const& ptile, int nreal, int nint)
822{
823 if constexpr (PTile::ParticleType::is_rtsoa_particle) {
824 // all components of a ParticleTileRT share one size
825 amrex::ignore_unused(ptile, nreal, nint);
826 return true;
827 } else {
828 auto const& soa = ptile.GetStructOfArrays();
829 if (soa.NumRealComps() != nreal || soa.NumIntComps() != nint) { return false; }
830 auto const np = static_cast<std::size_t>(ptile.numParticles());
831 for (int i = 0; i < nreal; ++i) { if (soa.GetRealData(i).size() != np) { return false; } }
832 for (int i = 0; i < nint; ++i) { if (soa.GetIntData(i).size() != np) { return false; } }
833 return true;
834 }
835}
836
845template <typename PTile>
846void resizeAndZeroNewSoA (PTile& ptile, Long np, bool zero_compile_time = false)
847{
848 auto const n = static_cast<std::size_t>(np);
849 if constexpr (PTile::ParticleType::is_rtsoa_particle) {
850 // all components of a ParticleTileRT share one size and are runtime components
851 amrex::ignore_unused(zero_compile_time);
852 auto const old_n = static_cast<std::size_t>(ptile.numParticles());
853 ptile.resize(n);
854 if (n <= old_n) { return; }
855 auto const ptd = ptile.getParticleTileData();
856 int const nreal = ptile.NumRealComps();
857 int const nint = ptile.NumIntComps();
858 auto zero = [=] AMREX_GPU_HOST_DEVICE (std::size_t i) noexcept {
859 for (int j = 0; j < nreal; ++j) { ptd.rdata(j, i) = 0; }
860 for (int j = 0; j < nint; ++j) { ptd.idata(j, i) = 0; }
861 };
862 if (ptile.arena()->isDeviceAccessible()) {
863 ParallelFor(n - old_n, [=] AMREX_GPU_DEVICE (std::size_t k) noexcept { zero(old_n + k); });
864 Gpu::streamSynchronize();
865 } else {
866 for (std::size_t i = old_n; i < n; ++i) { zero(i); }
867 }
868 } else {
869 auto& soa = ptile.GetStructOfArrays();
870 int const first_real = zero_compile_time ? 0 : PTile::NAR;
871 int const first_int = zero_compile_time ? 0 : PTile::NAI;
872 // PODVector::resize(n, value) fills on the host or the device, depending on
873 // where the vector's memory lives
874 for (int i = first_real; i < soa.NumRealComps(); ++i) { soa.GetRealData(i).resize(n, ParticleReal(0)); }
875 for (int i = first_int; i < soa.NumIntComps(); ++i) { soa.GetIntData(i).resize(n, 0); }
876 // resizes the other components and the particle data, and invalidates the
877 // tile's cached pointers
878 ptile.resize(n);
879 }
880}
881
889template <typename CellAssignor>
890struct SingleBoxLocator
891{
892 GpuArray<Real, AMREX_SPACEDIM> plo;
893 GpuArray<Real, AMREX_SPACEDIM> phi;
894 GpuArray<Real, AMREX_SPACEDIM> dxi;
895 GpuArray<ParticleReal, AMREX_SPACEDIM> rlo;
896 GpuArray<ParticleReal, AMREX_SPACEDIM> rhi;
897 GpuArray<int, AMREX_SPACEDIM> is_per;
898 Box domain;
899 Box grown_box;
900
901 SingleBoxLocator (Geometry const& geom, Box const& box, IntVect const& nGrow)
902 : plo(geom.ProbLoArray()), phi(geom.ProbHiArray()), dxi(geom.InvCellSizeArray()),
903 rlo(geom.ProbLoArrayInParticleReal()), rhi(geom.ProbHiArrayInParticleReal()),
904 is_per(geom.isPeriodicArray()), domain(geom.Domain()), grown_box(amrex::grow(box, nGrow))
905 {}
906
908 template <typename PTD>
910 static auto particle (PTD const& ptd, int i) noexcept
911 {
912 if constexpr (std::is_same_v<CellAssignor, DefaultAssignor>) {
913 Particle<0, 0> p;
914 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) { p.pos(idim) = ptd.pos(idim, i); }
915 return p;
916 } else {
917 return ptd.getSuperParticle(i);
918 }
919 }
920
922 template <typename P>
924 bool stays (P& p, bool& shifted) const noexcept
925 {
926 shifted = enforcePeriodic(p, plo, phi, rlo, rhi, is_per);
927 return grown_box.contains(CellAssignor{}(p, plo, dxi, domain));
928 }
929
931 template <typename P>
933 bool staysUnshifted (P const& p) const noexcept
934 {
935 return grown_box.contains(CellAssignor{}(p, plo, dxi, domain));
936 }
937};
938
950template <typename CellAssignor, typename PTile>
951GpuTuple<int, int, int>
952singleBoxRedistributeLocate (PTile& ptile, SingleBoxLocator<CellAssignor> const& loc,
953 bool remove_negative, uint8_t* stays)
954{
955 auto ptd = ptile.getParticleTileData();
956
957 ReduceOps<ReduceOpSum, ReduceOpSum, ReduceOpSum> reduce_op;
958 ReduceData<int, int, int> reduce_data(reduce_op);
959 using ReduceTuple = typename decltype(reduce_data)::Type;
960
961 reduce_op.eval(ptile.numParticles(), reduce_data,
962 [=] AMREX_GPU_DEVICE (int i) -> ReduceTuple
963 {
964 if (!ptd.id(i).is_valid()) {
965 stays[i] = uint8_t(!remove_negative);
966 return {0, 1, 0};
967 }
968
969 auto p = loc.particle(ptd, i);
970 bool shifted = false;
971 if (loc.stays(p, shifted)) {
972 stays[i] = 1;
973 if (shifted) {
974 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) { ptd.pos(idim, i) = p.pos(idim); }
975 }
976 return {0, 0, 0};
977 }
978
979 stays[i] = 0;
980 const bool kept_unshifted = shifted && loc.staysUnshifted(loc.particle(ptd, i));
981 return {kept_unshifted ? 0 : 1, 0, kept_unshifted ? 1 : 0};
982 });
983
984 return reduce_data.value(reduce_op);
985}
986
988template <typename PTile>
989int partitionParticlesByFlag (PTile& ptile, uint8_t const* flags)
990{
991 return partitionParticles(ptile,
992 [=] AMREX_GPU_DEVICE (auto& /* ptd */, int i) -> bool { return flags[i]; });
993}
994}
996
997template <class index_type, typename F>
999 index_type nbins, F const& f)
1000{
1001 BL_PROFILE("PermutationForDeposition()");
1002
1003#if defined(AMREX_USE_HIP)
1004 // MI250X has a small L2 cache and is more tolerant of atomic add contention,
1005 // so we use a small block size of 64 and the compressed layout.
1006 static constexpr index_type gpu_block_size = 64;
1007 static constexpr bool compressed_layout = true;
1008#else
1009 // A100 has a larger L2 cache and is very sensitive to atomic add contention,
1010 // so we use a large bock size of 1024 and not the compressed layout.
1011 static constexpr index_type gpu_block_size = 1024;
1012 static constexpr bool compressed_layout = false;
1013#endif
1014
1015 static constexpr index_type gpu_block_size_m1 = gpu_block_size - 1;
1016 static constexpr index_type llist_guard = std::numeric_limits<index_type>::max();
1017
1018 // round up to gpu_block_size
1019 nbins = (nbins + gpu_block_size_m1) / gpu_block_size * gpu_block_size;
1020
1021 Gpu::DeviceVector<index_type> llist_start(nbins, llist_guard);
1022 Gpu::DeviceVector<index_type> llist_next(nitems);
1023 perm.resize(0);
1024 perm.resize(nitems);
1025 Gpu::DeviceScalar<index_type> global_idx(0);
1026
1027 index_type* pllist_start = llist_start.dataPtr();
1028 index_type* pllist_next = llist_next.dataPtr();
1029 index_type* pperm = perm.dataPtr();
1030 index_type* pglobal_idx = global_idx.dataPtr();
1031
1032 amrex::ParallelFor(nitems, [=] AMREX_GPU_DEVICE (index_type i) noexcept
1033 {
1034 i = nitems - i - 1;
1035 pllist_next[i] = Gpu::Atomic::Exch(pllist_start + f(i), i);
1036 });
1037
1038#if defined(AMREX_USE_CUDA) || defined(AMREX_USE_HIP)
1039 amrex::launch<gpu_block_size>(nbins / gpu_block_size, Gpu::gpuStream(),
1040 [pllist_start,pllist_next,pperm,pglobal_idx] AMREX_GPU_DEVICE () {
1041 __shared__ index_type sdata[gpu_block_size];
1042 __shared__ index_type global_idx_start;
1043 __shared__ index_type idx_start;
1044
1045 index_type current_idx = 0;
1046
1047 if constexpr (compressed_layout) {
1048 // Compressed layout: subsequent sweeps of up to gpu_block_size contiguous particles
1049 // are put right next to each other, while without the compressed layout,
1050 // there can be other particle sweeps from different locations between them.
1051 current_idx = pllist_start[threadIdx.x + gpu_block_size * blockIdx.x];
1052
1053 index_type num_particles_thread = 0;
1054 while (current_idx != llist_guard) {
1055 ++num_particles_thread;
1056 current_idx = pllist_next[current_idx];
1057 }
1058
1059 index_type num_particles_block =
1060 Gpu::blockReduceSum<gpu_block_size>(num_particles_thread);
1061
1062 if (threadIdx.x == 0) {
1063 global_idx_start = Gpu::Atomic::Add(pglobal_idx, num_particles_block);
1064 }
1065 }
1066
1067 current_idx = pllist_start[threadIdx.x + gpu_block_size * blockIdx.x];
1068
1069 while (true) {
1070 sdata[threadIdx.x] = index_type(current_idx != llist_guard);
1071 index_type x = 0;
1072
1073 // simple block wide prefix sum
1074 for (index_type i = 1; i<gpu_block_size; i*=2) {
1075 __syncthreads();
1076 if (threadIdx.x >= i) {
1077 x = sdata[threadIdx.x - i];
1078 }
1079 __syncthreads();
1080 if (threadIdx.x >= i) {
1081 sdata[threadIdx.x] += x;
1082 }
1083 }
1084 __syncthreads();
1085 if (sdata[gpu_block_size_m1] == 0) {
1086 break;
1087 }
1088 if (threadIdx.x == gpu_block_size_m1) {
1089 if constexpr (compressed_layout) {
1090 idx_start = global_idx_start;
1091 global_idx_start += sdata[gpu_block_size_m1];
1092 } else {
1093 idx_start = Gpu::Atomic::Add(pglobal_idx, sdata[gpu_block_size_m1]);
1094 }
1095 }
1096 __syncthreads();
1097 sdata[threadIdx.x] += idx_start;
1098 if (current_idx != llist_guard) {
1099 pperm[sdata[threadIdx.x] - 1] = current_idx;
1100 current_idx = pllist_next[current_idx];
1101 }
1102 }
1103 });
1104#else
1105 amrex::ignore_unused(pperm, pglobal_idx, compressed_layout);
1106 Abort("PermutationForDeposition only implemented for CUDA and HIP");
1107#endif
1108
1110}
1111
1112template <class index_type, class PTile>
1114 const PTile& ptile, Box bx, Geometry geom, const IntVect idx_type)
1115{
1116 AMREX_ALWAYS_ASSERT(idx_type.allGE(0) && idx_type.allLE(2));
1117
1118 const IntVect refine_vect = max(idx_type, IntVect(1)).min(IntVect(2));
1119 const IntVect type_vect = idx_type - idx_type / 2 * 2;
1120
1121 geom.refine(refine_vect);
1122
1123 Box domain = geom.Domain();
1124
1125 bx.convert(type_vect);
1126 domain.convert(type_vect);
1127
1128 const RealVect dxi(geom.InvCellSize());
1129 const RealVect pos_offset = Real(0.5) * (RealVect(geom.ProbLo()) + RealVect(geom.ProbHi())
1130 - RealVect(geom.CellSize()) * RealVect(domain.smallEnd() + domain.bigEnd()));
1131
1132 const int ref_product = AMREX_D_TERM(refine_vect[0], * refine_vect[1], * refine_vect[2]);
1133 const IntVect ref_offset(AMREX_D_DECL(1, refine_vect[0], refine_vect[0] * refine_vect[1]));
1134
1135 auto ptd = ptile.getConstParticleTileData();
1136 PermutationForDeposition<index_type>(perm, nitems, bx.numPts() * ref_product,
1137 [=] AMREX_GPU_DEVICE (index_type idx) noexcept
1138 {
1139 const auto p = ptd[idx];
1140
1141 IntVect iv = ((p.pos() - pos_offset) * dxi).round();
1142
1143 IntVect iv_coarse = iv / refine_vect;
1144 IntVect iv_remainder = iv - iv_coarse * refine_vect;
1145
1146 iv_coarse = iv_coarse.max(bx.smallEnd());
1147 iv_coarse = iv_coarse.min(bx.bigEnd());
1148 return bx.index(iv_coarse) + bx.numPts() * (iv_remainder * ref_offset).sum();
1149 });
1150}
1151
1152template <typename P>
1153std::string getDefaultCompNameReal (const int i) {
1154 int first_r_name = 0;
1155 if constexpr (P::is_soa_particle) {
1156 if (i < AMREX_SPACEDIM) {
1157 constexpr int x_in_ascii = 120;
1158 std::string const name{char(x_in_ascii+i)};
1159 return name;
1160 }
1161 first_r_name = AMREX_SPACEDIM;
1162 }
1163 std::string const name{("real_comp" + std::to_string(i-first_r_name))};
1164 return name;
1165}
1166
1167template <typename P>
1168std::string getDefaultCompNameInt (const int i) {
1169 std::string const name{("int_comp" + std::to_string(i))};
1170 return name;
1171}
1172
1173
1183template <class PTile, class index_type>
1184requires (!PTile::ParticleType::is_rtsoa_particle)
1185void
1186ReorderParticles (PTile& ptile, const index_type* permutations)
1187{
1188 const size_t np = ptile.numParticles();
1189 const size_t np_total = np + ptile.numNeighborParticles();
1190
1191#if defined(AMREX_USE_CUDA) && defined(_WIN32)
1192 if (!PTile::ParticleType::is_soa_particle)
1193#else
1194 if constexpr (!PTile::ParticleType::is_soa_particle)
1195#endif
1196 {
1197 static_assert(sizeof(typename PTile::ParticleType)%4 == 0 && sizeof(uint32_t) == 4);
1198 using tmp_t = std::conditional_t<sizeof(typename PTile::ParticleType)%8 == 0,
1199 uint64_t, uint32_t>;
1200 constexpr std::size_t nchunks = sizeof(typename PTile::ParticleType) / sizeof(tmp_t);
1202 auto* ptmp = tmp.data();
1203 auto* paos = (tmp_t*)(ptile.getParticleTileData().m_aos);
1204 for (std::size_t ichunk = 0; ichunk < nchunks; ++ichunk) {
1205 // Do not need to reorder neighbor particles
1207 {
1208 ptmp[i] = paos[permutations[i]*nchunks+ichunk];
1209 });
1211 {
1212 paos[i*nchunks+ichunk] = ptmp[i];
1213 });
1214 }
1216 } else {
1217 typename PTile::SoA::IdCPU tmp_idcpu;
1218 if constexpr (PTile::has_polymorphic_allocator) {
1219 tmp_idcpu.setArena(ptile.GetStructOfArrays().GetIdCPUData().arena());
1220 }
1221 tmp_idcpu.resize(np_total);
1222 auto src = ptile.GetStructOfArrays().GetIdCPUData().data();
1223 uint64_t* dst = tmp_idcpu.data();
1224 AMREX_HOST_DEVICE_FOR_1D( np_total, i,
1225 {
1226 dst[i] = i < np ? src[permutations[i]] : src[i];
1227 });
1228
1230
1231 ptile.GetStructOfArrays().GetIdCPUData().swap(tmp_idcpu);
1232 }
1233
1234 { // Create a scope for the temporary vector below
1235 typename PTile::RealVector tmp_real;
1236 if (ptile.NumRealComps() > 0) {
1237 if constexpr (PTile::has_polymorphic_allocator) {
1238 tmp_real.setArena(ptile.GetStructOfArrays().GetRealData(0).arena());
1239 }
1240 tmp_real.resize(np_total);
1241 }
1242 for (int comp = 0; comp < ptile.NumRealComps(); ++comp) {
1243 auto src = ptile.GetStructOfArrays().GetRealData(comp).data();
1244 ParticleReal* dst = tmp_real.data();
1245 AMREX_HOST_DEVICE_FOR_1D( np_total, i,
1246 {
1247 dst[i] = i < np ? src[permutations[i]] : src[i];
1248 });
1249
1251
1252 ptile.GetStructOfArrays().GetRealData(comp).swap(tmp_real);
1253 }
1254 }
1255
1256 typename PTile::IntVector tmp_int;
1257 if (ptile.NumIntComps() > 0) {
1258 if constexpr (PTile::has_polymorphic_allocator) {
1259 tmp_int.setArena(ptile.GetStructOfArrays().GetIntData(0).arena());
1260 }
1261 tmp_int.resize(np_total);
1262 }
1263
1264 for (int comp = 0; comp < ptile.NumIntComps(); ++comp) {
1265 auto src = ptile.GetStructOfArrays().GetIntData(comp).data();
1266 int* dst = tmp_int.data();
1267 AMREX_HOST_DEVICE_FOR_1D( np_total , i,
1268 {
1269 dst[i] = i < np ? src[permutations[i]] : src[i];
1270 });
1271
1273
1274 ptile.GetStructOfArrays().GetIntData(comp).swap(tmp_int);
1275 }
1276}
1277
1287template <class PTile, class index_type>
1288requires (PTile::ParticleType::is_rtsoa_particle)
1289void
1290ReorderParticles (PTile& ptile, const index_type* permutations)
1291{
1292 const size_t np = ptile.numParticles();
1293 {
1295
1296 auto src = ptile.GetIdCPUData().data();
1297 uint64_t* dst = tmp_idcpu.data();
1299 [=] AMREX_GPU_DEVICE (size_t i) {
1300 dst[i] = src[permutations[i]];
1301 });
1303 [=] AMREX_GPU_DEVICE (size_t i) {
1304 src[i] = dst[i];
1305 });
1306 }
1307 {
1309
1310 for (int comp = 0; comp < ptile.NumRealComps(); ++comp) {
1311 auto src = ptile.GetRealData(comp).data();
1312 auto dst = tmp_real.data();
1314 [=] AMREX_GPU_DEVICE (size_t i) {
1315 dst[i] = src[permutations[i]];
1316 });
1318 [=] AMREX_GPU_DEVICE (size_t i) {
1319 src[i] = dst[i];
1320 });
1321 }
1322 }
1323 {
1325
1326 for (int comp = 0; comp < ptile.NumIntComps(); ++comp) {
1327 auto src = ptile.GetIntData(comp).data();
1328 auto dst = tmp_int.data();
1330 [=] AMREX_GPU_DEVICE (size_t i) {
1331 dst[i] = src[permutations[i]];
1332 });
1334 [=] AMREX_GPU_DEVICE (size_t i) {
1335 src[i] = dst[i];
1336 });
1337 }
1338 }
1339 // stream sync for deallocation of permutations variable in user code
1341}
1342
1343#ifdef AMREX_USE_HDF5_ASYNC
1344void async_vol_es_wait_particle();
1345void async_vol_es_wait_close_particle();
1346#endif
1347}
1348
1349#endif // include guard
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:593
#define AMREX_ASSERT(EX)
Definition AMReX_BLassert.H:38
#define AMREX_ALWAYS_ASSERT(EX)
Definition AMReX_BLassert.H:50
Integer-lattice boxes and helpers for defining index-space regions.
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:123
#define AMREX_HOST_DEVICE_FOR_1D(...)
Definition AMReX_GpuLaunchMacrosC.nolint.H:105
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
Convenience header for the core AMReX GPU facilities.
while parser_exe_t p
Definition AMReX_Parser_Exe_Body.H:33
#define AMREX_D_TERM(a, b, c)
Definition AMReX_SPACE.H:172
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
__host__ __device__ BoxND & grow(int i) noexcept
Grow in all directions by i cells (negative shrinks).
Definition AMReX_Box.H:668
__host__ __device__ const IntVectND< dim > & bigEnd() const &noexcept
Return the inclusive upper bound of the box.
Definition AMReX_Box.H:136
__host__ __device__ Long numPts() const noexcept
Return the number of points contained in the BoxND.
Definition AMReX_Box.H:385
__host__ __device__ BoxND & convert(IndexTypeND< dim > typ) noexcept
Convert the BoxND from the current type into the argument type. This may change the BoxND coordinates...
Definition AMReX_Box.H:1008
__host__ __device__ bool contains(const IntVectND< dim > &p) const noexcept
Return true if argument is contained within BoxND.
Definition AMReX_Box.H:233
__host__ __device__ Long index(const IntVectND< dim > &v) const noexcept
Return offset of point from smallend; i.e. index(smallend) -> 0, bigend would return numPts()-1....
Definition AMReX_Box.H:1089
__host__ __device__ const IntVectND< dim > & smallEnd() const &noexcept
Return the inclusive lower bound of the box.
Definition AMReX_Box.H:124
const Real * CellSize() const noexcept
Returns the cellsize for each coordinate direction.
Definition AMReX_CoordSys.H:79
const Real * InvCellSize() const noexcept
Returns the inverse cellsize for each coordinate direction.
Definition AMReX_CoordSys.H:91
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:87
void refine(IntVect const &rr)
Refine the Geometry by rr.
Definition AMReX_Geometry.H:502
const Real * ProbHi() const noexcept
Returns the hi end of the problem domain in each dimension.
Definition AMReX_Geometry.H:212
const Box & Domain() const noexcept
Returns our rectangular domain.
Definition AMReX_Geometry.H:246
const Real * ProbLo() const noexcept
Returns the lo end of the problem domain in each dimension.
Definition AMReX_Geometry.H:210
static void streamSynchronize() noexcept
Definition AMReX_GpuDevice.cpp:992
__host__ __device__ constexpr int min() const noexcept
minimum (no absolute values) value
Definition AMReX_IntVect.H:331
__host__ __device__ constexpr bool allGE(const IntVectND< dim > &rhs) const noexcept
Returns true if this is greater than or equal to argument for all components. NOTE: This is NOT a str...
Definition AMReX_IntVect.H:549
__host__ __device__ constexpr int max() const noexcept
maximum (no absolute values) value
Definition AMReX_IntVect.H:320
__host__ __device__ constexpr bool allLE(const IntVectND< dim > &rhs) const noexcept
Returns true if this is less than or equal to argument for all components. NOTE: This is NOT a strict...
Definition AMReX_IntVect.H:499
void resize(size_type a_new_size, GrowthStrategy strategy=GrowthStrategy::Poisson)
Definition AMReX_PODVector.H:734
void free_async() noexcept
Definition AMReX_PODVector.H:861
T * dataPtr() noexcept
Definition AMReX_PODVector.H:676
T * data() noexcept
Definition AMReX_PODVector.H:672
Definition AMReX_ParticleBufferMap.H:59
GetPID getPIDFunctor() const noexcept
Definition AMReX_ParticleBufferMap.H:195
Definition AMReX_Reduce.H:556
Type value()
Definition AMReX_Reduce.H:591
Definition AMReX_Reduce.H:715
void eval(MF const &mf, IntVect const &nghost, D &reduce_data, F &&f)
Definition AMReX_Reduce.H:855
amrex_real Real
Floating Point Type for Fields.
Definition AMReX_REAL.H:80
amrex_particle_real ParticleReal
Floating Point Type for Particles.
Definition AMReX_REAL.H:91
amrex_long Long
Definition AMReX_INT.H:30
T Sum(N n, T const *v, T init_val=0)
Compute the sum of an array of values.
Definition AMReX_Reduce.H:1082
void ParallelForOMP(T n, L const &f) noexcept
Performance-portable kernel launch function with optional OpenMP threading.
Definition AMReX_GpuLaunch.H:328
ArrayND< T, 4, true > Array4
Convenient alias for a 4D ArrayND with three spatial dimensions plus a component dimension.
Definition AMReX_Array4.H:1352
__host__ __device__ BoxND< dim > grow(const BoxND< dim > &b, int i) noexcept
Return a copy of b grown uniformly by i cells in every direction.
Definition AMReX_Box.H:1328
PODVector< T, AsyncArenaAllocator< T > > AsyncVector
A PODVector that uses the async memory arena. Maybe useful for temporary vectors inside MFIters that ...
Definition AMReX_GpuContainers.H:58
PODVector< T, ArenaAllocator< T > > DeviceVector
A PODVector that uses the standard memory Arena. Note that the memory might or might not be managed d...
Definition AMReX_GpuContainers.H:24
void Sum(Gpu::DeviceVector< T > &v, MPI_Comm comm)
Definition AMReX_GpuParallelReduce.H:37
__host__ __device__ constexpr const T & min(const T &a, const T &b) noexcept
Definition AMReX_Algorithm.H:31
__host__ __device__ constexpr const T & max(const T &a, const T &b) noexcept
Definition AMReX_Algorithm.H:54
__host__ __device__ AMREX_FORCE_INLINE T Exch(T *address, T val) noexcept
Definition AMReX_GpuAtomic.H:542
__host__ __device__ AMREX_FORCE_INLINE T Add(T *sum, T value) noexcept
Definition AMReX_GpuAtomic.H:239
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:329
gpuStream_t gpuStream() noexcept
Definition AMReX_GpuDevice.H:310
__device__ T blockReduceSum(T source) noexcept
Definition AMReX_GpuReduce.H:353
MPI_Comm CommunicatorSub() noexcept
sub-communicator for current frame
Definition AMReX_ParallelContext.H:70
int MyProcSub() noexcept
my sub-rank in current frame
Definition AMReX_ParallelContext.H:76
static constexpr struct amrex::Scan::Type::Exclusive exclusive
static constexpr RetSum noRetSum
Definition AMReX_Scan.H:35
T PrefixSum(N n, FIN const &fin, FOUT const &fout, TYPE, RetSum a_ret_sum=retSum)
Definition AMReX_Scan.H:815
Definition AMReX_Amr.cpp:50
__host__ __device__ void ignore_unused(const Ts &...)
No-op helper that marks variables as intentionally unused.
Definition AMReX.H:273
__host__ __device__ void swapParticle(const ParticleTileData< T_ParticleType, NAR, NAI > &dst, const ParticleTileData< T_ParticleType, NAR, NAI > &src, int src_i, int dst_i) noexcept
A general single particle swapping routine that can run on the GPU.
Definition AMReX_ParticleTransformation.H:120
__host__ __device__ int getTileIndex(const IntVect &iv, const Box &box, const bool a_do_tiling, const IntVect &a_tile_size, Box &tbx)
Definition AMReX_ParticleUtil.H:192
@ F
Definition AMReX_Order.H:7
@ zero
Definition AMReX.H:560
void EnsureThreadSafeTiles(PC &pc)
Definition AMReX_ParticleUtil.H:786
std::string getDefaultCompNameInt(const int i)
Definition AMReX_ParticleUtil.H:1168
int partitionParticlesByDest(PTile &ptile, const PLocator &ploc, CellAssignor const &assignor, const ParticleBufferMap &pmap, const GpuArray< Real, 3 > &plo, const GpuArray< Real, 3 > &phi, const GpuArray< ParticleReal, 3 > &rlo, const GpuArray< ParticleReal, 3 > &rhi, const GpuArray< int, 3 > &is_per, int lev, int gid, int tid, int lev_min, int lev_max, IntVect nGrow, bool remove_negative)
Definition AMReX_ParticleUtil.H:703
void launch(T const &n, L const &f) noexcept
Definition AMReX_GpuLaunchFunctsC.H:122
void ReorderParticles(PTile &ptile, const index_type *permutations)
Reorder particles on the tile ptile using a the permutations array.
Definition AMReX_ParticleUtil.H:1186
__host__ __device__ bool enforcePeriodic(P &p, amrex::GpuArray< amrex::Real, 3 > const &plo, amrex::GpuArray< amrex::Real, 3 > const &phi, amrex::GpuArray< amrex::ParticleReal, 3 > const &rlo, amrex::GpuArray< amrex::ParticleReal, 3 > const &rhi, amrex::GpuArray< int, 3 > const &is_per) noexcept
Definition AMReX_ParticleUtil.H:472
void ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition AMReX_CTOParallelForImpl.H:202
void removeInvalidParticles(PTile &ptile)
Definition AMReX_ParticleUtil.H:692
__host__ __device__ int numTilesInBox(const Box &box, const bool a_do_tiling, const IntVect &a_tile_size)
Definition AMReX_ParticleUtil.H:288
BoxND< 3 > Box
Box is an alias for amrex::BoxND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:35
std::string getDefaultCompNameReal(const int i)
Definition AMReX_ParticleUtil.H:1153
@ x
Definition AMReX_Orientation.H:14
Vector< int > computeNeighborProcs(const ParGDBBase *a_gdb, IntVect ngrow)
Definition AMReX_ParticleUtil.cpp:22
__host__ __device__ Box getTileBoxFromIndex(int tile_index, const Box &box, const IntVect &tile_size) noexcept
Return the tile box with the given index.
Definition AMReX_ParticleUtil.H:259
__host__ __device__ int getParticleGrid(P const &p, amrex::Array4< int > const &mask, amrex::GpuArray< amrex::Real, 3 > const &plo, amrex::GpuArray< amrex::Real, 3 > const &dxi, const Box &domain) noexcept
Definition AMReX_ParticleUtil.H:460
ParIter_impl< Particle< T_NStructReal, T_NStructInt >, T_NArrayReal, T_NArrayInt, Allocator, CellAssignor > ParIter
Definition AMReX_ParIter.H:239
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:38
int numParticlesOutOfRange(Iterator const &pti, int nGrow)
Returns the number of particles that are more than nGrow cells from the box correspond to the input i...
Definition AMReX_ParticleUtil.H:37
void PermutationForDeposition(Gpu::DeviceVector< index_type > &perm, index_type nitems, index_type nbins, F const &f)
Definition AMReX_ParticleUtil.H:998
bool SameIteratorsOK(const PC1 &pc1, const PC2 &pc2)
Definition AMReX_ParticleUtil.H:774
int partitionParticles(PTile &ptile, ParFunc const &is_left)
Reorders the ParticleTile into two partitions left [0, num_left-1] and right [num_left,...
Definition AMReX_ParticleUtil.H:523
void Abort(const std::string &msg)
Print a fatal-error message to stderr and abort execution.
Definition AMReX.cpp:239
const int[]
Definition AMReX_BLProfiler.cpp:1665
IntVect computeRefFac(const ParGDBBase *a_gdb, int src_lev, int lev)
Definition AMReX_ParticleUtil.cpp:6
RealVectND< 3 > RealVect
Definition AMReX_ParmParse.H:40
__host__ __device__ IntVect getParticleCell(P const &p, amrex::GpuArray< amrex::Real, 3 > const &plo, amrex::GpuArray< amrex::Real, 3 > const &dxi) noexcept
Returns the cell index for a given particle using the provided lower bounds and cell sizes.
Definition AMReX_ParticleUtil.H:392
__host__ __device__ constexpr int get(IntVectND< dim > const &iv) noexcept
Get the Idx-th element of IntVectND<dim>.
Definition AMReX_IntVect.H:1367
BinMapper(const int *off_bins_p, const GpuArray< Real, 3 > *dxi_p, const GpuArray< Real, 3 > *plo_p, const Dim3 *lo_p, const Dim3 *hi_p, int *bin_type_array=nullptr)
Definition AMReX_ParticleUtil.H:315
__host__ __device__ unsigned int operator()(const T &ptd, int i) const
Definition AMReX_ParticleUtil.H:326
Definition AMReX_ParticleUtil.H:445
__host__ __device__ IntVect operator()(P const &p, amrex::GpuArray< amrex::Real, 3 > const &plo, amrex::GpuArray< amrex::Real, 3 > const &dxi, const Box &domain) const noexcept
Definition AMReX_ParticleUtil.H:449
A simple struct holding 3 int values for a 3D index.
Definition AMReX_Dim3.H:24
Definition AMReX_ParticleUtil.H:359
GpuArray< Real, 3 > plo
Definition AMReX_ParticleUtil.H:360
IntVect bin_size
Definition AMReX_ParticleUtil.H:363
__host__ __device__ unsigned int operator()(const ParticleType &p) const noexcept
Definition AMReX_ParticleUtil.H:368
GpuArray< Real, 3 > dxi
Definition AMReX_ParticleUtil.H:361
Box domain
Definition AMReX_ParticleUtil.H:362
Box box
Definition AMReX_ParticleUtil.H:364
Fixed-size array that can be used on GPU.
Definition AMReX_Array.H:52
Definition AMReX_GpuMemory.H:57
T * dataPtr()
Definition AMReX_GpuMemory.H:91
Definition AMReX_TypeTraits.H:87