From 775d35161018215ec03dc68c6fb6139bdf6604ab Mon Sep 17 00:00:00 2001 From: ejjordan Date: Fri, 22 Oct 2021 06:13:06 +0200 Subject: [PATCH] Fix nblib pairlist update function Previously the function put_atoms_in_box was called only by the nblib integrator, or when constructing a system via a SimulationState object. In the case of the integrator, this is a needless performance degradation. When using an nb force calculator without first putting atoms in the box, this could lead to a cryptic error from nbnxm grid search failure. Both of these issues are rectified with this change, which also adds a member to the non-bonded force calculator to hold the requested number of OMP threads to use in a call to put_atoms_in_box_omp, which is faster than the non OMP version. --- api/nblib/gmxbackenddata.h | 6 +++++- api/nblib/gmxcalculatorcpu.cpp | 10 ++++++---- api/nblib/gmxcalculatorcpu.h | 2 +- api/nblib/integrator.cpp | 6 +++++- api/nblib/integrator.h | 7 +++++++ 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/api/nblib/gmxbackenddata.h b/api/nblib/gmxbackenddata.h index 6794bb47de..d9528f4e35 100644 --- a/api/nblib/gmxbackenddata.h +++ b/api/nblib/gmxbackenddata.h @@ -77,7 +77,8 @@ public: GmxBackendData(const NBKernelOptions& options, int numEnergyGroups, gmx::ArrayRef exclusionRanges, - gmx::ArrayRef exclusionElements) + gmx::ArrayRef exclusionElements) : + numThreads_(options.numOpenMPThreads) { // Set hardware params from the execution context setGmxNonBondedNThreads(options.numOpenMPThreads); @@ -120,6 +121,9 @@ public: //! Non-bonded flop counter; currently only needed as an argument for dispatchNonbondedKernel t_nrnb nrnb_; + //! Number of OpenMP threads to use + int numThreads_; + //! Keep track of whether updatePairlist has been called at least once bool updatePairlistCalled{ false }; }; diff --git a/api/nblib/gmxcalculatorcpu.cpp b/api/nblib/gmxcalculatorcpu.cpp index a976e5d370..5d8fa76fcb 100644 --- a/api/nblib/gmxcalculatorcpu.cpp +++ b/api/nblib/gmxcalculatorcpu.cpp @@ -75,7 +75,7 @@ public: const NBKernelOptions& options); //! calculates a new pair list based on new coordinates (for every NS step) - void updatePairlist(gmx::ArrayRef coordinates, const Box& box); + void updatePairlist(gmx::ArrayRef coordinates, const Box& box); //! Compute forces and return void compute(gmx::ArrayRef coordinateInput, @@ -120,8 +120,7 @@ GmxNBForceCalculatorCpu::CpuImpl::CpuImpl(gmx::ArrayRef particleTypeIdO system_.nonBondedParams_); } -void GmxNBForceCalculatorCpu::CpuImpl::updatePairlist(gmx::ArrayRef coordinates, - const Box& box) +void GmxNBForceCalculatorCpu::CpuImpl::updatePairlist(gmx::ArrayRef coordinates, const Box& box) { if (coordinates.size() != system_.numParticles_) { @@ -143,6 +142,9 @@ void GmxNBForceCalculatorCpu::CpuImpl::updatePairlist(gmx::ArrayRef(coordinates.size()) / det(legacyBox); + // If particles are too far outside the box, the grid setup can fail + put_atoms_in_box_omp(PbcType::Xyz, box.legacyMatrix(), coordinates, backend_.numThreads_); + // Put particles on a grid based on bounds specified by the box nbnxn_put_on_grid(backend_.nbv_.get(), legacyBox, @@ -288,7 +290,7 @@ GmxNBForceCalculatorCpu::GmxNBForceCalculatorCpu(gmx::ArrayRef particleTyp GmxNBForceCalculatorCpu::~GmxNBForceCalculatorCpu() = default; //! calculates a new pair list based on new coordinates (for every NS step) -void GmxNBForceCalculatorCpu::updatePairlist(gmx::ArrayRef coordinates, const Box& box) +void GmxNBForceCalculatorCpu::updatePairlist(gmx::ArrayRef coordinates, const Box& box) { impl_->updatePairlist(coordinates, box); } diff --git a/api/nblib/gmxcalculatorcpu.h b/api/nblib/gmxcalculatorcpu.h index efc079896e..81783db752 100644 --- a/api/nblib/gmxcalculatorcpu.h +++ b/api/nblib/gmxcalculatorcpu.h @@ -79,7 +79,7 @@ public: ~GmxNBForceCalculatorCpu(); //! calculates a new pair list based on new coordinates (for every NS step) - void updatePairlist(gmx::ArrayRef coordinates, const Box& box); + void updatePairlist(gmx::ArrayRef coordinates, const Box& box); //! Compute forces and return void compute(gmx::ArrayRef coordinateInput, diff --git a/api/nblib/integrator.cpp b/api/nblib/integrator.cpp index a80338d5ff..5168850eea 100644 --- a/api/nblib/integrator.cpp +++ b/api/nblib/integrator.cpp @@ -60,6 +60,11 @@ LeapFrog::LeapFrog(const Topology& topology, const Box& box) : box_(box) } } +LeapFrog::LeapFrog(gmx::ArrayRef inverseMasses, const Box& box) : + inverseMasses_(inverseMasses.begin(), inverseMasses.end()), box_(box) +{ +} + void LeapFrog::integrate(const real dt, gmx::ArrayRef x, gmx::ArrayRef v, gmx::ArrayRef f) { for (size_t i = 0; i < x.size(); i++) @@ -70,7 +75,6 @@ void LeapFrog::integrate(const real dt, gmx::ArrayRef x, gmx::ArrayRef inverseMasses, const Box& box); + /*! \brief Integrate * * Integrates the equation of motion using Leap-Frog algorithm. -- 2.22.0