2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019,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 CUDA
39 * This file contains backend-agnostic code for Leap-Frog integrator class on GPU,
40 * including class initialization, and data-structures management.
42 * \author Artem Zhmurov <zhmurov@gmail.com>
44 * \ingroup module_mdlib
48 #include "leapfrog_gpu.h"
56 #include "gromacs/gpu_utils/devicebuffer.h"
57 #include "gromacs/math/vec.h"
58 #include "gromacs/mdlib/leapfrog_gpu_internal.h"
59 #include "gromacs/mdtypes/group.h"
60 #include "gromacs/pbcutil/pbc.h"
61 #include "gromacs/utility/arrayref.h"
66 void LeapFrogGpu::integrate(DeviceBuffer<Float3> d_x,
67 DeviceBuffer<Float3> d_xp,
68 DeviceBuffer<Float3> d_v,
69 const DeviceBuffer<Float3> d_f,
71 const bool doTemperatureScaling,
72 gmx::ArrayRef<const t_grp_tcstat> tcstat,
73 const bool doParrinelloRahman,
74 const float dtPressureCouple,
75 const matrix prVelocityScalingMatrix)
78 if (doTemperatureScaling)
80 GMX_ASSERT(checkDeviceBuffer(d_lambdas_, numTempScaleValues_),
81 "Number of temperature scaling factors changed since it was set for the "
83 GMX_ASSERT(numTempScaleValues_ == ssize(h_lambdas_),
84 "Number of temperature scaling factors changed since it was set for the "
87 /* In SYCL, we could use host accessors here, without h_lambdas_.
88 * According to a quick test, host accessor is slightly faster when using DPC++ and
89 * LevelZero compared to using h_lambdas_ + cgh.copy. But with DPC++ and OpenCL, the host
90 * accessor waits for fReadyOnDevice in UpdateConstrainGpu::Impl::integrate. See #4023. */
91 for (int i = 0; i < numTempScaleValues_; i++)
93 h_lambdas_[i] = tcstat[i].lambda;
95 copyToDeviceBuffer(&d_lambdas_,
100 GpuApiCallBehavior::Async,
103 VelocityScalingType prVelocityScalingType = VelocityScalingType::None;
104 if (doParrinelloRahman)
106 prVelocityScalingType = VelocityScalingType::Diagonal;
107 GMX_ASSERT(prVelocityScalingMatrix[YY][XX] == 0 && prVelocityScalingMatrix[ZZ][XX] == 0
108 && prVelocityScalingMatrix[ZZ][YY] == 0 && prVelocityScalingMatrix[XX][YY] == 0
109 && prVelocityScalingMatrix[XX][ZZ] == 0 && prVelocityScalingMatrix[YY][ZZ] == 0,
110 "Fully anisotropic Parrinello-Rahman pressure coupling is not yet supported "
111 "in GPU version of Leap-Frog integrator.");
112 prVelocityScalingMatrixDiagonal_ = Float3{ dtPressureCouple * prVelocityScalingMatrix[XX][XX],
113 dtPressureCouple * prVelocityScalingMatrix[YY][YY],
114 dtPressureCouple * prVelocityScalingMatrix[ZZ][ZZ] };
117 launchLeapFrogKernel(numAtoms_,
124 doTemperatureScaling,
128 prVelocityScalingType,
129 prVelocityScalingMatrixDiagonal_,
133 LeapFrogGpu::LeapFrogGpu(const DeviceContext& deviceContext,
134 const DeviceStream& deviceStream,
135 const int numTempScaleValues) :
136 deviceContext_(deviceContext), deviceStream_(deviceStream), numTempScaleValues_(numTempScaleValues)
140 changePinningPolicy(&h_lambdas_, gmx::PinningPolicy::PinnedIfSupported);
142 // If the temperature coupling is enabled, we need to make space for scaling factors
143 if (numTempScaleValues_ > 0)
145 h_lambdas_.resize(numTempScaleValues_);
146 reallocateDeviceBuffer(
147 &d_lambdas_, numTempScaleValues_, &numLambdas_, &numLambdasAlloc_, deviceContext_);
151 LeapFrogGpu::~LeapFrogGpu()
153 freeDeviceBuffer(&d_inverseMasses_);
156 void LeapFrogGpu::set(const int numAtoms, const real* inverseMasses, const unsigned short* tempScaleGroups)
158 numAtoms_ = numAtoms;
160 reallocateDeviceBuffer(
161 &d_inverseMasses_, numAtoms_, &numInverseMasses_, &numInverseMassesAlloc_, deviceContext_);
163 &d_inverseMasses_, inverseMasses, 0, numAtoms_, deviceStream_, GpuApiCallBehavior::Sync, nullptr);
165 // Temperature scale group map only used if there are more than one group
166 if (numTempScaleValues_ > 1)
168 reallocateDeviceBuffer(
169 &d_tempScaleGroups_, numAtoms_, &numTempScaleGroups_, &numTempScaleGroupsAlloc_, deviceContext_);
171 &d_tempScaleGroups_, tempScaleGroups, 0, numAtoms_, deviceStream_, GpuApiCallBehavior::Sync, nullptr);