Skip to content

Commit

Permalink
Make idcpu signed
Browse files Browse the repository at this point in the history
  • Loading branch information
ax3l committed Jan 31, 2024
1 parent 076f79e commit 76448a6
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 53 deletions.
38 changes: 19 additions & 19 deletions Src/Particle/AMReX_Particle.H
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ namespace

using namespace LongParticleIds;

/** Flags used to set the entire uint64_t idcpu
/** Flags used to set the entire int64_t idcpu
to special values at once.
*/
namespace ParticleIdCpus {
constexpr std::uint64_t Invalid = 16777216; // corresponds to id = -1, cpu = 0
constexpr std::int64_t Invalid = -16777216LL; // corresponds to id = -1, cpu = 0
}

using namespace ParticleIdCpus;
}

struct ParticleIDWrapper
{
uint64_t& m_idata;
int64_t& m_idata;

~ParticleIDWrapper () noexcept = default;

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
ParticleIDWrapper (uint64_t& idata) noexcept
ParticleIDWrapper (int64_t& idata) noexcept
: m_idata(idata)
{}

Expand Down Expand Up @@ -99,7 +99,7 @@ struct ParticleIDWrapper
{
// The uppermost bit is our id sign (0: +, 1: -).
// We will XOR it with 1 to flip it.
m_idata ^= static_cast<uint64_t>(1) << 63;
m_idata ^= static_cast<int64_t>(1) << 63;
}

/** Check the particle is valid, via the sign of the id.
Expand All @@ -116,12 +116,12 @@ struct ParticleIDWrapper

struct ParticleCPUWrapper
{
uint64_t& m_idata;
int64_t& m_idata;

~ParticleCPUWrapper () noexcept = default;

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
ParticleCPUWrapper (uint64_t& idata) noexcept
ParticleCPUWrapper (int64_t& idata) noexcept
: m_idata(idata)
{}

Expand Down Expand Up @@ -164,10 +164,10 @@ struct ParticleCPUWrapper

struct ConstParticleIDWrapper
{
const uint64_t& m_idata;
const int64_t& m_idata;

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
ConstParticleIDWrapper (const uint64_t& idata) noexcept
ConstParticleIDWrapper (const int64_t& idata) noexcept
: m_idata(idata)
{}

Expand Down Expand Up @@ -197,10 +197,10 @@ struct ConstParticleIDWrapper

struct ConstParticleCPUWrapper
{
const uint64_t& m_idata;
const int64_t& m_idata;

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
ConstParticleCPUWrapper (const uint64_t& idata) noexcept
ConstParticleCPUWrapper (const int64_t& idata) noexcept
: m_idata(idata)
{}

Expand All @@ -214,8 +214,8 @@ struct ConstParticleCPUWrapper
* to avoid writing twice into the same memory bank.
*/
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
std::uint64_t SetParticleIDandCPU (Long id, int cpu) noexcept{
std::uint64_t idcpu = 0;
std::int64_t SetParticleIDandCPU (Long id, int cpu) noexcept{
std::int64_t idcpu = 0;
ParticleIDWrapper{idcpu} = id;
ParticleCPUWrapper{idcpu} = cpu;
return idcpu;
Expand All @@ -226,15 +226,15 @@ struct ParticleBase
{
T m_pos[AMREX_SPACEDIM];
T m_rdata[NReal];
uint64_t m_idcpu = 0;
int64_t m_idcpu = 0;
int m_idata[NInt];
};

template <typename T, int NInt>
struct ParticleBase<T,0,NInt>
{
T m_pos[AMREX_SPACEDIM];
uint64_t m_idcpu = 0;
int64_t m_idcpu = 0;
int m_idata[NInt];
};

Expand All @@ -243,14 +243,14 @@ struct ParticleBase<T,NReal,0>
{
T m_pos[AMREX_SPACEDIM];
T m_rdata[NReal];
uint64_t m_idcpu = 0;
int64_t m_idcpu = 0;
};

template <typename T>
struct ParticleBase<T,0,0>
{
T m_pos[AMREX_SPACEDIM];
uint64_t m_idcpu = 0;
int64_t m_idcpu = 0;
};


Expand Down Expand Up @@ -299,7 +299,7 @@ struct Particle

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
void atomicSetID (const Long id) {
uint64_t tmp = 0;
int64_t tmp = 0;
ParticleIDWrapper wrapper(tmp);
wrapper = id;
#if defined(AMREX_USE_OMP)
Expand Down Expand Up @@ -409,7 +409,7 @@ struct Particle

template <int U = T_NInt, typename std::enable_if<U == 0, int>::type = 0>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
uint64_t& idata (int /*index*/) &
int64_t& idata (int /*index*/) &
{
AMREX_ALWAYS_ASSERT(false);
return this->m_idcpu; //bc we must return something
Expand Down
2 changes: 1 addition & 1 deletion Src/Particle/AMReX_ParticleCommunication.H
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ struct ParticleCopyPlan
}

if constexpr (PC::ParticleType::is_soa_particle) {
m_superparticle_size = sizeof(uint64_t); // idcpu
m_superparticle_size = sizeof(int64_t); // idcpu
} else {
m_superparticle_size = sizeof(typename PC::ParticleType);
}
Expand Down
26 changes: 13 additions & 13 deletions Src/Particle/AMReX_ParticleContainerI.H
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
}

if constexpr (ParticleType::is_soa_particle) {
particle_size = sizeof(uint64_t); // idcpu
particle_size = sizeof(int64_t); // idcpu
} else {
particle_size = sizeof(ParticleType);
}
Expand Down Expand Up @@ -1098,7 +1098,7 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
if constexpr (!ParticleType::is_soa_particle) {
static_assert(sizeof(ParticleType)%4 == 0 && sizeof(uint32_t) == 4);
using tmp_t = std::conditional_t<sizeof(ParticleType)%8 == 0,
uint64_t, uint32_t>;
int64_t, uint32_t>;
constexpr std::size_t nchunks = sizeof(ParticleType) / sizeof(tmp_t);
Gpu::DeviceVector<tmp_t> tmp(np);
auto* ptmp = tmp.data();
Expand Down Expand Up @@ -1722,8 +1722,8 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig

char* dst = &particles_to_send[old_size];
{
std::memcpy(dst, &soa.GetIdCPUData()[pindex], sizeof(uint64_t));
dst += sizeof(uint64_t);
std::memcpy(dst, &soa.GetIdCPUData()[pindex], sizeof(int64_t));
dst += sizeof(int64_t);
}
int array_comp_start = AMREX_SPACEDIM + NStructReal;
for (int comp = 0; comp < NumRealComps(); comp++) {
Expand Down Expand Up @@ -2067,10 +2067,10 @@ RedistributeMPI (std::map<int, Vector<char> >& not_ours,
Particle<NStructReal, NStructInt> p;

if constexpr (ParticleType::is_soa_particle) {
std::memcpy(&p.m_idcpu, pbuf, sizeof(uint64_t));
std::memcpy(&p.m_idcpu, pbuf, sizeof(int64_t));

ParticleReal pos[AMREX_SPACEDIM];
std::memcpy(&pos[0], pbuf + sizeof(uint64_t), AMREX_SPACEDIM*sizeof(ParticleReal));
std::memcpy(&pos[0], pbuf + sizeof(int64_t), AMREX_SPACEDIM*sizeof(ParticleReal));
AMREX_D_TERM(p.pos(0) = pos[0];,
p.pos(1) = pos[1];,
p.pos(2) = pos[2]);
Expand Down Expand Up @@ -2115,9 +2115,9 @@ RedistributeMPI (std::map<int, Vector<char> >& not_ours,
char* pbuf = ((char*) &recvdata[offset]) + j*superparticle_size;

if constexpr (ParticleType::is_soa_particle) {
uint64_t idcpudata;
std::memcpy(&idcpudata, pbuf, sizeof(uint64_t));
pbuf += sizeof(uint64_t);
int64_t idcpudata;
std::memcpy(&idcpudata, pbuf, sizeof(int64_t));
pbuf += sizeof(int64_t);
ptile.GetStructOfArrays().GetIdCPUData().push_back(idcpudata);
} else {
ParticleType p;
Expand Down Expand Up @@ -2168,7 +2168,7 @@ RedistributeMPI (std::map<int, Vector<char> >& not_ours,
host_int_attribs.reserve(15);
host_int_attribs.resize(finestLevel()+1);

Vector<std::map<std::pair<int, int>, Gpu::HostVector<uint64_t> > > host_idcpu;
Vector<std::map<std::pair<int, int>, Gpu::HostVector<int64_t> > > host_idcpu;
host_idcpu.reserve(15);
host_idcpu.resize(finestLevel()+1);

Expand All @@ -2189,9 +2189,9 @@ RedistributeMPI (std::map<int, Vector<char> >& not_ours,
host_int_attribs[lev][ind].resize(NumIntComps());

if constexpr (ParticleType::is_soa_particle) {
uint64_t idcpudata;
std::memcpy(&idcpudata, pbuf, sizeof(uint64_t));
pbuf += sizeof(uint64_t);
int64_t idcpudata;
std::memcpy(&idcpudata, pbuf, sizeof(int64_t));
pbuf += sizeof(int64_t);
host_idcpu[lev][ind].push_back(idcpudata);
} else {
ParticleType p;
Expand Down
4 changes: 2 additions & 2 deletions Src/Particle/AMReX_ParticleIO.H
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
host_int_attribs.reserve(15);
host_int_attribs.resize(finest_level_in_file+1);

Vector<std::map<std::pair<int, int>, Gpu::HostVector<uint64_t> > > host_idcpu;
Vector<std::map<std::pair<int, int>, Gpu::HostVector<int64_t> > > host_idcpu;
host_idcpu.reserve(15);
host_idcpu.resize(finestLevel()+1);

Expand All @@ -1006,7 +1006,7 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
yi = iptr[1];
std::memcpy(&xu, &xi, sizeof(xi));
std::memcpy(&yu, &yi, sizeof(yi));
ptemp.m_idcpu = ((std::uint64_t)xu) << 32 | yu;
ptemp.m_idcpu = ((std::int64_t)xu) << 32 | yu;
} else {
ptemp.id() = iptr[0];
ptemp.cpu() = iptr[1];
Expand Down
4 changes: 2 additions & 2 deletions Src/Particle/AMReX_ParticleInit.H
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ InitRandom (Long icount,
host_int_attribs.reserve(15);
host_int_attribs.resize(finestLevel()+1);

Vector<std::map<std::pair<int, int>, Gpu::HostVector<uint64_t> > > host_idcpu;
Vector<std::map<std::pair<int, int>, Gpu::HostVector<int64_t> > > host_idcpu;
host_idcpu.reserve(15);
host_idcpu.resize(finestLevel()+1);

Expand Down Expand Up @@ -1211,7 +1211,7 @@ InitRandom (Long icount,
host_int_attribs.reserve(15);
host_int_attribs.resize(finestLevel()+1);

Vector<std::map<std::pair<int, int>, Gpu::HostVector<uint64_t> > > host_idcpu;
Vector<std::map<std::pair<int, int>, Gpu::HostVector<int64_t> > > host_idcpu;
host_idcpu.reserve(15);
host_idcpu.resize(finestLevel()+1);

Expand Down
22 changes: 11 additions & 11 deletions Src/Particle/AMReX_ParticleTile.H
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct ParticleTileData
void*, ParticleType*>;
AOS_PTR AMREX_RESTRICT m_aos;

uint64_t* m_idcpu;
int64_t* m_idcpu;
GpuArray<ParticleReal*, NAR> m_rdata;
GpuArray<int*, NAI> m_idata;

Expand Down Expand Up @@ -128,8 +128,8 @@ struct ParticleTileData
memcpy(dst, m_aos + src_index, sizeof(ParticleType));
dst += sizeof(ParticleType);
} else {
memcpy(dst, m_idcpu + src_index, sizeof(uint64_t));
dst += sizeof(uint64_t);
memcpy(dst, m_idcpu + src_index, sizeof(int64_t));
dst += sizeof(int64_t);
}
int array_start_index = AMREX_SPACEDIM + NStructReal;
for (int i = 0; i < NAR; ++i)
Expand Down Expand Up @@ -179,8 +179,8 @@ struct ParticleTileData
memcpy(m_aos + dst_index, src, sizeof(ParticleType));
src += sizeof(ParticleType);
} else {
memcpy(m_idcpu + dst_index, src, sizeof(uint64_t));
src += sizeof(uint64_t);
memcpy(m_idcpu + dst_index, src, sizeof(int64_t));
src += sizeof(int64_t);
}
int array_start_index = AMREX_SPACEDIM + NStructReal;
for (int i = 0; i < NAR; ++i)
Expand Down Expand Up @@ -393,7 +393,7 @@ struct SoAParticle : SoAParticleBase
ParticleIDWrapper id () & { return this->m_particle_tile_data.m_idcpu[m_index]; }

[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
uint64_t& idcpu () & { return this->m_particle_tile_data.m_idcpu[m_index]; }
int64_t& idcpu () & { return this->m_particle_tile_data.m_idcpu[m_index]; }

[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
ConstParticleCPUWrapper cpu () const & { return this->m_particle_tile_data.m_idcpu[m_index]; }
Expand All @@ -402,7 +402,7 @@ struct SoAParticle : SoAParticleBase
ConstParticleIDWrapper id () const & { return this->m_particle_tile_data.m_idcpu[m_index]; }

[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
const uint64_t& idcpu () const & { return this->m_particle_tile_data.m_idcpu[m_index]; }
const int64_t& idcpu () const & { return this->m_particle_tile_data.m_idcpu[m_index]; }

//functions to get positions of the particle in the SOA data

Expand Down Expand Up @@ -508,7 +508,7 @@ struct ConstParticleTileData
void const*, ParticleType const*>;
AOS_PTR AMREX_RESTRICT m_aos;

const uint64_t* m_idcpu;
const int64_t* m_idcpu;
GpuArray<const ParticleReal*, NArrayReal> m_rdata;
GpuArray<const int*, NArrayInt > m_idata;

Expand Down Expand Up @@ -589,8 +589,8 @@ struct ConstParticleTileData
memcpy(dst, m_aos + src_index, sizeof(ParticleType));
dst += sizeof(ParticleType);
} else {
memcpy(dst, m_idcpu + src_index, sizeof(uint64_t));
dst += sizeof(uint64_t);
memcpy(dst, m_idcpu + src_index, sizeof(int64_t));
dst += sizeof(int64_t);
}
int array_start_index = AMREX_SPACEDIM + NStructReal;
for (int i = 0; i < NArrayReal; ++i)
Expand Down Expand Up @@ -1069,7 +1069,7 @@ struct ParticleTile
{
Long nbytes = 0;
if constexpr (ParticleType::is_soa_particle) {
nbytes += GetStructOfArrays().GetIdCPUData().capacity() * sizeof(uint64_t);
nbytes += GetStructOfArrays().GetIdCPUData().capacity() * sizeof(int64_t);
} else {
nbytes += m_aos_tile().capacity() * sizeof(ParticleType);
}
Expand Down
4 changes: 2 additions & 2 deletions Src/Particle/AMReX_StructOfArrays.H
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ template <int NReal, int NInt,
bool use64BitIdCpu=false>
struct StructOfArrays {

using IdCPU = amrex::PODVector<uint64_t, Allocator<uint64_t> >;
using IdCPU = amrex::PODVector<int64_t, Allocator<int64_t> >;
using RealVector = amrex::PODVector<ParticleReal, Allocator<ParticleReal> >;
using IntVector = amrex::PODVector<int, Allocator<int> >;

Expand Down Expand Up @@ -195,7 +195,7 @@ struct StructOfArrays {
for (int i = 0; i < int(m_runtime_idata.size()); ++i) { m_runtime_idata[i].resize(count); }
}

[[nodiscard]] uint64_t* idcpuarray () {
[[nodiscard]] int64_t* idcpuarray () {
if constexpr (use64BitIdCpu == true) {
return m_idcpu.dataPtr();
} else {
Expand Down
4 changes: 2 additions & 2 deletions Src/Particle/AMReX_WriteBinaryParticleData.H
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ packIOData (Vector<int>& idata, Vector<ParticleReal>& rdata, const PC& pc, int l
else {
amrex::ignore_unused(is_checkpoint);
// Int: id, cpu
uint64_t idcpu = soa.GetIdCPUData()[pindex];
int64_t idcpu = soa.GetIdCPUData()[pindex];
*iptr = (int) ParticleIDWrapper(idcpu);
iptr += 1;
*iptr = (int) ParticleCPUWrapper(idcpu);
Expand Down Expand Up @@ -1033,7 +1033,7 @@ void WriteBinaryParticleDataAsync (PC const& pc,
}
else {
// Ints: id, cpu
uint64_t idcpu = soa.GetIdCPUData()[pindex];
int64_t idcpu = soa.GetIdCPUData()[pindex];
*iptr = (int) ParticleIDWrapper(idcpu);
iptr += 1;
*iptr = (int) ParticleCPUWrapper(idcpu);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Particles/RedistributeSOA/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class TestParticleContainer
{
const Box& tile_box = mfi.tilebox();

Gpu::HostVector<uint64_t> host_idcpu;
Gpu::HostVector<int64_t> host_idcpu;
std::array<Gpu::HostVector<ParticleReal>, NR> host_real;
std::array<Gpu::HostVector<int>, NI> host_int;

Expand Down

0 comments on commit 76448a6

Please sign in to comment.