Rename all source files from - to _ for consistency.
[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/topology/idef.h"
56
57 struct gmx_ffparams_t;
58 struct t_forcerec;
59
60 namespace gmx
61 {
62
63 /*! \internal \brief Version of InteractionList that supports pinning */
64 struct HostInteractionList
65 {
66     /*! \brief Returns the total number of elements in iatoms */
67     int size() const
68     {
69         return iatoms.size();
70     }
71
72     //! List of interactions, see \c HostInteractionLists
73     HostVector<int> iatoms = {{}, gmx::HostAllocationPolicy(gmx::PinningPolicy::PinnedIfSupported)};
74 };
75
76 /*! \internal \brief Implements GPU bondeds */
77 class GpuBonded::Impl
78 {
79     public:
80         //! Constructor
81         Impl(const gmx_ffparams_t &ffparams,
82              void                 *streamPtr);
83         /*! \brief Destructor, non-default needed for freeing
84          * device-side buffers */
85         ~Impl();
86         /*! \brief Update lists of interactions from idef suitable for the GPU,
87          * using the data structures prepared for PP work.
88          *
89          * Intended to be called after each neighbour search
90          * stage. Copies the bonded interactions assigned to the GPU
91          * to device data structures, and updates device buffers that
92          * may have been updated after search. */
93         void updateInteractionListsAndDeviceBuffers(ArrayRef<const int>  nbnxnAtomOrder,
94                                                     const t_idef        &idef,
95                                                     void                *xqDevice,
96                                                     void                *forceDevice,
97                                                     void                *fshiftDevice);
98
99         /*! \brief Launches bonded kernels on a GPU */
100         template <bool calcVir, bool calcEner>
101         void
102         launchKernels(const t_forcerec *fr,
103                       const matrix      box);
104         /*! \brief Returns whether there are bonded interactions
105          * assigned to the GPU */
106         bool haveInteractions() const;
107         /*! \brief Launches the transfer of computed bonded energies. */
108         void launchEnergyTransfer();
109         /*! \brief Waits on the energy transfer, and accumulates bonded energies to \c enerd. */
110         void accumulateEnergyTerms(gmx_enerdata_t *enerd);
111         /*! \brief Clears the device side energy buffer */
112         void clearEnergies();
113     private:
114         /*! \brief The interaction lists
115          *
116          * \todo This is potentially several pinned allocations, which
117          * could contribute to exhausting such pages. */
118         std::array<HostInteractionList, F_NRE> iLists;
119         //! Tells whether there are any interaction in iLists.
120         bool                                   haveInteractions_;
121         //! Interaction lists on the device.
122         t_ilist                                iListsDevice[F_NRE];
123
124         //! Bonded parameters for device-side use.
125         t_iparams            *forceparamsDevice = nullptr;
126         //! Position-charge vector on the device.
127         const float4         *xqDevice = nullptr;
128         //! Force vector on the device.
129         fvec                 *forceDevice = nullptr;
130         //! Shift force vector on the device.
131         fvec                 *fshiftDevice = nullptr;
132         //! \brief Host-side virial buffer
133         HostVector <float>    vtot = {{}, gmx::HostAllocationPolicy(gmx::PinningPolicy::PinnedIfSupported)};
134         //! \brief Device-side total virial
135         float                *vtotDevice   = nullptr;
136
137         //! \brief Bonded GPU stream, not owned by this module
138         CommandStream         stream;
139 };
140
141 }   // namespace gmx
142
143 #endif