Skip to content

Commit

Permalink
Add ParticleIDWrapper::make_invalid() (#3735)
Browse files Browse the repository at this point in the history
Add faster functions to ParticleIDWrapper: make_invalid, make_valid and is_valid.
  • Loading branch information
ax3l authored Feb 1, 2024
1 parent 52393d3 commit 296ed40
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Src/Particle/AMReX_Particle.H
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,41 @@ struct ParticleIDWrapper
r = (sign) ? lval : -lval;
return r;
}

/** Mark the particle as invalid
*
* Swaps the is_valid (sign) bit to invalid.
* This is NOT identical to id = -id, but it is equally reversible via make_valid().
*/
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
void make_invalid () noexcept
{
// RHS mask: 0111...
m_idata &= ~(uint64_t(1) << 63);
}

/** Mark the particle as valid
*
* Swaps the is_valid (sign) bit to valid.
* This is NOT identical to id = -id, but it is equally reversible via make_invalid().
*/
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
void make_valid () noexcept
{
// RHS mask: 1000...
m_idata |= uint64_t(1) << 63;
}

/** Check the particle is valid, via the sign of the id.
*
* Returns true if the particle is valid (the id is positive), otherwise false (invalid particle).
*/
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
bool is_valid () const noexcept
{
// the leftmost bit is our id's valid sign
return m_idata >> 63;
}
};

struct ParticleCPUWrapper
Expand Down Expand Up @@ -173,6 +208,17 @@ struct ConstParticleIDWrapper
r = (sign) ? lval : -lval;
return r;
}

/** Check the sign of the id.
*
* Returns true if the id is positive, otherwise false (invalid particle).
*/
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
bool is_valid () const noexcept
{
// the leftmost bit is our id's valid sign
return m_idata >> 63;
}
};

struct ConstParticleCPUWrapper
Expand Down

0 comments on commit 296ed40

Please sign in to comment.