ba6d1be3f209437744959e4fbe18de8ddc4bb649
[alexxy/gromacs.git] / src / gromacs / mdlib / leapfrog_gpu.cuh
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019,2020, 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 Declarations for GPU implementation of Leap-Frog.
38  *
39  * \author Artem Zhmurov <zhmurov@gmail.com>
40  *
41  * \ingroup module_mdlib
42  * \inlibraryapi
43  */
44 #ifndef GMX_MDLIB_LEAPFROG_GPU_CUH
45 #define GMX_MDLIB_LEAPFROG_GPU_CUH
46
47 #include "gromacs/gpu_utils/gputraits.cuh"
48 #include "gromacs/gpu_utils/hostallocator.h"
49 #include "gromacs/mdtypes/group.h"
50 #include "gromacs/mdtypes/mdatom.h"
51 #include "gromacs/pbcutil/pbc.h"
52 #include "gromacs/pbcutil/pbc_aiuc.h"
53 #include "gromacs/utility/arrayref.h"
54 #include "gromacs/utility/classhelpers.h"
55
56 namespace gmx
57 {
58
59 class LeapFrogGpu
60 {
61
62 public:
63     /*! \brief Constructor.
64      *
65      * \param[in] commandStream  Device command stream to use.
66      */
67     LeapFrogGpu(CommandStream commandStream);
68     ~LeapFrogGpu();
69
70     /*! \brief Integrate
71      *
72      * Integrates the equation of motion using Leap-Frog algorithm.
73      * Updates coordinates and velocities on the GPU. The current coordinates are saved for constraints.
74      *
75      * \param[in,out] d_x                      Coordinates to update
76      * \param[out]    d_xp                     Place to save the values of initial coordinates coordinates to.
77      * \param[in,out] d_v                      Velocities (will be updated).
78      * \param[in]     d_f                      Forces.
79      * \param[in]     dt                       Timestep.
80      * \param[in]     doTemperatureScaling     If velocities should be scaled for temperature coupling.
81      * \param[in]     tcstat                   Temperature coupling data.
82      * \param[in]     doParrinelloRahman       If current step is a Parrinello-Rahman pressure coupling step.
83      * \param[in]     dtPressureCouple         Period between pressure coupling steps
84      * \param[in]     prVelocityScalingMatrix  Parrinello-Rahman velocity scaling matrix
85      */
86     void integrate(const float3*                     d_x,
87                    float3*                           d_xp,
88                    float3*                           d_v,
89                    const float3*                     d_f,
90                    const real                        dt,
91                    const bool                        doTemperatureScaling,
92                    gmx::ArrayRef<const t_grp_tcstat> tcstat,
93                    const bool                        doParrinelloRahman,
94                    const float                       dtPressureCouple,
95                    const matrix                      prVelocityScalingMatrix);
96
97     /*! \brief Set the integrator
98      *
99      * Allocates memory for inverse masses, and, if needed for temperature scaling factor(s)
100      * and temperature coupling groups. Copies inverse masses and temperature coupling groups
101      * to the GPU.
102      *
103      * \param[in] md                  MD atoms, from which inverse masses are taken.
104      * \param[in] numTempScaleValues  Number of temperature scale groups.
105      * \param[in] tempScaleGroups     Maps the atom index to temperature scale value.
106      */
107     void set(const t_mdatoms& md, int numTempScaleValues, const unsigned short* tempScaleGroups);
108
109     /*! \brief Class with hardware-specific interfaces and implementations.*/
110     class Impl;
111
112 private:
113     //! GPU stream
114     CommandStream commandStream_;
115     //! GPU kernel launch config
116     KernelLaunchConfig kernelLaunchConfig_;
117     //! Number of atoms
118     int numAtoms_;
119
120     //! 1/mass for all atoms (GPU)
121     real* d_inverseMasses_;
122     //! Current size of the reciprocal masses array
123     int numInverseMasses_ = -1;
124     //! Maximum size of the reciprocal masses array
125     int numInverseMassesAlloc_ = -1;
126
127     //! Number of temperature coupling groups (zero = no coupling)
128     int numTempScaleValues_ = 0;
129     /*! \brief Array with temperature scaling factors.
130      * This is temporary solution to remap data from t_grp_tcstat into plain array
131      * \todo Replace with better solution.
132      */
133     gmx::HostVector<float> h_lambdas_;
134     //! Device-side temperature scaling factors
135     float* d_lambdas_;
136     //! Current size of the array with temperature scaling factors (lambdas)
137     int numLambdas_ = -1;
138     //! Maximum size of the array with temperature scaling factors (lambdas)
139     int numLambdasAlloc_ = -1;
140
141
142     //! Array that maps atom index onto the temperature scaling group to get scaling parameter
143     unsigned short* d_tempScaleGroups_;
144     //! Current size of the temperature coupling groups array
145     int numTempScaleGroups_ = -1;
146     //! Maximum size of the temperature coupling groups array
147     int numTempScaleGroupsAlloc_ = -1;
148
149     //! Vector with diagonal elements of the Parrinello-Rahman pressure coupling velocity rescale factors
150     float3 prVelocityScalingMatrixDiagonal_;
151 };
152
153 } // namespace gmx
154
155 #endif