chombo-discharge
Loading...
Searching...
No Matches
CD_ParticleContainerImplem.H
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2021-2026 SINTEF Energy Research
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
12
13#ifndef CD_PARTICLECONTAINERIMPLEM_H
14#define CD_PARTICLECONTAINERIMPLEM_H
15
16// Std includes
17#include <cmath>
18#include <cstddef>
19#include <cstdint>
20#include <cstdlib>
21#include <cstring>
22#include <limits>
23#include <map>
24#include <unordered_map>
25#include <utility>
26#include <vector>
27
28// Chombo includes
29#include <CH_Timer.H>
30#include <MayDay.H>
31#include <CH_assert.H>
32#include <BoxIterator.H>
33#include <parstream.H>
34
35// Our includes
37#include <CD_ParticleMemory.H>
38#include <CD_NamespaceHeader.H>
39
40template <typename P, typename Traits>
46
47template <typename P, typename Traits>
50 const Vector<DisjointBoxLayout>& a_sourceGrids)
51{
52 CH_TIME("ParticleContainer::gatherToPool");
53
54 MoverPool pool(numProc());
55
56 // Bin every particle of one leaf into a destination pool, returning the number of off-domain
57 // (outcast) particles. Shared by the serial and OpenMP paths so the binning logic lives once.
58 // findDestination() is const and reads only the (immutable-during-gather) tile maps, so it is
59 // safe to call concurrently; each thread bins into its own thread-local pool.
60 auto binLeaf = [this](MoverPool& a_target, const Leaf& a_leaf) -> unsigned long long {
61 unsigned long long outcast = 0;
62
63 for (std::size_t i = 0; i < a_leaf.size(); i++) {
64 RealVect pos = a_leaf.position(i);
65 auto dst = this->findDestination(pos);
66
67 if (!dst.valid && this->moveInsideDomain(pos)) {
68 dst = this->findDestination(pos);
69 }
70
71 if (!dst.valid) {
72 outcast++;
73 continue;
74 }
75
76 Leaf& bin = a_target[dst.rank][PoolKey(dst.level, dst.gridIndex)];
77
78 bin.append(pos, a_leaf.weight(i), a_leaf.gather(i));
79
80 const std::size_t k = bin.size() - 1;
81
82 bin.particleID(k) = a_leaf.particleID(i); // preserve id
83 bin.rankID(k) = dst.rank; // new owner
84 }
85
86 return outcast;
87 };
88
89 unsigned long long numOutcast = 0;
90
91 for (int lvl = 0; lvl < a_source.size(); lvl++) {
92 LevelParticles& level = *a_source[lvl];
93 const DataIterator& dit = a_sourceGrids[lvl].dataIterator();
94 const int nbox = dit.size();
95
96#ifdef _OPENMP
97#pragma omp parallel reduction(+ : numOutcast)
98 {
99 MoverPool threadPool(numProc()); // thread-local; no locking while binning
100
101#pragma omp for schedule(runtime)
102 for (int mybox = 0; mybox < nbox; mybox++) {
103 Leaf& leaf = level[dit[mybox]];
104
105 numOutcast += binLeaf(threadPool, leaf);
106
107 // The pool now holds this leaf's particles, and the source is discarded by every caller, so
108 // release the arena here rather than at the end. Otherwise the source and the pool are both
109 // fully live until the caller returns, which doubles the particle footprint at exactly the
110 // point in a regrid where it is already highest. Each thread owns its own boxes, so this is
111 // race-free.
112 leaf.clear();
113 leaf.shrinkToFit();
114 }
115
116 // Merge the thread-local pool into the shared pool. First writer to a (rank,key) slot gets the
117 // O(1) arena swap inside catenate(); later writers append.
118#pragma omp critical(ParticleContainer_gatherMerge)
119 {
120 for (int r = 0; r < numProc(); r++) {
121 for (auto& kv : threadPool[r]) {
122 pool[r][kv.first].catenate(kv.second);
123 }
124 }
125 }
126 }
127#else
128 for (int mybox = 0; mybox < nbox; mybox++) {
129 Leaf& leaf = level[dit[mybox]];
130
131 numOutcast += binLeaf(pool, leaf);
132
133 // See the OpenMP branch: drain as we go so the source and the pool are never both fully live.
134 leaf.clear();
135 leaf.shrinkToFit();
136 }
137#endif
138 }
139
140 m_numOutcastLocal = numOutcast;
141
142 return pool;
143}
144
145template <typename P, typename Traits>
148{
149 CH_TIME("ParticleContainer::gatherMoversToPool");
150
151 CH_assert(m_validCells != nullptr);
152
153 MoverPool pool(numProc());
154 unsigned long long numOutcast = 0;
155
156 for (int lvl = 0; lvl <= m_finestLevel; lvl++) {
157 LevelParticles& level = *m_particles[lvl];
158 const LevelData<BaseFab<bool>>* maskLevel = (*m_validCells)[lvl].isNull() ? nullptr : &(*(*m_validCells)[lvl]);
159 const DataIterator& dit = m_grids[lvl].dataIterator();
160 const int nbox = dit.size();
161
162 // Partition one leaf in place: keep stayers (still inside their box AND in a cell flagged valid =
163 // not covered by a finer level), pool the movers and swap-pop them out. a_mask == nullptr forces
164 // every particle to be treated as a mover (no usable mask for this leaf). Returns outcast movers.
165 // findDestination()/cellIndex() are const reads, so this is safe to call from many threads as long
166 // as each leaf is touched by a single thread (the omp-for partitions boxes disjointly).
167 auto partitionLeaf = [this, lvl](MoverPool& a_target,
168 [[maybe_unused]] const DataIndex& a_din,
169 Leaf& a_leaf,
170 const BaseFab<bool>* a_mask) -> unsigned long long {
171 unsigned long long outcast = 0;
172
173 const Box maskBox = (a_mask != nullptr) ? a_mask->box() : Box();
174
175 std::size_t i = 0;
176
177 while (i < a_leaf.size()) {
178 RealVect pos = a_leaf.position(i);
179 if (a_mask != nullptr) {
180 const IntVect iv = this->cellIndex(lvl, pos);
181
182 if (maskBox.contains(iv) && (*a_mask)(iv, 0)) {
183 // Stayer: in its own box and not under a finer grid, so it cannot have changed owner.
184 // Debug self-check of that invariant -- the destination the gather-all path would compute
185 // must be exactly this (level, grid, rank). If this ever fires, the skip is unsound.
186#ifndef NDEBUG
187 const auto chk = this->findDestination(pos);
188 CH_assert(chk.valid && chk.level == lvl && chk.rank == procID() &&
189 m_levelTiles[lvl]->getMyGrids().at(chk.gridIndex) == a_din);
190#endif
191 i++;
192
193 continue;
194 }
195 }
196
197 auto dst = this->findDestination(pos); // mover: route it
198
199 if (!dst.valid && this->moveInsideDomain(pos)) {
200 dst = this->findDestination(pos);
201 }
202
203 if (dst.valid) {
204 Leaf& bin = a_target[dst.rank][PoolKey(dst.level, dst.gridIndex)];
205
206 bin.append(pos, a_leaf.weight(i), a_leaf.gather(i));
207
208 const std::size_t k = bin.size() - 1;
209
210 bin.particleID(k) = a_leaf.particleID(i); // preserve id
211 bin.rankID(k) = dst.rank; // new owner
212 }
213 else {
214 outcast++;
215 }
216
217 a_leaf.remove(i); // swap-pop; do NOT advance i (a new particle now occupies slot i)
218 }
219
220 return outcast;
221 };
222
223#ifdef _OPENMP
224#pragma omp parallel reduction(+ : numOutcast)
225 {
226 MoverPool threadPool(numProc());
227
228#pragma omp for schedule(runtime)
229 for (int mybox = 0; mybox < nbox; mybox++) {
230 const DataIndex& din = dit[mybox];
231 const BaseFab<bool>* mask = (maskLevel != nullptr) ? &(*maskLevel)[din] : nullptr;
232
233 if (mask != nullptr && !mask->isUsable()) {
234 mask = nullptr;
235 }
236
237 numOutcast += partitionLeaf(threadPool, din, level[din], mask);
238 }
239
240#pragma omp critical(ParticleContainer_gatherMerge)
241 {
242 for (int r = 0; r < numProc(); r++) {
243 for (auto& kv : threadPool[r]) {
244 pool[r][kv.first].catenate(kv.second);
245 }
246 }
247 }
248 }
249#else
250 for (int mybox = 0; mybox < nbox; mybox++) {
251 const DataIndex& din = dit[mybox];
252 const BaseFab<bool>* mask = (maskLevel != nullptr) ? &(*maskLevel)[din] : nullptr;
253
254 if (mask != nullptr && !mask->isUsable()) {
255 mask = nullptr;
256 }
257
258 numOutcast += partitionLeaf(pool, din, level[din], mask);
259 }
260#endif
261 }
262
263 m_numOutcastLocal = numOutcast;
264
265 return pool;
266}
267
268template <typename P, typename Traits>
269inline void
270ParticleContainer<P, Traits>::distributeFromPool(MoverPool& a_pool, const bool a_setOwnerToReceiver)
271{
272 CH_TIME("ParticleContainer::distributeFromPool");
273
274 const int myRank = procID();
275 const int numRanks = numProc();
276
277 // ---- same-rank movers: catenate each pooled leaf into its destination leaf ----
278 // On the gather-all path the destination leaves are empty (remap clears them; regrid allocates them
279 // fresh) so catenate() takes the O(1) arena-swap fast path. On the mask fast path the destinations
280 // still hold their stayers, so catenate() appends (O(movers)); a mover never returns to its own
281 // origin leaf, so there is no self-append.
282 for (auto& kv : a_pool[myRank]) {
283 const int lvl = kv.first.first;
284 const unsigned int gidx = kv.first.second;
285 const DataIndex din = m_levelTiles[lvl]->getMyGrids().at(gidx);
286
287 (*m_particles[lvl])[din].catenate(kv.second);
288 }
289
290#ifdef CH_MPI
291 // ---- cross-rank movers: one Alltoallv of linearized particles ----
292 constexpr std::size_t pp = Leaf::bytesPerParticle();
293 constexpr std::size_t headerLen = 2 * sizeof(std::uint32_t) + sizeof(std::uint64_t);
294
295 // Size the send side before packing anything. A destination's byte count is arithmetic over the
296 // pool -- a header plus the payload for each pooled leaf -- so computing it up front costs nothing,
297 // and knowing it lets every particle be linearized straight into the flat buffer that MPI reads.
298 // Packing into per-destination buffers first and copying those into the flat one afterwards would
299 // hold a second full copy of every outbound particle for the duration of the copy.
300 std::vector<int> sendCounts(numRanks, 0);
301 std::vector<int> recvCounts(numRanks, 0);
302 std::vector<std::int64_t> sendBytes(numRanks, 0);
303
304 std::int64_t stot = 0;
305
306 for (int r = 0; r < numRanks; r++) {
307 if (r == myRank) {
308 continue;
309 }
310
311 std::int64_t bytes = 0;
312
313 for (const auto& kv : a_pool[r]) {
314 bytes += static_cast<std::int64_t>(headerLen + kv.second.size() * pp);
315 }
316
317 sendBytes[r] = bytes;
318 stot += bytes;
319 }
320
321 // MPI counts and displacements are int, so the byte totals have to fit in one. This is a real limit
322 // at scale rather than a formality -- a rank holding 2 GB of outbound particles reaches it -- and it
323 // has to be enforced in every build. CH_assert compiles out under OPT, which is precisely where the
324 // scale is, so a guard that used it would let the displacements overflow and corrupt the exchange
325 // silently instead of stopping it.
326 if (stot > static_cast<std::int64_t>(std::numeric_limits<int>::max())) {
327 MayDay::Abort("ParticleContainer::distributeFromPool - outbound particle bytes exceed MPI's int limit");
328 }
329
330 for (int r = 0; r < numRanks; r++) {
331 sendCounts[r] = static_cast<int>(sendBytes[r]);
332 }
333
334 MPI_Alltoall(sendCounts.data(), 1, MPI_INT, recvCounts.data(), 1, MPI_INT, Chombo_MPI::comm);
335
336 std::vector<int> sdispl(numRanks, 0);
337 std::vector<int> rdispl(numRanks, 0);
338
339 std::int64_t sacc = 0;
340 std::int64_t rtot = 0;
341
342 for (int r = 0; r < numRanks; r++) {
343 sdispl[r] = static_cast<int>(sacc);
344 sacc += sendCounts[r];
345
346 rdispl[r] = static_cast<int>(rtot);
347 rtot += recvCounts[r];
348 }
349
350 if (rtot > static_cast<std::int64_t>(std::numeric_limits<int>::max())) {
351 MayDay::Abort("ParticleContainer::distributeFromPool - inbound particle bytes exceed MPI's int limit");
352 }
353
354 std::vector<char> sflat(stot);
355 std::vector<char> rflat(rtot);
356
357 // The flat buffers are particle payload in flight, so account for them alongside the arenas. They
358 // are part of the peak this rank has to fit, and on a particle-heavy step they are comparable in
359 // size to the arenas themselves -- leaving them out would report a peak that understates the point
360 // in the step where an out-of-memory failure actually happens.
361 const std::size_t flatBytes = static_cast<std::size_t>(stot) + static_cast<std::size_t>(rtot);
362
364
365 // Each destination's slice of sflat is disjoint and linearizeParticle is a const read, so packing
366 // different destinations in parallel is race-free. A pooled leaf is spent once it has been
367 // linearized, so release it there and then: holding it until the pool goes out of scope would keep
368 // a second copy of every outbound particle alive across the exchange itself, which is the point in
369 // the step where the footprint peaks.
370#ifdef _OPENMP
371#pragma omp parallel for schedule(runtime)
372#endif
373 for (int r = 0; r < numRanks; r++) {
374 if (r == myRank) {
375 continue;
376 }
377
378 char* p = sflat.data() + sdispl[r];
379
380 for (auto& kv : a_pool[r]) {
381 Leaf& src = kv.second;
382
383 const std::uint32_t lvl = static_cast<std::uint32_t>(kv.first.first);
384 const std::uint32_t gidx = static_cast<std::uint32_t>(kv.first.second);
385 const std::uint64_t count = static_cast<std::uint64_t>(src.size());
386
387 std::memcpy(p, &lvl, sizeof(std::uint32_t));
388 p += sizeof(std::uint32_t);
389
390 std::memcpy(p, &gidx, sizeof(std::uint32_t));
391 p += sizeof(std::uint32_t);
392
393 std::memcpy(p, &count, sizeof(std::uint64_t));
394 p += sizeof(std::uint64_t);
395
396 for (std::size_t i = 0; i < count; i++) {
397 src.linearizeParticle(p, i);
398 p += pp;
399 }
400
401 src.clear();
402 src.shrinkToFit();
403 }
404 }
405
406 MPI_Alltoallv(sflat.data(),
407 sendCounts.data(),
408 sdispl.data(),
409 MPI_BYTE,
410 rflat.data(),
411 recvCounts.data(),
412 rdispl.data(),
413 MPI_BYTE,
414 Chombo_MPI::comm);
415
416 // ---- unpack: group received chunks by destination leaf, then append in parallel ----
417 // Pass 1 (serial, header-only walk): record each chunk's payload pointer + count, grouped by its
418 // destination (level, gridIndex). Pass 2 appends in parallel with one destination leaf per thread,
419 // so no two threads ever touch the same leaf (delinearizeAndAppend mutates the leaf arena).
420 struct RecvChunk
421 {
422 const char* payload; // first particle of the chunk within rflat
423 std::uint64_t count;
424 };
425 struct DestGroup
426 {
427 int level;
428 unsigned int gridIndex;
429 std::uint64_t total;
430 std::vector<RecvChunk> chunks;
431 };
432
433 std::vector<DestGroup> groups;
434 std::map<PoolKey, std::size_t> keyToGroup;
435 {
436 const char* p = rflat.data();
437 const char* end = p + rtot;
438
439 while (p < end) {
440 std::uint32_t lvl;
441 std::uint32_t gidx;
442 std::uint64_t count;
443
444 std::memcpy(&lvl, p, sizeof(std::uint32_t));
445 p += sizeof(std::uint32_t);
446
447 std::memcpy(&gidx, p, sizeof(std::uint32_t));
448 p += sizeof(std::uint32_t);
449
450 std::memcpy(&count, p, sizeof(std::uint64_t));
451 p += sizeof(std::uint64_t);
452
453 const PoolKey key(static_cast<int>(lvl), gidx);
454 const auto it = keyToGroup.find(key);
455
456 if (it == keyToGroup.end()) {
457 keyToGroup.emplace(key, groups.size());
458 groups.push_back(DestGroup{static_cast<int>(lvl), gidx, count, {RecvChunk{p, count}}});
459 }
460 else {
461 DestGroup& g = groups[it->second];
462 g.total += count;
463 g.chunks.push_back(RecvChunk{p, count});
464 }
465
466 p += count * pp;
467 }
468 }
469
470 const int numGroups = static_cast<int>(groups.size());
471
472#ifdef _OPENMP
473#pragma omp parallel for schedule(runtime)
474#endif
475 for (int d = 0; d < numGroups; d++) {
476 const DestGroup& g = groups[d];
477 const DataIndex din = m_levelTiles[g.level]->getMyGrids().at(g.gridIndex);
478
479 Leaf& tgt = (*m_particles[g.level])[din];
480
481 tgt.reserve(tgt.size() + g.total); // one allocation; avoids geometric regrow during the appends
482 for (const RecvChunk& chunk : g.chunks) {
483 const char* q = chunk.payload;
484
485 for (std::uint64_t c = 0; c < chunk.count; c++) {
487 q += pp;
488 if (a_setOwnerToReceiver) {
489 tgt.rankID(tgt.size() - 1) = myRank; // remap/regrid: owner is the receiving rank
490 }
491 // else: keep the rankID carried in the buffer (ghost particles record their true owner)
492 }
493 }
494 }
495
497#endif
498}
499
500template <typename P, typename Traits>
501inline void
503{
504 CH_TIME("ParticleContainer::remap");
505
506 CH_assert(m_isDefined);
507
508 // Ghost particles are transient, non-owned copies; drop them so they are never routed as if owned.
509 this->clearGhostParticles();
510
511 if (m_validCells != nullptr && static_cast<int>(m_validCells->size()) >= m_finestLevel + 1) {
512 // Mask fast path: a particle still in its box and not under a finer grid cannot change owner, so we
513 // leave those "stayers" in place and pool only the movers. Destinations therefore keep their stayers
514 // -- do NOT clearParticles() here; distributeFromPool() appends the movers onto the existing leaves.
515 MoverPool pool = this->gatherMoversToPool();
516
517 this->distributeFromPool(pool, true); // remap: the receiving rank becomes the new owner
518 }
519 else {
520 // Fallback: re-pool every valid particle (used when no mask is available, e.g. addParticlesDestructive
521 // staging, or before the realm's valid-cell mask has been built).
523
524 this->clearParticles();
525 this->distributeFromPool(pool, true); // remap: the receiving rank becomes the new owner
526 }
527
528 m_isOrganizedByCell = false;
529}
530
531template <typename P, typename Traits>
532inline void
534{
535 CH_TIME("ParticleContainer::preRegrid");
536
537 CH_assert(m_isDefined);
538
539 // Ghost particles are transient, non-owned copies; drop them so they are never cached/regridded.
540 this->clearGhostParticles();
541
544}
545
546template <typename P, typename Traits>
547inline void
548ParticleContainer<P, Traits>::regrid(const Vector<DisjointBoxLayout>& a_grids,
549 const Vector<ProblemDomain>& a_domains,
550 const Vector<Real>& a_dx,
551 const Vector<int>& a_refRat,
552 const int a_minBlockSize,
553 const Vector<RefCountedPtr<LevelTiles>>& a_levelTiles,
554 const int a_newFinestLevel)
555{
556 CH_TIME("ParticleContainer::regrid");
557
558 CH_assert(m_isDefined);
559 CH_assert(a_levelTiles.size() > a_newFinestLevel);
560
561 // ---- adopt the new layout ----
562 m_grids = a_grids;
563 m_domains = a_domains;
564 m_refRat = a_refRat;
565 m_minBlockSize = a_minBlockSize;
566 m_finestLevel = a_newFinestLevel;
567
568 // Alias the Realm's per-level tile->box maps (the single source of truth); never build our own.
569 m_levelTiles = a_levelTiles;
570
571 m_dx.resize(a_newFinestLevel + 1);
572 for (int lvl = 0; lvl <= a_newFinestLevel; lvl++) {
573 m_dx[lvl] = a_dx[lvl] * RealVect::Unit;
574 }
575
576 // ---- re-allocate the (empty) holders over the new grids ----
577 m_particles.resize(a_newFinestLevel + 1);
578 m_maskParticles.resize(a_newFinestLevel + 1);
579
580 for (int lvl = 0; lvl <= a_newFinestLevel; lvl++) {
581 m_particles[lvl] = RefCountedPtr<LevelParticles>(new LevelParticles(a_grids[lvl]));
582 m_maskParticles[lvl] = RefCountedPtr<LevelParticles>(new LevelParticles(a_grids[lvl]));
583 }
584
585 this->setupGrownGrids();
586
587 m_bufferParticles.resize(a_newFinestLevel + 1);
588
589 for (int lvl = 0; lvl <= a_newFinestLevel; lvl++) {
590 m_bufferParticles[lvl] = RefCountedPtr<LevelParticles>(new LevelParticles(m_grownGrids[lvl]));
591 }
592
593 // ---- redistribute the cached particles onto the new layout ----
595 this->distributeFromPool(pool, true); // regrid: the receiving rank becomes the new owner
596
597 // ---- release the cache ----
598 m_cacheParticles.resize(0);
599 m_cacheGrids.resize(0);
600
601 m_isOrganizedByCell = false;
602}
604template <typename P, typename Traits>
605inline void
607{
608 CH_TIME("ParticleContainer::clearGhostParticles");
609
610 CH_assert(m_isDefined);
611
612 for (int lvl = 0; lvl <= m_finestLevel; lvl++) {
613 LevelParticles& level = *m_particles[lvl];
614 const DisjointBoxLayout& dbl = m_grids[lvl];
615 const DataIterator& dit = dbl.dataIterator();
616
617 const int nbox = dit.size();
618
619#pragma omp parallel for schedule(runtime)
620 for (int mybox = 0; mybox < nbox; mybox++) {
621 const DataIndex& din = dit[mybox];
622 Leaf& leaf = level[din];
623
624 // Swap-and-pop every ghost; the swapped-in particle now occupies index i, so do NOT advance on a hit.
625 std::size_t i = 0;
626 while (i < leaf.size()) {
627 if (leaf.isGhost(i)) {
628 leaf.remove(i);
629 }
630 else {
631 i++;
632 }
633 }
634 }
635 }
636
637 m_isOrganizedByCell = false; // swap-pop reorders the surviving valid particles
638}
639
640template <typename P, typename Traits>
641inline void
643{
644 CH_TIME("ParticleContainer::claimOwnership");
645
646 CH_assert(m_isDefined);
647
648 const RankID myRank = static_cast<RankID>(procID());
649
650 // Ghosts are left untouched (they carry their true owner rank). Each non-ghost particle in the valid
651 // region (or all non-ghost particles when !a_onlyValidRegion) is stamped with this rank. Per-particle
652 // and box-local -> embarrassingly parallel.
653 for (int lvl = 0; lvl <= m_finestLevel; lvl++) {
654 LevelParticles& level = *m_particles[lvl];
655 const DisjointBoxLayout& dbl = m_grids[lvl];
656 const DataIterator& dit = dbl.dataIterator();
657 const int nbox = dit.size();
658
659#pragma omp parallel for schedule(runtime)
660 for (int mybox = 0; mybox < nbox; mybox++) {
661 const DataIndex& din = dit[mybox];
662 const Box box = dbl[din];
663 Leaf& leaf = level[din];
664
665 for (std::size_t i = 0; i < leaf.size(); i++) {
666 if (leaf.isGhost(i)) {
667 continue;
668 }
669 if (!a_onlyValidRegion || this->inValidBox(lvl, box, leaf.position(i))) {
670 leaf.rankID(i) = myRank;
671 }
672 }
673 }
674 }
675}
676
677template <typename P, typename Traits>
678inline long long
679ParticleContainer<P, Traits>::resetParticleIDs(const bool a_onlyValidRegion, const ParticleID a_startID)
680{
681 CH_TIME("ParticleContainer::resetParticleIDs");
682
683 CH_assert(m_isDefined);
684
685 // A non-ghost particle qualifies for a new id iff (!a_onlyValidRegion) or its cell lies in the leaf's
686 // grid box. Ghosts never qualify (and are left untouched); non-qualifying non-ghost strays get
687 // s_invalidID. The predicate must be identical in the count and assign passes below.
688
689 // Flatten the local boxes in (level, box) order -- this fixes the local id ordering.
690 std::vector<std::pair<int, DataIndex>> boxes;
691 for (int lvl = 0; lvl <= m_finestLevel; lvl++) {
692 const DataIterator& dit = m_grids[lvl].dataIterator();
693 const int nbox = dit.size();
694 for (int mybox = 0; mybox < nbox; mybox++) {
695 boxes.emplace_back(lvl, dit[mybox]);
697 }
698 const int numBoxes = static_cast<int>(boxes.size());
699
700 // Pass 1: count qualifying particles per box (parallel; each box independent).
701 std::vector<long long> boxCount(numBoxes, 0);
702#pragma omp parallel for schedule(runtime)
703 for (int b = 0; b < numBoxes; b++) {
704 const int lvl = boxes[b].first;
705 const DataIndex& din = boxes[b].second;
706 const Box box = m_grids[lvl][din];
707 const Leaf& leaf = (*m_particles[lvl])[din];
708
709 long long count = 0;
710 for (std::size_t i = 0; i < leaf.size(); i++) {
711 if (!leaf.isGhost(i) && (!a_onlyValidRegion || this->inValidBox(lvl, box, leaf.position(i)))) {
712 count++;
713 }
714 }
715 boxCount[b] = count;
716 }
717
718 // Local exclusive prefix over boxes + local total.
719 std::vector<long long> boxLocalOffset(numBoxes, 0);
720 long long localTotal = 0;
721 for (int b = 0; b < numBoxes; b++) {
722 boxLocalOffset[b] = localTotal;
723 localTotal += boxCount[b];
724 }
725
726 // Per-rank exclusive offset: all-gather each rank's local total, then sum the ranks before this one.
727 const Vector<long long> perRankCount = ParallelOps::gather(localTotal);
728
729 ParticleID rankOffset = a_startID;
730 for (int r = 0; r < procID(); r++) {
731 rankOffset += perRankCount[r];
732 }
733
734 // Pass 2: assign ids (parallel; each box starts at its own offset). Ghosts untouched; qualifying get a
735 // contiguous id; non-qualifying non-ghost strays get s_invalidID.
736#pragma omp parallel for schedule(runtime)
737 for (int b = 0; b < numBoxes; b++) {
738 const int lvl = boxes[b].first;
739 const DataIndex& din = boxes[b].second;
740 const Box box = m_grids[lvl][din];
741 Leaf& leaf = (*m_particles[lvl])[din];
742
743 ParticleID id = rankOffset + static_cast<ParticleID>(boxLocalOffset[b]);
744 for (std::size_t i = 0; i < leaf.size(); i++) {
745 if (leaf.isGhost(i)) {
746 continue;
747 }
748 if (!a_onlyValidRegion || this->inValidBox(lvl, box, leaf.position(i))) {
749 leaf.particleID(i) = id++;
750 }
751 else {
752 leaf.particleID(i) = ParticleSoA<P, Traits>::s_invalidID;
753 }
754 }
755 }
756
757 return ParallelOps::sum(localTotal);
758}
759
760template <typename P, typename Traits>
763 const AMRParticleGhostMask& a_maskCoarToFine,
764 const AMRParticleGhostMask& a_maskFineToCoar)
765{
766 CH_TIME("ParticleContainer::gatherGhostsFromMasks");
767
768 MoverPool pool(numProc());
769
770 // Bin one source leaf's valid particles as ghosts of every target box listed by its cell's masks. The
771 // three masks are all indexed by the SOURCE cell on the source level, so a single cell lookup drives all
772 // directions. GhostType is from the RECEIVER's view: same level -> SameLevel, coarse source scattered to
773 // a finer patch -> Coarse, fine source scattered to a coarser patch -> Fine.
774 auto binLeaf = [&](MoverPool& a_target, const Leaf& a_leaf, const int a_srcLvl, const DataIndex& a_din) {
775 const bool haveSame = (a_srcLvl < a_maskSame.size()) && !a_maskSame[a_srcLvl].isNull();
776 const bool haveC2F = (a_srcLvl < m_finestLevel) && (a_srcLvl < a_maskCoarToFine.size()) &&
777 !a_maskCoarToFine[a_srcLvl].isNull();
778 const bool haveF2C = (a_srcLvl >= 1) && (a_srcLvl < a_maskFineToCoar.size()) &&
779 !a_maskFineToCoar[a_srcLvl].isNull();
780
781 const ParticleGhostMask* same = haveSame ? &((*a_maskSame[a_srcLvl])[a_din]) : nullptr;
782 const ParticleGhostMask* c2f = haveC2F ? &((*a_maskCoarToFine[a_srcLvl])[a_din]) : nullptr;
783 const ParticleGhostMask* f2c = haveF2C ? &((*a_maskFineToCoar[a_srcLvl])[a_din]) : nullptr;
784
785 if (same == nullptr && c2f == nullptr && f2c == nullptr) {
786 return;
787 }
788
789 for (std::size_t i = 0; i < a_leaf.size(); i++) {
790 const RealVect pos = a_leaf.position(i);
791 const IntVect iv = this->cellIndex(a_srcLvl, pos);
792
793 auto scatter =
794 [&](const ParticleGhostMask* a_mask, const int a_dstLvl, const GhostType a_gt, const bool a_pruneByTargetBox) {
795 if (a_mask == nullptr || !a_mask->box().contains(iv)) {
796 return;
797 }
798 const int nt = a_mask->numTargets(iv);
799
800 // Coarse-to-fine only: the source (coarse) cell is coarser than the fine ghost width, so a whole
801 // coarse cell would over-scatter. Keep the particle for a target only if its DESTINATION (fine)
802 // cell lies in that target's acceptance box (the exact fine ghost shell).
803 const IntVect dstCell = a_pruneByTargetBox ? this->cellIndex(a_dstLvl, pos) : IntVect::Zero;
804
805 for (int k = 0; k < nt; k++) {
806 if (a_pruneByTargetBox && !a_mask->targetBox(iv, k).contains(dstCell)) {
807 continue;
808 }
809
810 Leaf& bin = a_target[a_mask->targetRank(iv, k)][PoolKey(a_dstLvl, a_mask->targetGridIndex(iv, k))];
811
812 bin.append(pos, a_leaf.weight(i), a_leaf.gather(i));
813
814 const std::size_t j = bin.size() - 1;
815
816 bin.particleID(j) = a_leaf.particleID(i); // preserve id
817 bin.rankID(j) = procID(); // owner is this rank (it owns its valid particles)
818 bin.ghost(j) = a_gt;
819 }
820 };
821
822 scatter(same, a_srcLvl, GhostType::SameLevel, false);
823 scatter(c2f, a_srcLvl + 1, GhostType::Coarse, true);
824 scatter(f2c, a_srcLvl - 1, GhostType::Fine, false);
825 }
826 };
827
828 for (int lvl = 0; lvl <= m_finestLevel; lvl++) {
829 const LevelParticles& level = *m_particles[lvl];
830 const DataIterator& dit = m_grids[lvl].dataIterator();
831 const int nbox = dit.size();
832
833#ifdef _OPENMP
834#pragma omp parallel
835 {
836 MoverPool threadPool(numProc()); // thread-local; no locking while binning
837
838#pragma omp for schedule(runtime)
839 for (int mybox = 0; mybox < nbox; mybox++) {
840 binLeaf(threadPool, level[dit[mybox]], lvl, dit[mybox]);
841 }
842
843#pragma omp critical(ParticleContainer_gatherGhostsFromMasksMerge)
844 {
845 for (int r = 0; r < numProc(); r++) {
846 for (auto& kv : threadPool[r]) {
847 pool[r][kv.first].catenate(kv.second);
848 }
849 }
850 }
851 }
852#else
853 for (int mybox = 0; mybox < nbox; mybox++) {
854 binLeaf(pool, level[dit[mybox]], lvl, dit[mybox]);
855 }
856#endif
857 }
858
859 return pool;
860}
861
862template <typename P, typename Traits>
863inline void
865 const AMRParticleGhostMask& a_maskCoarToFine,
866 const AMRParticleGhostMask& a_maskFineToCoar)
867{
868 CH_TIME("ParticleContainer::fillGhostParticles");
869
870 CH_assert(m_isDefined);
871
872 // Refill from a clean slate: drop any existing ghost halo first.
873 this->clearGhostParticles();
874
875 MoverPool pool = this->gatherGhostsFromMasks(a_maskSame, a_maskCoarToFine, a_maskFineToCoar);
876
877 // a_setOwnerToReceiver == false: ghost copies keep their true owner's rankID (not the receiver's).
878 this->distributeFromPool(pool, false);
879
880 m_isOrganizedByCell = false; // ghosts appended to the leaves invalidate any cell sort
881}
882
883template <typename P, typename Traits>
884inline void
886{
887 CH_TIME("ParticleContainer::setupGrownGrids");
888
889 m_grownGrids.resize(m_finestLevel + 1);
890 for (int lvl = 0; lvl <= m_finestLevel; lvl++) {
891 const DisjointBoxLayout& dbl = m_grids[lvl];
892
893 Vector<Box> boxes = dbl.boxArray();
894
895 if (lvl > 0) {
896 const Box domainBox = m_domains[lvl].domainBox();
897 for (int i = 0; i < boxes.size(); i++) {
898 boxes[i].grow(m_refRat[lvl - 1]);
899 boxes[i] &= domainBox;
901 }
902
903 m_grownGrids[lvl] = BoxLayout(boxes, dbl.procIDs());
904 }
905}
906
907template <typename P, typename Traits>
908inline void
909ParticleContainer<P, Traits>::copyMaskParticles(const Vector<RefCountedPtr<LevelData<BaseFab<bool>>>>& a_mask)
910{
911 CH_TIME("ParticleContainer::copyMaskParticles(amr)");
912
913 CH_assert(m_isDefined);
914
915 for (int lvl = 0; lvl <= m_finestLevel; lvl++) {
916 if (!a_mask[lvl].isNull()) {
917 this->copyMaskParticles(lvl, *a_mask[lvl]);
918 }
919 }
920}
921
922template <typename P, typename Traits>
923inline void
924ParticleContainer<P, Traits>::copyMaskParticles(const int a_level, const LevelData<BaseFab<bool>>& a_mask)
925{
926 CH_TIME("ParticleContainer::copyMaskParticles(level)");
927
928 CH_assert(m_isDefined);
930 LevelParticles& maskLevel = *m_maskParticles[a_level];
931 const LevelParticles& srcLevel = *m_particles[a_level];
932
933 const DisjointBoxLayout& dbl = m_grids[a_level];
934 const DataIterator& dit = dbl.dataIterator();
935
936 const int nbox = dit.size();
937
938#pragma omp parallel for schedule(runtime)
939 for (int mybox = 0; mybox < nbox; mybox++) {
940 const DataIndex& din = dit[mybox];
941
942 maskLevel[din].clear();
943
944 const BaseFab<bool>& mask = a_mask[din];
945 if (!mask.isUsable()) {
946 continue;
947 }
948 const Box maskBox = mask.box();
949
950 const Leaf& src = srcLevel[din];
951 Leaf& dst = maskLevel[din];
952
953 for (std::size_t i = 0; i < src.size(); i++) {
954 const IntVect iv = this->cellIndex(a_level, src.position(i));
955
956 if (maskBox.contains(iv) && mask(iv, 0)) {
957 dst.append(src.position(i), src.weight(i), src.gather(i));
958
959 const std::size_t k = dst.size() - 1;
960
961 dst.particleID(k) = src.particleID(i);
962 dst.rankID(k) = src.rankID(i);
963 }
964 }
965 }
966}
967
968template <typename P, typename Traits>
969inline void
970ParticleContainer<P, Traits>::transferMaskParticles(const Vector<RefCountedPtr<LevelData<BaseFab<bool>>>>& a_mask)
971{
972 CH_TIME("ParticleContainer::transferMaskParticles(amr)");
973
974 CH_assert(m_isDefined);
975
976 for (int lvl = 0; lvl <= m_finestLevel; lvl++) {
977 if (!a_mask[lvl].isNull()) {
978 this->transferMaskParticles(lvl, *a_mask[lvl]);
979 }
980 }
981}
982
983template <typename P, typename Traits>
984inline void
985ParticleContainer<P, Traits>::transferMaskParticles(const int a_level, const LevelData<BaseFab<bool>>& a_mask)
986{
987 CH_TIME("ParticleContainer::transferMaskParticles(level)");
988
989 CH_assert(m_isDefined);
990
991 LevelParticles& maskLevel = *m_maskParticles[a_level];
992 LevelParticles& srcLevel = *m_particles[a_level];
993
994 const DisjointBoxLayout& dbl = m_grids[a_level];
995 const DataIterator& dit = dbl.dataIterator();
996
997 const int nbox = dit.size();
998
999#pragma omp parallel for schedule(runtime)
1000 for (int mybox = 0; mybox < nbox; mybox++) {
1001 const DataIndex& din = dit[mybox];
1002
1003 const BaseFab<bool>& mask = a_mask[din];
1004 if (!mask.isUsable()) {
1005 continue;
1006 }
1007 const Box maskBox = mask.box();
1008
1009 Leaf& src = srcLevel[din];
1010 Leaf& dst = maskLevel[din];
1011
1012 std::size_t i = 0;
1013 while (i < src.size()) {
1014 const IntVect iv = this->cellIndex(a_level, src.position(i));
1015
1016 if (maskBox.contains(iv) && mask(iv, 0)) {
1017 dst.append(src.position(i), src.weight(i), src.gather(i));
1018
1019 const std::size_t k = dst.size() - 1;
1020
1021 dst.particleID(k) = src.particleID(i);
1022 dst.rankID(k) = src.rankID(i);
1023
1024 src.remove(i); // swap-and-pop; the swapped-in particle now occupies index i
1025 }
1026 else {
1027 ++i;
1028 }
1029 }
1030 }
1031}
1032
1033#include <CD_NamespaceFooter.H>
1034
1035#endif
Declaration of an AMR-hierarchy container that owns per-patch ParticleSoA leaves.
Vector< RefCountedPtr< LayoutData< ParticleSoA< P, Traits > > > > AMRParticlesSoA
Per-level holder vector: one ParticleSoA leaf container per box, per level.
Definition CD_ParticleContainer.H:56
Vector< RefCountedPtr< LayoutData< ParticleGhostMask > > > AMRParticleGhostMask
AMR-wide particle ghost-target lookup: one per-box CSR table per level, indexed by SOURCE level.
Definition CD_ParticleGhostMask.H:210
Process-wide accounting of the memory held by particle data.
std::int32_t RankID
Owning-rank identifier type (container-owned metadata column; fixed-width for I/O).
Definition CD_ParticleSoA.H:166
GhostType
Ghost designation of a particle (container-owned metadata column).
Definition CD_ParticleSoA.H:177
@ SameLevel
Ghost from an adjacent patch on the same level.
Definition CD_ParticleSoA.H:179
@ Coarse
Ghost from the next-coarser level (level l-1).
Definition CD_ParticleSoA.H:180
@ Fine
Ghost from the next-finer level (level l+1).
Definition CD_ParticleSoA.H:181
std::int64_t ParticleID
Global particle identifier type (container-owned metadata column; fixed-width for I/O).
Definition CD_ParticleSoA.H:161
static LevelAndBox findDestination(const RealVect &a_pos, const RealVect &a_probLo, const Vector< RealVect > &a_dx, const int a_minBlockSize, const Vector< RefCountedPtr< LevelTiles > > &a_levelTiles, const int a_finestLevel) noexcept
Map a physical position to its owning (level, grid index, rank) via the finest containing tile.
Definition CD_LevelTiles.H:161
void fillGhostParticles(const AMRParticleGhostMask &a_maskSame, const AMRParticleGhostMask &a_maskCoarToFine, const AMRParticleGhostMask &a_maskFineToCoar)
Fill each patch's ghost halo from prebuilt Realm particle ghost masks.
Definition CD_ParticleContainerImplem.H:864
std::vector< std::map< PoolKey, Leaf > > MoverPool
Per-rank pool of movers: pool[rank] maps a destination patch to a small SoA of particles.
Definition CD_ParticleContainer.H:917
void regrid(const Vector< DisjointBoxLayout > &a_grids, const Vector< ProblemDomain > &a_domains, const Vector< Real > &a_dx, const Vector< int > &a_refRat, const int a_minBlockSize, const Vector< RefCountedPtr< LevelTiles > > &a_levelTiles, const int a_newFinestLevel)
Rebuild over a new AMR layout and redistribute the preRegrid()-cached particles onto it.
Definition CD_ParticleContainerImplem.H:548
void clearParticles()
Drop all valid particles on every level (keeps each leaf's arena capacity).
Definition CD_ParticleContainer.H:447
AMRParticlesSoA< P, Traits > m_maskParticles
Halo/mask particles per level.
Definition CD_ParticleContainer.H:1063
IntVect cellIndex(const int a_lvl, const RealVect &a_pos) const
Cell index containing a position on a given level (floor((x - probLo)/dx)).
Definition CD_ParticleContainer.H:1082
void transferMaskParticles(const Vector< RefCountedPtr< LevelData< BaseFab< bool > > > > &a_mask)
Move the valid particles selected by a per-cell mask into the mask holder, all levels.
Definition CD_ParticleContainerImplem.H:970
AMRParticlesSoA< P, Traits > m_cacheParticles
Regrid cache: the pre-regrid valid holders, kept alive over the old layout by preRegrid().
Definition CD_ParticleContainer.H:1053
int m_finestLevel
Finest AMR level index.
Definition CD_ParticleContainer.H:1014
void setupGrownGrids()
Build the grown grids (buffer-particle layout): boxes grown by the refinement factor.
Definition CD_ParticleContainerImplem.H:885
MoverPool gatherMoversToPool()
Mask fast path for remap(): pool ONLY the movers, leaving stayers in place.
Definition CD_ParticleContainerImplem.H:147
void clearGhostParticles()
Remove every ghost particle (any non-Valid GhostType) from all levels and patches.
Definition CD_ParticleContainerImplem.H:606
Vector< BoxLayout > m_grownGrids
Per-level grown grids (boxes grown by the refinement factor on finer levels) for the buffer.
Definition CD_ParticleContainer.H:1073
void preRegrid()
Cache the current valid particles ahead of a regrid.
Definition CD_ParticleContainerImplem.H:533
AMRParticlesSoA< P, Traits > m_bufferParticles
Grown-grid buffer particles per level (on m_grownGrids; filled during halo deposition).
Definition CD_ParticleContainer.H:1068
Vector< int > m_refRat
Per-level refinement ratios (entry l is the ratio between level l and l+1).
Definition CD_ParticleContainer.H:989
AMRParticlesSoA< P, Traits > m_particles
Valid particles: one ParticleSoA leaf per box, per level.
Definition CD_ParticleContainer.H:1048
void claimOwnership(const bool a_onlyValidRegion)
Claim ownership of this rank's particles by stamping their owner rank (rankID = procID()).
Definition CD_ParticleContainerImplem.H:642
const Vector< RefCountedPtr< LevelData< BaseFab< bool > > > > * m_validCells
Optional alias to the realm's valid-cell mask (true = cell not covered by a finer level).
Definition CD_ParticleContainer.H:1033
bool m_isOrganizedByCell
Whether the valid leaves are currently cell-sorted.
Definition CD_ParticleContainer.H:1043
void distributeFromPool(MoverPool &a_pool, const bool a_setOwnerToReceiver)
Assign a pool into the valid holders: local appends + MPI scatter.
Definition CD_ParticleContainerImplem.H:270
ParticleSoA< P, Traits > Leaf
The per-patch leaf type owned at every (level, box).
Definition CD_ParticleContainer.H:128
unsigned long long m_numOutcastLocal
Particles dropped off-domain by the most recent remap() (local count).
Definition CD_ParticleContainer.H:1019
int m_minBlockSize
Grid blocking factor (tile size).
Definition CD_ParticleContainer.H:1009
RealVect m_probLo
Lower-left corner of the physical domain.
Definition CD_ParticleContainer.H:999
long long resetParticleIDs(const bool a_onlyValidRegion, const ParticleID a_startID)
Assign globally-unique, contiguous particle IDs to the owned particles.
Definition CD_ParticleContainerImplem.H:679
void remap()
Redistribute every valid particle to the patch/level/rank that owns its cell.
Definition CD_ParticleContainerImplem.H:502
Vector< RefCountedPtr< LevelTiles > > m_levelTiles
Per-level tile-ownership maps (cell tile -> owning grid index / rank), used by remap().
Definition CD_ParticleContainer.H:994
MoverPool gatherToPool(AMRParticlesSoA< P, Traits > &a_source, const Vector< DisjointBoxLayout > &a_sourceGrids)
Gather every particle of a source holder into a per-rank, per-destination-patch pool.
Definition CD_ParticleContainerImplem.H:49
LayoutData< Leaf > LevelParticles
The per-level holder type (one Leaf per box on the level's DisjointBoxLayout).
Definition CD_ParticleContainer.H:133
Vector< ProblemDomain > m_domains
Per-level problem domains.
Definition CD_ParticleContainer.H:979
LevelTiles::LevelAndBox findDestination(const RealVect &a_pos) const
Map a position to its owning (level, grid index, rank) via the finest containing tile.
Definition CD_ParticleContainerImplem.H:42
void copyMaskParticles(const Vector< RefCountedPtr< LevelData< BaseFab< bool > > > > &a_mask)
Copy the valid particles selected by a per-cell mask into the mask holder, all levels.
Definition CD_ParticleContainerImplem.H:909
Vector< RealVect > m_dx
Per-level grid spacing (isotropic, stored as RealVect).
Definition CD_ParticleContainer.H:984
Vector< DisjointBoxLayout > m_grids
Per-level AMR grids.
Definition CD_ParticleContainer.H:974
std::pair< int, unsigned int > PoolKey
Pool key identifying a destination patch: (level, grid index within the level).
Definition CD_ParticleContainer.H:912
bool m_isDefined
Whether define() has been called.
Definition CD_ParticleContainer.H:1038
MoverPool gatherGhostsFromMasks(const AMRParticleGhostMask &a_maskSame, const AMRParticleGhostMask &a_maskCoarToFine, const AMRParticleGhostMask &a_maskFineToCoar)
Gather ghost copies into a per-rank pool using prebuilt particle ghost masks.
Definition CD_ParticleContainerImplem.H:762
Vector< DisjointBoxLayout > m_cacheGrids
The old (pre-regrid) grids the cache holders are defined on (for iterating the cache).
Definition CD_ParticleContainer.H:1058
Per-box CSR lookup from a source cell to the destination boxes whose ghosted region covers it.
Definition CD_ParticleGhostMask.H:53
const Box & box() const noexcept
Get the box that the source cells live on.
Definition CD_ParticleGhostMaskImplem.H:90
unsigned int targetGridIndex(const IntVect &a_iv, const int a_i) const noexcept
Get the destination grid index of the i-th target for a cell.
Definition CD_ParticleGhostMaskImplem.H:116
const Box & targetBox(const IntVect &a_iv, const int a_i) const noexcept
Get the acceptance box of the i-th target for a cell.
Definition CD_ParticleGhostMaskImplem.H:128
int numTargets(const IntVect &a_iv) const noexcept
Get the number of scatter targets for a cell.
Definition CD_ParticleGhostMaskImplem.H:100
unsigned int targetRank(const IntVect &a_iv, const int a_i) const noexcept
Get the receiving rank of the i-th target for a cell.
Definition CD_ParticleGhostMaskImplem.H:122
RankID & rankID(const std::size_t a_index) noexcept
Owning rank of particle i (container-owned metadata).
Definition CD_ParticleSoA.H:1286
void append(const RealVect &a_position, const double a_weight)
Append one particle with a default-constructed payload.
Definition CD_ParticleSoA.H:962
void shrinkToFit()
Reclaim unused capacity by reallocating the arena down to the current size.
Definition CD_ParticleSoAImplem.H:187
bool isGhost(const std::size_t a_index) const noexcept
Whether particle i is a ghost particle (any non-Valid designation).
Definition CD_ParticleSoA.H:1338
void delinearizeAndAppend(const void *a_buffer)
Append a particle delinearized from a byte buffer (all columns, for MPI receive).
Definition CD_ParticleSoAImplem.H:243
void linearizeParticle(void *a_buffer, const std::size_t a_index) const noexcept
Linearize particle i into a byte buffer (all columns, for MPI send).
Definition CD_ParticleSoAImplem.H:232
RealVect position(const std::size_t a_index) const noexcept
Position of particle i as a RealVect (by value, assembled from the scalar columns).
Definition CD_ParticleSoA.H:1195
GhostType & ghost(const std::size_t a_index) noexcept
Ghost designation of particle i (container-owned metadata).
Definition CD_ParticleSoA.H:1312
double & weight(const std::size_t a_index) noexcept
Weight of particle i.
Definition CD_ParticleSoA.H:1229
static constexpr std::size_t bytesPerParticle() noexcept
Definition CD_ParticleSoA.H:1355
std::size_t size() const noexcept
Number of particles currently stored.
Definition CD_ParticleSoA.H:882
P gather(const std::size_t a_index) const
Gather particle i's payload back into the AoS payload view.
Definition CD_ParticleSoA.H:1028
ParticleID & particleID(const std::size_t a_index) noexcept
Global id of particle i (container-owned metadata).
Definition CD_ParticleSoA.H:1260
void remove(const std::size_t a_index) noexcept
Remove particle i using swap-and-pop (O(1), does NOT preserve order).
Definition CD_ParticleSoA.H:1040
static constexpr ParticleID s_invalidID
Sentinel for an unassigned/invalid global particle id.
Definition CD_ParticleSoA.H:1252
void clear() noexcept
Drop all particles (keeps the arena; invalidates the cell sort unless there was nothing to drop).
Definition CD_ParticleSoA.H:915
void reserve(const std::size_t a_capacity)
Ensure capacity for at least a_capacity particles (reallocates + moves on growth).
Definition CD_ParticleSoAImplem.H:88
Vector< int > gather(const int &a_localValue) noexcept
Gather local values – return a vector of local values.
Definition CD_ParallelOpsImplem.H:560
Real sum(const Real &a_value) noexcept
Compute the sum across all MPI ranks.
Definition CD_ParallelOpsImplem.H:354
void removeBytes(const Kind a_kind, const std::size_t a_bytes) noexcept
Record a release.
Definition CD_ParticleMemory.H:140
void addBytes(const Kind a_kind, const std::size_t a_bytes) noexcept
Record an allocation.
Definition CD_ParticleMemory.H:124
@ Buffer
Flat exchange buffers: payload in flight.
Definition CD_ParticleMemory.H:63
Result of a point->block query. See findDestination.
Definition CD_LevelTiles.H:123