Cleaning up the GPU bonded kernel
[gromacs.git] / src / gromacs / listed_forces / gpubonded_impl.h
blobf33f6453246ac60bea10dc0c330d39c4e888b6fc
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
35 /*! \internal \file
36 * \brief Declares GPU implementation class for CUDA bonded
37 * interactions.
39 * This header file is needed to include from both the device-side
40 * kernels file, and the host-side management code.
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>
46 * \ingroup module_listed_forces
48 #ifndef GMX_LISTED_FORCES_GPUBONDED_IMPL_H
49 #define GMX_LISTED_FORCES_GPUBONDED_IMPL_H
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"
57 struct gmx_ffparams_t;
58 struct t_forcerec;
60 namespace gmx
63 /*! \internal \brief Version of InteractionList that supports pinning */
64 struct HostInteractionList
66 /*! \brief Returns the total number of elements in iatoms */
67 int size() const
69 return iatoms.size();
72 //! List of interactions, see \c HostInteractionLists
73 HostVector<int> iatoms = {{}, gmx::HostAllocationPolicy(gmx::PinningPolicy::PinnedIfSupported)};
76 /* \brief Bonded parameters and GPU pointers
78 * This is used to accumulate all the parameters and pointers so they can be passed
79 * to the GPU as a single structure.
82 struct BondedCudaKernelParameters
84 //! Periodic boundary data
85 PbcAiuc pbcAiuc;
86 //! Scale factor
87 float scaleFactor;
88 //! The bonded types on GPU
89 int fTypesOnGpu[numFTypesOnGpu];
90 //! The number of interaction atom (iatom) elements for every function type
91 int numFTypeIAtoms[numFTypesOnGpu];
92 //! The number of bonds for every function type
93 int numFTypeBonds[numFTypesOnGpu];
94 //! The start index in the range of each interaction type
95 int fTypeRangeStart[numFTypesOnGpu];
96 //! The end index in the range of each interaction type
97 int fTypeRangeEnd[numFTypesOnGpu];
99 //! Force parameters (on GPU)
100 t_iparams *d_forceParams;
101 //! Coordinates before the timestep (on GPU)
102 const float4 *d_xq;
103 //! Forces on atoms (on GPU)
104 fvec *d_f;
105 //! Force shifts on atoms (on GPU)
106 fvec *d_fShift;
107 //! Total Energy (on GPU)
108 float *d_vTot;
109 //! Interaction list atoms (on GPU)
110 t_iatom *d_iatoms[numFTypesOnGpu];
112 BondedCudaKernelParameters()
114 matrix boxDummy = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };
116 setPbcAiuc(0, boxDummy, &pbcAiuc);
118 scaleFactor = 1.0;
119 d_forceParams = nullptr;
120 d_xq = nullptr;
121 d_f = nullptr;
122 d_fShift = nullptr;
123 d_vTot = nullptr;
127 /*! \internal \brief Implements GPU bondeds */
128 class GpuBonded::Impl
130 public:
131 //! Constructor
132 Impl(const gmx_ffparams_t &ffparams,
133 void *streamPtr);
134 /*! \brief Destructor, non-default needed for freeing
135 * device-side buffers */
136 ~Impl();
137 /*! \brief Update lists of interactions from idef suitable for the GPU,
138 * using the data structures prepared for PP work.
140 * Intended to be called after each neighbour search
141 * stage. Copies the bonded interactions assigned to the GPU
142 * to device data structures, and updates device buffers that
143 * may have been updated after search. */
144 void updateInteractionListsAndDeviceBuffers(ArrayRef<const int> nbnxnAtomOrder,
145 const t_idef &idef,
146 void *xqDevice,
147 void *forceDevice,
148 void *fshiftDevice);
150 /*! \brief Launches bonded kernel on a GPU */
151 template <bool calcVir, bool calcEner>
152 void
153 launchKernel(const t_forcerec *fr,
154 const matrix box);
155 /*! \brief Returns whether there are bonded interactions
156 * assigned to the GPU */
157 bool haveInteractions() const;
158 /*! \brief Launches the transfer of computed bonded energies. */
159 void launchEnergyTransfer();
160 /*! \brief Waits on the energy transfer, and accumulates bonded energies to \c enerd. */
161 void accumulateEnergyTerms(gmx_enerdata_t *enerd);
162 /*! \brief Clears the device side energy buffer */
163 void clearEnergies();
164 private:
165 /*! \brief The interaction lists
167 * \todo This is potentially several pinned allocations, which
168 * could contribute to exhausting such pages. */
169 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 d_iLists_[F_NRE];
175 //! Bonded parameters for device-side use.
176 t_iparams *d_forceParams_ = nullptr;
177 //! Position-charge vector on the device.
178 const float4 *d_xq_ = nullptr;
179 //! Force vector on the device.
180 fvec *d_f_ = nullptr;
181 //! Shift force vector on the device.
182 fvec *d_fShift_ = nullptr;
183 //! \brief Host-side virial buffer
184 HostVector <float> vTot_ = {{}, gmx::HostAllocationPolicy(gmx::PinningPolicy::PinnedIfSupported)};
185 //! \brief Device-side total virial
186 float *d_vTot_ = nullptr;
188 //! \brief Bonded GPU stream, not owned by this module
189 CommandStream stream_;
191 //! Parameters and pointers, passed to the CUDA kernel
192 BondedCudaKernelParameters kernelParams_;
195 } // namespace gmx
197 #endif