Skip to content

Commit

Permalink
particle array copy impl isolation
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Mar 6, 2025
1 parent fdc768d commit bf61400
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 61 deletions.
70 changes: 24 additions & 46 deletions src/amr/messengers/hybrid_hybrid_messenger_strategy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ namespace PHARE
namespace amr
{

/** \brief An HybridMessenger is the specialization of a HybridMessengerStrategy for hybrid to
* hybrid data communications.
/** \brief An HybridMessenger is the specialization of a HybridMessengerStrategy for hybrid
* to hybrid data communications.
*/
template<typename HybridModel, typename RefinementParams>
class HybridHybridMessengerStrategy : public HybridMessengerStrategy<HybridModel>
Expand Down Expand Up @@ -82,7 +82,7 @@ namespace amr
using DefaultCoarsenOp = BaseCoarsenOp<DefaultFieldCoarsener<dimension>>;

public:
static const inline std::string stratName = "HybridModel-HybridModel";
static inline std::string const stratName = "HybridModel-HybridModel";
static constexpr std::size_t rootLevelNumber = 0;


Expand Down Expand Up @@ -191,7 +191,7 @@ namespace amr
, all quantities that are in initialization refiners need to be regridded
*/
void regrid(std::shared_ptr<SAMRAI::hier::PatchHierarchy> const& hierarchy,
const int levelNumber,
int const levelNumber,
std::shared_ptr<SAMRAI::hier::PatchLevel> const& oldLevel,
IPhysicalModel& model, double const initDataTime) override
{
Expand Down Expand Up @@ -345,7 +345,7 @@ namespace amr
auto dataOnPatch = resourcesManager_->setOnPatch(*patch, ions);
for (auto& pop : ions)
{
empty(pop.patchGhostParticles());
pop.patchGhostParticles().clear();
}
}
patchGhostPartRefiners_.fill(level.getLevelNumber(), fillTime);
Expand Down Expand Up @@ -465,44 +465,24 @@ namespace amr
*/
void lastStep(IPhysicalModel& model, SAMRAI::hier::PatchLevel& level) override
{
if (level.getLevelNumber() > 0)
{
PHARE_LOG_SCOPE(3, "HybridHybridMessengerStrategy::lastStep");
if (level.getLevelNumber() == 0)
return;

PHARE_LOG_SCOPE(3, "HybridHybridMessengerStrategy::lastStep");

auto& hybridModel = static_cast<HybridModel&>(model);
for (auto& patch : level)
auto& hybridModel = static_cast<HybridModel&>(model);
for (auto& patch : level)
{
auto& ions = hybridModel.state.ions;
auto dataOnPatch = resourcesManager_->setOnPatch(*patch, ions);
for (auto& pop : ions)
{
auto& ions = hybridModel.state.ions;
auto dataOnPatch = resourcesManager_->setOnPatch(*patch, ions);
for (auto& pop : ions)
{
auto& levelGhostParticlesOld = pop.levelGhostParticlesOld();
auto& levelGhostParticlesNew = pop.levelGhostParticlesNew();
auto& levelGhostParticles = pop.levelGhostParticles();

core::swap(levelGhostParticlesNew, levelGhostParticlesOld);
core::empty(levelGhostParticlesNew);
core::empty(levelGhostParticles);
std::copy(std::begin(levelGhostParticlesOld),
std::end(levelGhostParticlesOld),
std::back_inserter(levelGhostParticles));

if (level.getLevelNumber() == 0)
{
if (levelGhostParticlesNew.size() != 0)
throw std::runtime_error(
"levelGhostParticlesNew detected in root level : "
+ std::to_string(levelGhostParticlesNew.size()));
if (levelGhostParticles.size() != 0)
throw std::runtime_error(
"levelGhostParticles detected in root level : "
+ std::to_string(levelGhostParticles.size()));
if (levelGhostParticlesOld.size() != 0)
throw std::runtime_error(
"levelGhostParticlesOld detected in root level : "
+ std::to_string(levelGhostParticlesOld.size()));
}
}
auto& levelGhostParticlesOld = pop.levelGhostParticlesOld();
auto& levelGhostParticlesNew = pop.levelGhostParticlesNew();
auto& levelGhostParticles = pop.levelGhostParticles();

levelGhostParticlesOld = std::move(levelGhostParticlesNew);
levelGhostParticles = levelGhostParticlesOld;
}
}
}
Expand Down Expand Up @@ -737,9 +717,7 @@ namespace amr
auto& levelGhostParticlesOld = pop.levelGhostParticlesOld();
auto& levelGhostParticles = pop.levelGhostParticles();

core::empty(levelGhostParticles);
std::copy(std::begin(levelGhostParticlesOld), std::end(levelGhostParticlesOld),
std::back_inserter(levelGhostParticles));
levelGhostParticles = levelGhostParticlesOld;
}
}
}
Expand Down Expand Up @@ -959,15 +937,15 @@ namespace amr

