1a231d2c2cc83289379bed789dd9a0247e1cddfa
[alexxy/gromacs.git] / src / gromacs / listed_forces / gpubonded.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2016,2017,2018 by the GROMACS development team.
5  * Copyright (c) 2019,2020, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 /*! \libinternal \file
37  *
38  * \brief This file contains declarations of high-level functions used
39  * by mdrun to compute energies and forces for listed interactions.
40  *
41  * Clients of libgromacs that want to evaluate listed interactions
42  * should call functions declared here.
43  *
44  * \author Mark Abraham <mark.j.abraham@gmail.com>
45  *
46  * \inlibraryapi
47  * \ingroup module_listed_forces
48  */
49 #ifndef GMX_LISTED_FORCES_GPUBONDED_H
50 #define GMX_LISTED_FORCES_GPUBONDED_H
51
52 #include "gromacs/gpu_utils/devicebuffer_datatype.h"
53 #include "gromacs/math/vectypes.h"
54 #include "gromacs/topology/idef.h"
55 #include "gromacs/utility/arrayref.h"
56 #include "gromacs/utility/classhelpers.h"
57
58 struct gmx_enerdata_t;
59 struct gmx_ffparams_t;
60 struct gmx_mtop_t;
61 struct t_forcerec;
62 struct t_inputrec;
63 struct gmx_wallcycle;
64
65
66 namespace gmx
67 {
68
69 class StepWorkload;
70
71 /*! \brief The number on bonded function types supported on GPUs */
72 static constexpr int numFTypesOnGpu = 8;
73
74 /*! \brief List of all bonded function types supported on GPUs
75  *
76  * \note This list should be in sync with the actual GPU code.
77  * \note Perturbed interactions are not supported on GPUs.
78  * \note The function types in the list are ordered on increasing value.
79  * \note Currently bonded are only supported with CUDA, not with OpenCL.
80  */
81 constexpr std::array<int, numFTypesOnGpu> fTypesOnGpu = { F_BONDS,  F_ANGLES, F_UREY_BRADLEY,
82                                                           F_PDIHS,  F_RBDIHS, F_IDIHS,
83                                                           F_PIDIHS, F_LJ14 };
84
85 /*! \brief Checks whether the GROMACS build allows to compute bonded interactions on a GPU.
86  *
87  * \param[out] error  If non-null, the diagnostic message when bondeds cannot run on a GPU.
88  *
89  * \returns true when this build can run bonded interactions on a GPU, false otherwise.
90  *
91  * \throws std::bad_alloc when out of memory.
92  */
93 bool buildSupportsGpuBondeds(std::string* error);
94
95 /*! \brief Checks whether the input system allows to compute bonded interactions on a GPU.
96  *
97  * \param[in]  ir     Input system.
98  * \param[in]  mtop   Complete system topology to search for supported interactions.
99  * \param[out] error  If non-null, the error message if the input is not supported on GPU.
100  *
101  * \returns true if PME can run on GPU with this input, false otherwise.
102  */
103 bool inputSupportsGpuBondeds(const t_inputrec& ir, const gmx_mtop_t& mtop, std::string* error);
104
105 class GpuBonded
106 {
107 public:
108     //! Construct the manager with constant data and the stream to use.
109     GpuBonded(const gmx_ffparams_t& ffparams, void* streamPtr, gmx_wallcycle* wcycle);
110     //! Destructor
111     ~GpuBonded();
112
113     /*! \brief Update lists of interactions from idef suitable for the GPU,
114      * using the data structures prepared for PP work.
115      *
116      * Intended to be called after each neighbour search
117      * stage. Copies the bonded interactions assigned to the GPU
118      * to device data structures, and updates device buffers that
119      * may have been updated after search. */
120     void updateInteractionListsAndDeviceBuffers(ArrayRef<const int>           nbnxnAtomOrder,
121                                                 const InteractionDefinitions& idef,
122                                                 void*                         xqDevice,
123                                                 DeviceBuffer<RVec>            forceDevice,
124                                                 DeviceBuffer<RVec>            fshiftDevice);
125
126     /*! \brief Returns whether there are bonded interactions
127      * assigned to the GPU */
128     bool haveInteractions() const;
129     /*! \brief Launches bonded kernel on a GPU */
130     void launchKernel(const t_forcerec* fr, const gmx::StepWorkload& stepWork, const matrix box);
131     /*! \brief Launches the transfer of computed bonded energies. */
132     void launchEnergyTransfer();
133     /*! \brief Waits on the energy transfer, and accumulates bonded energies to \c enerd. */
134     void waitAccumulateEnergyTerms(gmx_enerdata_t* enerd);
135     /*! \brief Clears the device side energy buffer */
136     void clearEnergies();
137
138 private:
139     class Impl;
140     PrivateImplPointer<Impl> impl_;
141 };
142
143 } // namespace gmx
144
145 #endif