e85d15bf18d6d446956ffaff249af399b6e00016
[alexxy/gromacs.git] / src / gromacs / taskassignment / usergpuids.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2017,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 /*! \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 routines for handling user-specified GPU IDs.
42  *
43  * \author Mark Abraham <mark.j.abraham@gmail.com>
44  * \ingroup module_taskassignment
45  * \inlibraryapi
46  */
47 #ifndef GMX_TASKASSIGNMENT_USERGPUIDS_H
48 #define GMX_TASKASSIGNMENT_USERGPUIDS_H
49
50 #include <cstddef>
51
52 #include <memory>
53 #include <string>
54 #include <vector>
55
56 #include "gromacs/utility/arrayref.h"
57
58 struct DeviceInformation;
59
60 namespace gmx
61 {
62
63 /*! \brief Parse a GPU ID specifier string into a container describing device IDs exposed to the run.
64  *
65  * \param[in]   gpuIdString  String like "013" or "0,1,3" typically
66  *                           supplied by the user to mdrun -gpu_id.
67  *                           Must contain only unique decimal digits, or only decimal
68  *                           digits separated by comma delimiters. A terminal
69  *                           comma is accceptable (and required to specify a
70  *                           single ID that is larger than 9).
71  *
72  * \returns  A vector of unique GPU IDs.
73  *
74  * \throws   std::bad_alloc     If out of memory.
75  *           InvalidInputError  If an invalid character is found (ie not a digit or ',') or if
76  *                              identifiers are duplicated in the specifier list.
77  */
78 std::vector<int> parseUserGpuIdString(const std::string& gpuIdString);
79
80 /*! \brief Implement GPU ID selection by returning the available GPU
81  * IDs on this physical node that are compatible.
82  *
83  * If the string supplied by the user is empty, then return the IDs of
84  * all compatible GPUs on this physical node. Otherwise, check the
85  * user specified compatible GPUs and return their IDs.
86  *
87  * \param[in]  deviceInfoList         Information on the GPUs on this physical node.
88  * \param[in]  gpuIdsAvailableString  String like "013" or "0,1,3" typically
89  *                                    supplied by the user to mdrun -gpu_id.
90  *                                    Must contain only unique decimal digits, or only decimal
91  *                                    digits separated by comma delimiters. A terminal
92  *                                    comma is accceptable (and required to specify a
93  *                                    single ID that is larger than 9).
94  *
95  * \returns  A vector of unique compatible GPU IDs on this physical node.
96  *
97  * \throws   std::bad_alloc     If out of memory.
98  *           InvalidInputError  If an invalid character is found (ie not a digit or ',') or if
99  *                              identifiers are duplicated in the specifier list.
100  *           InvalidInputError  If gpuIdsAvailableString specifies GPU IDs that are
101  *                              not compatible.
102  */
103 std::vector<int> makeGpuIdsToUse(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList,
104                                  const std::string& gpuIdsAvailableString);
105
106 /*! \brief Parse a GPU ID specifier string into a container describing device ID to task mapping.
107  *
108  * \param[in]   gpuIdString  String like "0011" or "0,0,1,1" typically
109  *                           supplied by the user to mdrun -gputasks.
110  *                           Must contain only decimal digits, or only decimal
111  *                           digits separated by comma delimiters. A terminal
112  *                           comma is accceptable (and required to specify a
113  *                           single ID that is larger than 9).
114  *
115  * \returns  A vector of GPU IDs.
116  *
117  * \throws   std::bad_alloc     If out of memory.
118  *           InvalidInputError  If an invalid character is found (ie not a digit or ',').
119  */
120 std::vector<int> parseUserTaskAssignmentString(const std::string& gpuIdString);
121
122
123 /*! \brief Make a vector containing \c numGpuTasks IDs of the IDs found in \c compatibleGpus.
124  *
125  * \throws  std::bad_alloc          If out of memory
126  *
127  * \returns A sorted vector of IDs of compatible vectors, whose
128  * length matches that of the number of GPU tasks required.
129  */
130 std::vector<int> makeGpuIds(ArrayRef<const int> compatibleGpus, size_t numGpuTasks);
131
132 /*! \brief Convert a container of GPU deviced IDs to a string that
133  * can be used by gmx tune_pme as input to mdrun -gputasks.
134  *
135  * Produce a valid input for mdrun -gputasks that refers to the device
136  * IDs in \c gpuIds but produces a mapping for \c
137  * totalNumberOfTasks tasks. Note that gmx tune_pme does not
138  * currently support filling mdrun -gputasks.
139  *
140  * \param[in]   gpuIds              Container of device IDs
141  * \param[in]   totalNumberOfTasks  Total number of tasks for the output mapping produced by the returned string.
142  *
143  * \returns  A string that is suitable to pass to mdrun -gputasks.
144  *
145  * \throws   std::bad_alloc     If out of memory.
146  */
147 std::string makeGpuIdString(const std::vector<int>& gpuIds, int totalNumberOfTasks);
148
149 /*! \brief Check that all user-selected GPUs are compatible.
150  *
151  * Given the \c gpuIds and \c hardwareInfo, throw if
152  * any selected GPUs is not compatible.
153  *
154  * The error is given with a suitable descriptive message, which will
155  * have context if this check is done after the hardware detection
156  * results have been reported to the user. However, note that only the
157  * GPUs detected on the master rank are reported, because of the
158  * existing limitations of that reporting.
159  *
160  * \todo Note that the selected GPUs can be different on each rank,
161  * and the IDs of compatible GPUs can be different on each node, so
162  * this routine ought to do communication to determine whether all
163  * ranks are able to proceed. Currently this relies on the MPI runtime
164  * to kill the other processes because GROMACS lacks the appropriate
165  * infrastructure to do a good job of coordinating error messages and
166  * behaviour across MPMD ranks and multiple simulations.
167  *
168  * \param[in]   deviceInfoList  Information on the GPUs on this physical node.
169  * \param[in]   compatibleGpus  Vector of GPUs that are compatible
170  * \param[in]   gpuIds          The GPU IDs selected by the user.
171  *
172  * \throws  std::bad_alloc          If out of memory
173  *          InconsistentInputError  If the assigned GPUs are not valid
174  */
175 void checkUserGpuIds(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList,
176                      const std::vector<int>&                                compatibleGpus,
177                      const std::vector<int>&                                gpuIds);
178
179 } // namespace gmx
180
181 #endif