Skip to content

Commit

Permalink
Disable m_aos for SoA Particle (AMReX-Codes#3736)
Browse files Browse the repository at this point in the history
Change the type of m_aos for SoA particle to void*. Because void* cannot
be dereferenced, this can avoid bugs that use m_aos on SoA particles.
  • Loading branch information
WeiqunZhang authored Jan 31, 2024
1 parent 09fc44a commit d861bdf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
8 changes: 6 additions & 2 deletions Src/Particle/AMReX_ParticleMesh.H
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <AMReX_TypeTraits.H>
#include <AMReX_MultiFab.H>
#include <AMReX_ParticleUtil.H>
#include <type_traits>

namespace amrex {

Expand All @@ -18,9 +19,12 @@ auto call_f (F const& f,
GpuArray<Real,AMREX_SPACEDIM> const& plo,
GpuArray<Real,AMREX_SPACEDIM> const& dxi) noexcept
{
if constexpr (IsCallable<F, decltype(p.m_aos[i]), decltype(fabarr), decltype(plo), decltype(dxi)>::value) {
using PTDTypeT = std::remove_const_t<std::remove_reference_t<decltype(p)>>;
if constexpr ( ! T_ParticleType::is_soa_particle &&
IsCallable<F, typename PTDTypeT::ParticleRefType, decltype(fabarr), decltype(plo), decltype(dxi)>::value) {
return f(p.m_aos[i], fabarr, plo, dxi);
} else if constexpr (IsCallable<F, decltype(p.m_aos[i]), decltype(fabarr)>::value) {
} else if constexpr ( ! T_ParticleType::is_soa_particle &&
IsCallable<F, typename PTDTypeT::ParticleRefType, decltype(fabarr)>::value) {
return f(p.m_aos[i], fabarr);
} else if constexpr (IsCallable<F, decltype(p.getSuperParticle(i)), decltype(fabarr), decltype(plo), decltype(dxi)>::value) {
return f(p.getSuperParticle(i), fabarr, plo, dxi);
Expand Down
4 changes: 3 additions & 1 deletion Src/Particle/AMReX_ParticleReduce.H
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <AMReX_Vector.H>

#include <limits>
#include <type_traits>

namespace amrex {

Expand All @@ -23,7 +24,8 @@ auto call_f (F const& f,
const ConstParticleTileData<T_ParticleType, NAR, NAI>& p,
const int i) noexcept
{
if constexpr (IsCallable<F, decltype(p.m_aos[i])>::value) {
if constexpr ( ! T_ParticleType::is_soa_particle &&
IsCallable<F, T_ParticleType const&>::value) {
return f(p.m_aos[i]);
} else if constexpr (IsCallable<F, decltype(p.getSuperParticle(i))>::value) {
return f(p.getSuperParticle(i));
Expand Down
12 changes: 10 additions & 2 deletions Src/Particle/AMReX_ParticleTile.H
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <AMReX_RealVect.H>

#include <array>
#include <type_traits>

namespace amrex {

Expand All @@ -30,6 +31,7 @@ struct ParticleTileData
static constexpr int NAI = NArrayInt;

using ParticleType = T_ParticleType;
using ParticleRefType = T_ParticleType&;
using Self = ParticleTileData<ParticleType, NAR, NAI>;

static constexpr int NStructReal = ParticleType::NReal;
Expand All @@ -41,7 +43,9 @@ struct ParticleTileData

Long m_size;

ParticleType* AMREX_RESTRICT m_aos;
using AOS_PTR = std::conditional_t<T_ParticleType::is_soa_particle,
void*, ParticleType*>;
AOS_PTR AMREX_RESTRICT m_aos;

uint64_t* m_idcpu;
GpuArray<ParticleReal*, NAR> m_rdata;
Expand Down Expand Up @@ -489,6 +493,7 @@ struct ConstParticleTileData
static constexpr int NAR = NArrayReal;
static constexpr int NAI = NArrayInt;
using ParticleType = T_ParticleType;
using ParticleRefType = T_ParticleType const&;

static constexpr int NStructReal = ParticleType::NReal;
static constexpr int NStructInt = ParticleType::NInt;
Expand All @@ -498,7 +503,10 @@ struct ConstParticleTileData
static constexpr bool is_particle_tile_data = true;

Long m_size;
const ParticleType* AMREX_RESTRICT m_aos;

using AOS_PTR = std::conditional_t<T_ParticleType::is_soa_particle,
void const*, ParticleType const*>;
AOS_PTR AMREX_RESTRICT m_aos;

const uint64_t* m_idcpu;
GpuArray<const ParticleReal*, NArrayReal> m_rdata;
Expand Down

0 comments on commit d861bdf

Please sign in to comment.