// maybe we should keep these for some time
// as comments in case they are useful again
PHARE_DEBUG_DO(const std::string before = "BEFORE";
PHARE_DEBUG_DO(std::string const before = "BEFORE";
debug_print(B, layout, loc, ix, iy, before);)
Bx(ix, iy - 1)
= Bx(ix + 1, iy - 1) + dx / dy * (By(ix, iy) - By(ix, iy - 1));

By(ix - 1, iy)
= By(ix - 1, iy + 1) + dy / dx * (Bx(ix, iy) - Bx(ix - 1, iy));

PHARE_DEBUG_DO(const std::string after = "AFTER";
PHARE_DEBUG_DO(std::string const after = "AFTER";
debug_print(B, layout, loc, ix, iy, after);)
}
} // end corner loops
Expand Down
47 changes: 32 additions & 15 deletions src/core/data/particles/particle_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,33 @@ class ParticleArray
assert(box_.size() > 0);
}

ParticleArray(ParticleArray const& from) = default;
ParticleArray(ParticleArray&& from) = default;
ParticleArray& operator=(ParticleArray&& from) = default;
ParticleArray& operator=(ParticleArray const& from) = default;
ParticleArray(ParticleArray&& from) = default;

ParticleArray(ParticleArray const& from)
: particles_{from.particles_}
, box_{from.box_}
, cellMap_{from.cellMap_}
{
remap_particles();
}

ParticleArray& operator=(ParticleArray const& from)
{
this->particles_ = from.particles_;
this->box_ = from.box_;
this->cellMap_ = from.cellMap_; // copy for alloc, not alloc on pushback
remap_particles();
return *this;
};

ParticleArray& operator=(ParticleArray&& from)
{
this->particles_ = std::move(from.particles_);
this->box_ = from.box_; // not movable
this->cellMap_ = from.cellMap_; // std::move == segfault / bug in cellmap?
from.cellMap_.clear();
return *this;
};

NO_DISCARD std::size_t size() const { return particles_.size(); }
NO_DISCARD std::size_t capacity() const { return particles_.capacity(); }
Expand Down Expand Up @@ -148,6 +171,11 @@ class ParticleArray

void map_particles() const { cellMap_.add(particles_); }
void empty_map() { cellMap_.empty(); }
void remap_particles()
{
empty_map();
map_particles();
}


NO_DISCARD auto nbr_particles_in(box_t const& box) const { return cellMap_.size(box); }
Expand Down Expand Up @@ -262,17 +290,6 @@ namespace PHARE
{
namespace core
{
template<std::size_t dim>
void empty(ParticleArray<dim>& array)
{
array.clear();
}

template<std::size_t dim>
void swap(ParticleArray<dim>& array1, ParticleArray<dim>& array2)
{
array1.swap(array2);
}


template<std::size_t dim, bool OwnedState = true>
Expand Down

0 comments on commit bf61400

Please sign in to comment.