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.
36 * \brief Utilities to setup GROMACS data structures for non-bonded force calculations.
38 * \author Victor Holanda <victor.holanda@cscs.ch>
39 * \author Joe Jordan <ejjordan@kth.se>
40 * \author Prashanth Kanduri <kanduri@cscs.ch>
41 * \author Sebastian Keller <keller@cscs.ch>
43 #include "gromacs/ewald/ewald_utils.h"
44 #include "gromacs/gpu_utils/device_stream_manager.h"
45 #include "gromacs/gmxlib/nrnb.h"
46 #include "gromacs/mdlib/forcerec.h"
47 #include "gromacs/mdlib/gmx_omp_nthreads.h"
48 #include "gromacs/mdlib/rf_util.h"
49 #include "gromacs/mdtypes/forcerec.h"
50 #include "gromacs/mdtypes/interaction_const.h"
51 #include "gromacs/mdtypes/simulation_workload.h"
52 #include "gromacs/nbnxm/atomdata.h"
53 #include "gromacs/nbnxm/gpu_data_mgmt.h"
54 #include "gromacs/nbnxm/nbnxm_gpu.h"
55 #include "gromacs/nbnxm/nbnxm.h"
56 #include "gromacs/nbnxm/nbnxm_simd.h"
57 #include "gromacs/nbnxm/pairlistset.h"
58 #include "gromacs/nbnxm/pairlistsets.h"
59 #include "gromacs/nbnxm/pairsearch.h"
60 #include "gromacs/pbcutil/pbc.h"
61 #include "gromacs/utility/logger.h"
62 #include "gromacs/utility/smalloc.h"
63 #include "nblib/exception.h"
64 #include "nblib/kerneloptions.h"
65 #include "nblib/particletype.h"
67 #include "nbnxmsetuphelpers.h"
72 int64_t findNumEnergyGroups(gmx::ArrayRef<int64_t> particleInteractionFlags)
74 auto groupId = [](int code1, int code2) {
75 return (code1 & gmx::sc_atomInfo_EnergyGroupIdMask) < (code2 & gmx::sc_atomInfo_EnergyGroupIdMask);
78 int maxElement = *std::max_element(
79 std::begin(particleInteractionFlags), std::end(particleInteractionFlags), groupId);
80 return ((maxElement + 1) & gmx::sc_atomInfo_EnergyGroupIdMask);
83 Nbnxm::KernelType translateBenchmarkEnum(const SimdKernels& kernel)
85 int kernelInt = static_cast<int>(kernel);
86 return static_cast<Nbnxm::KernelType>(kernelInt);
89 void checkKernelSetup(const SimdKernels nbnxmSimd)
91 if (nbnxmSimd >= SimdKernels::Count || nbnxmSimd == SimdKernels::SimdAuto)
93 throw InputException("Need a valid kernel SIMD type");
96 if ((nbnxmSimd != SimdKernels::SimdNo && !GMX_SIMD)
97 #ifndef GMX_NBNXN_SIMD_4XN
98 || nbnxmSimd == SimdKernels::Simd4XM
100 #ifndef GMX_NBNXN_SIMD_2XNN
101 || nbnxmSimd == SimdKernels::Simd2XMM
105 throw InputException("The requested SIMD kernel was not set up at configuration time");
109 Nbnxm::KernelSetup createKernelSetupCPU(const NBKernelOptions& options)
111 checkKernelSetup(options.nbnxmSimd);
113 Nbnxm::KernelSetup kernelSetup;
115 // The int enum options.nbnxnSimd is set up to match Nbnxm::KernelType + 1
116 kernelSetup.kernelType = translateBenchmarkEnum(options.nbnxmSimd);
118 // The plain-C kernel does not support analytical ewald correction
119 if (kernelSetup.kernelType == Nbnxm::KernelType::Cpu4x4_PlainC)
121 kernelSetup.ewaldExclusionType = Nbnxm::EwaldExclusionType::Table;
125 kernelSetup.ewaldExclusionType = options.useTabulatedEwaldCorr
126 ? Nbnxm::EwaldExclusionType::Table
127 : Nbnxm::EwaldExclusionType::Analytical;
133 std::vector<int64_t> createParticleInfoAllVdv(const size_t numParticles)
136 std::vector<int64_t> particleInfoAllVdw(numParticles);
137 for (size_t particleI = 0; particleI < numParticles; particleI++)
139 particleInfoAllVdw[particleI] |= gmx::sc_atomInfo_HasVdw;
140 particleInfoAllVdw[particleI] |= gmx::sc_atomInfo_HasCharge;
142 return particleInfoAllVdw;
145 std::vector<real> createNonBondedParameters(const std::vector<ParticleType>& particleTypes,
146 const NonBondedInteractionMap& nonBondedInteractionMap)
148 /* Todo: Refactor nbnxm to take nonbondedParameters_ directly
150 * initial self-handling of combination rules
151 * size: 2*(numParticleTypes^2)
153 std::vector<real> nonbondedParameters;
154 nonbondedParameters.reserve(2 * particleTypes.size() * particleTypes.size());
156 constexpr real c6factor = 6.0;
157 constexpr real c12factor = 12.0;
159 for (const ParticleType& particleType1 : particleTypes)
161 for (const ParticleType& particleType2 : particleTypes)
163 nonbondedParameters.push_back(
164 nonBondedInteractionMap.getC6(particleType1.name(), particleType2.name()) * c6factor);
165 nonbondedParameters.push_back(
166 nonBondedInteractionMap.getC12(particleType1.name(), particleType2.name()) * c12factor);
169 return nonbondedParameters;
172 gmx::StepWorkload createStepWorkload([[maybe_unused]] const NBKernelOptions& options)
174 gmx::StepWorkload stepWorkload;
175 stepWorkload.computeForces = true;
176 stepWorkload.computeNonbondedForces = true;
177 stepWorkload.useGpuFBufferOps = false;
178 stepWorkload.useGpuXBufferOps = false;
183 real ewaldCoeff(const real ewald_rtol, const real pairlistCutoff)
185 return calc_ewaldcoeff_q(pairlistCutoff, ewald_rtol);
188 interaction_const_t createInteractionConst(const NBKernelOptions& options)
190 interaction_const_t interactionConst;
191 interactionConst.vdwtype = VanDerWaalsType::Cut;
192 interactionConst.vdw_modifier = InteractionModifiers::PotShift;
193 interactionConst.rvdw = options.pairlistCutoff;
195 switch (options.coulombType)
197 case CoulombType::Pme: interactionConst.eeltype = CoulombInteractionType::Pme; break;
198 case CoulombType::Cutoff: interactionConst.eeltype = CoulombInteractionType::Cut; break;
199 case CoulombType::ReactionField:
200 interactionConst.eeltype = CoulombInteractionType::RF;
202 case CoulombType::Count: throw InputException("Unsupported electrostatic interaction");
204 interactionConst.coulomb_modifier = InteractionModifiers::PotShift;
205 interactionConst.rcoulomb = options.pairlistCutoff;
206 // Note: values correspond to ic->coulomb_modifier = eintmodPOTSHIFT
207 interactionConst.dispersion_shift.cpot = -1.0 / gmx::power6(interactionConst.rvdw);
208 interactionConst.repulsion_shift.cpot = -1.0 / gmx::power12(interactionConst.rvdw);
210 // These are the initialized values but we leave them here so that later
211 // these can become options.
212 interactionConst.epsilon_r = 1.0;
213 interactionConst.reactionFieldPermitivity = 1.0;
215 /* Set the Coulomb energy conversion factor */
216 if (interactionConst.epsilon_r != 0)
218 interactionConst.epsfac = ONE_4PI_EPS0 / interactionConst.epsilon_r;
222 /* eps = 0 is infinite dieletric: no Coulomb interactions */
223 interactionConst.epsfac = 0;
227 interactionConst.epsilon_r,
228 interactionConst.reactionFieldPermitivity,
229 interactionConst.rcoulomb,
230 &interactionConst.reactionFieldCoefficient,
231 &interactionConst.reactionFieldShift);
234 if (EEL_PME_EWALD(interactionConst.eeltype))
236 // Ewald coefficients, we ignore the potential shift
237 interactionConst.ewaldcoeff_q = ewaldCoeff(1e-5, options.pairlistCutoff);
238 if (interactionConst.ewaldcoeff_q <= 0)
240 throw InputException("Ewald coefficient should be > 0");
242 interactionConst.coulombEwaldTables = std::make_unique<EwaldCorrectionTables>();
243 init_interaction_const_tables(nullptr, &interactionConst, 0, 0);
245 return interactionConst;
248 std::unique_ptr<nonbonded_verlet_t> createNbnxmCPU(const size_t numParticleTypes,
249 const NBKernelOptions& options,
251 gmx::ArrayRef<const real> nonbondedParameters)
253 const auto pinPolicy = gmx::PinningPolicy::CannotBePinned;
254 const int numThreads = options.numOpenMPThreads;
255 // Note: the options and Nbnxm combination rule enums values should match
256 const int combinationRule = static_cast<int>(options.ljCombinationRule);
258 Nbnxm::KernelSetup kernelSetup = createKernelSetupCPU(options);
260 PairlistParams pairlistParams(kernelSetup.kernelType, false, options.pairlistCutoff, false);
262 auto pairlistSets = std::make_unique<PairlistSets>(pairlistParams, false, 0);
263 auto pairSearch = std::make_unique<PairSearch>(
264 PbcType::Xyz, false, nullptr, nullptr, pairlistParams.pairlistType, false, numThreads, pinPolicy);
266 // Needs to be called with the number of unique ParticleTypes
267 auto atomData = std::make_unique<nbnxn_atomdata_t>(pinPolicy,
269 kernelSetup.kernelType,
276 // Put everything together
277 auto nbv = std::make_unique<nonbonded_verlet_t>(
278 std::move(pairlistSets), std::move(pairSearch), std::move(atomData), kernelSetup, nullptr, nullptr);
283 void setGmxNonBondedNThreads(int numThreads)
285 gmx_omp_nthreads_set(ModuleMultiThread::Pairsearch, numThreads);
286 gmx_omp_nthreads_set(ModuleMultiThread::Nonbonded, numThreads);
289 void updateForcerec(t_forcerec* forcerec, const matrix& box)
291 assert(forcerec != nullptr && "Forcerec not initialized");
292 forcerec->shift_vec.resize(numShiftVectors);
293 calc_shifts(box, forcerec->shift_vec);