Improve UpdateGroups testing infra
[alexxy/gromacs.git] / src / gromacs / mdlib / lincs_gpu.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \libinternal \file
36  *
37  * \brief Declares the class for GPU implementation of LINCS.
38  *
39  * \author Artem Zhmurov <zhmurov@gmail.com>
40  *
41  * \ingroup module_mdlib
42  * \inlibraryapi
43  */
44 #ifndef GMX_MDLIB_LINCS_GPU_CUH
45 #define GMX_MDLIB_LINCS_GPU_CUH
46
47 #include <memory>
48
49 #include "gromacs/gpu_utils/devicebuffer_datatype.h"
50 #include "gromacs/gpu_utils/device_context.h"
51 #include "gromacs/gpu_utils/device_stream.h"
52 #include "gromacs/gpu_utils/gputraits.h"
53 #include "gromacs/mdlib/constr.h"
54 #include "gromacs/pbcutil/pbc_aiuc.h"
55
56 class InteractionDefinitions;
57
58 namespace gmx
59 {
60
61 //! A pair of atoms indexes
62 struct AtomPair
63 {
64     //! First atom
65     int i;
66     //! Second atom
67     int j;
68 };
69
70 /* \brief LINCS parameters and GPU pointers
71  *
72  * This is used to accumulate all the parameters and pointers so they can be passed
73  * to the GPU as a single structure.
74  *
75  */
76 struct LincsGpuKernelParameters
77 {
78     //! Periodic boundary data
79     PbcAiuc pbcAiuc;
80     //! Order of expansion when inverting the matrix
81     int expansionOrder;
82     //! Number of iterations used to correct the projection
83     int numIterations;
84     //! 1/mass for all atoms (GPU)
85     DeviceBuffer<float> d_inverseMasses;
86     //! Scaled virial tensor (6 floats: [XX, XY, XZ, YY, YZ, ZZ], GPU)
87     DeviceBuffer<float> d_virialScaled;
88     /*! \brief Total number of threads.
89      *
90      *  This covers all constraints and gaps in the ends of the thread blocks
91      *  that are necessary to avoid inter-block synchronizations.
92      *  Should be a multiple of block size (the last block is filled with dummy to the end).
93      */
94     int numConstraintsThreads;
95     //! List of constrained atoms (GPU memory)
96     DeviceBuffer<AtomPair> d_constraints;
97     //! Equilibrium distances for the constraints (GPU)
98     DeviceBuffer<float> d_constraintsTargetLengths;
99     //! Number of constraints, coupled with the current one (GPU)
100     DeviceBuffer<int> d_coupledConstraintsCounts;
101     //! List of coupled with the current one (GPU)
102     DeviceBuffer<int> d_coupledConstraintsIndices;
103     //! Elements of the coupling matrix.
104     DeviceBuffer<float> d_matrixA;
105     //! Mass factors (GPU)
106     DeviceBuffer<float> d_massFactors;
107 };
108
109 /*! \internal \brief Class with interfaces and data for GPU version of LINCS. */
110 class LincsGpu
111 {
112
113 public:
114     /*! \brief Constructor.
115      *
116      * \param[in] numIterations    Number of iteration for the correction of the projection.
117      * \param[in] expansionOrder   Order of the matrix inversion algorithm.
118      * \param[in] deviceContext    Device context (dummy in CUDA).
119      * \param[in] deviceStream     Device command stream.
120      */
121     LincsGpu(int                  numIterations,
122              int                  expansionOrder,
123              const DeviceContext& deviceContext,
124              const DeviceStream&  deviceStream);
125     /*! \brief Destructor.*/
126     ~LincsGpu();
127
128     /*! \brief Apply LINCS.
129      *
130      * Applies LINCS to coordinates and velocities, stored on GPU.
131      * The results are not automatically copied back to the CPU memory.
132      * Method uses this class data structures which should be updated
133      * when needed using set() method.
134      *
135      * \param[in]     d_x               Coordinates before timestep (in GPU memory)
136      * \param[in,out] d_xp              Coordinates after timestep (in GPU memory). The
137      *                                  resulting constrained coordinates will be saved here.
138      * \param[in]     updateVelocities  If the velocities should be updated.
139      * \param[in,out] d_v               Velocities to update (in GPU memory, can be nullptr
140      *                                  if not updated)
141      * \param[in]     invdt             Reciprocal timestep (to scale Lagrange
142      *                                  multipliers when velocities are updated)
143      * \param[in]     computeVirial     If virial should be updated.
144      * \param[in,out] virialScaled      Scaled virial tensor to be updated.
145      * \param[in]     pbcAiuc           PBC data.
146      */
147     void apply(const DeviceBuffer<Float3> d_x,
148                DeviceBuffer<Float3>       d_xp,
149                const bool                 updateVelocities,
150                DeviceBuffer<Float3>       d_v,
151                const real                 invdt,
152                const bool                 computeVirial,
153                tensor                     virialScaled,
154                const PbcAiuc              pbcAiuc);
155
156     /*! \brief
157      * Update data-structures (e.g. after NB search step).
158      *
159      * Updates the constraints data and copies it to the GPU. Should be
160      * called if the particles were sorted, redistributed between domains, etc.
161      * This version uses common data formats so it can be called from anywhere
162      * in the code. Does not recycle the data preparation routines from the CPU
163      * version. Works only with simple case when all the constraints in idef are
164      * are handled by a single GPU. Triangles are not handled as special case.
165      *
166      * Information about constraints is taken from:
167      *     idef.il[F_CONSTR].iatoms  --- type (T) of constraint and two atom indexes (i1, i2)
168      *     idef.iparams[T].constr.dA --- target length for constraint of type T
169      *
170      * \param[in] idef      Local topology data to get information on constraints from.
171      * \param[in] numAtoms  Number of atoms.
172      * \param[in] invmass   Inverse masses of atoms.
173      */
174     void set(const InteractionDefinitions& idef, int numAtoms, const real* invmass);
175
176     /*! \brief
177      * Returns whether the maximum number of coupled constraints is supported
178      * by the GPU LINCS code.
179      *
180      * \param[in] mtop The molecular topology
181      */
182     static bool isNumCoupledConstraintsSupported(const gmx_mtop_t& mtop);
183
184 private:
185     //! GPU context object
186     const DeviceContext& deviceContext_;
187     //! GPU stream
188     const DeviceStream& deviceStream_;
189
190     //! Parameters and pointers, passed to the GPU kernel
191     LincsGpuKernelParameters kernelParams_;
192
193     //! Scaled virial tensor (6 floats: [XX, XY, XZ, YY, YZ, ZZ])
194     std::vector<float> h_virialScaled_;
195
196     /*! \brief Maximum total number of constraints so far.
197      *
198      * If the new number of constraints is larger then previous maximum, the GPU data arrays are
199      * reallocated.
200      */
201     int numConstraintsThreadsAlloc_;
202
203     /*! \brief Maximum total number of atoms so far.
204      *
205      * If the new number of atoms is larger then previous maximum, the GPU array with masses is
206      * reallocated.
207      */
208     int numAtomsAlloc_;
209 };
210
211 } // namespace gmx
212
213 #endif // GMX_MDLIB_LINCS_GPU_CUH