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