13#ifndef CD_PARTICLESOAIMPLEM_H
14#define CD_PARTICLESOAIMPLEM_H
25#include <CD_NamespaceHeader.H>
27template <
typename P,
typename Traits>
31 std::array<std::size_t, s_numColumns> off{};
32 const std::size_t total = this->
computeOffsets(a_capacity, off, std::make_index_sequence<s_numColumns>{});
34 const std::size_t bytes =
alignUp(total);
36 void*
const newBuf = std::aligned_alloc(
s_align, bytes);
38 if (newBuf ==
nullptr) {
39 MayDay::Abort(
"ParticleSoA::reallocate - std::aligned_alloc returned null (out of memory)");
43 this->
moveColumns(newBuf, off, std::make_index_sequence<s_numColumns>{});
55 this->
setColPtrs(off, std::make_index_sequence<s_numColumns>{});
58template <
typename P,
typename Traits>
62 std::array<std::size_t, s_numColumns> off{};
65 const std::size_t bytes =
alignUp(total);
67 void*
const newBuf = std::aligned_alloc(
s_align, bytes);
69 if (newBuf ==
nullptr) {
70 MayDay::Abort(
"ParticleSoA::permute - std::aligned_alloc returned null (out of memory)");
73 this->
scatterColumns(newBuf, off, a_dest, std::make_index_sequence<s_numColumns>{});
83 this->
setColPtrs(off, std::make_index_sequence<s_numColumns>{});
86template <
typename P,
typename Traits>
97template <
typename P,
typename Traits>
107template <
typename P,
typename Traits>
128template <
typename P,
typename Traits>
132 if (
this == &a_dst) {
151template <
typename P,
typename Traits>
155 const std::size_t n = a_other.
m_size;
161 const std::size_t oldSize =
m_size;
163 this->
growTo(oldSize + n);
164 this->
appendColumnsFrom(a_other, oldSize, std::make_index_sequence<s_numColumns>{});
170template <
typename P,
typename Traits>
174 CH_assert(a_index < a_src.
m_size);
176 const std::size_t at =
m_size;
185template <
typename P,
typename Traits>
209template <
typename P,
typename Traits>
215 for (
int dir = 0; dir < SpaceDim; dir++) {
223 this->
writePayload(a_payload, std::make_index_sequence<s_numPayloadColumns>{});
230template <
typename P,
typename Traits>
234 CH_assert(a_index <
m_size);
236 auto* p =
static_cast<unsigned char*
>(a_buffer);
238 this->
linearizeImpl(p, a_index, std::make_index_sequence<s_numColumns>{});
241template <
typename P,
typename Traits>
247 const auto* p =
static_cast<const unsigned char*
>(a_buffer);
256template <
typename P,
typename Traits>
260 CH_assert(a_index <
m_size);
262 auto* p =
static_cast<unsigned char*
>(a_buffer);
264 for (
int dir = 0; dir < SpaceDim; dir++) {
273template <
typename P,
typename Traits>
279 const auto* p =
static_cast<const unsigned char*
>(a_buffer);
281 for (
int dir = 0; dir < SpaceDim; dir++) {
294 this->
writePayload(def, std::make_index_sequence<s_numPayloadColumns>{});
301template <
typename P,
typename Traits>
317 const std::size_t nCells = a_box.numPts();
330 std::vector<std::size_t> key(
m_size);
331 for (std::size_t i = 0; i <
m_size; i++) {
332 const std::size_t lin = this->
cellKey(i, a_box, a_dx, a_probLo);
339 for (std::size_t c = 1; c <= nCells; c++) {
345 std::vector<std::size_t> dest(
m_size);
347 for (std::size_t i = 0; i <
m_size; i++) {
348 dest[i] = cursor[key[i]]++;
356#include <CD_NamespaceFooter.H>
Declaration of ParticleSoA, an arena-backed Struct-of-Arrays particle container.
void pushBytes(unsigned char *&a_buf, const T &a_value) noexcept
memcpy one trivially-copyable value onto a byte buffer and advance the buffer pointer.
Definition CD_ParticleSoA.H:590
std::int32_t RankID
Owning-rank identifier type (container-owned metadata column; fixed-width for I/O).
Definition CD_ParticleSoA.H:166
@ Valid
Owned, valid particle of this patch (not a ghost).
Definition CD_ParticleSoA.H:178
void pullBytes(const unsigned char *&a_buf, T &a_value) noexcept
memcpy one trivially-copyable value off a byte buffer and advance the buffer pointer.
Definition CD_ParticleSoA.H:605
Arena-backed Struct-of-Arrays particle container for a single grid patch.
Definition CD_ParticleSoA.H:655
void h5DelinearizePayload(const unsigned char *&a_buf, std::index_sequence< J... >) noexcept
Pull the HDF5 payload subset off a byte buffer into slot m_size.
Definition CD_ParticleSoA.H:2113
void permute(const std::vector< std::size_t > &a_dest)
Reorder every column in place by a_dest (a_dest[i] = new slot of old particle i).
Definition CD_ParticleSoAImplem.H:60
void h5LinearizePayload(unsigned char *&a_buf, const std::size_t a_index, std::index_sequence< J... >) const noexcept
Push the HDF5 payload subset of particle a_index onto a byte buffer.
Definition CD_ParticleSoA.H:2099
void sortByCell(const Box &a_box, const RealVect &a_dx, const RealVect &a_probLo)
Counting-sort the columns into Fortran cell order and build CSR cell offsets.
Definition CD_ParticleSoAImplem.H:303
void append(const RealVect &a_position, const double a_weight)
Append one particle with a default-constructed payload.
Definition CD_ParticleSoA.H:962
ColumnType< K > * columnByIndex() noexcept
Typed base pointer of column K (low-level).
Definition CD_ParticleSoA.H:1063
void shrinkToFit()
Reclaim unused capacity by reallocating the arena down to the current size.
Definition CD_ParticleSoAImplem.H:187
bool m_sorted
Whether m_cellStart is valid.
Definition CD_ParticleSoA.H:1656
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
std::size_t m_capacity
Capacity in particles.
Definition CD_ParticleSoA.H:1636
void deepCopyTo(ParticleSoA &a_dst) const
Deep copy into an existing destination, reusing its arena when possible.
Definition CD_ParticleSoAImplem.H:130
static constexpr std::size_t s_align
Cache-line alignment for each column (also enables aligned SIMD).
Definition CD_ParticleSoA.H:705
void setColPtrs(const std::array< std::size_t, s_numColumns > &a_off, std::index_sequence< I... >) noexcept
Cache the column base pointers from a fresh buffer + offsets.
Definition CD_ParticleSoA.H:1863
std::size_t m_size
Live particle count.
Definition CD_ParticleSoA.H:1651
std::size_t computeOffsets(const std::size_t a_capacity, std::array< std::size_t, s_numColumns > &a_off, std::index_sequence< I... >) const noexcept
Compute per-column byte offsets for a given capacity; returns the total bytes.
Definition CD_ParticleSoA.H:1846
double * positionColumn(const int a_dir) noexcept
Raw position component column dir (double*, for SIMD kernels).
Definition CD_ParticleSoA.H:1144
void copyLiveColumnsTo(ParticleSoA &a_dst, std::index_sequence< I... >) const noexcept
Copy this container's m_size live elements of every column into another container.
Definition CD_ParticleSoA.H:1894
void appendColumnsFrom(const ParticleSoA &a_other, const std::size_t a_at, std::index_sequence< I... >) noexcept
Bulk-append another container's live columns at slot a_at of this container's columns.
Definition CD_ParticleSoA.H:1911
ParticleSoA deepCopy() const
Explicit deep copy: a new container with a private arena duplicating every particle.
Definition CD_ParticleSoAImplem.H:109
void h5DelinearizeAndAppend(const void *a_buffer)
Append a particle whose HDF5-checkpointed columns come from a buffer.
Definition CD_ParticleSoAImplem.H:275
void h5LinearizeParticle(void *a_buffer, const std::size_t a_index) const noexcept
Linearize the HDF5-checkpointed columns of particle i (no id/rank).
Definition CD_ParticleSoAImplem.H:258
void linearizeImpl(unsigned char *a_buf, const std::size_t a_index, std::index_sequence< I... >) const noexcept
Push every column of particle a_index onto a byte buffer (MPI).
Definition CD_ParticleSoA.H:2048
std::array< void *, s_numColumns > m_colPtrs
Cached column base pointers into m_buffer.
Definition CD_ParticleSoA.H:1661
void * m_buffer
The single arena allocation.
Definition CD_ParticleSoA.H:1631
void delinearizeImpl(const unsigned char *a_buf, std::index_sequence< I... >) noexcept
Pull every column of one particle off a byte buffer into slot m_size (MPI).
Definition CD_ParticleSoA.H:2061
void resize(const std::size_t a_count)
Set the particle count (grows capacity if needed; new slots are uninitialized).
Definition CD_ParticleSoAImplem.H:99
void writePayload(const P &a_p, std::index_sequence< J... >) noexcept
Write the payload columns of slot m_size from a payload struct.
Definition CD_ParticleSoA.H:1999
std::size_t m_bytes
Byte size of the arena at m_buffer (zero when m_buffer is null).
Definition CD_ParticleSoA.H:1646
static std::size_t alignUp(const std::size_t a_x) noexcept
Round a_x up to the next multiple of s_align.
Definition CD_ParticleSoA.H:1762
void scatterColumns(void *a_newBuf, const std::array< std::size_t, s_numColumns > &a_off, const std::vector< std::size_t > &a_dest, std::index_sequence< I... >) noexcept
Scatter every column for permute() into a freshly-allocated buffer.
Definition CD_ParticleSoA.H:1983
std::vector< std::size_t > m_cellStart
CSR cell offsets (size numCells+1 when sorted).
Definition CD_ParticleSoA.H:1666
std::size_t cellKey(const std::size_t a_index, const Box &a_box, const RealVect &a_dx, const RealVect &a_probLo) const noexcept
Compute the Fortran (x-fastest) cell index of particle i within the sort domain.
Definition CD_ParticleSoA.H:1694
RealVect m_sortDx
Grid spacing the CSR mapping was built against (meaningful only when m_sorted).
Definition CD_ParticleSoA.H:1676
static constexpr ParticleID s_invalidID
Sentinel for an unassigned/invalid global particle id.
Definition CD_ParticleSoA.H:1252
void growTo(const std::size_t a_needed)
Grow the arena to hold at least a_needed particles, geometrically.
Definition CD_ParticleSoA.H:1810
void growIfFull()
Grow geometrically if the container is full (called before writing slot m_size).
Definition CD_ParticleSoA.H:1830
RealVect m_sortProbLo
Lower domain corner the CSR mapping was built against (meaningful only when m_sorted).
Definition CD_ParticleSoA.H:1681
void copyParticleColumns(const ParticleSoA &a_src, const std::size_t a_srcIdx, const std::size_t a_dstIdx, std::index_sequence< I... >) noexcept
Copy one particle's every column from a_src[a_srcIdx] into this[a_dstIdx].
Definition CD_ParticleSoA.H:1932
Box m_sortBox
Patch box the CSR mapping in m_cellStart was built against (meaningful only when m_sorted).
Definition CD_ParticleSoA.H:1671
typename detail::H5ColumnSubset< Traits, std::make_index_sequence< s_numPayloadColumns > >::Type H5PayloadColumns
The HDF5 payload column subset (indices into the payload columns).
Definition CD_ParticleSoA.H:759
bool isSortedAgainst(const Box &a_box, const RealVect &a_dx, const RealVect &a_probLo) const noexcept
Whether the container is cell-sorted against exactly this cell domain.
Definition CD_ParticleSoA.H:1495
void moveColumns(void *a_newBuf, const std::array< std::size_t, s_numColumns > &a_off, std::index_sequence< I... >) noexcept
Contiguously memcpy every column's live region into a freshly-allocated buffer.
Definition CD_ParticleSoA.H:1877
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 appendParticle(const ParticleSoA &a_src, const std::size_t a_index)
Append a single particle (all columns, incl. id/rank) copied from another container.
Definition CD_ParticleSoAImplem.H:172
bool checkCellSort(const Box &a_box, const RealVect &a_dx, const RealVect &a_probLo) const noexcept
Verify that the CSR mapping really does describe the current particle positions.
Definition CD_ParticleSoA.H:1729
void reallocate(const std::size_t a_capacity)
Allocate a new arena and reallocate/move the columns into it (growth path).
Definition CD_ParticleSoAImplem.H:29
ParticleSoA()=default
Construct an empty container.
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
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
@ Container
ParticleSoA arenas: capacity a rank is holding.
Definition CD_ParticleMemory.H:62