Skip to content

Commit

Permalink
Improvements to maxParticleVelocity (ECP-WarpX#5169)
Browse files Browse the repository at this point in the history
* Improve maxParticleVelocity

* gpu fix 1

* gpu fix 2

* gpu fix 3 - change to general reduction

* Remove seperate OMP bit

* Add const qualifier for clang tidy

* Refactor maxVelocity function

---------

Co-authored-by: Remi Lehe <remi.lehe@normalesup.org>
  • Loading branch information
archermarx and RemiLehe authored Sep 10, 2024
1 parent 10d1615 commit ab19438
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Source/Particles/MultiParticleContainer.H
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public:
return allcontainers[index]->meanParticleVelocity();
}

amrex::ParticleReal maxParticleVelocity();

void AllocData ();

void InitData ();
Expand Down
9 changes: 9 additions & 0 deletions Source/Particles/MultiParticleContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,15 @@ MultiParticleContainer::GetParticleContainerFromName (const std::string& name) c
return *allcontainers[i];
}

amrex::ParticleReal
MultiParticleContainer::maxParticleVelocity() {
amrex::ParticleReal max_v = 0.0_prt;
for (const auto &pc : allcontainers) {
max_v = std::max(max_v, pc->maxParticleVelocity());
}
return max_v;
}

void
MultiParticleContainer::AllocData ()
{
Expand Down
27 changes: 17 additions & 10 deletions Source/Particles/WarpXParticleContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1425,26 +1425,33 @@ std::array<ParticleReal, 3> WarpXParticleContainer::meanParticleVelocity(bool lo

amrex::ParticleReal WarpXParticleContainer::maxParticleVelocity(bool local) {

amrex::ParticleReal max_v = 0.0;
const amrex::ParticleReal inv_clight_sq = 1.0_prt/(PhysConst::c*PhysConst::c);
ReduceOps<ReduceOpMax> reduce_op;
ReduceData<ParticleReal> reduce_data(reduce_op);

const int nLevels = finestLevel();
for (int lev = 0; lev <= nLevels; ++lev)
{

#ifdef AMREX_USE_OMP
#pragma omp parallel reduction(max:max_v)
#pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
#endif
for (int lev = 0; lev <= nLevels; ++lev) {
for (WarpXParIter pti(*this, lev); pti.isValid(); ++pti)
{
auto& ux = pti.GetAttribs(PIdx::ux);
auto& uy = pti.GetAttribs(PIdx::uy);
auto& uz = pti.GetAttribs(PIdx::uz);
for (unsigned long i = 0; i < ux.size(); i++) {
max_v = std::max(max_v, std::sqrt(ux[i]*ux[i] + uy[i]*uy[i] + uz[i]*uz[i]));
}
auto *const ux = pti.GetAttribs(PIdx::ux).data();
auto *const uy = pti.GetAttribs(PIdx::uy).data();
auto *const uz = pti.GetAttribs(PIdx::uz).data();

reduce_op.eval(pti.numParticles(), reduce_data,
[=] AMREX_GPU_DEVICE (int ip)
{ return (ux[ip]*ux[ip] + uy[ip]*uy[ip] + uz[ip]*uz[ip]) * inv_clight_sq; });
}
}

const amrex::ParticleReal max_usq = amrex::get<0>(reduce_data.value());

const amrex::ParticleReal gaminv = 1.0_prt/std::sqrt(1.0_prt + max_usq);
amrex::ParticleReal max_v = gaminv * std::sqrt(max_usq) * PhysConst::c;

if (!local) { ParallelAllReduce::Max(max_v, ParallelDescriptor::Communicator()); }
return max_v;
}
Expand Down

0 comments on commit ab19438

Please sign in to comment.