Block-Structured AMR Software Framework
 
Loading...
Searching...
No Matches
AMReX_ParticleContainerI.H
Go to the documentation of this file.
2
3#include <algorithm>
4#include <iterator>
5#include <set>
6#include <stdexcept>
7#include <string>
8#include <type_traits>
9#include <vector>
10
11namespace amrex {
12
13template <typename ParticleType, int NArrayReal, int NArrayInt,
14 template<class> class Allocator, class CellAssignor>
15void
17{
18 num_real_comm_comps = 0;
19 int comm_comps_start = 0;
20 if constexpr (!ParticleType::is_soa_particle) {
21 comm_comps_start += AMREX_SPACEDIM + NStructReal;
22 }
23 for (int i = comm_comps_start; i < comm_comps_start + NumRealComps(); ++i) {
24 if (h_redistribute_real_comp[i]) {++num_real_comm_comps;}
25 }
26
27 num_int_comm_comps = 0;
28 comm_comps_start = 2 + NStructInt;
29 for (int i = comm_comps_start; i < comm_comps_start + NumIntComps(); ++i) {
30 if (h_redistribute_int_comp[i]) {++num_int_comm_comps;}
31 }
32
33 if constexpr (ParticleType::is_soa_particle) {
34 particle_size = sizeof(uint64_t); // idcpu
35 } else {
36 particle_size = sizeof(ParticleType);
37 }
38 superparticle_size = particle_size +
39 num_real_comm_comps*sizeof(ParticleReal) + num_int_comm_comps*sizeof(int);
40}
41
42template <typename ParticleType, int NArrayReal, int NArrayInt,
43 template<class> class Allocator, class CellAssignor>
44void
46{
47 levelDirectoriesCreated = false;
48 usePrePost = false;
49 doUnlink = true;
50
51 // by default communicate all components
52 if constexpr (ParticleType::is_soa_particle)
53 {
54 h_redistribute_real_comp.resize(NArrayReal, true);
55 } else {
56 h_redistribute_real_comp.resize(AMREX_SPACEDIM + NStructReal + NArrayReal, true);
57 }
58 h_redistribute_int_comp.resize(2 + NStructInt + NArrayInt, true);
59
60 SetParticleSize();
61
62 // add default names for SoA Real and Int compile-time arguments
63 m_soa_rdata_names.clear();
64 for (int i=0; i<NArrayReal; ++i)
65 {
66 m_soa_rdata_names.push_back(getDefaultCompNameReal<ParticleType>(i));
67 }
68 m_soa_idata_names.clear();
69 for (int i=0; i<NArrayInt; ++i)
70 {
71 m_soa_idata_names.push_back(getDefaultCompNameInt<ParticleType>(i));
72 }
73
74 static bool initialized = false;
75 if ( ! initialized)
76 {
77 static_assert(sizeof(ParticleType)%sizeof(RealType) == 0,
78 "sizeof ParticleType is not a multiple of sizeof RealType");
79
80 ParmParse pp("particles");
81 pp.queryAdd("do_tiling", do_tiling);
82 Vector<int> tilesize(AMREX_SPACEDIM);
83 if (pp.queryarr("tile_size", tilesize, 0, AMREX_SPACEDIM)) {
84 for (int i=0; i<AMREX_SPACEDIM; ++i) { tile_size[i] = tilesize[i]; }
85 }
86
87 static_assert(std::is_standard_layout<ParticleType>::value,
88 "Particle type must be standard layout");
89 // && std::is_trivial<ParticleType>::value,
90 // "Particle type must be standard layout and trivial.");
91
92 pp.query("use_prepost", usePrePost);
93 pp.query("do_unlink", doUnlink);
94 pp.queryAdd("do_mem_efficient_sort", memEfficientSort);
95 pp.queryAdd("use_comms_arena", use_comms_arena);
96
97 initialized = true;
98 }
99}
100
101template<
102 typename ParticleType,
103 int NArrayReal,
104 int NArrayInt,
105 template<class> class Allocator,
106 class CellAssignor
107>
108void
110 std::vector<std::string> const & rdata_name, std::vector<std::string> const & idata_name
111)
112{
113 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(rdata_name.size() == NArrayReal, "rdata_name must be equal to NArrayReal");
114 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(idata_name.size() == NArrayInt, "idata_name must be equal to NArrayInt");
115
116 // ensure names for components are unique
117 std::set<std::string> const unique_r_names(rdata_name.begin(), rdata_name.end());
118 std::set<std::string> const unique_i_names(idata_name.begin(), idata_name.end());
119 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(rdata_name.size() == unique_r_names.size(), "SetSoACompileTimeNames: Provided names in rdata_name are not unique!");
120 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(idata_name.size() == unique_i_names.size(), "SetSoACompileTimeNames: Provided names in idata_name are not unique!");
121
122 for (int i=0; i<NArrayReal; ++i)
123 {
124 m_soa_rdata_names.at(i) = rdata_name.at(i);
125 }
126 for (int i=0; i<NArrayInt; ++i)
127 {
128 m_soa_idata_names.at(i) = idata_name.at(i);
129 }
130}
131
132template<
133 typename ParticleType,
134 int NArrayReal,
135 int NArrayInt,
136 template<class> class Allocator,
137 class CellAssignor
138>
139bool
141{
142 return std::find(m_soa_rdata_names.begin(), m_soa_rdata_names.end(), name) != std::end(m_soa_rdata_names);
143}
144
145template <typename ParticleType, int NArrayReal, int NArrayInt,
146 template<class> class Allocator, class CellAssignor>
147bool
149{
150 return std::find(m_soa_idata_names.begin(), m_soa_idata_names.end(), name) != std::end(m_soa_idata_names);
151}
152
153template<
154 typename ParticleType,
155 int NArrayReal,
156 int NArrayInt,
157 template<class> class Allocator,
158 class CellAssignor
159>
160int
162{
163 const auto it = std::find(m_soa_rdata_names.begin(), m_soa_rdata_names.end(), name);
164
165 if (it == m_soa_rdata_names.end())
166 {
167 throw std::runtime_error("GetRealCompIndex: Component " + name + " does not exist!");
168 }
169 else
170 {
171 return std::distance(m_soa_rdata_names.begin(), it);
172 }
173}
174
175template<
176 typename ParticleType,
177 int NArrayReal,
178 int NArrayInt,
179 template<class> class Allocator,
180 class CellAssignor
181>
182int
184{
185 const auto it = std::find(m_soa_idata_names.begin(), m_soa_idata_names.end(), name);
186
187 if (it == m_soa_idata_names.end())
188 {
189 throw std::runtime_error("GetIntCompIndex: Component " + name + " does not exist!");
190 }
191 else
192 {
193 return std::distance(m_soa_idata_names.begin(), it);
194 }
195}
196
197template <typename ParticleType, int NArrayReal, int NArrayInt,
198 template<class> class Allocator, class CellAssignor>
199template <typename P, typename Assignor>
202{
203 const Geometry& geom = Geom(lev);
204 const auto& domain = geom.Domain();
205 const auto& plo = geom.ProbLoArray();
206 const auto& dxi = geom.InvCellSizeArray();
207
208 return Assignor{}(p, plo, dxi, domain);
209}
210
211template <typename ParticleType, int NArrayReal, int NArrayInt,
212 template<class> class Allocator, class CellAssignor>
213template <typename P>
214bool
216::Where (const P& p,
217 ParticleLocData& pld,
218 int lev_min,
219 int lev_max,
220 int nGrow,
221 int local_grid) const
222{
223
224 AMREX_ASSERT(m_gdb != nullptr);
225
226 if (lev_max == -1) {
227 lev_max = finestLevel();
228 }
229
230 AMREX_ASSERT(lev_max <= finestLevel());
231
232 AMREX_ASSERT(nGrow == 0 || (nGrow >= 0 && lev_min == lev_max));
233
234 std::vector< std::pair<int, Box> > isects;
235
236 for (int lev = lev_max; lev >= lev_min; lev--) {
237 const IntVect& iv = Index(p, lev);
238 if (lev == pld.m_lev) {
239 // The fact that we are here means this particle does not belong to any finer grids.
240 if (pld.m_grid >= 0) {
241 if (pld.m_grown_gridbox.contains(iv)) {
242 pld.m_cell = iv;
243 if (!pld.m_tilebox.contains(iv)) {
244 pld.m_tile = getTileIndex(iv, pld.m_gridbox, do_tiling, tile_size, pld.m_tilebox);
245 }
246 return true;
247 }
248 }
249 }
250
251 int grid;
252 const BoxArray& ba = ParticleBoxArray(lev);
254
255 if (local_grid < 0) {
256 bool findfirst = (nGrow == 0) ? true : false;
257 ba.intersections(Box(iv, iv), isects, findfirst, nGrow);
258 grid = isects.empty() ? -1 : isects[0].first;
259 if (nGrow > 0 && isects.size() > 1) {
260 for (auto & isect : isects) {
261 Box bx = ba.getCellCenteredBox(isect.first);
262 for (int dir = 0; dir < AMREX_SPACEDIM; ++dir) {
263 Box gbx = bx;
265 gr[dir] = nGrow;
266 gbx.grow(gr);
267 if (gbx.contains(iv)) {
268 grid = isect.first;
269 }
270 }
271 }
272 }
273 } else {
274 grid = (*redistribute_mask_ptr)[local_grid](iv, 0);
275 }
276
277 if (grid >= 0) {
278 const Box& bx = ba.getCellCenteredBox(grid);
279 pld.m_lev = lev;
280 pld.m_grid = grid;
281 pld.m_tile = getTileIndex(iv, bx, do_tiling, tile_size, pld.m_tilebox);
282 pld.m_cell = iv;
283 pld.m_gridbox = bx;
284 pld.m_grown_gridbox = amrex::grow(bx, nGrow);
285 return true;
286 }
287 }
288
289 return false;
290}
291
292template <typename ParticleType, int NArrayReal, int NArrayInt,
293 template<class> class Allocator, class CellAssignor>
294template <typename P>
295bool
298 ParticleLocData& pld,
299 int lev_min,
300 int lev_max,
301 int local_grid) const
302{
303
304 AMREX_ASSERT(m_gdb != nullptr);
305
306 if (!Geom(0).isAnyPeriodic()) { return false; }
307
308 if (lev_max == -1) {
309 lev_max = finestLevel();
310 }
311
312 AMREX_ASSERT(lev_max <= finestLevel());
313
314 // Create a copy "dummy" particle to check for periodic outs.
315 Particle<0, 0> p_prime;
316 AMREX_D_TERM(p_prime.pos(0) = p.pos(0);,
317 p_prime.pos(1) = p.pos(1);,
318 p_prime.pos(2) = p.pos(2));
319 if (PeriodicShift(p_prime)) {
320 std::vector< std::pair<int,Box> > isects;
321 for (int lev = lev_max; lev >= lev_min; lev--) {
322
323 int grid;
324 IntVect iv;
325 const BoxArray& ba = ParticleBoxArray(lev);
327
328 if (local_grid < 0) {
329 iv = Index<amrex::Particle<0, 0>, DefaultAssignor>(p_prime, lev);
330 ba.intersections(Box(iv, iv), isects, true, 0);
331 grid = isects.empty() ? -1 : isects[0].first;
332 } else {
333 iv = Index<amrex::Particle<0, 0>, DefaultAssignor>(p_prime, lev);
334 if (ba[local_grid].contains(iv))
335 {
336 grid = local_grid;
337 }
338 else
339 {
340 ba.intersections(Box(iv, iv), isects, true, 0);
341 grid = isects.empty() ? -1 : isects[0].first;
342 if(grid == -1)
343 {
344 grid = (*redistribute_mask_ptr)[local_grid](Index(p, lev), 0);
345 }
346 }
347 }
348
349 if (grid >= 0) {
350 AMREX_D_TERM(p.pos(0) = p_prime.pos(0);,
351 p.pos(1) = p_prime.pos(1);,
352 p.pos(2) = p_prime.pos(2););
353
354 const Box& bx = ba.getCellCenteredBox(grid);
355
356 pld.m_lev = lev;
357 pld.m_grid = grid;
358 pld.m_tile = getTileIndex(iv, bx, do_tiling, tile_size, pld.m_tilebox);
359 pld.m_cell = iv;
360 pld.m_gridbox = bx;
361 pld.m_grown_gridbox = bx;
362 return true;
363 }
364 }
365 }
366
367 return false;
368}
369
370
371template <typename ParticleType, int NArrayReal, int NArrayInt,
372 template<class> class Allocator, class CellAssignor>
373template <typename P>
374bool
376::PeriodicShift (P& p) const
377{
378 const auto& geom = Geom(0);
379 const auto plo = geom.ProbLoArray();
380 const auto phi = geom.ProbHiArray();
381 const auto rlo = geom.ProbLoArrayInParticleReal();
382 const auto rhi = geom.ProbHiArrayInParticleReal();
383 const auto is_per = geom.isPeriodicArray();
384
385 return enforcePeriodic(p, plo, phi, rlo, rhi, is_per);
386}
387
388template <typename ParticleType, int NArrayReal, int NArrayInt,
389 template<class> class Allocator, class CellAssignor>
393 bool /*update*/,
394 bool verbose,
395 ParticleLocData pld) const
396{
397 AMREX_ASSERT(m_gdb != nullptr);
398
399 bool ok = Where(p, pld);
400
401 if (!ok && Geom(0).isAnyPeriodic())
402 {
403 // Attempt to shift the particle back into the domain if it
404 // crossed a periodic boundary.
405 PeriodicShift(p);
406 ok = Where(p, pld);
407 }
408
409 if (!ok) {
410 // invalidate the particle.
411 if (verbose) {
412 amrex::AllPrint()<< "Invalidating out-of-domain particle: " << p << '\n';
413 }
414
415 AMREX_ASSERT(p.id().is_valid());
416
417 p.id().make_invalid();
418 }
419
420 return pld;
421}
422
423template <typename ParticleType, int NArrayReal, int NArrayInt,
424 template<class> class Allocator, class CellAssignor>
425void
431
432template <typename ParticleType, int NArrayReal, int NArrayInt,
433 template<class> class Allocator, class CellAssignor>
434void
436{
438 int nlevs = std::max(0, finestLevel()+1);
439 m_particles.resize(nlevs);
440}
441
442template <typename ParticleType, int NArrayReal, int NArrayInt,
443 template<class> class Allocator, class CellAssignor>
444template <typename P>
445void
447 int lev_min, int lev_max, int nGrow, int local_grid) const
448{
449 bool success;
450 if (Geom(0).outsideRoundoffDomain(AMREX_D_DECL(p.pos(0), p.pos(1), p.pos(2))))
451 {
452 // Note that EnforcePeriodicWhere may shift the particle if it is successful.
453 success = EnforcePeriodicWhere(p, pld, lev_min, lev_max, local_grid);
454 if (!success && lev_min == 0)
455 {
456 // The particle has left the domain; invalidate it.
457 p.id().make_invalid();
458 success = true;
459 }
460 }
461 else
462 {
463 success = Where(p, pld, lev_min, lev_max, 0, local_grid);
464 }
465
466 if (!success)
467 {
468 success = (nGrow > 0) && Where(p, pld, lev_min, lev_min, nGrow);
469 pld.m_grown_gridbox = pld.m_gridbox; // reset grown box for subsequent calls.
470 }
471
472 if (!success)
473 {
474 amrex::Abort("ParticleContainer::locateParticle(): invalid particle.");
475 }
476}
477
478template <typename ParticleType, int NArrayReal, int NArrayInt,
479 template<class> class Allocator, class CellAssignor>
480Long
482{
483 Long nparticles = 0;
484 for (int lev = 0; lev <= finestLevel(); lev++) {
485 nparticles += NumberOfParticlesAtLevel(lev,only_valid,true);
486 }
487 if (!only_local) {
489 }
490 return nparticles;
491}
492
493template <typename ParticleType, int NArrayReal, int NArrayInt,
494 template<class> class Allocator, class CellAssignor>
497{
498 AMREX_ASSERT(lev >= 0 && lev < int(m_particles.size()));
499
500 LayoutData<Long> np_per_grid_local(ParticleBoxArray(lev),
501 ParticleDistributionMap(lev));
503 for (ParConstIterType pti(*this, lev); pti.isValid(); ++pti)
504 {
505 int gid = pti.index();
506 if (only_valid)
507 {
508 const auto& ptile = ParticlesAt(lev, pti);
509 const int np = ptile.numParticles();
510 auto const ptd = ptile.getConstParticleTileData();
511
512 ReduceOps<ReduceOpSum> reduce_op;
513 ReduceData<int> reduce_data(reduce_op);
514 using ReduceTuple = typename decltype(reduce_data)::Type;
515
516 reduce_op.eval(np, reduce_data,
517 [=] AMREX_GPU_DEVICE (int i) -> ReduceTuple
519 return (ptd.id(i).is_valid()) ? 1 : 0;
520 });
521
522 int np_valid = amrex::get<0>(reduce_data.value(reduce_op));
523 np_per_grid_local[gid] += np_valid;
524 } else
525 {
526 np_per_grid_local[gid] += pti.numParticles();
527 }
528 }
529
530 Vector<Long> nparticles(np_per_grid_local.size(), 0);
531 if (only_local)
532 {
533 for (ParConstIterType pti(*this, lev); pti.isValid(); ++pti)
535 nparticles[pti.index()] = np_per_grid_local[pti.index()];
536 }
537 }
538 else
540 ParallelDescriptor::GatherLayoutDataToVector(np_per_grid_local, nparticles,
542 ParallelDescriptor::Bcast(nparticles.data(), nparticles.size(),
544 }
545
546 return nparticles;
548
549template <typename ParticleType, int NArrayReal, int NArrayInt,
550 template<class> class Allocator, class CellAssignor>
552{
553 Long nparticles = 0;
554
555 if (level < 0 || level >= int(m_particles.size())) { return nparticles; }
556
557 if (only_valid) {
558 ReduceOps<ReduceOpSum> reduce_op;
559 ReduceData<unsigned long long> reduce_data(reduce_op);
560 using ReduceTuple = typename decltype(reduce_data)::Type;
562 for (const auto& kv : GetParticles(level)) {
563 const auto& ptile = kv.second;
564 auto const ptd = ptile.getConstParticleTileData();
566 reduce_op.eval(ptile.numParticles(), reduce_data,
567 [=] AMREX_GPU_DEVICE (int i) -> ReduceTuple
568 {
569 return (ptd.id(i).is_valid()) ? 1 : 0;
570 });
571 }
572
573 nparticles = static_cast<Long>(amrex::get<0>(reduce_data.value(reduce_op)));
574 }
575 else {
576 for (const auto& kv : GetParticles(level)) {
577 const auto& ptile = kv.second;
578 nparticles += ptile.numParticles();
580 }
581
582 if (!only_local) {
584 }
586 return nparticles;
587}
588
589template <typename ParticleType, int NArrayReal, int NArrayInt,
590 template<class> class Allocator, class CellAssignor>
591template <typename I, std::enable_if_t<std::is_integral_v<I> && (sizeof(I) >= sizeof(Long)), int> FOO>
592void
594{
595 AMREX_ASSERT(lev >= 0 && lev < int(m_particles.size()));
596
597 AMREX_ASSERT(BoxArray::SameRefs(mem.boxArray(), ParticleBoxArray(lev)) &&
598 mem.DistributionMap() == ParticleDistributionMap(lev));
599
600 [[maybe_unused]] Gpu::NoSyncRegion no_sync{};
601 for (ParConstIterType pti(*this, lev); pti.isValid(); ++pti)
602 {
603 int gid = pti.index();
604 mem[gid] += static_cast<I>(pti.capacity());
606}
608//
609// This includes both valid and invalid particles since invalid particles still take up space.
610//
611
612template <typename ParticleType, int NArrayReal, int NArrayInt,
613 template<class> class Allocator, class CellAssignor>
614std::array<Long, 3>
617{
618 Long cnt = 0;
619
620 for (unsigned lev = 0; lev < m_particles.size(); lev++) {
621 const auto& pmap = m_particles[lev];
622 for (const auto& kv : pmap) {
623 const auto& ptile = kv.second;
624 cnt += ptile.numParticles();
625 }
627
628 Long mn = cnt, mx = mn;
629
630 const int IOProc = ParallelContext::IOProcessorNumberSub();
631 const Long sz = sizeof(ParticleType)+NumRealComps()*sizeof(ParticleReal)+NumIntComps()*sizeof(int);
632
633#ifdef AMREX_LAZY
634 Lazy::QueueReduction( [=] () mutable {
635#endif
639
640 amrex::Print() << "ParticleContainer spread across MPI nodes - bytes (num particles): [Min: "
641 << mn*sz
642 << " (" << mn << ")"
643 << ", Max: "
644 << mx*sz
645 << " (" << mx << ")"
646 << ", Total: "
647 << cnt*sz
648 << " (" << cnt << ")]\n";
649#ifdef AMREX_LAZY
650 });
651#endif
652
653 return {mn*sz, mx*sz, cnt*sz};
655
656template <typename ParticleType, int NArrayReal, int NArrayInt,
657 template<class> class Allocator, class CellAssignor>
658std::array<Long, 3>
661{
662 Long cnt = 0;
664 for (unsigned lev = 0; lev < m_particles.size(); lev++) {
665 const auto& pmap = m_particles[lev];
666 for (const auto& kv : pmap) {
667 const auto& ptile = kv.second;
668 cnt += ptile.capacity();
670 }
671
672 Long mn = cnt, mx = mn;
673
674 const int IOProc = ParallelContext::IOProcessorNumberSub();
675
676#ifdef AMREX_LAZY
677 Lazy::QueueReduction( [=] () mutable {
678#endif
683 amrex::Print() << "ParticleContainer spread across MPI nodes - bytes: [Min: "
684 << mn
685 << ", Max: "
686 << mx
687 << ", Total: "
688 << cnt
689 << "]\n";
690#ifdef AMREX_LAZY
691 });
692#endif
694 return {mn, mx, cnt};
695}
696
697template <typename ParticleType, int NArrayReal, int NArrayInt,
698 template<class> class Allocator, class CellAssignor>
699void
701{
702 for (unsigned lev = 0; lev < m_particles.size(); lev++) {
703 auto& pmap = m_particles[lev];
704 for (auto& kv : pmap) {
705 auto& ptile = kv.second;
706 ptile.shrink_to_fit();
707 }
708 }
709}
710
717template <typename ParticleType, int NArrayReal, int NArrayInt,
718 template<class> class Allocator, class CellAssignor>
719void
721{
722 BL_PROFILE("ParticleContainer::Increment");
723
724 AMREX_ASSERT(OK());
725 if (m_particles.empty()) { return; }
726 AMREX_ASSERT(lev >= 0 && lev < int(m_particles.size()));
727 AMREX_ASSERT(numParticlesOutOfRange(*this, 0) == 0);
728
729 const auto& geom = Geom(lev);
730 const auto plo = geom.ProbLoArray();
731 const auto dxi = geom.InvCellSizeArray();
732 const auto domain = geom.Domain();
733 amrex::ParticleToMesh(*this, mf, lev,
734 [=] AMREX_GPU_DEVICE (const typename ParticleTileType::ConstParticleTileDataType& ptd, int ip,
735 amrex::Array4<amrex::Real> const& count)
736 {
737 const auto p = ptd[ip];
738 CellAssignor assignor;
739 IntVect iv = assignor(p, plo, dxi, domain);
740 amrex::Gpu::Atomic::AddNoRet(&count(iv), 1.0_rt);
741 }, false);
742}
743
744template <typename ParticleType, int NArrayReal, int NArrayInt,
745 template<class> class Allocator, class CellAssignor>
746Long
748{
749 BL_PROFILE("ParticleContainer::IncrementWithTotal(lev)");
750 Increment(mf, lev);
751 return TotalNumberOfParticles(true, local);
752}
753
754template <typename ParticleType, int NArrayReal, int NArrayInt,
755 template<class> class Allocator, class CellAssignor>
756void
758{
759 BL_PROFILE("ParticleContainer::RemoveParticlesAtLevel()");
760 if (level >= int(this->m_particles.size())) { return; }
761
762 if (!this->m_particles[level].empty())
763 {
764 ParticleLevel().swap(this->m_particles[level]);
765 }
766}
767
768template <typename ParticleType, int NArrayReal, int NArrayInt,
769 template<class> class Allocator, class CellAssignor>
770void
772{
773 BL_PROFILE("ParticleContainer::RemoveParticlesNotAtFinestLevel()");
774 AMREX_ASSERT(this->finestLevel()+1 == int(this->m_particles.size()));
775
776 Long cnt = 0;
777
778 for (unsigned lev = 0; lev < m_particles.size() - 1; ++lev) {
779 auto& pmap = m_particles[lev];
780 if (!pmap.empty()) {
781 for (auto& kv : pmap) {
782 const auto& pbx = kv.second;
783 cnt += pbx.numParticles();
784 }
785 ParticleLevel().swap(pmap);
786 }
787 }
788
789 //
790 // Print how many particles removed on each processor if any were removed.
791 //
792 if (this->m_verbose > 1 && cnt > 0) {
793 amrex::AllPrint() << "Processor " << ParallelContext::MyProcSub() << " removed " << cnt
794 << " particles not in finest level\n";
795 }
796}
797
799{
800
804
806 const GpuArray<Real, AMREX_SPACEDIM> & dxi, const Box& domain)
807 : m_assign_buffer_grid(assign_buffer_grid), m_plo(plo), m_dxi(dxi), m_domain(domain)
808 {}
809
810 template <typename SrcData>
812 int operator() (const SrcData& src, int src_i) const noexcept
813 {
814 auto iv = getParticleCell(src, src_i, m_plo, m_dxi, m_domain);
815 return (m_assign_buffer_grid(iv)!=-1);
816 }
817};
818
820{
821 template <typename DstData, typename SrcData>
823 void operator() (DstData& dst, const SrcData& src,
824 int src_i, int dst_i) const noexcept
825 {
826 copyParticle(dst, src, src_i, dst_i);
827
829 dst.cpu(dst_i) = 0;
830 }
831};
832
833
834template <typename ParticleType, int NArrayReal, int NArrayInt,
835 template<class> class Allocator, class CellAssignor>
836void
837ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>
838::CreateVirtualParticles (int level, AoS& virts) const
839{
840 ParticleTileType ptile;
841 CreateVirtualParticles(level, ptile);
842 ptile.GetArrayOfStructs().swap(virts);
843}
844
845template <typename ParticleType, int NArrayReal, int NArrayInt,
846 template<class> class Allocator, class CellAssignor>
847void
850{
851 BL_PROFILE("ParticleContainer::CreateVirtualParticles()");
852 AMREX_ASSERT(level > 0);
853 AMREX_ASSERT(virts.empty());
854
855 if (level >= static_cast<int>(m_particles.size())) {
856 return;
857 }
858
859 std::string aggregation_type = AggregationType();
860 int aggregation_buffer = AggregationBuffer();
861
862 if (aggregation_type == "None")
863 {
864 auto virts_offset = virts.numParticles();
865 for(ParConstIterType pti(*this, level); pti.isValid(); ++pti)
866 {
867 const auto& src_tile = ParticlesAt(level, pti);
868
869 auto np = src_tile.numParticles();
870 virts.resize(virts_offset+np);
871 transformParticles(virts, src_tile, 0, virts_offset, np, TransformerVirt());
872 virts_offset += np;
873 }
874 }
875 if (aggregation_type == "Cell")
876 {
877 //Components would be based on
878 int nComp = AMREX_SPACEDIM + NStructReal + NArrayReal;
879 // NArrayReal, NStructInt, NArrayInt behavior as before
880 int nGhost = 0;
881 MultiFab mf(ParticleBoxArray(level), ParticleDistributionMap(level), nComp, nGhost);
882
883 nComp = 1 + NStructInt + NArrayInt;
884 iMultiFab imf(ParticleBoxArray(level), ParticleDistributionMap(level), nComp, nGhost);
885
886 const auto& geom = Geom(level);
887 const auto plo = geom.ProbLoArray();
888 const auto dxi = geom.InvCellSizeArray();
889 const auto domain = geom.Domain();
890
891 BoxList bl_buffer;
892 bl_buffer.complementIn(Geom(level).Domain(), ParticleBoxArray(level));
893 BoxArray buffer(std::move(bl_buffer));
894 buffer.grow(aggregation_buffer);
895
897 locator.build(buffer, geom);
898 AssignGrid<DenseBinIteratorFactory<Box>> assign_buffer_grid = locator.getGridAssignor();
899
900 amrex::ParticleToMesh(*this, mf, level,
901 [=] AMREX_GPU_DEVICE (const ParticleType& p,
902 amrex::Array4<amrex::Real> const& partData,
905 {
906 auto iv = getParticleCell(p, plo_loc, dxi_loc, domain);
907 if(assign_buffer_grid(iv)==-1)
908 {
909 //Ordering may make this not unique
910 for (int i = 0; i < NArrayReal; ++i)
911 {
912 amrex::Gpu::Atomic::AddNoRet(&partData(iv,AMREX_SPACEDIM+NStructReal+i), partData(iv,AMREX_SPACEDIM)!=0.0 ? static_cast<Real>(0) : static_cast<Real>(p.rdata(NStructReal+i)));
913 }
914 //Add the rdata(0)-weighted sum of position
915 for (int i = 0; i < AMREX_SPACEDIM; ++i)
916 {
917 amrex::Gpu::Atomic::AddNoRet(&partData(iv,i), static_cast<Real>((p.rdata(0)*p.pos(i))));
918 }
919 //Add the rdata(0)-weighted sum of other rdata fields
920 for (int i = 1; i < NStructReal; ++i)
921 {
922 amrex::Gpu::Atomic::AddNoRet(&partData(iv,AMREX_SPACEDIM+i), static_cast<Real>((p.rdata(0)*p.rdata(i))));
923 }
924 //Add the rdata(0) sum
925 for (int i = 0; i < 1; ++i)
926 {
927 amrex::Gpu::Atomic::AddNoRet(&partData(iv,AMREX_SPACEDIM+i), static_cast<Real>(p.rdata(0)));
928 }
929 }
930
931 }); //skipping extra false argument, doing mf.setVal(0) at beginning
932
933 amrex::ParticleToMesh(*this, imf, level,
934 [=] AMREX_GPU_DEVICE (const ParticleType& p,
935 amrex::Array4<int> const& partData,
938 {
939
940 auto iv = getParticleCell(p, plo_loc, dxi_loc, domain);
941 if(assign_buffer_grid(iv)==-1)
942 {
943 //if this cell has no particle id info, do a straight copy to store idata
944 if(partData(iv,0)==0)
945 {
946 //Add 1 to indicate at least 1 particle at cell iv
947 amrex::Gpu::Atomic::AddNoRet(&partData(iv,0), 1);
948 for (int i = 0; i < NStructInt; ++i)
949 {
950 amrex::Gpu::Atomic::AddNoRet(&partData(iv,1+i), p.idata(i));
951 }
952 for (int i = 0; i < NArrayInt; ++i)
953 {
954 amrex::Gpu::Atomic::AddNoRet(&partData(iv,1+NStructInt+i), p.idata(NStructInt+i));
955 }
956 }
957 }
958 });
959
960 //There may be a better way to ensure virts is the right length
961 virts.resize(imf.sum(0));
962
963 int last_offset = 0;
964 for (MFIter mfi(mf); mfi.isValid(); ++mfi)
965 {
966 const auto bx = mfi.tilebox();
967 const auto partData = mf.array(mfi);
968 const auto imf_arr = imf.array(mfi);
969
970 Gpu::DeviceVector<int> offsets(bx.numPts());
971 auto *offsets_ptr = offsets.dataPtr();
972 int next_offset = Scan::ExclusiveSum((int) bx.numPts(),(imf_arr.ptr(bx.smallEnd(),0)),(offsets.dataPtr()),Scan::retSum);
973 auto dst = virts.getParticleTileData();
974 ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k)
975 {
976 if(imf_arr(i,j,k,0)!=0)
977 {
978 const auto idx = last_offset + offsets_ptr[
979 ((i-imf_arr.begin.x)
980 +(j-imf_arr.begin.y)*imf_arr.jstride
981 +(k-imf_arr.begin.z)*imf_arr.kstride)
982 ];
983
984 dst.cpu(idx) = 0;
986
987 auto& p = dst[idx];
988 //Set rdata(0) first so we can normalize the weighted fields
989 //Note that this does not work for soa PC
990 p.rdata(0) = static_cast<ParticleReal>(partData(i,j,k,AMREX_SPACEDIM));;
991 //Set pos with the normalized weighted field
992 for (int n = 0; n < AMREX_SPACEDIM; ++n)
993 {
994 p.pos(n) = static_cast<ParticleReal>(partData(i,j,k,n) / p.rdata(0));
995 }
996 //Set rdata(n>0) with the normalized weighted field for NStructReal
997 for (int n = 1; n < NStructReal; ++n)
998 {
999 p.rdata(n) = static_cast<ParticleReal>(partData(i,j,k,AMREX_SPACEDIM+n) / p.rdata(0));
1000 }
1001 //Set rdata(n>0) with the normalized weighted field for NArrayReal
1002 for (int n = 0; n < NArrayReal; ++n)
1003 {
1004 dst.rdata(n)[idx] = static_cast<ParticleReal>(partData(i,j,k,AMREX_SPACEDIM+NStructReal+n));
1005 }
1006 //Set idata with the "first" particles idata field for NStructInt
1007 for (int n = 0; n < NStructInt; ++n)
1008 {
1009 p.idata(n) = imf_arr(i,j,k,1+n);
1010 }
1011 //Set idata with the "first" particles idata field for NArrayInt
1012 for (int n = 0; n < NArrayInt; ++n)
1013 {
1014 dst.idata(n)[idx] = imf_arr(i,j,k,1+NStructInt+n);
1015 }
1016 }
1017
1018 });
1019 last_offset+=next_offset;
1021 }
1022
1023 // last_offset should equal virts.numParticles()
1024 auto virts_offset = last_offset;
1025 for(ParConstIterType pti(*this, level); pti.isValid(); ++pti)
1026 {
1027 const auto& src_tile = ParticlesAt(level, pti);
1028
1029 auto np = src_tile.numParticles();
1030 virts.resize(virts_offset+np);
1031 virts_offset += filterAndTransformParticles(virts, src_tile, FilterVirt(assign_buffer_grid,plo,dxi,domain), TransformerVirt(),0,virts_offset);
1033 }
1034 virts.resize(virts_offset);
1036 }
1037}
1038
1040{
1041
1044
1050 : m_lev_min(level), m_lev_max(level+1), m_nGrow(nGrow), m_gid(gid), m_assign_grid(assign_grid)
1051 {}
1052
1053 template <typename SrcData>
1055 int operator() (const SrcData& src, int src_i) const noexcept
1056 {
1057 const auto tup_min = (m_assign_grid)(src[src_i], m_lev_min, m_lev_max, m_nGrow, DefaultAssignor{});
1058 const auto tup_max = (m_assign_grid)(src[src_i], m_lev_max, m_lev_max, m_nGrow, DefaultAssignor{});
1059 const auto p_boxes = amrex::get<0>(tup_min);
1060 const auto p_boxes_max = amrex::get<0>(tup_max);
1061 const auto p_levs_max = amrex::get<1>(tup_max);
1062 return p_boxes_max >=0 && p_boxes == m_gid && p_levs_max == m_lev_max;
1063 }
1064};
1065
1067{
1068
1069 template <typename DstData, typename SrcData>
1071 void operator() (DstData& dst, const SrcData& src,
1072 int src_i, int dst_i) const noexcept
1073 {
1074 copyParticle(dst, src, src_i, dst_i);
1075
1076 dst.id(dst_i) = LongParticleIds::GhostParticleID;
1077 dst.cpu(dst_i) = 0;
1078 }
1079};
1080
1081template <typename ParticleType, int NArrayReal, int NArrayInt,
1082 template<class> class Allocator, class CellAssignor>
1083void
1084ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>
1085::CreateGhostParticles (int level, int nGrow, AoS& ghosts) const
1086{
1087 ParticleTileType ptile;
1088 CreateGhostParticles(level, nGrow, ptile);
1089 ptile.GetArrayOfStructs().swap(ghosts);
1090}
1091
1092template <typename ParticleType, int NArrayReal, int NArrayInt,
1093 template<class> class Allocator, class CellAssignor>
1094void
1096::CreateGhostParticles (int level, int nGrow, ParticleTileType& ghosts) const
1097{
1098 BL_PROFILE("ParticleContainer::CreateGhostParticles()");
1099 AMREX_ASSERT(ghosts.empty());
1100 AMREX_ASSERT(level < finestLevel());
1101
1102 if (level >= static_cast<int>(m_particles.size())) {
1103 return;
1104 }
1105
1106 if (! m_particle_locator.isValid(GetParGDB())) {
1107 m_particle_locator.build(GetParGDB());
1108 }
1109
1110 m_particle_locator.setGeometry(GetParGDB());
1111 AmrAssignGrid<DenseBinIteratorFactory<Box>> assign_grid = m_particle_locator.getGridAssignor();
1112 auto ghost_offset = ghosts.numParticles();
1113 for(ParConstIterType pti(*this, level); pti.isValid(); ++pti)
1114 {
1115 const auto& src_tile = ParticlesAt(level, pti);
1116 int gid = pti.index();
1117
1118 auto np = src_tile.numParticles();
1119 ghosts.resize(ghost_offset+np);
1120 ghost_offset += filterAndTransformParticles(ghosts, src_tile, AssignGridFilter(assign_grid,gid,level,nGrow), TransformerGhost(),0,ghost_offset);
1121 }
1122 ghosts.resize(ghost_offset);
1124}
1125
1126template <typename ParticleType, int NArrayReal, int NArrayInt,
1127 template<class> class Allocator, class CellAssignor>
1128void
1131{
1132 BL_PROFILE("ParticleContainer::clearParticles()");
1133
1134 for (int lev = 0; lev < static_cast<int>(m_particles.size()); ++lev)
1135 {
1136 for (auto& kv : m_particles[lev]) { kv.second.resize(0); }
1137 particle_detail::clearEmptyEntries(m_particles[lev]);
1138 }
1139}
1140
1141template <typename ParticleType, int NArrayReal, int NArrayInt,
1142 template<class> class Allocator, class CellAssignor>
1143template <class PCType, std::enable_if_t<IsParticleContainer<PCType>::value, int> foo>
1144void
1151
1152template <typename ParticleType, int NArrayReal, int NArrayInt,
1153 template<class> class Allocator, class CellAssignor>
1154template <class PCType, std::enable_if_t<IsParticleContainer<PCType>::value, int> foo>
1155void
1157addParticles (const PCType& other, bool local)
1158{
1160 addParticles(other, [] AMREX_GPU_HOST_DEVICE (const PData& /*data*/, int /*i*/) { return 1; }, local);
1161}
1162
1163template <typename ParticleType, int NArrayReal, int NArrayInt,
1164 template<class> class Allocator, class CellAssignor>
1165template <class F, class PCType,
1166 std::enable_if_t<IsParticleContainer<PCType>::value, int> foo,
1167 std::enable_if_t<! std::is_integral_v<F>, int> bar>
1168void
1170copyParticles (const PCType& other, F&& f, bool local)
1171{
1172 BL_PROFILE("ParticleContainer::copyParticles");
1173 clearParticles();
1174 addParticles(other, std::forward<F>(f), local);
1175}
1176
1177template <typename ParticleType, int NArrayReal, int NArrayInt,
1178 template<class> class Allocator, class CellAssignor>
1179template <class F, class PCType,
1180 std::enable_if_t<IsParticleContainer<PCType>::value, int> foo,
1181 std::enable_if_t<! std::is_integral_v<F>, int> bar>
1182void
1184addParticles (const PCType& other, F const& f, bool local)
1185{
1186 BL_PROFILE("ParticleContainer::addParticles");
1187
1188 // touch all tiles in serial
1189 for (int lev = 0; lev < other.numLevels(); ++lev)
1190 {
1191 [[maybe_unused]] Gpu::NoSyncRegion no_sync{};
1192 const auto& plevel_other = other.GetParticles(lev);
1193 for(MFIter mfi = other.MakeMFIter(lev); mfi.isValid(); ++mfi)
1194 {
1195 auto index = std::make_pair(mfi.index(), mfi.LocalTileIndex());
1196 if(plevel_other.find(index) == plevel_other.end()) { continue; }
1197
1198 DefineAndReturnParticleTile(lev, mfi.index(), mfi.LocalTileIndex());
1199 }
1200 }
1201
1202#ifdef AMREX_USE_OMP
1203#pragma omp parallel if (Gpu::notInLaunchRegion())
1204#endif
1205 for (int lev = 0; lev < other.numLevels(); ++lev)
1206 {
1207 const auto& plevel_other = other.GetParticles(lev);
1208 for(MFIter mfi = other.MakeMFIter(lev); mfi.isValid(); ++mfi)
1209 {
1210 auto index = std::make_pair(mfi.index(), mfi.LocalTileIndex());
1211 if(plevel_other.find(index) == plevel_other.end()) { continue; }
1212
1213 // this has already had define() called above
1214 auto& ptile = ParticlesAt(lev, mfi.index(), mfi.LocalTileIndex());
1215 const auto& ptile_other = plevel_other.at(index);
1216 auto np = ptile_other.numParticles();
1217 if (np == 0) { continue; }
1218
1219 auto dst_index = ptile.numParticles();
1220 ptile.resize(dst_index + np);
1221
1222 auto count = amrex::filterParticles(ptile, ptile_other, f, 0, dst_index, np);
1223
1224 ptile.resize(dst_index + count);
1225 }
1226 }
1227
1228 if (! local) { Redistribute(); }
1229}
1230
1231//
1232// This redistributes valid particles and discards invalid ones.
1233//
1234template <typename ParticleType, int NArrayReal, int NArrayInt,
1235 template<class> class Allocator, class CellAssignor>
1236void
1237ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>
1238::Redistribute (int lev_min, int lev_max, int nGrow, int local, bool remove_negative)
1240 BL_PROFILE_SYNC_START_TIMED("SyncBeforeComms: Redist");
1241
1242#ifdef AMREX_USE_GPU
1243 if ( Gpu::inLaunchRegion() )
1244 {
1245 RedistributeGPU(lev_min, lev_max, nGrow, local, remove_negative);
1246 }
1247 else
1249 RedistributeCPU(lev_min, lev_max, nGrow, local, remove_negative);
1250 }
1251#else
1252 RedistributeCPU(lev_min, lev_max, nGrow, local, remove_negative);
1253#endif
1254
1256}
1257
1258template <typename ParticleType, int NArrayReal, int NArrayInt,
1259 template<class> class Allocator, class CellAssignor>
1260template <class index_type>
1261void
1262ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>
1263::ReorderParticles (int lev, const MFIter& mfi, const index_type* permutations)
1264{
1265 auto& ptile = ParticlesAt(lev, mfi);
1266 const size_t np = ptile.numParticles();
1267 const size_t np_total = np + ptile.numNeighborParticles();
1268
1269 if (memEfficientSort) {
1270#if defined(AMREX_USE_CUDA) && defined(_WIN32)
1271 if (!ParticleType::is_soa_particle) {
1272#else
1273 if constexpr (!ParticleType::is_soa_particle) {
1274#endif
1275 static_assert(sizeof(ParticleType)%4 == 0 && sizeof(uint32_t) == 4);
1276 using tmp_t = std::conditional_t<sizeof(ParticleType)%8 == 0,
1277 uint64_t, uint32_t>;
1278 constexpr std::size_t nchunks = sizeof(ParticleType) / sizeof(tmp_t);
1280 auto* ptmp = tmp.data();
1281 auto* paos = (tmp_t*)(ptile.getParticleTileData().m_aos);
1282 for (std::size_t ichunk = 0; ichunk < nchunks; ++ichunk) {
1283 // Do not need to reorder neighbor particles
1285 {
1286 ptmp[i] = paos[permutations[i]*nchunks+ichunk];
1287 });
1289 {
1290 paos[i*nchunks+ichunk] = ptmp[i];
1291 });
1292 }
1294 } else {
1295 typename SoA::IdCPU tmp_idcpu;
1296 if constexpr (has_polymorphic_allocator) {
1297 tmp_idcpu.setArena(arena());
1298 }
1299 tmp_idcpu.resize(np_total);
1300 auto src = ptile.GetStructOfArrays().GetIdCPUData().data();
1301 uint64_t* dst = tmp_idcpu.data();
1302 AMREX_HOST_DEVICE_FOR_1D( np_total, i,
1303 {
1304 dst[i] = i < np ? src[permutations[i]] : src[i];
1305 });
1306
1308
1309 ptile.GetStructOfArrays().GetIdCPUData().swap(tmp_idcpu);
1310 }
1311
1312 { // Create a scope for the temporary vector below
1313 RealVector tmp_real;
1314 if constexpr (has_polymorphic_allocator) {
1315 tmp_real.setArena(arena());
1316 }
1317 tmp_real.resize(np_total);
1318 for (int comp = 0; comp < NArrayReal + m_num_runtime_real; ++comp) {
1319 auto src = ptile.GetStructOfArrays().GetRealData(comp).data();
1320 ParticleReal* dst = tmp_real.data();
1321 AMREX_HOST_DEVICE_FOR_1D( np_total, i,
1322 {
1323 dst[i] = i < np ? src[permutations[i]] : src[i];
1324 });
1325
1327
1328 ptile.GetStructOfArrays().GetRealData(comp).swap(tmp_real);
1329 }
1330 }
1331
1332 IntVector tmp_int;
1333 if constexpr (has_polymorphic_allocator) {
1334 tmp_int.setArena(arena());
1335 }
1336 tmp_int.resize(np_total);
1337 for (int comp = 0; comp < NArrayInt + m_num_runtime_int; ++comp) {
1338 auto src = ptile.GetStructOfArrays().GetIntData(comp).data();
1339 int* dst = tmp_int.data();
1340 AMREX_HOST_DEVICE_FOR_1D( np_total , i,
1341 {
1342 dst[i] = i < np ? src[permutations[i]] : src[i];
1343 });
1344
1346
1347 ptile.GetStructOfArrays().GetIntData(comp).swap(tmp_int);
1348 }
1349 } else {
1350 ParticleTileType ptile_tmp;
1351 ptile_tmp.define(m_num_runtime_real, m_num_runtime_int,
1352 &m_soa_rdata_names, &m_soa_idata_names, arena());
1353 ptile_tmp.resize(np_total);
1354 // copy re-ordered particles
1355 gatherParticles(ptile_tmp, ptile, np, permutations);
1356 // copy neighbor particles
1357 amrex::copyParticles(ptile_tmp, ptile, np, np, np_total-np);
1358 ptile.swap(ptile_tmp);
1359 }
1360}
1361
1362template <typename ParticleType, int NArrayReal, int NArrayInt,
1363 template<class> class Allocator, class CellAssignor>
1364void
1369
1370template <typename ParticleType, int NArrayReal, int NArrayInt,
1371 template<class> class Allocator, class CellAssignor>
1372void
1375{
1376 BL_PROFILE("ParticleContainer::SortParticlesByBin()");
1377
1378 if (bin_size == IntVect::TheZeroVector()) { return; }
1379
1380 for (int lev = 0; lev < numLevels(); ++lev)
1381 {
1382 const Geometry& geom = Geom(lev);
1383 const auto dxi = geom.InvCellSizeArray();
1384 const auto plo = geom.ProbLoArray();
1385 const auto domain = geom.Domain();
1386
1387 for(MFIter mfi = MakeMFIter(lev); mfi.isValid(); ++mfi)
1388 {
1389 auto& ptile = ParticlesAt(lev, mfi);
1390 const size_t np = ptile.numParticles();
1391
1392 const Box& box = mfi.validbox();
1393
1394 int ntiles = numTilesInBox(box, true, bin_size);
1395
1396 m_bins.build(np, ptile.getParticleTileData(), ntiles,
1397 GetParticleBin{plo, dxi, domain, bin_size, box});
1398 ReorderParticles(lev, mfi, m_bins.permutationPtr());
1399 }
1400 }
1401}
1402
1403template <typename ParticleType, int NArrayReal, int NArrayInt,
1404 template<class> class Allocator, class CellAssignor>
1405void
1408{
1409 BL_PROFILE("ParticleContainer::SortParticlesForDeposition()");
1410
1411 for (int lev = 0; lev < numLevels(); ++lev)
1412 {
1413 const Geometry& geom = Geom(lev);
1414
1415 for(MFIter mfi = MakeMFIter(lev); mfi.isValid(); ++mfi)
1416 {
1417 const auto& ptile = ParticlesAt(lev, mfi);
1418 const size_t np = ptile.numParticles();
1419
1420 const Box& box = mfi.validbox();
1421
1422 using index_type = typename decltype(m_bins)::index_type;
1424 PermutationForDeposition<index_type>(perm, np, ptile, box, geom, idx_type);
1425 ReorderParticles(lev, mfi, perm.dataPtr());
1426 }
1427 }
1428}
1429
1430//
1431// The GPU implementation of Redistribute
1432//
1433template <typename ParticleType, int NArrayReal, int NArrayInt,
1434 template<class> class Allocator, class CellAssignor>
1435void
1437::RedistributeGPU (int lev_min, int lev_max, int nGrow, int local, bool remove_negative)
1438{
1439#ifdef AMREX_USE_GPU
1440
1441 if (local) { AMREX_ASSERT(numParticlesOutOfRange(*this, lev_min, lev_max, local) == 0); }
1442
1443 // sanity check
1444 AMREX_ALWAYS_ASSERT(do_tiling == false);
1446 BL_PROFILE("ParticleContainer::RedistributeGPU()");
1447 BL_PROFILE_VAR_NS("Redistribute_partition", blp_partition);
1448
1449 int theEffectiveFinestLevel = m_gdb->finestLevel();
1450 while (!m_gdb->LevelDefined(theEffectiveFinestLevel)) { theEffectiveFinestLevel--; }
1451
1452 if (int(m_particles.size()) < theEffectiveFinestLevel+1) {
1453 if (Verbose()) {
1454 amrex::Print() << "ParticleContainer::Redistribute() resizing containers from "
1455 << m_particles.size() << " to "
1456 << theEffectiveFinestLevel + 1 << '\n';
1457 }
1458 m_particles.resize(theEffectiveFinestLevel+1);
1459 m_dummy_mf.resize(theEffectiveFinestLevel+1);
1461
1462 for (int lev = 0; lev < theEffectiveFinestLevel+1; ++lev) { RedefineDummyMF(lev); }
1463
1464 int finest_lev_particles;
1465 if (lev_max == -1) {
1466 lev_max = theEffectiveFinestLevel;
1467 finest_lev_particles = m_particles.size() - 1;
1468 } else {
1469 finest_lev_particles = lev_max;
1470 }
1471 AMREX_ASSERT(lev_max <= finestLevel());
1472
1473 this->defineBufferMap();
1475 if (! m_particle_locator.isValid(GetParGDB())) { m_particle_locator.build(GetParGDB()); }
1476 m_particle_locator.setGeometry(GetParGDB());
1477 auto assign_grid = m_particle_locator.getGridAssignor();
1478
1479 BL_PROFILE_VAR_START(blp_partition);
1480 ParticleCopyOp op;
1481 int num_levels = finest_lev_particles + 1;
1482 op.setNumLevels(num_levels);
1483 Vector<std::map<int, int> > new_sizes(num_levels);
1484 const auto plo = Geom(0).ProbLoArray();
1485 const auto phi = Geom(0).ProbHiArray();
1486 const auto rlo = Geom(0).ProbLoArrayInParticleReal();
1487 const auto rhi = Geom(0).ProbHiArrayInParticleReal();
1488 const auto is_per = Geom(0).isPeriodicArray();
1489 for (int lev = lev_min; lev <= finest_lev_particles; ++lev)
1490 {
1491 auto& plev = m_particles[lev];
1492 for (auto& kv : plev)
1493 {
1494 int gid = kv.first.first;
1495 int tid = kv.first.second;
1496 auto index = std::make_pair(gid, tid);
1497
1498 auto& src_tile = plev[index];
1499 const size_t np = src_tile.numParticles();
1500
1501 int num_stay = partitionParticlesByDest(src_tile, assign_grid,
1502 std::forward<CellAssignor>(CellAssignor{}),
1503 BufferMap(),
1504 plo, phi, rlo, rhi, is_per, lev, gid, tid,
1505 lev_min, lev_max, nGrow, remove_negative);
1506
1507 int num_move = np - num_stay;
1508 new_sizes[lev][gid] = num_stay;
1509 op.resize(gid, lev, num_move);
1510
1511 auto p_boxes = op.m_boxes[lev][gid].dataPtr();
1512 auto p_levs = op.m_levels[lev][gid].dataPtr();
1513 auto p_src_indices = op.m_src_indices[lev][gid].dataPtr();
1514 auto p_periodic_shift = op.m_periodic_shift[lev][gid].dataPtr();
1515 auto ptd = src_tile.getParticleTileData();
1516
1517 AMREX_FOR_1D ( num_move, i,
1518 {
1519 const auto p = ptd[i + num_stay];
1520
1521 if (!p.id().is_valid())
1522 {
1523 p_boxes[i] = -1;
1524 p_levs[i] = -1;
1525 }
1526 else
1527 {
1528 const auto tup = assign_grid(p, lev_min, lev_max, nGrow,
1529 std::forward<CellAssignor>(CellAssignor{}));
1530 p_boxes[i] = amrex::get<0>(tup);
1531 p_levs[i] = amrex::get<1>(tup);
1532 }
1533 p_periodic_shift[i] = IntVect(AMREX_D_DECL(0,0,0));
1534 p_src_indices[i] = i+num_stay;
1535 });
1536 }
1538 BL_PROFILE_VAR_STOP(blp_partition);
1539
1540 ParticleCopyPlan plan;
1541
1542 plan.build(*this, op, h_redistribute_int_comp,
1543 h_redistribute_real_comp, local);
1544
1545 // by default, this uses The_Arena();
1548
1549 if (use_comms_arena) {
1550 snd_buffer.setArena(The_Comms_Arena());
1551 rcv_buffer.setArena(The_Comms_Arena());
1552 }
1553
1554 packBuffer(*this, op, plan, snd_buffer);
1555
1556 // clear particles from container
1557 for (int lev = lev_min; lev <= lev_max; ++lev)
1558 {
1559 auto& plev = m_particles[lev];
1560 for (auto& kv : plev)
1561 {
1562 int gid = kv.first.first;
1563 int tid = kv.first.second;
1564 auto index = std::make_pair(gid, tid);
1565 auto& tile = plev[index];
1566 tile.resize(new_sizes[lev][gid]);
1567 }
1568 }
1569
1570 for (int lev = lev_min; lev <= lev_max; lev++)
1571 {
1572 particle_detail::clearEmptyEntries(m_particles[lev]);
1573 }
1574
1575 if (int(m_particles.size()) > theEffectiveFinestLevel+1) {
1576 if (m_verbose > 0) {
1577 amrex::Print() << "ParticleContainer::Redistribute() resizing m_particles from "
1578 << m_particles.size() << " to " << theEffectiveFinestLevel+1 << '\n';
1579 }
1580 AMREX_ASSERT(int(m_particles.size()) >= 2);
1581
1582 m_particles.resize(theEffectiveFinestLevel + 1);
1583 m_dummy_mf.resize(theEffectiveFinestLevel + 1);
1584 }
1585
1587 {
1588 plan.buildMPIFinish(BufferMap());
1589 communicateParticlesStart(*this, plan, snd_buffer, rcv_buffer);
1590 unpackBuffer(*this, plan, snd_buffer, RedistributeUnpackPolicy());
1592 unpackRemotes(*this, plan, rcv_buffer, RedistributeUnpackPolicy());
1593 }
1594 else
1595 {
1597 Gpu::PinnedVector<char> pinned_snd_buffer;
1598 Gpu::PinnedVector<char> pinned_rcv_buffer;
1599
1600 if (snd_buffer.arena()->isPinned()) {
1601 plan.buildMPIFinish(BufferMap());
1603 communicateParticlesStart(*this, plan, snd_buffer, pinned_rcv_buffer);
1604 } else {
1605 pinned_snd_buffer.resize(snd_buffer.size());
1606 Gpu::dtoh_memcpy_async(pinned_snd_buffer.dataPtr(), snd_buffer.dataPtr(), snd_buffer.size());
1607 plan.buildMPIFinish(BufferMap());
1609 communicateParticlesStart(*this, plan, pinned_snd_buffer, pinned_rcv_buffer);
1610 }
1611
1612 rcv_buffer.resize(pinned_rcv_buffer.size());
1613 unpackBuffer(*this, plan, snd_buffer, RedistributeUnpackPolicy());
1615 Gpu::htod_memcpy_async(rcv_buffer.dataPtr(), pinned_rcv_buffer.dataPtr(), pinned_rcv_buffer.size());
1616 unpackRemotes(*this, plan, rcv_buffer, RedistributeUnpackPolicy());
1617 }
1618
1620 AMREX_ASSERT(numParticlesOutOfRange(*this, lev_min, lev_max, nGrow) == 0);
1621#else
1622 amrex::ignore_unused(lev_min,lev_max,nGrow,local,remove_negative);
1623#endif
1624}
1625
1626//
1627// The CPU implementation of Redistribute
1628//
1629template <typename ParticleType, int NArrayReal, int NArrayInt,
1630 template<class> class Allocator, class CellAssignor>
1631void
1632ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>
1633::RedistributeCPU (int lev_min, int lev_max, int nGrow, int local, bool remove_negative)
1634{
1635 BL_PROFILE("ParticleContainer::RedistributeCPU()");
1636
1637 const int MyProc = ParallelContext::MyProcSub();
1638 auto strttime = amrex::second();
1639
1640 if (local > 0) { BuildRedistributeMask(0, local); }
1641
1642 // On startup there are cases where Redistribute() could be called
1643 // with a given finestLevel() where that AmrLevel has yet to be defined.
1644 int theEffectiveFinestLevel = m_gdb->finestLevel();
1645
1646 while (!m_gdb->LevelDefined(theEffectiveFinestLevel)) {
1647 theEffectiveFinestLevel--;
1648 }
1649
1650 if (int(m_particles.size()) < theEffectiveFinestLevel+1) {
1651 if (Verbose()) {
1652 amrex::Print() << "ParticleContainer::Redistribute() resizing containers from "
1653 << m_particles.size() << " to "
1654 << theEffectiveFinestLevel + 1 << '\n';
1655 }
1656 m_particles.resize(theEffectiveFinestLevel+1);
1657 m_dummy_mf.resize(theEffectiveFinestLevel+1);
1658 }
1659
1660 // It is important to do this even if we don't have more levels because we may have changed the
1661 // grids at this level in a regrid.
1662 for (int lev = 0; lev < theEffectiveFinestLevel+1; ++lev) {
1663 RedefineDummyMF(lev);
1664 }
1665
1666 int finest_lev_particles;
1667 if (lev_max == -1) {
1668 lev_max = theEffectiveFinestLevel;
1669 finest_lev_particles = m_particles.size() - 1;
1670 } else {
1671 finest_lev_particles = lev_max;
1672 }
1673 AMREX_ASSERT(lev_max <= finestLevel());
1674
1675 // This will hold the valid particles that go to another process
1676 std::map<int, Vector<char> > not_ours;
1677
1678 int num_threads = OpenMP::get_max_threads();
1679
1680 // these are temporary buffers for each thread
1681 std::map<int, Vector<Vector<char> > > tmp_remote;
1684 tmp_local.resize(theEffectiveFinestLevel+1);
1685 soa_local.resize(theEffectiveFinestLevel+1);
1686
1687 // we resize these buffers outside the parallel region
1688 for (int lev = lev_min; lev <= lev_max; lev++) {
1689 for (MFIter mfi(*m_dummy_mf[lev], this->do_tiling ? this->tile_size : IntVect::TheZeroVector());
1690 mfi.isValid(); ++mfi) {
1691 auto index = std::make_pair(mfi.index(), mfi.LocalTileIndex());
1692 tmp_local[lev][index].resize(num_threads);
1693 soa_local[lev][index].resize(num_threads);
1694 for (int t = 0; t < num_threads; ++t) {
1695 soa_local[lev][index][t].define(m_num_runtime_real, m_num_runtime_int,
1696 &m_soa_rdata_names, &m_soa_idata_names);
1697 if constexpr (has_polymorphic_allocator) {
1698 if constexpr (ParticleType::is_soa_particle) {
1699 soa_local[lev][index][t].GetIdCPUData().setArena(arena());
1700 } else {
1701 tmp_local[lev][index][t].setArena(arena());
1702 }
1703 for (int j = 0; j < soa_local[lev][index][t].NumRealComps(); ++j) {
1704 soa_local[lev][index][t].GetRealData(j).setArena(arena());
1705 }
1706 for (int j = 0; j < soa_local[lev][index][t].NumIntComps(); ++j) {
1707 soa_local[lev][index][t].GetIntData(j).setArena(arena());
1708 }
1709 }
1710 }
1711 }
1712 }
1713 if (local) {
1714 for (int i = 0; i < neighbor_procs.size(); ++i) {
1715 tmp_remote[neighbor_procs[i]].resize(num_threads);
1716 }
1717 } else {
1718 for (int i = 0; i < ParallelContext::NProcsSub(); ++i) {
1719 tmp_remote[i].resize(num_threads);
1720 }
1721 }
1722
1723 // first pass: for each tile in parallel, in each thread copies the particles that
1724 // need to be moved into it's own, temporary buffer.
1725 for (int lev = lev_min; lev <= finest_lev_particles; lev++) {
1726 auto& pmap = m_particles[lev];
1727
1728 Vector<std::pair<int, int> > grid_tile_ids;
1729 Vector<ParticleTileType*> ptile_ptrs;
1730 for (auto& kv : pmap)
1731 {
1732 grid_tile_ids.push_back(kv.first);
1733 ptile_ptrs.push_back(&(kv.second));
1734 }
1735
1736#ifdef AMREX_USE_OMP
1737#pragma omp parallel for
1738#endif
1739 for (int pmap_it = 0; pmap_it < static_cast<int>(ptile_ptrs.size()); ++pmap_it)
1740 {
1741 int thread_num = OpenMP::get_thread_num();
1742 int grid = grid_tile_ids[pmap_it].first;
1743 int tile = grid_tile_ids[pmap_it].second;
1744 auto& soa = ptile_ptrs[pmap_it]->GetStructOfArrays();
1745 auto& aos = ptile_ptrs[pmap_it]->GetArrayOfStructs();
1746
1747 // AMREX_ASSERT_WITH_MESSAGE((NumRealComps() == 0 && NumIntComps() == 0)
1748 // || aos.size() == soa.size(),
1749 // "The AoS and SoA data on this tile are different sizes - "
1750 // "perhaps particles have not been initialized correctly?");
1751 unsigned npart = ptile_ptrs[pmap_it]->numParticles();
1752 ParticleLocData pld;
1753
1754 if constexpr (!ParticleType::is_soa_particle){
1755
1756 if (npart != 0) {
1757 Long last = npart - 1;
1758 Long pindex = 0;
1759 while (pindex <= last) {
1760 ParticleType& p = aos[pindex];
1761
1762 if ((remove_negative == false) && (!p.id().is_valid())) {
1763 ++pindex;
1764 continue;
1765 }
1766
1767 if (!p.id().is_valid())
1768 {
1769 aos[pindex] = aos[last];
1770 for (int comp = 0; comp < NumRealComps(); comp++) {
1771 soa.GetRealData(comp)[pindex] = soa.GetRealData(comp)[last];
1772 }
1773 for (int comp = 0; comp < NumIntComps(); comp++) {
1774 soa.GetIntData(comp)[pindex] = soa.GetIntData(comp)[last];
1775 }
1776 correctCellVectors(last, pindex, grid, aos[pindex]);
1777 --last;
1778 continue;
1779 }
1780
1781 locateParticle(p, pld, lev_min, lev_max, nGrow, local ? grid : -1);
1782
1783 particlePostLocate(p, pld, lev);
1784
1785 if (!p.id().is_valid())
1786 {
1787 aos[pindex] = aos[last];
1788 for (int comp = 0; comp < NumRealComps(); comp++) {
1789 soa.GetRealData(comp)[pindex] = soa.GetRealData(comp)[last];
1790 }
1791 for (int comp = 0; comp < NumIntComps(); comp++) {
1792 soa.GetIntData(comp)[pindex] = soa.GetIntData(comp)[last];
1793 }
1794 correctCellVectors(last, pindex, grid, aos[pindex]);
1795 --last;
1796 continue;
1797 }
1798
1799 const int who = ParallelContext::global_to_local_rank(ParticleDistributionMap(pld.m_lev)[pld.m_grid]);
1800 if (who == MyProc) {
1801 if (pld.m_lev != lev || pld.m_grid != grid || pld.m_tile != tile) {
1802 // We own it but must shift it to another place.
1803 auto index = std::make_pair(pld.m_grid, pld.m_tile);
1804 AMREX_ASSERT(tmp_local[pld.m_lev][index].size() == num_threads);
1805 tmp_local[pld.m_lev][index][thread_num].push_back(p);
1806 for (int comp = 0; comp < NumRealComps(); ++comp) {
1807 RealVector& arr = soa_local[pld.m_lev][index][thread_num].GetRealData(comp);
1808 arr.push_back(soa.GetRealData(comp)[pindex]);
1809 }
1810 for (int comp = 0; comp < NumIntComps(); ++comp) {
1811 IntVector& arr = soa_local[pld.m_lev][index][thread_num].GetIntData(comp);
1812 arr.push_back(soa.GetIntData(comp)[pindex]);
1813 }
1814
1815 p.id().make_invalid(); // Invalidate the particle
1816 }
1817 }
1818 else {
1819 auto& particles_to_send = tmp_remote[who][thread_num];
1820 auto old_size = particles_to_send.size();
1821 auto new_size = old_size + superparticle_size;
1822 particles_to_send.resize(new_size);
1823 std::memcpy(&particles_to_send[old_size], &p, particle_size);
1824 char* dst = &particles_to_send[old_size] + particle_size;
1825 int array_comp_start = 0;
1826 if constexpr (!ParticleType::is_soa_particle) {
1827 array_comp_start = AMREX_SPACEDIM + NStructReal;
1828 }
1829 for (int comp = 0; comp < NumRealComps(); comp++) {
1830 if (h_redistribute_real_comp[array_comp_start + comp]) {
1831 std::memcpy(dst, &soa.GetRealData(comp)[pindex], sizeof(ParticleReal));
1832 dst += sizeof(ParticleReal);
1833 }
1834 }
1835 array_comp_start = 2 + NStructInt;
1836 for (int comp = 0; comp < NumIntComps(); comp++) {
1837 if (h_redistribute_int_comp[array_comp_start + comp]) {
1838 std::memcpy(dst, &soa.GetIntData(comp)[pindex], sizeof(int));
1839 dst += sizeof(int);
1840 }
1841 }
1842
1843 p.id().make_invalid(); // Invalidate the particle
1844 }
1845
1846 if (!p.id().is_valid())
1847 {
1848 aos[pindex] = aos[last];
1849 for (int comp = 0; comp < NumRealComps(); comp++) {
1850 soa.GetRealData(comp)[pindex] = soa.GetRealData(comp)[last];
1851 }
1852 for (int comp = 0; comp < NumIntComps(); comp++) {
1853 soa.GetIntData(comp)[pindex] = soa.GetIntData(comp)[last];
1854 }
1855 correctCellVectors(last, pindex, grid, aos[pindex]);
1856 --last;
1857 continue;
1858 }
1859
1860 ++pindex;
1861 }
1862
1863 aos().erase(aos().begin() + last + 1, aos().begin() + npart);
1864 for (int comp = 0; comp < NumRealComps(); comp++) {
1865 RealVector& rdata = soa.GetRealData(comp);
1866 rdata.erase(rdata.begin() + last + 1, rdata.begin() + npart);
1867 }
1868 for (int comp = 0; comp < NumIntComps(); comp++) {
1869 IntVector& idata = soa.GetIntData(comp);
1870 idata.erase(idata.begin() + last + 1, idata.begin() + npart);
1871 }
1872 }
1873
1874 } else { // soa particle
1875
1876 auto particle_tile = ptile_ptrs[pmap_it];
1877 if (npart != 0) {
1878 Long last = npart - 1;
1879 Long pindex = 0;
1880 auto ptd = particle_tile->getParticleTileData();
1881 while (pindex <= last) {
1882 ParticleType p = ptd[pindex];
1883
1884 if ((remove_negative == false) && (!ptd.id(pindex).is_valid())) {
1885 ++pindex;
1886 continue;
1887 }
1888
1889 if (!ptd.id(pindex).is_valid()){
1890 soa.GetIdCPUData()[pindex] = soa.GetIdCPUData()[last];
1891 for (int comp = 0; comp < NumRealComps(); comp++) {
1892 soa.GetRealData(comp)[pindex] = soa.GetRealData(comp)[last];
1893 }
1894 for (int comp = 0; comp < NumIntComps(); comp++) {
1895 soa.GetIntData(comp)[pindex] = soa.GetIntData(comp)[last];
1896 }
1897 correctCellVectors(last, pindex, grid, ptd[pindex]);
1898 --last;
1899 continue;
1900 }
1901
1902 locateParticle(p, pld, lev_min, lev_max, nGrow, local ? grid : -1);
1903
1904 particlePostLocate(p, pld, lev);
1905
1906 if (!ptd.id(pindex).is_valid()) {
1907 soa.GetIdCPUData()[pindex] = soa.GetIdCPUData()[last];
1908 for (int comp = 0; comp < NumRealComps(); comp++) {
1909 soa.GetRealData(comp)[pindex] = soa.GetRealData(comp)[last];
1910 }
1911 for (int comp = 0; comp < NumIntComps(); comp++) {
1912 soa.GetIntData(comp)[pindex] = soa.GetIntData(comp)[last];
1913 }
1914 correctCellVectors(last, pindex, grid, ptd[pindex]);
1915 --last;
1916 continue;
1917 }
1918
1919 const int who = ParallelContext::global_to_local_rank(ParticleDistributionMap(pld.m_lev)[pld.m_grid]);
1920 if (who == MyProc) {
1921 if (pld.m_lev != lev || pld.m_grid != grid || pld.m_tile != tile) {
1922 // We own it but must shift it to another place.
1923 auto index = std::make_pair(pld.m_grid, pld.m_tile);
1924 AMREX_ASSERT(soa_local[pld.m_lev][index].size() == num_threads);
1925 {
1926 auto& arr = soa_local[pld.m_lev][index][thread_num].GetIdCPUData();
1927 arr.push_back(soa.GetIdCPUData()[pindex]);
1928 }
1929 for (int comp = 0; comp < NumRealComps(); ++comp) {
1930 RealVector& arr = soa_local[pld.m_lev][index][thread_num].GetRealData(comp);
1931 arr.push_back(soa.GetRealData(comp)[pindex]);
1932 }
1933 for (int comp = 0; comp < NumIntComps(); ++comp) {
1934 IntVector& arr = soa_local[pld.m_lev][index][thread_num].GetIntData(comp);
1935 arr.push_back(soa.GetIntData(comp)[pindex]);
1936 }
1937
1938 ptd.id(pindex).make_invalid(); // Invalidate the particle
1939 }
1940 }
1941 else {
1942 auto& particles_to_send = tmp_remote[who][thread_num];
1943 auto old_size = particles_to_send.size();
1944 auto new_size = old_size + superparticle_size;
1945 particles_to_send.resize(new_size);
1946
1947 char* dst = &particles_to_send[old_size];
1948 {
1949 std::memcpy(dst, &soa.GetIdCPUData()[pindex], sizeof(uint64_t));
1950 dst += sizeof(uint64_t);
1951 }
1952 int array_comp_start = 0;
1953 if constexpr (!ParticleType::is_soa_particle) {
1954 array_comp_start = AMREX_SPACEDIM + NStructReal;
1955 }
1956 for (int comp = 0; comp < NumRealComps(); comp++) {
1957 if (h_redistribute_real_comp[array_comp_start + comp]) {
1958 std::memcpy(dst, &soa.GetRealData(comp)[pindex], sizeof(ParticleReal));
1959 dst += sizeof(ParticleReal);
1960 }
1961 }
1962 array_comp_start = 2 + NStructInt;
1963 for (int comp = 0; comp < NumIntComps(); comp++) {
1964 if (h_redistribute_int_comp[array_comp_start + comp]) {
1965 std::memcpy(dst, &soa.GetIntData(comp)[pindex], sizeof(int));
1966 dst += sizeof(int);
1967 }
1968 }
1969 ptd.id(pindex).make_invalid(); // Invalidate the particle
1970 }
1971
1972 if (!ptd.id(pindex).is_valid()){
1973 soa.GetIdCPUData()[pindex] = soa.GetIdCPUData()[last];
1974 for (int comp = 0; comp < NumRealComps(); comp++) {
1975 soa.GetRealData(comp)[pindex] = soa.GetRealData(comp)[last];
1976 }
1977 for (int comp = 0; comp < NumIntComps(); comp++) {
1978 soa.GetIntData(comp)[pindex] = soa.GetIntData(comp)[last];
1979 }
1980 correctCellVectors(last, pindex, grid, ptd[pindex]);
1981 --last;
1982 continue;
1983 }
1984
1985 ++pindex;
1986 }
1987
1988 {
1989 auto& iddata = soa.GetIdCPUData();
1990 iddata.erase(iddata.begin() + last + 1, iddata.begin() + npart);
1991 }
1992 for (int comp = 0; comp < NumRealComps(); comp++) {
1993 RealVector& rdata = soa.GetRealData(comp);
1994 rdata.erase(rdata.begin() + last + 1, rdata.begin() + npart);
1995 }
1996 for (int comp = 0; comp < NumIntComps(); comp++) {
1997 IntVector& idata = soa.GetIntData(comp);
1998 idata.erase(idata.begin() + last + 1, idata.begin() + npart);
1999 }
2000 }
2001 }
2002 }
2003 }
2004
2005 for (int lev = lev_min; lev <= lev_max; lev++) {
2006 particle_detail::clearEmptyEntries(m_particles[lev]);
2007 }
2008
2009 // Second pass - for each tile in parallel, collect the particles we are owed from all thread's buffers.
2010 for (int lev = lev_min; lev <= lev_max; lev++) {
2011 typename std::map<std::pair<int, int>, Vector<ParticleVector > >::iterator pmap_it;
2012
2013 if constexpr(!ParticleType::is_soa_particle) {
2014 Vector<std::pair<int, int> > grid_tile_ids;
2015 Vector<Vector<ParticleVector>* > pvec_ptrs;
2016
2017 // we need to create any missing map entries in serial here
2018 for (pmap_it=tmp_local[lev].begin(); pmap_it != tmp_local[lev].end(); pmap_it++)
2019 {
2020 DefineAndReturnParticleTile(lev, pmap_it->first.first, pmap_it->first.second);
2021 grid_tile_ids.push_back(pmap_it->first);
2022 pvec_ptrs.push_back(&(pmap_it->second));
2023 }
2024
2025#ifdef AMREX_USE_OMP
2026#pragma omp parallel for
2027#endif
2028 for (int pit = 0; pit < static_cast<int>(pvec_ptrs.size()); ++pit)
2029 {
2030 auto index = grid_tile_ids[pit];
2031 auto& ptile = ParticlesAt(lev, index.first, index.second);
2032 auto& aos = ptile.GetArrayOfStructs();
2033 auto& soa = ptile.GetStructOfArrays();
2034 auto& aos_tmp = *(pvec_ptrs[pit]);
2035 auto& soa_tmp = soa_local[lev][index];
2036 for (int i = 0; i < num_threads; ++i) {
2037 aos.insert(aos.end(), aos_tmp[i].begin(), aos_tmp[i].end());
2038 aos_tmp[i].erase(aos_tmp[i].begin(), aos_tmp[i].end());
2039 for (int comp = 0; comp < NumRealComps(); ++comp) {
2040 RealVector& arr = soa.GetRealData(comp);
2041 RealVector& tmp = soa_tmp[i].GetRealData(comp);
2042 arr.insert(arr.end(), tmp.begin(), tmp.end());
2043 tmp.erase(tmp.begin(), tmp.end());
2044 }
2045 for (int comp = 0; comp < NumIntComps(); ++comp) {
2046 IntVector& arr = soa.GetIntData(comp);
2047 IntVector& tmp = soa_tmp[i].GetIntData(comp);
2048 arr.insert(arr.end(), tmp.begin(), tmp.end());
2049 tmp.erase(tmp.begin(), tmp.end());
2050 }
2051 }
2052 }
2053 } else { // soa particle
2054 Vector<std::pair<int, int> > grid_tile_ids;
2055
2056 // we need to create any missing map entries in serial here
2057 for (auto soa_map_it=soa_local[lev].begin(); soa_map_it != soa_local[lev].end(); soa_map_it++)
2058 {
2059 DefineAndReturnParticleTile(lev, soa_map_it->first.first, soa_map_it->first.second);
2060 grid_tile_ids.push_back(soa_map_it->first);
2061 }
2062
2063#ifdef AMREX_USE_OMP
2064#pragma omp parallel for
2065#endif
2066 for (int pit = 0; pit < static_cast<int>(grid_tile_ids.size()); ++pit) // NOLINT(modernize-loop-convert)
2067 {
2068 auto index = grid_tile_ids[pit];
2069 auto& ptile = ParticlesAt(lev, index.first, index.second);
2070 auto& soa = ptile.GetStructOfArrays();
2071 auto& soa_tmp = soa_local[lev][index];
2072 for (int i = 0; i < num_threads; ++i) {
2073 {
2074 auto& arr = soa.GetIdCPUData();
2075 auto& tmp = soa_tmp[i].GetIdCPUData();
2076 arr.insert(arr.end(), tmp.begin(), tmp.end());
2077 tmp.erase(tmp.begin(), tmp.end());
2078 }
2079 for (int comp = 0; comp < NumRealComps(); ++comp) {
2080 RealVector& arr = soa.GetRealData(comp);
2081 RealVector& tmp = soa_tmp[i].GetRealData(comp);
2082 arr.insert(arr.end(), tmp.begin(), tmp.end());
2083 tmp.erase(tmp.begin(), tmp.end());
2084 }
2085 for (int comp = 0; comp < NumIntComps(); ++comp) {
2086 IntVector& arr = soa.GetIntData(comp);
2087 IntVector& tmp = soa_tmp[i].GetIntData(comp);
2088 arr.insert(arr.end(), tmp.begin(), tmp.end());
2089 tmp.erase(tmp.begin(), tmp.end());
2090 }
2091 }
2092 }
2093 }
2094 }
2095
2096 for (auto& map_it : tmp_remote) {
2097 int who = map_it.first;
2098 not_ours[who];
2099 }
2100
2101 Vector<int> dest_proc_ids;
2102 Vector<Vector<Vector<char> >* > pbuff_ptrs;
2103 for (auto& kv : tmp_remote)
2104 {
2105 dest_proc_ids.push_back(kv.first);
2106 pbuff_ptrs.push_back(&(kv.second));
2107 }
2108
2109#ifdef AMREX_USE_OMP
2110#pragma omp parallel for
2111#endif
2112 for (int pmap_it = 0; pmap_it < static_cast<int>(pbuff_ptrs.size()); ++pmap_it)
2113 {
2114 int who = dest_proc_ids[pmap_it];
2115 Vector<Vector<char> >& tmp = *(pbuff_ptrs[pmap_it]);
2116 for (int i = 0; i < num_threads; ++i) {
2117 not_ours[who].insert(not_ours[who].end(), tmp[i].begin(), tmp[i].end());
2118 tmp[i].erase(tmp[i].begin(), tmp[i].end());
2119 }
2120 }
2121
2123
2124 if (int(m_particles.size()) > theEffectiveFinestLevel+1) {
2125 // Looks like we lost an AmrLevel on a regrid.
2126 if (m_verbose > 0) {
2127 amrex::Print() << "ParticleContainer::Redistribute() resizing m_particles from "
2128 << m_particles.size() << " to " << theEffectiveFinestLevel+1 << '\n';
2129 }
2130 AMREX_ASSERT(int(m_particles.size()) >= 2);
2131
2132 m_particles.resize(theEffectiveFinestLevel + 1);
2133 m_dummy_mf.resize(theEffectiveFinestLevel + 1);
2134 }
2135
2136 if (ParallelContext::NProcsSub() == 1) {
2137 AMREX_ASSERT(not_ours.empty());
2138 }
2139 else {
2140 RedistributeMPI(not_ours, lev_min, lev_max, nGrow, local);
2141 }
2142
2143 AMREX_ASSERT(OK(lev_min, lev_max, nGrow));
2144
2145 if (m_verbose > 0) {
2146 auto stoptime = amrex::second() - strttime;
2147
2148 ByteSpread();
2149
2150#ifdef AMREX_LAZY
2151 Lazy::QueueReduction( [=] () mutable {
2152#endif
2155
2156 amrex::Print() << "ParticleContainer::Redistribute() time: " << stoptime << "\n\n";
2157#ifdef AMREX_LAZY
2158 });
2159#endif
2160 }
2161}
2162
2163template <typename ParticleType, int NArrayReal, int NArrayInt,
2164 template<class> class Allocator, class CellAssignor>
2165void
2167RedistributeMPI (std::map<int, Vector<char> >& not_ours,
2168 int lev_min, int lev_max, int nGrow, int local)
2169{
2170 BL_PROFILE("ParticleContainer::RedistributeMPI()");
2171 BL_PROFILE_VAR_NS("RedistributeMPI_locate", blp_locate);
2172 BL_PROFILE_VAR_NS("RedistributeMPI_copy", blp_copy);
2173
2174#ifdef AMREX_USE_MPI
2175
2176 using buffer_type = unsigned long long;
2177
2178 std::map<int, Vector<buffer_type> > mpi_snd_data;
2179 for (const auto& kv : not_ours)
2180 {
2181 auto nbt = (kv.second.size() + sizeof(buffer_type)-1)/sizeof(buffer_type);
2182 mpi_snd_data[kv.first].resize(nbt);
2183 std::memcpy((char*) mpi_snd_data[kv.first].data(), kv.second.data(), kv.second.size());
2184 }
2185
2186 const int NProcs = ParallelContext::NProcsSub();
2187 const int NNeighborProcs = neighbor_procs.size();
2188
2189 // We may now have particles that are rightfully owned by another CPU.
2190 Vector<Long> Snds(NProcs, 0), Rcvs(NProcs, 0); // bytes!
2191
2192 Long NumSnds = 0;
2193 if (local > 0)
2194 {
2195 AMREX_ALWAYS_ASSERT(lev_min == 0);
2196 AMREX_ALWAYS_ASSERT(lev_max == 0);
2197 BuildRedistributeMask(0, local);
2198 NumSnds = doHandShakeLocal(not_ours, neighbor_procs, Snds, Rcvs);
2199 }
2200 else
2201 {
2202 NumSnds = doHandShake(not_ours, Snds, Rcvs);
2203 }
2204
2205 const int SeqNum = ParallelDescriptor::SeqNum();
2206
2207 if ((! local) && NumSnds == 0) {
2208 return; // There's no parallel work to do.
2209 }
2210
2211 if (local)
2212 {
2213 Long tot_snds_this_proc = 0;
2214 Long tot_rcvs_this_proc = 0;
2215 for (int i = 0; i < NNeighborProcs; ++i) {
2216 tot_snds_this_proc += Snds[neighbor_procs[i]];
2217 tot_rcvs_this_proc += Rcvs[neighbor_procs[i]];
2218 }
2219 if ( (tot_snds_this_proc == 0) && (tot_rcvs_this_proc == 0) ) {
2220 return; // There's no parallel work to do.
2221 }
2222 }
2223
2224 Vector<int> RcvProc;
2225 Vector<std::size_t> rOffset; // Offset (in bytes) in the receive buffer
2226
2227 std::size_t TotRcvInts = 0;
2228 std::size_t TotRcvBytes = 0;
2229 for (int i = 0; i < NProcs; ++i) {
2230 if (Rcvs[i] > 0) {
2231 RcvProc.push_back(i);
2232 rOffset.push_back(TotRcvInts);
2233 TotRcvBytes += Rcvs[i];
2234 auto nbt = (Rcvs[i] + sizeof(buffer_type)-1)/sizeof(buffer_type);
2235 TotRcvInts += nbt;
2236 }
2237 }
2238
2239 const auto nrcvs = static_cast<int>(RcvProc.size());
2240 Vector<MPI_Status> stats(nrcvs);
2241 Vector<MPI_Request> rreqs(nrcvs);
2242
2243 // Allocate data for rcvs as one big chunk.
2244 Vector<unsigned long long> recvdata(TotRcvInts);
2245
2246 // Post receives.
2247 for (int i = 0; i < nrcvs; ++i) {
2248 const auto Who = RcvProc[i];
2249 const auto offset = rOffset[i];
2250 const auto Cnt = (Rcvs[Who] + sizeof(buffer_type)-1)/sizeof(buffer_type);
2251 AMREX_ASSERT(Cnt > 0);
2252 AMREX_ASSERT(Cnt < size_t(std::numeric_limits<int>::max()));
2253 AMREX_ASSERT(Who >= 0 && Who < NProcs);
2254
2255 rreqs[i] = ParallelDescriptor::Arecv(&recvdata[offset], Cnt, Who, SeqNum,
2257 }
2258
2259 // Send.
2260 for (const auto& kv : mpi_snd_data) {
2261 const auto Who = kv.first;
2262 const auto Cnt = kv.second.size();
2263
2264 AMREX_ASSERT(Cnt > 0);
2265 AMREX_ASSERT(Who >= 0 && Who < NProcs);
2266 AMREX_ASSERT(Cnt < std::numeric_limits<int>::max());
2267
2268 ParallelDescriptor::Send(kv.second.data(), Cnt, Who, SeqNum,
2270 }
2271
2272 if (nrcvs > 0) {
2273 ParallelDescriptor::Waitall(rreqs, stats);
2274
2275 BL_PROFILE_VAR_START(blp_locate);
2276
2277 int npart = TotRcvBytes / superparticle_size;
2278
2279 Vector<int> rcv_levs(npart);
2280 Vector<int> rcv_grid(npart);
2281 Vector<int> rcv_tile(npart);
2282
2283 int ipart = 0;
2284 ParticleLocData pld;
2285 for (int j = 0; j < nrcvs; ++j)
2286 {
2287 const auto offset = rOffset[j];
2288 const auto Who = RcvProc[j];
2289 const auto Cnt = Rcvs[Who] / superparticle_size;
2290 for (int i = 0; i < int(Cnt); ++i)
2291 {
2292 char* pbuf = ((char*) &recvdata[offset]) + i*superparticle_size;
2293
2295
2296 if constexpr (ParticleType::is_soa_particle) {
2297 std::memcpy(&p.m_idcpu, pbuf, sizeof(uint64_t));
2298
2299 ParticleReal pos[AMREX_SPACEDIM];
2300 std::memcpy(&pos[0], pbuf + sizeof(uint64_t), AMREX_SPACEDIM*sizeof(ParticleReal));
2301 AMREX_D_TERM(p.pos(0) = pos[0];,
2302 p.pos(1) = pos[1];,
2303 p.pos(2) = pos[2]);
2304 } else {
2305 std::memcpy(&p, pbuf, sizeof(ParticleType));
2306 }
2307
2308 bool success = Where(p, pld, lev_min, lev_max, 0);
2309 if (!success)
2310 {
2311 success = (nGrow > 0) && Where(p, pld, lev_min, lev_min, nGrow);
2312 pld.m_grown_gridbox = pld.m_gridbox; // reset grown box for subsequent calls.
2313 }
2314 if (!success)
2315 {
2316 amrex::Abort("RedistributeMPI_locate:: invalid particle.");
2317 }
2318
2319 rcv_levs[ipart] = pld.m_lev;
2320 rcv_grid[ipart] = pld.m_grid;
2321 rcv_tile[ipart] = pld.m_tile;
2322
2323 ++ipart;
2324 }
2325 }
2326
2327 BL_PROFILE_VAR_STOP(blp_locate);
2328
2329 BL_PROFILE_VAR_START(blp_copy);
2330
2331#ifndef AMREX_USE_GPU
2332 ipart = 0;
2333 for (int i = 0; i < nrcvs; ++i)
2334 {
2335 const auto offset = rOffset[i];
2336 const auto Who = RcvProc[i];
2337 const auto Cnt = Rcvs[Who] / superparticle_size;
2338 for (int j = 0; j < int(Cnt); ++j)
2339 {
2340 auto& ptile = m_particles[rcv_levs[ipart]][std::make_pair(rcv_grid[ipart],
2341 rcv_tile[ipart])];
2342 char* pbuf = ((char*) &recvdata[offset]) + j*superparticle_size;
2343
2344 if constexpr (ParticleType::is_soa_particle) {
2345 uint64_t idcpudata;
2346 std::memcpy(&idcpudata, pbuf, sizeof(uint64_t));
2347 pbuf += sizeof(uint64_t);
2348 ptile.GetStructOfArrays().GetIdCPUData().push_back(idcpudata);
2349 } else {
2350 ParticleType p;
2351 std::memcpy(&p, pbuf, sizeof(ParticleType));
2352 pbuf += sizeof(ParticleType);
2353 ptile.push_back(p);
2354 }
2355
2356 int array_comp_start = 0;
2357 if constexpr (!ParticleType::is_soa_particle) {
2358 array_comp_start = AMREX_SPACEDIM + NStructReal;
2359 }
2360 for (int comp = 0; comp < NumRealComps(); ++comp) {
2361 if (h_redistribute_real_comp[array_comp_start + comp]) {
2362 ParticleReal rdata;
2363 std::memcpy(&rdata, pbuf, sizeof(ParticleReal));
2364 pbuf += sizeof(ParticleReal);
2365 ptile.push_back_real(comp, rdata);
2366 } else {
2367 ptile.push_back_real(comp, 0.0);
2368 }
2369 }
2370
2371 array_comp_start = 2 + NStructInt;
2372 for (int comp = 0; comp < NumIntComps(); ++comp) {
2373 if (h_redistribute_int_comp[array_comp_start + comp]) {
2374 int idata;
2375 std::memcpy(&idata, pbuf, sizeof(int));
2376 pbuf += sizeof(int);
2377 ptile.push_back_int(comp, idata);
2378 } else {
2379 ptile.push_back_int(comp, 0);
2380 }
2381 }
2382 ++ipart;
2383 }
2384 }
2385
2386#else
2388 host_particles.reserve(15);
2389 host_particles.resize(finestLevel()+1);
2390
2392 std::vector<Gpu::HostVector<ParticleReal> > > > host_real_attribs;
2393 host_real_attribs.reserve(15);
2394 host_real_attribs.resize(finestLevel()+1);
2395
2397 std::vector<Gpu::HostVector<int> > > > host_int_attribs;
2398 host_int_attribs.reserve(15);
2399 host_int_attribs.resize(finestLevel()+1);
2400
2402 host_idcpu.reserve(15);
2403 host_idcpu.resize(finestLevel()+1);
2404
2405 ipart = 0;
2406 for (int i = 0; i < nrcvs; ++i)
2407 {
2408 const auto offset = rOffset[i];
2409 const auto Who = RcvProc[i];
2410 const auto Cnt = Rcvs[Who] / superparticle_size;
2411 for (auto j = decltype(Cnt)(0); j < Cnt; ++j)
2412 {
2413 int lev = rcv_levs[ipart];
2414 std::pair<int, int> ind(std::make_pair(rcv_grid[ipart], rcv_tile[ipart]));
2415
2416 char* pbuf = ((char*) &recvdata[offset]) + j*superparticle_size;
2417
2418 host_real_attribs[lev][ind].resize(NumRealComps());
2419 host_int_attribs[lev][ind].resize(NumIntComps());
2420
2421 if constexpr (ParticleType::is_soa_particle) {
2422 uint64_t idcpudata;
2423 std::memcpy(&idcpudata, pbuf, sizeof(uint64_t));
2424 pbuf += sizeof(uint64_t);
2425 host_idcpu[lev][ind].push_back(idcpudata);
2426 } else {
2427 ParticleType p;
2428 std::memcpy(&p, pbuf, sizeof(ParticleType));
2429 pbuf += sizeof(ParticleType);
2430 host_particles[lev][ind].push_back(p);
2431 }
2432
2433 host_real_attribs[lev][ind].resize(NumRealComps());
2434 host_int_attribs[lev][ind].resize(NumIntComps());
2435
2436 // add the real...
2437 int array_comp_start = 0;
2438 if constexpr (!ParticleType::is_soa_particle) {
2439 array_comp_start = AMREX_SPACEDIM + NStructReal;
2440 }
2441 for (int comp = 0; comp < NumRealComps(); ++comp) {
2442 if (h_redistribute_real_comp[array_comp_start + comp]) {
2443 Real rdata;
2444 std::memcpy(&rdata, pbuf, sizeof(Real));
2445 pbuf += sizeof(Real);
2446 host_real_attribs[lev][ind][comp].push_back(rdata);
2447 } else {
2448 host_real_attribs[lev][ind][comp].push_back(0.0);
2449 }
2450 }
2451
2452 // ... and int array data
2453 array_comp_start = 2 + NStructInt;
2454 for (int comp = 0; comp < NumIntComps(); ++comp) {
2455 if (h_redistribute_int_comp[array_comp_start + comp]) {
2456 int idata;
2457 std::memcpy(&idata, pbuf, sizeof(int));
2458 pbuf += sizeof(int);
2459 host_int_attribs[lev][ind][comp].push_back(idata);
2460 } else {
2461 host_int_attribs[lev][ind][comp].push_back(0);
2462 }
2463 }
2464 ++ipart;
2465 }
2466 }
2467
2468 for (int host_lev = 0; host_lev < static_cast<int>(host_particles.size()); ++host_lev)
2469 {
2470 for (auto& kv : host_particles[host_lev]) {
2471 auto grid = kv.first.first;
2472 auto tile = kv.first.second;
2473 const auto& src_tile = kv.second;
2474
2475 auto& dst_tile = GetParticles(host_lev)[std::make_pair(grid,tile)];
2476 auto old_size = dst_tile.size();
2477 auto new_size = old_size + src_tile.size();
2478 dst_tile.resize(new_size);
2479
2480 if constexpr (ParticleType::is_soa_particle) {
2482 host_idcpu[host_lev][std::make_pair(grid,tile)].begin(),
2483 host_idcpu[host_lev][std::make_pair(grid,tile)].end(),
2484 dst_tile.GetStructOfArrays().GetIdCPUData().begin() + old_size);
2485 } else {
2487 src_tile.begin(), src_tile.end(),
2488 dst_tile.GetArrayOfStructs().begin() + old_size);
2489 }
2490
2491 for (int i = 0; i < NumRealComps(); ++i) {
2493 host_real_attribs[host_lev][std::make_pair(grid,tile)][i].begin(),
2494 host_real_attribs[host_lev][std::make_pair(grid,tile)][i].end(),
2495 dst_tile.GetStructOfArrays().GetRealData(i).begin() + old_size);
2496 }
2497
2498 for (int i = 0; i < NumIntComps(); ++i) {
2500 host_int_attribs[host_lev][std::make_pair(grid,tile)][i].begin(),
2501 host_int_attribs[host_lev][std::make_pair(grid,tile)][i].end(),
2502 dst_tile.GetStructOfArrays().GetIntData(i).begin() + old_size);
2503 }
2504 }
2505 }
2506
2508#endif
2509
2510 BL_PROFILE_VAR_STOP(blp_copy);
2511 }
2512#else
2513 amrex::ignore_unused(not_ours,lev_min,lev_max,nGrow,local);
2514#endif
2515}
2516
2517template <typename ParticleType, int NArrayReal, int NArrayInt,
2518 template<class> class Allocator, class CellAssignor>
2519bool
2521{
2522 BL_PROFILE("ParticleContainer::OK()");
2523
2524 if (lev_max == -1) {
2525 lev_max = finestLevel();
2526 }
2527
2528 return (numParticlesOutOfRange(*this, lev_min, lev_max, nGrow) == 0);
2529}
2530
2531template <typename ParticleType, int NArrayReal, int NArrayInt,
2532 template<class> class Allocator, class CellAssignor>
2533void
2535::AddParticlesAtLevel (AoS& particles, int level, int nGrow)
2536{
2537 ParticleTileType ptile;
2538 ptile.GetArrayOfStructs().swap(particles);
2539 AddParticlesAtLevel(ptile, level, nGrow);
2540}
2541
2542template <typename ParticleType, int NArrayReal, int NArrayInt,
2543 template<class> class Allocator, class CellAssignor>
2544void
2546::AddParticlesAtLevel (ParticleTileType& particles, int level, int nGrow)
2547{
2548 BL_PROFILE("ParticleContainer::AddParticlesAtLevel()");
2549
2550 if (int(m_particles.size()) < level+1)
2551 {
2552 if (Verbose())
2553 {
2554 amrex::Print() << "ParticleContainer::AddParticlesAtLevel resizing m_particles from "
2555 << m_particles.size()
2556 << " to "
2557 << level+1 << '\n';
2558 }
2559 m_particles.resize(level+1);
2560 m_dummy_mf.resize(level+1);
2561 for (int lev = 0; lev < level+1; ++lev) {
2562 RedefineDummyMF(lev);
2563 }
2564 }
2565
2566 auto& ptile = DefineAndReturnParticleTile(level, 0, 0);
2567 int old_np = ptile.size();
2568 int num_to_add = particles.size();
2569 int new_np = old_np + num_to_add;
2570 ptile.resize(new_np);
2571 amrex::copyParticles(ptile, particles, 0, old_np, num_to_add);
2572 Redistribute(level, level, nGrow);
2573 particles.resize(0);
2574}
2575
2576// This is the single-level version for cell-centered density
2577template <typename ParticleType, int NArrayReal, int NArrayInt,
2578 template<class> class Allocator, class CellAssignor>
2579void
2581AssignCellDensitySingleLevel (int rho_index,
2582 MultiFab& mf_to_be_filled,
2583 int lev,
2584 int ncomp,
2585 int particle_lvl_offset) const
2586{
2587 BL_PROFILE("ParticleContainer::AssignCellDensitySingleLevel()");
2588
2589 if (rho_index != 0) { amrex::Abort("AssignCellDensitySingleLevel only works if rho_index = 0"); }
2590
2591 MultiFab* mf_pointer;
2592
2593 if (OnSameGrids(lev, mf_to_be_filled)) {
2594 // If we are already working with the internal mf defined on the
2595 // particle_box_array, then we just work with this.
2596 mf_pointer = &mf_to_be_filled;
2597 }
2598 else {
2599 // If mf_to_be_filled is not defined on the particle_box_array, then we need
2600 // to make a temporary here and copy into mf_to_be_filled at the end.
2601 mf_pointer = new MultiFab(ParticleBoxArray(lev),
2602 ParticleDistributionMap(lev),
2603 ncomp, mf_to_be_filled.nGrow());
2604 }
2605
2606 // We must have ghost cells for each FAB so that a particle in one grid can spread
2607 // its effect to an adjacent grid by first putting the value into ghost cells of its
2608 // own grid. The mf->SumBoundary call then adds the value from one grid's ghost cell
2609 // to another grid's valid region.
2610 if (mf_pointer->nGrow() < 1) {
2611 amrex::Error("Must have at least one ghost cell when in AssignCellDensitySingleLevel");
2612 }
2613
2614 const auto strttime = amrex::second();
2615
2616 const auto dxi = Geom(lev).InvCellSizeArray();
2617 const auto plo = Geom(lev).ProbLoArray();
2618 const auto pdxi = Geom(lev + particle_lvl_offset).InvCellSizeArray();
2619
2620 if (Geom(lev).isAnyPeriodic() && ! Geom(lev).isAllPeriodic())
2621 {
2622 amrex::Error("AssignCellDensitySingleLevel: problem must be periodic in no or all directions");
2623 }
2624
2625 mf_pointer->setVal(0);
2626
2628#ifdef AMREX_USE_OMP
2629#pragma omp parallel if (Gpu::notInLaunchRegion())
2630#endif
2631 {
2632 FArrayBox local_rho;
2633 for (ParConstIter pti(*this, lev); pti.isValid(); ++pti) {
2634 const Long np = pti.numParticles();
2635 auto ptd = pti.GetParticleTile().getConstParticleTileData();
2636 FArrayBox& fab = (*mf_pointer)[pti];
2637 auto rhoarr = fab.array();
2638#ifdef AMREX_USE_OMP
2639 Box tile_box;
2641 {
2642 tile_box = pti.tilebox();
2643 tile_box.grow(mf_pointer->nGrow());
2644 local_rho.resize(tile_box,ncomp);
2645 local_rho.setVal<RunOn::Host>(0.0);
2646 rhoarr = local_rho.array();
2647 }
2648#endif
2649
2650 if (particle_lvl_offset == 0)
2651 {
2653 {
2654 auto p = ptd[i];
2655 amrex_deposit_cic(p, ncomp, rhoarr, plo, dxi);
2656 });
2657 }
2658 else
2659 {
2661 {
2662 auto p = ptd[i];
2663 amrex_deposit_particle_dx_cic(p, ncomp, rhoarr, plo, dxi, pdxi);
2664 });
2665 }
2666
2667#ifdef AMREX_USE_OMP
2669 {
2670 fab.atomicAdd<RunOn::Host>(local_rho, tile_box, tile_box, 0, 0, ncomp);
2671 }
2672#endif
2673 }
2674 }
2675
2676 mf_pointer->SumBoundary(Geom(lev).periodicity());
2677
2678 // If ncomp > 1, first divide the momenta (component n)
2679 // by the mass (component 0) in order to get velocities.
2680 // Be careful not to divide by zero.
2681 for (int n = 1; n < ncomp; n++)
2682 {
2683 for (MFIter mfi(*mf_pointer); mfi.isValid(); ++mfi)
2684 {
2685 (*mf_pointer)[mfi].protected_divide<RunOn::Device>((*mf_pointer)[mfi],0,n,1);
2686 }
2687 }
2688
2689 // Only multiply the first component by (1/vol) because this converts mass
2690 // to density. If there are additional components (like velocity), we don't
2691 // want to divide those by volume.
2692 const Real* dx = Geom(lev).CellSize();
2693 const Real vol = AMREX_D_TERM(dx[0], *dx[1], *dx[2]);
2694
2695 mf_pointer->mult(Real(1.0)/vol, 0, 1, mf_pointer->nGrow());
2696
2697 // If mf_to_be_filled is not defined on the particle_box_array, then we need
2698 // to copy here from mf_pointer into mf_to_be_filled.
2699 if (mf_pointer != &mf_to_be_filled)
2700 {
2701 mf_to_be_filled.ParallelCopy(*mf_pointer,0,0,ncomp,0,0);
2702 delete mf_pointer;
2703 }
2704
2705 if (m_verbose > 1)
2706 {
2707 auto stoptime = amrex::second() - strttime;
2708
2711
2712 amrex::Print() << "ParticleContainer::AssignCellDensitySingleLevel) time: "
2713 << stoptime << '\n';
2714 }
2715}
2716
2717template <typename ParticleType, int NArrayReal, int NArrayInt,
2718 template<class> class Allocator, class CellAssignor>
2719void
2721ResizeRuntimeRealComp (int new_size, bool communicate)
2722{
2723 int old_size = m_num_runtime_real;
2724
2725 m_runtime_comps_defined = (new_size > 0);
2726 m_num_runtime_real = new_size;
2727 int cur_size = h_redistribute_real_comp.size();
2728 h_redistribute_real_comp.resize(cur_size-old_size+new_size, communicate);
2729 SetParticleSize();
2730
2731 for (int lev = 0; lev < numLevels(); ++lev) {
2732 for (ParIterType pti(*this,lev); pti.isValid(); ++pti) {
2733 auto& tile = DefineAndReturnParticleTile(lev, pti);
2734 auto np = tile.numParticles();
2735 if (np > 0 && new_size > old_size) {
2736 auto& soa = tile.GetStructOfArrays();
2737 soa.resize(np);
2738 }
2739 }
2740 }
2741}
2742
2743template <typename ParticleType, int NArrayReal, int NArrayInt,
2744 template<class> class Allocator, class CellAssignor>
2745void
2747ResizeRuntimeIntComp (int new_size, bool communicate)
2748{
2749 int old_size = m_num_runtime_int;
2750
2751 m_runtime_comps_defined = (new_size > 0);
2752 m_num_runtime_int = new_size;
2753 int cur_size = h_redistribute_int_comp.size();
2754 h_redistribute_int_comp.resize(cur_size-old_size+new_size, communicate);
2755 SetParticleSize();
2756
2757 for (int lev = 0; lev < numLevels(); ++lev) {
2758 for (ParIterType pti(*this,lev); pti.isValid(); ++pti) {
2759 auto& tile = DefineAndReturnParticleTile(lev, pti);
2760 auto np = tile.numParticles();
2761 if (np > 0 && new_size > old_size) {
2762 auto& soa = tile.GetStructOfArrays();
2763 soa.resize(np);
2764 }
2765 }
2766 }
2767}
2768
2769}
#define BL_PROFILE_VAR_START(vname)
Definition AMReX_BLProfiler.H:562
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:551
#define BL_PROFILE_VAR_STOP(vname)
Definition AMReX_BLProfiler.H:563
#define BL_PROFILE_SYNC_STOP()
Definition AMReX_BLProfiler.H:645
#define BL_PROFILE_SYNC_START_TIMED(fname)
Definition AMReX_BLProfiler.H:644
#define BL_PROFILE_VAR_NS(fname, vname)
Definition AMReX_BLProfiler.H:561
#define AMREX_ALWAYS_ASSERT_WITH_MESSAGE(EX, MSG)
Definition AMReX_BLassert.H:49
#define AMREX_ASSERT(EX)
Definition AMReX_BLassert.H:38
#define AMREX_ALWAYS_ASSERT(EX)
Definition AMReX_BLassert.H:50
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:119
#define AMREX_FOR_1D(...)
Definition AMReX_GpuLaunchMacrosC.nolint.H:97
#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
amrex::ParmParse pp
Input file parser instance for the given namespace.
Definition AMReX_HypreIJIface.cpp:15
Array4< int const > offset
Definition AMReX_HypreMLABecLap.cpp:1089
#define AMREX_D_TERM(a, b, c)
Definition AMReX_SPACE.H:172
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
Print on all processors of the default communicator.
Definition AMReX_Print.H:117
BaseFab< T > & atomicAdd(const BaseFab< T > &x) noexcept
Atomic FAB addition (a[i] <- a[i] + b[i]).
Definition AMReX_BaseFab.H:2478
Array4< T const > array() const noexcept
Definition AMReX_BaseFab.H:375
void setVal(T const &x, const Box &bx, int dcomp, int ncomp) noexcept
The setVal functions set sub-regions in the BaseFab to a constant value. This most general form speci...
Definition AMReX_BaseFab.H:1393
A collection of Boxes stored in an Array.
Definition AMReX_BoxArray.H:551
IndexType ixType() const noexcept
Return index type of this BoxArray.
Definition AMReX_BoxArray.H:841
BoxArray & grow(int n)
Grow each Box in the BoxArray by the specified amount.
Definition AMReX_BoxArray.cpp:699
std::vector< std::pair< int, Box > > intersections(const Box &bx) const
Return intersections of Box and BoxArray.
Definition AMReX_BoxArray.cpp:1179
Box getCellCenteredBox(int index) const noexcept
Return cell-centered box at element index of this BoxArray.
Definition AMReX_BoxArray.H:731
static bool SameRefs(const BoxArray &lhs, const BoxArray &rhs)
whether two BoxArrays share the same data
Definition AMReX_BoxArray.H:824
A class for managing a List of Boxes that share a common IndexType. This class implements operations ...
Definition AMReX_BoxList.H:52
BoxList & complementIn(const Box &b, const BoxList &bl)
Definition AMReX_BoxList.cpp:307
__host__ __device__ BoxND & grow(int i) noexcept
Definition AMReX_Box.H:630
__host__ __device__ bool contains(const IntVectND< dim > &p) const noexcept
Returns true if argument is contained within BoxND.
Definition AMReX_Box.H:207
GpuArray< Real, 3 > InvCellSizeArray() const noexcept
Definition AMReX_CoordSys.H:87
A Fortran Array of REALs.
Definition AMReX_FArrayBox.H:229
void resize(const Box &b, int N=1, Arena *ar=nullptr)
For debugging purposes we hide BaseFab version and do some extra work.
Definition AMReX_FArrayBox.cpp:178
int size() const noexcept
Return the number of FABs in the FabArray.
Definition AMReX_FabArrayBase.H:110
int nGrow(int direction=0) const noexcept
Return the grow factor that defines the region of definition.
Definition AMReX_FabArrayBase.H:78
const DistributionMapping & DistributionMap() const noexcept
Return constant reference to associated DistributionMapping.
Definition AMReX_FabArrayBase.H:131
const BoxArray & boxArray() const noexcept
Return a constant reference to the BoxArray that defines the valid region associated with this FabArr...
Definition AMReX_FabArrayBase.H:95
void ParallelCopy(const FabArray< FAB > &src, const Periodicity &period=Periodicity::NonPeriodic(), CpOp op=FabArrayBase::COPY)
Definition AMReX_FabArray.H:845
Array4< typename FabArray< FAB >::value_type const > array(const MFIter &mfi) const noexcept
Definition AMReX_FabArray.H:561
void setVal(value_type val)
Set all components in the entire region of each FAB to val.
Definition AMReX_FabArray.H:2609
void SumBoundary(const Periodicity &period=Periodicity::NonPeriodic(), bool deterministic=false)
Sum values in overlapped cells.
Definition AMReX_FabArray.H:3540
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:73
const Box & Domain() const noexcept
Returns our rectangular domain.
Definition AMReX_Geometry.H:210
GpuArray< Real, 3 > ProbLoArray() const noexcept
Definition AMReX_Geometry.H:186
static void streamSynchronize() noexcept
Definition AMReX_GpuDevice.cpp:750
__host__ __device__ bool cellCentered() const noexcept
True if the IndexTypeND is CELL based in all directions.
Definition AMReX_IndexType.H:101
__host__ static __device__ constexpr IntVectND< dim > TheZeroVector() noexcept
This static member function returns a reference to a constant IntVectND object, all of whose dim argu...
Definition AMReX_IntVect.H:677
a one-thingy-per-box distributed object
Definition AMReX_LayoutData.H:13
Definition AMReX_MFIter.H:57
int LocalTileIndex() const noexcept
The current local tile index in the current grid;.
Definition AMReX_MFIter.H:150
bool isValid() const noexcept
Is the iterator valid i.e. is it associated with a FAB?
Definition AMReX_MFIter.H:141
Box validbox() const noexcept
Return the valid Box in which the current tile resides.
Definition AMReX_MFIter.H:132
int index() const noexcept
The index into the underlying BoxArray of the current FAB.
Definition AMReX_MFIter.H:144
A collection (stored as an array) of FArrayBox objects.
Definition AMReX_MultiFab.H:38
void mult(Real val, int comp, int num_comp, int nghost=0)
Scales the value of each cell in the specified subregion of the MultiFab by the scalar val (a[i] <- a...
Definition AMReX_MultiFab.cpp:1417
Definition AMReX_PODVector.H:297
size_type size() const noexcept
Definition AMReX_PODVector.H:637
void resize(size_type a_new_size, GrowthStrategy strategy=GrowthStrategy::Poisson)
Definition AMReX_PODVector.H:717
T * dataPtr() noexcept
Definition AMReX_PODVector.H:659
Definition AMReX_ParIter.H:144
Definition AMReX_ParIter.H:115
MPI_Request req() const
Definition AMReX_ParallelDescriptor.H:74
Parse Parameters From Command Line and Input Files.
Definition AMReX_ParmParse.H:343
int queryAdd(std::string_view name, T &ref)
If name is found, the value in the ParmParse database will be stored in the ref argument....
Definition AMReX_ParmParse.H:1037
int queryarr(std::string_view name, std::vector< int > &ref, int start_ix=FIRST, int num_val=ALL) const
Same as queryktharr() but searches for last occurrence of name.
Definition AMReX_ParmParse.cpp:1479
int query(std::string_view name, bool &ref, int ival=FIRST) const
Same as querykth() but searches for the last occurrence of name.
Definition AMReX_ParmParse.cpp:1393
virtual void reserveData()
Definition AMReX_ParticleContainerBase.cpp:41
virtual void resizeData()
Definition AMReX_ParticleContainerBase.cpp:46
A distributed container for Particles sorted onto the levels, grids, and tiles of a block-structured ...
Definition AMReX_ParticleContainer.H:147
void addParticles(const PCType &other, bool local=false)
Add particles from other to this ParticleContainer. local controls whether or not to call Redistribut...
Definition AMReX_ParticleContainerI.H:1157
IntVect Index(const P &p, int lev) const
Definition AMReX_ParticleContainerI.H:201
void ResizeRuntimeIntComp(int new_size, bool communicate)
Definition AMReX_ParticleContainerI.H:2747
bool OK(int lev_min=0, int lev_max=-1, int nGrow=0) const
OK checks that all particles are in the right places (for some value of right)
Definition AMReX_ParticleContainerI.H:2520
std::map< std::pair< int, int >, ParticleTileType > ParticleLevel
Definition AMReX_ParticleContainer.H:188
void SetSoACompileTimeNames(std::vector< std::string > const &rdata_name, std::vector< std::string > const &idata_name)
Definition AMReX_ParticleContainerI.H:109
typename ParticleTileType::AoS AoS
Definition AMReX_ParticleContainer.H:191
typename SoA::RealVector RealVector
Definition AMReX_ParticleContainer.H:194
void locateParticle(P &p, ParticleLocData &pld, int lev_min, int lev_max, int nGrow, int local_grid=-1) const
Definition AMReX_ParticleContainerI.H:446
void SetParticleSize()
Definition AMReX_ParticleContainerI.H:16
void RemoveParticlesAtLevel(int level)
The Following methods are for managing Virtual and Ghost Particles.
Definition AMReX_ParticleContainerI.H:757
void clearParticles()
Clear all the particles in this container. This does not free memory.
Definition AMReX_ParticleContainerI.H:1130
void Increment(MultiFab &mf, int level)
Definition AMReX_ParticleContainerI.H:720
void AssignCellDensitySingleLevel(int rho_index, MultiFab &mf, int level, int ncomp=1, int particle_lvl_offset=0) const
Definition AMReX_ParticleContainerI.H:2581
bool HasIntComp(std::string const &name)
Definition AMReX_ParticleContainerI.H:148
void ShrinkToFit()
Definition AMReX_ParticleContainerI.H:700
bool HasRealComp(std::string const &name)
Definition AMReX_ParticleContainerI.H:140
void resizeData() override
This resizes the vector of dummy MultiFabs used by the ParticleContainer for the current number of le...
Definition AMReX_ParticleContainerI.H:435
void ResizeRuntimeRealComp(int new_size, bool communicate)
Definition AMReX_ParticleContainerI.H:2721
Long IncrementWithTotal(MultiFab &mf, int level, bool local=false)
Definition AMReX_ParticleContainerI.H:747
Long NumberOfParticlesAtLevel(int level, bool only_valid=true, bool only_local=false) const
Returns # of particles at specified the level.
Definition AMReX_ParticleContainerI.H:551
void SortParticlesByCell()
Sort the particles on each tile by cell, using Fortran ordering.
Definition AMReX_ParticleContainerI.H:1365
typename Particle< NStructReal, NStructInt >::RealType RealType
The type of the Real data.
Definition AMReX_ParticleContainer.H:174
int GetRealCompIndex(std::string const &name)
Definition AMReX_ParticleContainerI.H:161
void RedistributeMPI(std::map< int, Vector< char > > &not_ours, int lev_min=0, int lev_max=0, int nGrow=0, int local=0)
Definition AMReX_ParticleContainerI.H:2167
int GetIntCompIndex(std::string const &name)
Definition AMReX_ParticleContainerI.H:183
void RemoveParticlesNotAtFinestLevel()
Definition AMReX_ParticleContainerI.H:771
Vector< Long > NumberOfParticlesInGrid(int level, bool only_valid=true, bool only_local=false) const
Definition AMReX_ParticleContainerI.H:496
typename SoA::IntVector IntVector
Definition AMReX_ParticleContainer.H:195
void copyParticles(const PCType &other, bool local=false)
Copy particles from other to this ParticleContainer. Will clear all the particles from this container...
Definition AMReX_ParticleContainerI.H:1146
ParticleLocData Reset(ParticleType &prt, bool update, bool verbose=true, ParticleLocData pld=ParticleLocData()) const
Updates a particle's location (Where), tries to periodic shift any particles that have left the domai...
Definition AMReX_ParticleContainerI.H:392
void CapacityOfParticlesInGrid(LayoutData< I > &mem, int lev) const
Return capacity of memory for particles at specific grid.
Definition AMReX_ParticleContainerI.H:593
Long TotalNumberOfParticles(bool only_valid=true, bool only_local=false) const
Returns # of particles at all levels.
Definition AMReX_ParticleContainerI.H:481
void reserveData() override
This reserves data in the vector of dummy MultiFabs used by the ParticleContainer for the maximum num...
Definition AMReX_ParticleContainerI.H:426
T_ParticleType ParticleType
Definition AMReX_ParticleContainer.H:149
Definition AMReX_ParticleLocator.H:104
AssignGrid< BinIteratorFactory > getGridAssignor() const noexcept
Definition AMReX_ParticleLocator.H:183
void build(const BoxArray &ba, const Geometry &geom)
Definition AMReX_ParticleLocator.H:111
This class provides the user with a few print options.
Definition AMReX_Print.H:35
Definition AMReX_Reduce.H:249
Type value()
Definition AMReX_Reduce.H:281
Definition AMReX_Reduce.H:364
std::enable_if_t< IsFabArray< MF >::value > eval(MF const &mf, IntVect const &nghost, D &reduce_data, F &&f)
Definition AMReX_Reduce.H:433
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:28
Long size() const noexcept
Definition AMReX_Vector.H:53
Definition AMReX_iMultiFab.H:32
Long sum(int comp, int nghost=0, bool local=false) const
Returns the sum in component comp.
Definition AMReX_iMultiFab.cpp:454
__host__ __device__ AMREX_FORCE_INLINE void AddNoRet(T *sum, T value) noexcept
Definition AMReX_GpuAtomic.H:281
void copyAsync(HostToDevice, InIter begin, InIter end, OutIter result) noexcept
A host-to-device copy routine. Note this is just a wrapper around memcpy, so it assumes contiguous st...
Definition AMReX_GpuContainers.H:221
static constexpr HostToDevice hostToDevice
Definition AMReX_GpuContainers.H:98
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:260
void dtoh_memcpy_async(void *p_h, const void *p_d, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:303
bool inLaunchRegion() noexcept
Definition AMReX_GpuControl.H:92
bool notInLaunchRegion() noexcept
Definition AMReX_GpuControl.H:93
void htod_memcpy_async(void *p_d, const void *p_h, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:289
void QueueReduction(Func f)
Definition AMReX_Lazy.cpp:7
constexpr Long GhostParticleID
Definition AMReX_Particle.H:19
constexpr Long VirtualParticleID
Definition AMReX_Particle.H:20
constexpr int get_thread_num()
Definition AMReX_OpenMP.H:37
constexpr int get_max_threads()
Definition AMReX_OpenMP.H:36
void Sum(T &v, MPI_Comm comm)
Definition AMReX_ParallelReduce.H:204
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
int global_to_local_rank(int rank) noexcept
Definition AMReX_ParallelContext.H:98
int NProcsSub() noexcept
number of ranks in current frame
Definition AMReX_ParallelContext.H:74
int IOProcessorNumberSub() noexcept
IO sub-rank in current frame.
Definition AMReX_ParallelContext.H:78
bool UseGpuAwareMpi()
Definition AMReX_ParallelDescriptor.H:111
void Waitall(Vector< MPI_Request > &, Vector< MPI_Status > &)
Definition AMReX_ParallelDescriptor.cpp:1304
Message Send(const T *buf, size_t n, int dst_pid, int tag)
Definition AMReX_ParallelDescriptor.H:1109
void Bcast(void *, int, MPI_Datatype, int, MPI_Comm)
Definition AMReX_ParallelDescriptor.cpp:1291
int SeqNum() noexcept
Returns sequential message sequence numbers, usually used as tags for send/recv.
Definition AMReX_ParallelDescriptor.H:613
void GatherLayoutDataToVector(const LayoutData< T > &sendbuf, Vector< T > &recvbuf, int root)
Gather LayoutData values to a vector on root.
Definition AMReX_ParallelDescriptor.H:1211
Message Arecv(T *, size_t n, int pid, int tag)
Definition AMReX_ParallelDescriptor.H:1130
void Min(KeyValuePair< K, V > &vi, int root, MPI_Comm comm)
Definition AMReX_ParallelReduce.H:263
void Max(KeyValuePair< K, V > &vi, int root, MPI_Comm comm)
Definition AMReX_ParallelReduce.H:232
void Sum(T &v, int root, MPI_Comm comm)
Definition AMReX_ParallelReduce.H:320
T ExclusiveSum(N n, T const *in, T *out, RetSum a_ret_sum=retSum)
Definition AMReX_Scan.H:1267
static constexpr RetSum retSum
Definition AMReX_Scan.H:32
void clearEmptyEntries(C &c)
Definition AMReX_ParticleUtil.H:750
Definition AMReX_Amr.cpp:49
__host__ __device__ void ignore_unused(const Ts &...)
This shuts up the compiler about unused variables.
Definition AMReX.H:138
int nComp(FabArrayBase const &fa)
Definition AMReX_FabArrayBase.cpp:2852
__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:184
void communicateParticlesStart(const PC &pc, ParticleCopyPlan &plan, const SndBuffer &snd_buffer, RcvBuffer &rcv_buffer)
Definition AMReX_ParticleCommunication.H:500
std::enable_if_t< std::is_integral_v< T > > ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition AMReX_CTOParallelForImpl.H:191
__host__ __device__ void copyParticle(const ParticleTileData< T_ParticleType, NAR, NAI > &dst, const ConstParticleTileData< T_ParticleType, NAR, NAI > &src, int src_i, int dst_i) noexcept
A general single particle copying routine that can run on the GPU.
Definition AMReX_ParticleTransformation.H:31
void unpackRemotes(PC &pc, const ParticleCopyPlan &plan, Buffer &rcv_buffer, UnpackPolicy const &policy)
Definition AMReX_ParticleCommunication.H:600
void copyParticles(DstTile &dst, const SrcTile &src) noexcept
Copy particles from src to dst. This version copies all the particles, writing them to the beginning ...
Definition AMReX_ParticleTransformation.H:161
Long doHandShake(const std::map< int, Vector< char > > &not_ours, Vector< Long > &Snds, Vector< Long > &Rcvs)
Definition AMReX_ParticleMPIUtil.cpp:25
Index filterAndTransformParticles(DstTile &dst, const SrcTile &src, Index *mask, F const &f, Index src_start, Index dst_start) noexcept
Conditionally copy particles from src to dst based on the value of mask. A transformation will also b...
Definition AMReX_ParticleTransformation.H:456
bool initialized
Definition AMReX_DistributionMapping.cpp:32
__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:421
Long doHandShakeLocal(const std::map< int, Vector< char > > &not_ours, const Vector< int > &neighbor_procs, Vector< Long > &Snds, Vector< Long > &Rcvs)
Definition AMReX_ParticleMPIUtil.cpp:50
__host__ __device__ int numTilesInBox(const Box &box, const bool a_do_tiling, const IntVect &a_tile_size)
Definition AMReX_ParticleUtil.H:232
BoxND< 3 > Box
Definition AMReX_BaseFwd.H:27
__host__ __device__ Dim3 begin(BoxND< dim > const &box) noexcept
Definition AMReX_Box.H:1899
double second() noexcept
Definition AMReX_Utility.cpp:940
void communicateParticlesFinish(const ParticleCopyPlan &plan)
Definition AMReX_ParticleCommunication.cpp:384
Arena * The_Comms_Arena()
Definition AMReX_Arena.cpp:765
Index filterParticles(DstTile &dst, const SrcTile &src, const Index *mask) noexcept
Conditionally copy particles from src to dst based on the value of mask.
Definition AMReX_ParticleTransformation.H:330
IntVectND< 3 > IntVect
Definition AMReX_BaseFwd.H:30
void ParticleToMesh(PC const &pc, const Vector< MultiFab * > &mf, int lev_min, int lev_max, F &&f, bool zero_out_input=true, bool vol_weight=true)
Definition AMReX_AmrParticles.H:156
void Error(const std::string &msg)
Print out message to cerr and exit via amrex::Abort().
Definition AMReX.cpp:224
__host__ __device__ void amrex_deposit_cic(P const &p, int nc, amrex::Array4< amrex::Real > const &rho, amrex::GpuArray< amrex::Real, 3 > const &plo, amrex::GpuArray< amrex::Real, 3 > const &dxi)
Definition AMReX_Particle_mod_K.H:13
void gatherParticles(PTile &dst, const PTile &src, N np, const Index *inds)
Gather particles copies particles into contiguous order from an arbitrary order. Specifically,...
Definition AMReX_ParticleTransformation.H:674
__host__ __device__ BoxND< dim > grow(const BoxND< dim > &b, int i) noexcept
Grow BoxND in all directions by given amount.
Definition AMReX_Box.H:1229
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:34
int Verbose() noexcept
Definition AMReX.cpp:169
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, int lev_min, int lev_max, int nGrow, bool remove_negative)
Definition AMReX_ParticleUtil.H:654
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition AMReX.cpp:230
__host__ __device__ void amrex_deposit_particle_dx_cic(P const &p, int nc, amrex::Array4< amrex::Real > const &rho, amrex::GpuArray< amrex::Real, 3 > const &plo, amrex::GpuArray< amrex::Real, 3 > const &dxi, amrex::GpuArray< amrex::Real, 3 > const &pdxi)
Definition AMReX_Particle_mod_K.H:118
int verbose
Definition AMReX_DistributionMapping.cpp:36
void transformParticles(DstTile &dst, const SrcTile &src, F &&f) noexcept
Apply the function f to all the particles in src, writing the result to dst. This version does all th...
Definition AMReX_ParticleTransformation.H:213
__host__ __device__ Dim3 end(BoxND< dim > const &box) noexcept
Definition AMReX_Box.H:1908
void unpackBuffer(PC &pc, const ParticleCopyPlan &plan, const Buffer &snd_buffer, UnpackPolicy const &policy)
Definition AMReX_ParticleCommunication.H:435
void packBuffer(const PC &pc, const ParticleCopyOp &op, const ParticleCopyPlan &plan, Buffer &snd_buffer)
Definition AMReX_ParticleCommunication.H:336
__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:336
Definition AMReX_ParticleLocator.H:216
Definition AMReX_Array4.H:61
Definition AMReX_ParticleContainerI.H:1040
amrex::AmrAssignGrid< amrex::DenseBinIteratorFactory< amrex::Box > > m_assign_grid
Definition AMReX_ParticleContainerI.H:1043
int m_lev_max
Definition AMReX_ParticleContainerI.H:1042
AssignGridFilter(amrex::AmrAssignGrid< amrex::DenseBinIteratorFactory< amrex::Box > > assign_grid, int gid, int level, int nGrow)
This filters based on matching grids.
Definition AMReX_ParticleContainerI.H:1049
int m_nGrow
Definition AMReX_ParticleContainerI.H:1042
int m_lev_min
Definition AMReX_ParticleContainerI.H:1042
AMREX_GPU_HOST_DEVICE int operator()(const SrcData &src, int src_i) const noexcept
Definition AMReX_ParticleContainerI.H:1055
int m_gid
Definition AMReX_ParticleContainerI.H:1042
Definition AMReX_ParticleLocator.H:14
Definition AMReX_ParticleTile.H:513
Definition AMReX_ParticleUtil.H:394
Definition AMReX_DenseBins.H:32
Definition AMReX_ParticleContainerI.H:799
Box m_domain
Definition AMReX_ParticleContainerI.H:803
GpuArray< Real, 3 > m_dxi
Definition AMReX_ParticleContainerI.H:802
FilterVirt(const amrex::AssignGrid< amrex::DenseBinIteratorFactory< amrex::Box > > &assign_buffer_grid, const GpuArray< Real, 3 > &plo, const GpuArray< Real, 3 > &dxi, const Box &domain)
Definition AMReX_ParticleContainerI.H:805
AMREX_GPU_HOST_DEVICE int operator()(const SrcData &src, int src_i) const noexcept
Definition AMReX_ParticleContainerI.H:812
amrex::AssignGrid< amrex::DenseBinIteratorFactory< amrex::Box > > m_assign_buffer_grid
Definition AMReX_ParticleContainerI.H:801
GpuArray< Real, 3 > m_plo
Definition AMReX_ParticleContainerI.H:802
Definition AMReX_ParticleUtil.H:303
Definition AMReX_Array.H:34
Definition AMReX_GpuControl.H:184
uint64_t m_idcpu
Definition AMReX_Particle.H:359
Definition AMReX_ParticleCommunication.H:58
void setNumLevels(int num_levels)
Definition AMReX_ParticleCommunication.cpp:14
Vector< std::map< int, Gpu::DeviceVector< IntVect > > > m_periodic_shift
Definition AMReX_ParticleCommunication.H:62
Vector< std::map< int, Gpu::DeviceVector< int > > > m_boxes
Definition AMReX_ParticleCommunication.H:59
Vector< std::map< int, Gpu::DeviceVector< int > > > m_levels
Definition AMReX_ParticleCommunication.H:60
Vector< std::map< int, Gpu::DeviceVector< int > > > m_src_indices
Definition AMReX_ParticleCommunication.H:61
void resize(int gid, int lev, int size)
Definition AMReX_ParticleCommunication.cpp:22
Definition AMReX_ParticleCommunication.H:81
void buildMPIFinish(const ParticleBufferMap &map)
Definition AMReX_ParticleCommunication.cpp:213
void build(const PC &pc, const ParticleCopyOp &op, const Vector< int > &int_comp_mask, const Vector< int > &real_comp_mask, bool local)
Definition AMReX_ParticleCommunication.H:131
A struct used for storing a particle's position in the AMR hierarchy.
Definition AMReX_ParticleContainer.H:92
Box m_grown_gridbox
Definition AMReX_ParticleContainer.H:99
IntVect m_cell
Definition AMReX_ParticleContainer.H:96
int m_grid
Definition AMReX_ParticleContainer.H:94
int m_tile
Definition AMReX_ParticleContainer.H:95
int m_lev
Definition AMReX_ParticleContainer.H:93
Box m_tilebox
Definition AMReX_ParticleContainer.H:98
Box m_gridbox
Definition AMReX_ParticleContainer.H:97
Definition AMReX_ParticleTile.H:720
std::size_t size() const
Returns the total number of particles (real and neighbor)
Definition AMReX_ParticleTile.H:889
ParticleTileDataType getParticleTileData()
Definition AMReX_ParticleTile.H:1202
int numParticles() const
Returns the number of real particles (excluding neighbors)
Definition AMReX_ParticleTile.H:902
void resize(std::size_t count, GrowthStrategy strategy=GrowthStrategy::Poisson)
Definition AMReX_ParticleTile.H:968
AoS & GetArrayOfStructs()
Definition AMReX_ParticleTile.H:877
bool empty() const
Definition AMReX_ParticleTile.H:883
The struct used to store particles.
Definition AMReX_Particle.H:402
__host__ __device__ RealVect pos() const &
Definition AMReX_Particle.H:452
Definition AMReX_ParticleContainerI.H:1067
AMREX_GPU_HOST_DEVICE void operator()(DstData &dst, const SrcData &src, int src_i, int dst_i) const noexcept
Definition AMReX_ParticleContainerI.H:1071
Definition AMReX_ParticleContainerI.H:820
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator()(DstData &dst, const SrcData &src, int src_i, int dst_i) const noexcept
Definition AMReX_ParticleContainerI.H:823