Clarify data flow in GPU task assignment
[gromacs.git] / src / gromacs / hardware / hardwareassign.cpp
blobbd9d3fc50e60eb1b52f51e5db10bd4e6e3de4bc8
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,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 #include "gmxpre.h"
37 #include "hardwareassign.h"
39 #include "config.h"
41 #include <cstring>
43 #include <algorithm>
44 #include <string>
45 #include <vector>
47 #include "gromacs/gmxlib/network.h"
48 #include "gromacs/gpu_utils/gpu_utils.h"
49 #include "gromacs/hardware/gpu_hw_info.h"
50 #include "gromacs/mdtypes/commrec.h"
51 #include "gromacs/utility/basenetwork.h"
52 #include "gromacs/utility/exceptions.h"
53 #include "gromacs/utility/fatalerror.h"
54 #include "gromacs/utility/gmxassert.h"
55 #include "gromacs/utility/logger.h"
56 #include "gromacs/utility/smalloc.h"
57 #include "gromacs/utility/stringutil.h"
58 #include "gromacs/utility/sysinfo.h"
60 #define HOSTNAMELEN 80
62 namespace gmx
65 std::vector<int> parseGpuTaskAssignment(const std::string &gpuTaskAssignment)
67 std::vector<int> digits;
68 if (gpuTaskAssignment.empty())
70 return digits;
73 /* Parse a "plain" or comma-separated GPU ID string which contains
74 * a sequence of digits corresponding to GPU IDs; the order will
75 * indicate the assignment of GPU tasks on this node to GPU
76 * device IDs on this node. */
77 try
79 digits = parseDigitsFromString(gpuTaskAssignment);
81 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
83 if (digits.empty())
85 gmx_fatal(FARGS, "Empty GPU ID string encountered.\n"
86 "An empty, delimiter-free, or comma-separated sequence of valid numeric IDs of available GPUs is required.\n");
88 return digits;
91 /*! \brief This function is responsible for the automated mapping the
92 * GPUs to the processes on a single node.
94 * This selects the GPUs we will use. This is an operation local to each physical node.
95 * If we have less MPI ranks than GPUs, we will waste some GPUs.
97 * \param[in] compatibleGpus Vector of GPUs that are compatible
98 * \param[in] nrank Number of PP GPU ranks on the node.
99 * \param[in] rank Index of PP GPU rank on the node.
101 * \returns The assignment of GPU tasks on ranks of this node to GPU devices on this node.
103 static std::vector<int> assign_rank_gpu_ids(const std::vector<int> &compatibleGpus,
104 int nrank, int rank)
106 int numCompatibleGpus = static_cast<int>(compatibleGpus.size());
107 GMX_RELEASE_ASSERT(nrank >= 1,
108 gmx::formatString("Invalid limit (%d) for the number of GPUs (detected %d compatible GPUs)",
109 rank, numCompatibleGpus).c_str());
111 if (numCompatibleGpus == 0)
113 char host[HOSTNAMELEN];
115 gmx_gethostname(host, HOSTNAMELEN);
116 gmx_fatal(FARGS, "A GPU was requested on host %s, but no compatible GPUs were detected. All nodes with PP ranks need to have GPUs. If you intended to use GPU acceleration in a parallel run, you can either avoid using the nodes that don't have GPUs or place PME ranks on these nodes.", host);
119 int nshare;
121 nshare = 1;
122 if (nrank > numCompatibleGpus)
124 if (nrank % numCompatibleGpus == 0)
126 nshare = nrank/numCompatibleGpus;
128 else
130 if (rank == 0)
132 gmx_fatal(FARGS, "The number of MPI ranks (%d) in a physical node is not a multiple of the number of GPUs (%d). Select a different number of MPI ranks or use the -gpu_id option to manually specify the GPU to be used.",
133 nrank, numCompatibleGpus);
136 #if GMX_MPI
137 /* We use a global barrier to prevent ranks from continuing with
138 * an invalid setup.
140 MPI_Barrier(MPI_COMM_WORLD);
141 #endif
145 /* Here we will waste GPUs when nrank < numCompatibleGpus */
146 std::vector<int> taskAssignment;
147 taskAssignment.resize(std::min(numCompatibleGpus*nshare, nrank));
148 for (size_t i = 0; i != taskAssignment.size(); ++i)
150 /* TODO: improve this implementation: either sort GPUs or remove the weakest here */
151 taskAssignment[i] = compatibleGpus[i/nshare];
153 return taskAssignment;
156 /*! \brief Check that all user-selected GPUs are compatible.
158 * Given the \c userGpuTaskAssignment and \c compatibleGPUs, give a fatal
159 * error if any selected GPUs is not compatible
161 * The error is given with a suitable descriptive message, which will
162 * have context if this check is done after the hardware detection
163 * results have been reported to the user. However, note that only the
164 * GPUs detected on the master rank are reported, because of the
165 * existing limitations of that reporting.
167 * \todo Note that the selected GPUs can be different on each rank,
168 * and the IDs of compatible GPUs can be different on each node, so
169 * this routine ought to do communication to determine whether all
170 * ranks are able to proceed. Currently this relies on the MPI runtime
171 * to kill the other processes because GROMACS lacks the appropriate
172 * infrastructure to do a good job of coordinating error messages and
173 * behaviour across MPMD ranks and multiple simulations.
175 * \param[in] gpu_info GPU information including device description.
176 * \param[in] compatibleGpus Vector of compatible GPUs
177 * \param[in] userGpuTaskAssignment The GPU selection from the user.
179 static void exitUnlessUserGpuTaskAssignmentIsValid(const gmx_gpu_info_t &gpu_info,
180 const std::vector<int> &compatibleGpus,
181 const std::vector<int> &userGpuTaskAssignment)
183 int numIncompatibleGpuIds = 0;
184 std::string message
185 = "Some of the requested GPUs do not exist, behave strangely, or are not compatible:\n";
187 for (const auto &gpuId : userGpuTaskAssignment)
189 if (std::find(compatibleGpus.begin(), compatibleGpus.end(), gpuId) == compatibleGpus.end())
191 numIncompatibleGpuIds++;
192 message += gmx::formatString(" GPU #%d: %s\n",
193 gpuId,
194 getGpuCompatibilityDescription(gpu_info, gpuId));
198 if (numIncompatibleGpuIds > 0)
200 gmx_fatal(FARGS, message.c_str());
204 /*! \brief Filter the compatible GPUs
206 * This function filters gpu_info.gpu_dev for compatible GPUs based
207 * on the previously run compatibility tests.
209 * \param[in] gpu_info Information detected about GPUs, including compatibility
210 * \return vector of IDs of GPUs already recorded as compatible */
211 static std::vector<int> getCompatibleGpus(const gmx_gpu_info_t &gpu_info)
213 // Possible minor over-allocation here, but not important for anything
214 std::vector<int> compatibleGpus;
215 compatibleGpus.reserve(gpu_info.n_dev);
216 for (int i = 0; i < gpu_info.n_dev; i++)
218 GMX_ASSERT(gpu_info.gpu_dev, "Invalid gpu_info.gpu_dev");
219 if (isGpuCompatible(gpu_info, i))
221 compatibleGpus.push_back(i);
224 return compatibleGpus;
227 std::vector<int> mapPpRanksToGpus(bool rankCanUseGpu,
228 const t_commrec *cr,
229 const gmx_gpu_info_t &gpu_info,
230 const std::vector<int> &userGpuTaskAssignment)
232 std::vector<int> taskAssignment;
234 if (!rankCanUseGpu)
236 return taskAssignment;
239 auto compatibleGpus = getCompatibleGpus(gpu_info);
240 if (!userGpuTaskAssignment.empty())
242 exitUnlessUserGpuTaskAssignmentIsValid(gpu_info, compatibleGpus, userGpuTaskAssignment);
243 taskAssignment = userGpuTaskAssignment;
245 else
247 taskAssignment = assign_rank_gpu_ids(compatibleGpus, cr->nrank_pp_intranode, cr->rank_pp_intranode);
249 return taskAssignment;
252 } // namespace