From 296ed40e16ae1877640f5b78e9162dbd4ba1c279 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Thu, 1 Feb 2024 09:09:09 -0800 Subject: [PATCH] Add `ParticleIDWrapper::make_invalid()` (#3735) Add faster functions to ParticleIDWrapper: make_invalid, make_valid and is_valid. --- Src/Particle/AMReX_Particle.H | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Src/Particle/AMReX_Particle.H b/Src/Particle/AMReX_Particle.H index 618d4f185a9..baaafe4c51a 100644 --- a/Src/Particle/AMReX_Particle.H +++ b/Src/Particle/AMReX_Particle.H @@ -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 @@ -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