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 * \brief Implements Leap-Frog using SYCL
39 * This file contains SYCL implementation of back-end specific code for Leap-Frog.
41 * \author Artem Zhmurov <zhmurov@gmail.com>
42 * \author Andrey Alekseenko <al42and@gmail.com>
44 * \ingroup module_mdlib
48 #include "leapfrog_gpu_internal.h"
50 #include "gromacs/gpu_utils/devicebuffer.h"
51 #include "gromacs/gpu_utils/gmxsycl.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/mdlib/leapfrog_gpu.h"
54 #include "gromacs/mdtypes/group.h"
55 #include "gromacs/utility/arrayref.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/template_mp.h"
59 //! \brief Class name for leap-frog kernel
60 template<gmx::NumTempScaleValues numTempScaleValues, gmx::VelocityScalingType velocityScaling>
66 using cl::sycl::access::mode;
68 /*! \brief Main kernel for the Leap-Frog integrator.
70 * The coordinates and velocities are updated on the GPU. Also saves the intermediate values of the coordinates for
71 * further use in constraints.
73 * Each GPU thread works with a single particle.
75 * \tparam numTempScaleValues The number of different T-couple values.
76 * \tparam velocityScaling Type of the Parrinello-Rahman velocity rescaling.
77 * \param cgh SYCL's command group handler.
78 * \param[in,out] a_x Coordinates to update upon integration.
79 * \param[out] a_xp A copy of the coordinates before the integration (for constraints).
80 * \param[in,out] a_v Velocities to update.
81 * \param[in] a_f Atomic forces.
82 * \param[in] a_inverseMasses Reciprocal masses.
83 * \param[in] dt Timestep.
84 * \param[in] a_lambdas Temperature scaling factors (one per group).
85 * \param[in] a_tempScaleGroups Mapping of atoms into groups.
86 * \param[in] prVelocityScalingMatrixDiagonal Diagonal elements of Parrinello-Rahman velocity scaling matrix.
88 template<NumTempScaleValues numTempScaleValues, VelocityScalingType velocityScaling>
90 cl::sycl::handler& cgh,
91 DeviceAccessor<Float3, mode::read_write> a_x,
92 DeviceAccessor<Float3, mode::discard_write> a_xp,
93 DeviceAccessor<Float3, mode::read_write> a_v,
94 DeviceAccessor<Float3, mode::read> a_f,
95 DeviceAccessor<float, mode::read> a_inverseMasses,
97 OptionalAccessor<float, mode::read, numTempScaleValues != NumTempScaleValues::None> a_lambdas,
98 OptionalAccessor<unsigned short, mode::read, numTempScaleValues == NumTempScaleValues::Multiple> a_tempScaleGroups,
99 Float3 prVelocityScalingMatrixDiagonal)
105 cgh.require(a_inverseMasses);
106 if constexpr (numTempScaleValues != NumTempScaleValues::None)
108 cgh.require(a_lambdas);
110 if constexpr (numTempScaleValues == NumTempScaleValues::Multiple)
112 cgh.require(a_tempScaleGroups);
115 return [=](cl::sycl::id<1> itemIdx) {
116 const Float3 x = a_x[itemIdx];
117 const Float3 v = a_v[itemIdx];
118 const Float3 f = a_f[itemIdx];
119 const float im = a_inverseMasses[itemIdx];
120 const float imdt = im * dt;
122 // Swapping places for xp and x so that the x will contain the updated coordinates and xp -
123 // the coordinates before update. This should be taken into account when (if) constraints
124 // are applied after the update: x and xp have to be passed to constraints in the 'wrong'
125 // order. See Issue #3727
128 const float lambda = [=]() {
129 if constexpr (numTempScaleValues == NumTempScaleValues::None)
133 else if constexpr (numTempScaleValues == NumTempScaleValues::Single)
137 else if constexpr (numTempScaleValues == NumTempScaleValues::Multiple)
139 const int tempScaleGroup = a_tempScaleGroups[itemIdx];
140 return a_lambdas[tempScaleGroup];
144 const Float3 prVelocityDelta = [=]() {
145 if constexpr (velocityScaling == VelocityScalingType::Diagonal)
147 return Float3{ prVelocityScalingMatrixDiagonal[0] * v[0],
148 prVelocityScalingMatrixDiagonal[1] * v[1],
149 prVelocityScalingMatrixDiagonal[2] * v[2] };
151 else if constexpr (velocityScaling == VelocityScalingType::None)
153 return Float3{ 0, 0, 0 };
157 const Float3 v_new = v * lambda - prVelocityDelta + f * imdt;
158 a_v[itemIdx] = v_new;
159 a_x[itemIdx] = x + v_new * dt;
163 //! \brief Leap Frog SYCL kernel launch code.
164 template<NumTempScaleValues numTempScaleValues, VelocityScalingType velocityScaling, class... Args>
165 static cl::sycl::event launchLeapFrogKernel(const DeviceStream& deviceStream, int numAtoms, Args&&... args)
167 // Should not be needed for SYCL2020.
168 using kernelNameType = LeapFrogKernel<numTempScaleValues, velocityScaling>;
170 const cl::sycl::range<1> rangeAllAtoms(numAtoms);
171 cl::sycl::queue q = deviceStream.stream();
173 cl::sycl::event e = q.submit([&](cl::sycl::handler& cgh) {
175 leapFrogKernel<numTempScaleValues, velocityScaling>(cgh, std::forward<Args>(args)...);
176 cgh.parallel_for<kernelNameType>(rangeAllAtoms, kernel);
182 //! Convert \p doTemperatureScaling and \p numTempScaleValues to \ref NumTempScaleValues.
183 static NumTempScaleValues getTempScalingType(bool doTemperatureScaling, int numTempScaleValues)
185 if (!doTemperatureScaling)
187 return NumTempScaleValues::None;
189 else if (numTempScaleValues == 1)
191 return NumTempScaleValues::Single;
193 else if (numTempScaleValues > 1)
195 return NumTempScaleValues::Multiple;
199 gmx_incons("Temperature coupling was requested with no temperature coupling groups.");
203 /*! \brief Select templated kernel and launch it. */
204 template<class... Args>
205 static inline cl::sycl::event launchLeapFrogKernel(NumTempScaleValues tempScalingType,
206 VelocityScalingType prVelocityScalingType,
209 GMX_ASSERT(prVelocityScalingType == VelocityScalingType::None
210 || prVelocityScalingType == VelocityScalingType::Diagonal,
211 "Only isotropic Parrinello-Rahman pressure coupling is supported.");
213 return dispatchTemplatedFunction(
214 [&](auto tempScalingType_, auto prScalingType_) {
215 return launchLeapFrogKernel<tempScalingType_, prScalingType_>(std::forward<Args>(args)...);
218 prVelocityScalingType);
221 void launchLeapFrogKernel(int numAtoms,
222 DeviceBuffer<Float3> d_x,
223 DeviceBuffer<Float3> d_xp,
224 DeviceBuffer<Float3> d_v,
225 const DeviceBuffer<Float3> d_f,
226 const DeviceBuffer<float> d_inverseMasses,
228 const bool doTemperatureScaling,
229 const int numTempScaleValues,
230 const DeviceBuffer<unsigned short> d_tempScaleGroups,
231 const DeviceBuffer<float> d_lambdas,
232 const VelocityScalingType prVelocityScalingType,
233 const Float3 prVelocityScalingMatrixDiagonal,
234 const DeviceStream& deviceStream)
236 NumTempScaleValues tempVelocityScalingType =
237 getTempScalingType(doTemperatureScaling, numTempScaleValues);
240 launchLeapFrogKernel(tempVelocityScalingType,
241 prVelocityScalingType,
252 prVelocityScalingMatrixDiagonal);