Task assignment for bonded interactions on CUDA GPUs
[alexxy/gromacs.git] / src / gromacs / taskassignment / taskassignment.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2017,2018, 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 /*! \defgroup module_taskassignment Assigning simulation tasks to hardware (taskassignment)
36  * \ingroup group_mdrun
37  * \brief Provides code that manages assignment of simulation tasks to hardware.
38  */
39 /*! \libinternal
40  * \file
41  * \brief Declares high-level functionality for managing assigning
42  * tasks on ranks of a node to hardware on that node, and the factory
43  * function to build the correct flavours of gmx::INodeTaskAssigner
44  * required to implement the user's requirements.
45  *
46  * \author Mark Abraham <mark.j.abraham@gmail.com>
47  * \ingroup module_taskassignment
48  * \inlibraryapi
49  */
50 #ifndef GMX_TASKASSIGNMENT_TASKASSIGNMENT_H
51 #define GMX_TASKASSIGNMENT_TASKASSIGNMENT_H
52
53 #include <vector>
54
55 struct gmx_hw_info_t;
56 struct gmx_multisim_t;
57 struct t_commrec;
58
59 enum class PmeRunMode;
60
61 namespace gmx
62 {
63
64 class MDLogger;
65 class PhysicalNodeCommunicator;
66
67 /*! \brief Types of compute tasks that can be run on a GPU.
68  *
69  * These names refer to existing practice in GROMACS, which is not
70  * strictly accurate. */
71 enum class GpuTask : int
72 {
73     //! Short-ranged interactions.
74     Nonbonded,
75     //! Long-ranged interactions.
76     Pme
77 };
78
79 /*! \libinternal
80  * \brief Specifies the GPU deviceID_ available for task_ to use. */
81 struct GpuTaskMapping
82 {
83     //! The type of this GPU task.
84     GpuTask task_;
85     //! Device ID on this node to which this GPU task is mapped.
86     int     deviceId_;
87 };
88
89 //! Container of GPU tasks on a rank, specifying the task type and GPU device ID, e.g. potentially ready for consumption by the modules on that rank.
90 using GpuTaskAssignment = std::vector <GpuTaskMapping>;
91 //! Container of compute tasks suitable to run on a GPU e.g. on each rank of a node.
92 using GpuTasksOnRanks = std::vector< std::vector<GpuTask> >;
93 //! Container of RankGpuTaskAssignments e.g. for all ranks on a node.
94 using GpuTaskAssignments = std::vector<GpuTaskAssignment>;
95
96 /*! \brief Coordinate the final stages of task assignment and
97  * reporting, and return the assignment for this rank.
98  *
99  * Communicates between ranks on a node to coordinate task assignment
100  * between them onto available hardware, e.g. accelerators.
101  *
102  * Releases the taskAssigner once its work is complete.
103  *
104  * \param[in]  gpuIdsToUse                The compatible GPUs that the user permitted us to use.
105  * \param[in]  userGpuTaskAssignment      The user-specified assignment of GPU tasks to device IDs.
106  * \param[in]  hardwareInfo               The detected hardware
107  * \param[in]  mdlog                      Logging object to write to.
108  * \param[in]  cr                         Communication object.
109  * \param[in]  ms                         Multi-simulation handler.
110  * \param[in]  physicalNodeComm           Communication object for this physical node.
111  * \param[in]  gpuTasksOnThisRank         Information about what GPU tasks
112  *                                        exist on this rank.
113  * \param[in]  useGpuForBonded            Whether GPU PP tasks will do bonded work on the GPU
114  * \param[in]  pmeRunMode                 Describes the execution of PME tasks
115  *
116  * \returns  A GPU task assignment for this rank.
117  *
118  * \throws   std::bad_alloc          If out of memory.
119  *           InconsistentInputError  If user and/or detected inputs are inconsistent.
120  */
121 GpuTaskAssignments::value_type
122 runTaskAssignment(const std::vector<int>         &gpuIdsToUse,
123                   const std::vector<int>         &userGpuTaskAssignment,
124                   const gmx_hw_info_t            &hardwareInfo,
125                   const MDLogger                 &mdlog,
126                   const t_commrec                *cr,
127                   const gmx_multisim_t           *ms,
128                   const PhysicalNodeCommunicator &physicalNodeComm,
129                   const std::vector<GpuTask>     &gpuTasksOnThisRank,
130                   bool                            useGpuForBonded,
131                   PmeRunMode                      pmeRunMode);
132
133 //! Function for whether the task of \c mapping has value \c TaskType.
134 template<GpuTask TaskType>
135 bool hasTaskType(const GpuTaskMapping &mapping)
136 {
137     return mapping.task_ == TaskType;
138 }
139
140 }  // namespace gmx
141
142 #endif