Add InteractionDefinitions
[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,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 /*! \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/gputraits.cuh"
52 #include "gromacs/gpu_utils/hostallocator.h"
53 #include "gromacs/listed_forces/gpubonded.h"
54 #include "gromacs/pbcutil/pbc_aiuc.h"
55
56 struct gmx_ffparams_t;
57 struct t_forcerec;
58
59 namespace gmx
60 {
61
62 /*! \internal \brief Version of InteractionList that supports pinning */
63 struct HostInteractionList
64 {
65     /*! \brief Returns the total number of elements in iatoms */
66     int size() const { return iatoms.size(); }
67
68     //! List of interactions, see \c HostInteractionLists
69     HostVector<int> iatoms = { {}, gmx::HostAllocationPolicy(gmx::PinningPolicy::PinnedIfSupported) };
70 };
71
72 /* \brief Bonded parameters and GPU pointers
73  *
74  * This is used to accumulate all the parameters and pointers so they can be passed
75  * to the GPU as a single structure.
76  *
77  */
78 struct BondedCudaKernelParameters
79 {
80     //! Periodic boundary data
81     PbcAiuc pbcAiuc;
82     //! Scale factor
83     float scaleFactor;
84     //! The bonded types on GPU
85     int fTypesOnGpu[numFTypesOnGpu];
86     //! The number of interaction atom (iatom) elements for every function type
87     int numFTypeIAtoms[numFTypesOnGpu];
88     //! The number of bonds for every function type
89     int numFTypeBonds[numFTypesOnGpu];
90     //! The start index in the range of each interaction type
91     int fTypeRangeStart[numFTypesOnGpu];
92     //! The end index in the range of each interaction type
93     int fTypeRangeEnd[numFTypesOnGpu];
94
95     //! Force parameters (on GPU)
96     t_iparams* d_forceParams;
97     //! Coordinates before the timestep (on GPU)
98     const float4* d_xq;
99     //! Forces on atoms (on GPU)
100     float3* d_f;
101     //! Force shifts on atoms (on GPU)
102     float3* d_fShift;
103     //! Total Energy (on GPU)
104     float* d_vTot;
105     //! Interaction list atoms (on GPU)
106     t_iatom* d_iatoms[numFTypesOnGpu];
107
108     BondedCudaKernelParameters()
109     {
110         matrix boxDummy = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
111
112         setPbcAiuc(0, boxDummy, &pbcAiuc);
113
114         scaleFactor   = 1.0;
115         d_forceParams = nullptr;
116         d_xq          = nullptr;
117         d_f           = nullptr;
118         d_fShift      = nullptr;
119         d_vTot        = nullptr;
120     }
121 };
122
123 /*! \internal \brief Implements GPU bondeds */
124 class GpuBonded::Impl
125 {
126 public:
127     //! Constructor
128     Impl(const gmx_ffparams_t& ffparams, void* streamPtr, gmx_wallcycle* wcycle);
129     /*! \brief Destructor, non-default needed for freeing
130      * device-side buffers */
131     ~Impl();
132     /*! \brief Update lists of interactions from idef suitable for the GPU,
133      * using the data structures prepared for PP work.
134      *
135      * Intended to be called after each neighbour search
136      * stage. Copies the bonded interactions assigned to the GPU
137      * to device data structures, and updates device buffers that
138      * may have been updated after search. */
139     void updateInteractionListsAndDeviceBuffers(ArrayRef<const int>           nbnxnAtomOrder,
140                                                 const InteractionDefinitions& idef,
141                                                 void*                         xqDevice,
142                                                 DeviceBuffer<RVec>            forceDevice,
143                                                 DeviceBuffer<RVec>            fshiftDevice);
144
145     /*! \brief Launches bonded kernel on a GPU */
146     template<bool calcVir, bool calcEner>
147     void launchKernel(const t_forcerec* fr, const matrix box);
148     /*! \brief Returns whether there are bonded interactions
149      * assigned to the GPU */
150     bool haveInteractions() const;
151     /*! \brief Launches the transfer of computed bonded energies. */
152     void launchEnergyTransfer();
153     /*! \brief Waits on the energy transfer, and accumulates bonded energies to \c enerd. */
154     void waitAccumulateEnergyTerms(gmx_enerdata_t* enerd);
155     /*! \brief Clears the device side energy buffer */
156     void clearEnergies();
157
158 private:
159     /*! \brief The interaction lists
160      *
161      * \todo This is potentially several pinned allocations, which
162      * could contribute to exhausting such pages. */
163     std::array<HostInteractionList, F_NRE> iLists_;
164
165     //! Tells whether there are any interaction in iLists.
166     bool haveInteractions_;
167     //! Interaction lists on the device.
168     t_ilist d_iLists_[F_NRE] = {};
169     //! Bonded parameters for device-side use.
170     t_iparams* d_forceParams_ = nullptr;
171     //! Position-charge vector on the device.
172     const float4* d_xq_ = nullptr;
173     //! Force vector on the device.
174     float3* d_f_ = nullptr;
175     //! Shift force vector on the device.
176     float3* d_fShift_ = nullptr;
177     //! \brief Host-side virial buffer
178     HostVector<float> vTot_ = { {}, gmx::HostAllocationPolicy(gmx::PinningPolicy::PinnedIfSupported) };
179     //! \brief Device-side total virial
180     float* d_vTot_ = nullptr;
181
182     //! \brief Bonded GPU stream, not owned by this module
183     CommandStream stream_;
184
185     //! Parameters and pointers, passed to the CUDA kernel
186     BondedCudaKernelParameters kernelParams_;
187
188     //! \brief Pointer to wallcycle structure.
189     gmx_wallcycle* wcycle_;
190 };
191
192 } // namespace gmx
193
194 #endif