Separate management of GPU contexts from modules
[gromacs.git] / src / gromacs / taskassignment / usergpuids.h
blob2209fe258ae1e9b19b887ed5251146c6307050f2
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2017, 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 /*! \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.
39 /*! \libinternal
40 * \file
41 * \brief Declares routines for handling user-specified GPU IDs.
43 * \author Mark Abraham <mark.j.abraham@gmail.com>
44 * \ingroup module_taskassignment
45 * \inlibraryapi
47 #ifndef GMX_TASKASSIGNMENT_USERGPUIDS_H
48 #define GMX_TASKASSIGNMENT_USERGPUIDS_H
50 #include <cstddef>
52 #include <string>
53 #include <vector>
55 struct gmx_gpu_info_t;
57 namespace gmx
60 /*! \brief Parse a GPU ID string into a container describing the task types and associated device IDs.
62 * \param[in] gpuIdString String like "013" or "0,1,3" typically
63 * supplied by the user to mdrun -gpu_id or -gputasks.
64 * Must contain only decimal digits, or only decimal
65 * digits separated by comma delimiters. A terminal
66 * comma is accceptable (and required to specify a
67 * single ID that is larger than 9).
69 * \returns A vector of GPU ID task mappings, like { 0, 1, 3 }
71 * \throws std::bad_alloc If out of memory.
72 * InvalidInputError If an invalid character is found (ie not a digit or ',').
74 std::vector<int>
75 parseUserGpuIds(const std::string &gpuIdString);
77 /*! \brief Make a vector containing \c numGpuTasks IDs of the IDs found in \c compatibleGpus.
79 * \throws std::bad_alloc If out of memory
81 * \returns A sorted vector of IDs of compatible vectors, whose
82 * length matches that of the number of GPU tasks required.
84 std::vector<int>
85 makeGpuIds(const std::vector<int> &compatibleGpus,
86 size_t numGpuTasks);
88 /*! \brief Convert a container of GPU deviced IDs to a string that
89 * can be used by gmx tune_pme as input to mdrun -gputasks.
91 * Produce a valid input for mdrun -gputasks that refers to the device
92 * IDs in \c gpuIds but produces a mapping for \c
93 * totalNumberOfTasks tasks. Note that gmx tune_pme does not
94 * currently support filling mdrun -gputasks.
96 * \param[in] gpuIds Container of device IDs
97 * \param[in] totalNumberOfTasks Total number of tasks for the output mapping produced by the returned string.
99 * \returns A string that is suitable to pass to mdrun -gputasks.
101 * \throws std::bad_alloc If out of memory.
103 std::string
104 makeGpuIdString(const std::vector<int> &gpuIds, int totalNumberOfTasks);
106 /*! \brief Check that all user-selected GPUs are compatible.
108 * Given the \c gpuIds and \c hardwareInfo, throw if
109 * any selected GPUs is not compatible.
111 * The error is given with a suitable descriptive message, which will
112 * have context if this check is done after the hardware detection
113 * results have been reported to the user. However, note that only the
114 * GPUs detected on the master rank are reported, because of the
115 * existing limitations of that reporting.
117 * \todo Note that the selected GPUs can be different on each rank,
118 * and the IDs of compatible GPUs can be different on each node, so
119 * this routine ought to do communication to determine whether all
120 * ranks are able to proceed. Currently this relies on the MPI runtime
121 * to kill the other processes because GROMACS lacks the appropriate
122 * infrastructure to do a good job of coordinating error messages and
123 * behaviour across MPMD ranks and multiple simulations.
125 * \param[in] gpu_info Information detected about GPUs
126 * \param[in] compatibleGpus Vector of GPUs that are compatible
127 * \param[in] gpuIds The GPU IDs selected by the user.
129 * \throws std::bad_alloc If out of memory
130 * InconsistentInputError If the assigned GPUs are not valid
132 void checkUserGpuIds(const gmx_gpu_info_t &gpu_info,
133 const std::vector<int> &compatibleGpus,
134 const std::vector<int> &gpuIds);
136 } // namespace
138 #endif