0cfc429f4467898a7676aca934b1a98571a550d9
[alexxy/gromacs.git] / src / gromacs / listed_forces / gpubonded_impl.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019, 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 /*! \internal \file
36  * \brief Declares GPU implementation class for CUDA bonded
37  * interactions.
38  *
39  * This header file is needed to include from both the device-side
40  * kernels file, and the host-side management code.
41  *
42  * \author Berk Hess <hess@kth.se>
43  * \author Szilárd Páll <pall.szilard@gmail.com>
44  * \author Mark Abraham <mark.j.abraham@gmail.com>
45  *
46  * \ingroup module_listed_forces
47  */
48 #ifndef GMX_LISTED_FORCES_GPUBONDED_IMPL_H
49 #define GMX_LISTED_FORCES_GPUBONDED_IMPL_H
50
51 #include "gromacs/gpu_utils/gpu_vec.cuh"
52 #include "gromacs/gpu_utils/gputraits.cuh"
53 #include "gromacs/gpu_utils/hostallocator.h"
54 #include "gromacs/listed_forces/gpubonded.h"
55 #include "gromacs/pbcutil/pbc_aiuc.h"
56 #include "gromacs/topology/idef.h"
57
58 struct gmx_ffparams_t;
59 struct t_forcerec;
60
61 namespace gmx
62 {
63
64 /*! \internal \brief Version of InteractionList that supports pinning */
65 struct HostInteractionList
66 {
67     /*! \brief Returns the total number of elements in iatoms */
68     int size() const
69     {
70         return iatoms.size();
71     }
72
73     //! List of interactions, see \c HostInteractionLists
74     HostVector<int> iatoms = {{}, gmx::HostAllocationPolicy(gmx::PinningPolicy::PinnedIfSupported)};
75 };
76
77 /* \brief Bonded parameters and GPU pointers
78  *
79  * This is used to accumulate all the parameters and pointers so they can be passed
80  * to the GPU as a single structure.
81  *
82  */
83 struct BondedCudaKernelParameters
84 {
85     //! Periodic boundary data
86     PbcAiuc             pbcAiuc;
87     //! Scale factor
88     float               scaleFactor;
89     //! The bonded types on GPU
90     int                 ftypesOnGpu[nFtypesOnGpu];
91     //! The number of interaction atom (iatom) elements for every function type
92     int                 nrFTypeIAtoms[nFtypesOnGpu];
93     //! The number of bonds for every function type
94     int                 nrFTypeBonds[nFtypesOnGpu];
95     //! The start index in the range of each interaction type
96     int                 ftypeRangeStart[nFtypesOnGpu];
97     //! The end index in the range of each interaction type
98     int                 ftypeRangeEnd[nFtypesOnGpu];
99
100     //! Force parameters (on GPU)
101     t_iparams          *forceparamsDevice;
102     //! Coordinates before the timestep (on GPU)
103     const float4       *xqDevice;
104     //! Forces on atoms (on GPU)
105     fvec               *forceDevice;
106     //! Force shifts on atoms (on GPU)
107     fvec               *fshiftDevice;
108     //! Total Energy (on GPU)
109     float              *vtotDevice;
110     //! Interaction list atoms (on GPU)
111     t_iatom            *iatoms[nFtypesOnGpu];
112
113     BondedCudaKernelParameters()
114     {
115         matrix boxDummy = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };
116
117         setPbcAiuc(0, boxDummy, &pbcAiuc);
118
119         scaleFactor       = 1.0;
120         forceparamsDevice = nullptr;
121         xqDevice          = nullptr;
122         forceDevice       = nullptr;
123         fshiftDevice      = nullptr;
124         vtotDevice        = nullptr;
125     }
126 };
127
128 /*! \internal \brief Implements GPU bondeds */
129 class GpuBonded::Impl
130 {
131     public:
132         //! Constructor
133         Impl(const gmx_ffparams_t &ffparams,
134              void                 *streamPtr);
135         /*! \brief Destructor, non-default needed for freeing
136          * device-side buffers */
137         ~Impl();
138         /*! \brief Update lists of interactions from idef suitable for the GPU,
139          * using the data structures prepared for PP work.
140          *
141          * Intended to be called after each neighbour search
142          * stage. Copies the bonded interactions assigned to the GPU
143          * to device data structures, and updates device buffers that
144          * may have been updated after search. */
145         void updateInteractionListsAndDeviceBuffers(ArrayRef<const int>  nbnxnAtomOrder,
146                                                     const t_idef        &idef,
147                                                     void                *xqDevice,
148                                                     void                *forceDevice,
149                                                     void                *fshiftDevice);
150
151         /*! \brief Launches bonded kernel on a GPU */
152         template <bool calcVir, bool calcEner>
153         void
154         launchKernel(const t_forcerec *fr,
155                      const matrix      box);
156         /*! \brief Returns whether there are bonded interactions
157          * assigned to the GPU */
158         bool haveInteractions() const;
159         /*! \brief Launches the transfer of computed bonded energies. */
160         void launchEnergyTransfer();
161         /*! \brief Waits on the energy transfer, and accumulates bonded energies to \c enerd. */
162         void accumulateEnergyTerms(gmx_enerdata_t *enerd);
163         /*! \brief Clears the device side energy buffer */
164         void clearEnergies();
165     private:
166         /*! \brief The interaction lists
167          *
168          * \todo This is potentially several pinned allocations, which
169          * could contribute to exhausting such pages. */
170         std::array<HostInteractionList, F_NRE> iLists;
171         //! Tells whether there are any interaction in iLists.
172         bool                                   haveInteractions_;
173         //! Interaction lists on the device.
174         t_ilist                                iListsDevice[F_NRE];
175
176         //! Bonded parameters for device-side use.
177         t_iparams            *forceparamsDevice = nullptr;
178         //! Position-charge vector on the device.
179         const float4         *xqDevice = nullptr;
180         //! Force vector on the device.
181         fvec                 *forceDevice = nullptr;
182         //! Shift force vector on the device.
183         fvec                 *fshiftDevice = nullptr;
184         //! \brief Host-side virial buffer
185         HostVector <float>    vtot = {{}, gmx::HostAllocationPolicy(gmx::PinningPolicy::PinnedIfSupported)};
186         //! \brief Device-side total virial
187         float                *vtotDevice   = nullptr;
188
189         //! \brief Bonded GPU stream, not owned by this module
190         CommandStream         stream;
191
192         //! Parameters and pointers, passed to the CUDA kernel
193         BondedCudaKernelParameters kernelParams_;
194 };
195
196 }   // namespace gmx
197
198 #endif