2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2020,2021, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements a bonded force calculator
39 * \author Victor Holanda <victor.holanda@cscs.ch>
40 * \author Joe Jordan <ejjordan@kth.se>
41 * \author Prashanth Kanduri <kanduri@cscs.ch>
42 * \author Sebastian Keller <keller@cscs.ch>
43 * \author Artem Zhmurov <zhmurov@gmail.com>
47 #include "nblib/box.h"
48 #include "nblib/exception.h"
49 #include "nblib/pbc.hpp"
50 #include "nblib/listed_forces/calculator.h"
51 #include "nblib/listed_forces/dataflow.hpp"
52 #include "nblib/listed_forces/helpers.hpp"
57 ListedForceCalculator::~ListedForceCalculator() = default;
59 ListedForceCalculator::ListedForceCalculator(const ListedInteractionData& interactions,
64 threadedForceBuffers_(numThreads),
65 threadedShiftForceBuffers_(numThreads),
66 pbcHolder_(std::make_unique<PbcHolder>(PbcType::Xyz, box))
69 threadedInteractions_ = splitListedWork(interactions, bufferSize, numThreads);
71 // set up the reduction buffers
72 int threadRange = (bufferSize + numThreads - 1) / numThreads;
73 #pragma omp parallel for num_threads(numThreads) schedule(static)
74 for (int i = 0; i < numThreads; ++i)
76 int rangeStart = i * threadRange;
77 int rangeEnd = rangeStart + threadRange;
78 // the last range is a bit bigger due to integer division
79 if (i == numThreads - 1)
81 rangeEnd = bufferSize;
84 threadedForceBuffers_[i] = ForceBufferProxy<Vec3>(rangeStart, rangeEnd);
85 threadedShiftForceBuffers_[i] = std::vector<Vec3>(gmx::c_numShiftVectors);
89 template<class ShiftForce>
90 void ListedForceCalculator::computeForcesAndEnergies(gmx::ArrayRef<const Vec3> x,
91 gmx::ArrayRef<Vec3> forces,
92 [[maybe_unused]] gmx::ArrayRef<ShiftForce> shiftForces,
95 if (x.size() != forces.size())
97 throw InputException("Coordinates array and force buffer size mismatch");
100 energyBuffer_.fill(0);
101 std::vector<std::array<real, std::tuple_size<ListedInteractionData>::value>> energiesPerThread(numThreads);
103 constexpr bool haveShiftForces = !std::is_same_v<ShiftForce, std::nullptr_t>;
104 if constexpr (haveShiftForces)
106 if (shiftForces.size() != gmx::c_numShiftVectors)
108 throw InputException("Shift vectors array size mismatch");
112 #pragma omp parallel for num_threads(numThreads) schedule(static)
113 for (int thread = 0; thread < numThreads; ++thread)
115 std::conditional_t<haveShiftForces, gmx::ArrayRef<Vec3>, gmx::ArrayRef<std::nullptr_t>> shiftForceBuffer;
116 if constexpr (haveShiftForces)
118 shiftForceBuffer = gmx::ArrayRef<Vec3>(threadedShiftForceBuffers_[thread]);
119 std::fill(shiftForceBuffer.begin(), shiftForceBuffer.end(), Vec3{ 0, 0, 0 });
122 ForceBufferProxy<Vec3>* threadBuffer = &threadedForceBuffers_[thread];
124 // forces in range of this thread are directly written into the output buffer
125 threadBuffer->setMasterBuffer(forces);
127 // zero out the outliers in the thread buffer
128 threadBuffer->clearOutliers();
132 energiesPerThread[thread] = reduceListedForces(
133 threadedInteractions_[thread], x, threadBuffer, shiftForceBuffer, *pbcHolder_);
137 energiesPerThread[thread] = reduceListedForces(
138 threadedInteractions_[thread], x, threadBuffer, shiftForceBuffer, NoPbc{});
142 // reduce shift forces
143 // This is a potential candidate for OMP parallelization, but attention should be paid to the
144 // relative costs of thread synchronization overhead vs reduction cost in contexts where the
145 // number of threads could be large vs where number of threads could be small
146 if constexpr (haveShiftForces)
148 for (int i = 0; i < gmx::c_numShiftVectors; ++i)
150 Vec3 threadSum{ 0, 0, 0 };
151 for (int thread = 0; thread < numThreads; ++thread)
153 threadSum += (threadedShiftForceBuffers_[thread])[i];
155 shiftForces[i] += threadSum;
160 for (int thread = 0; thread < numThreads; ++thread)
162 for (int type = 0; type < int(energyBuffer_.size()); ++type)
164 energyBuffer_[type] += energiesPerThread[thread][type];
168 #pragma omp parallel for num_threads(numThreads) schedule(static)
169 for (int thread = 0; thread < numThreads; ++thread)
171 auto& thisBuffer = threadedForceBuffers_[thread];
172 // access outliers from other threads
173 for (int otherThread = 0; otherThread < numThreads; ++otherThread)
175 auto& otherBuffer = threadedForceBuffers_[otherThread];
176 for (const auto& outlier : otherBuffer)
178 int index = outlier.first;
179 // check whether this outlier falls within the range of <thread>
180 if (thisBuffer.inRange(index))
182 auto force = outlier.second;
183 forces[index] += force;
190 void ListedForceCalculator::compute(gmx::ArrayRef<const Vec3> coordinates,
191 gmx::ArrayRef<Vec3> forces,
192 gmx::ArrayRef<Vec3> shiftForces,
193 gmx::ArrayRef<real> energies,
196 computeForcesAndEnergies(coordinates, forces, shiftForces, usePbc);
197 if (!energies.empty())
199 std::copy(energyBuffer_.begin(), energyBuffer_.end(), energies.begin());
203 void ListedForceCalculator::compute(gmx::ArrayRef<const Vec3> coordinates,
204 gmx::ArrayRef<Vec3> forces,
205 gmx::ArrayRef<real> energies,
208 computeForcesAndEnergies(coordinates, forces, gmx::ArrayRef<std::nullptr_t>{}, usePbc);
209 if (!energies.empty())
211 std::copy(energyBuffer_.begin(), energyBuffer_.end(), energies.begin());