e3e06e0677b8a09b40188246ed1fa3370cd97a4d
[alexxy/gromacs.git] / src / gromacs / taskassignment / usergpuids.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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 /*! \internal \file
36  * \brief Defines routines for handling user-specified GPU IDs.
37  *
38  * \author Mark Abraham <mark.j.abraham@gmail.com>
39  * \ingroup module_taskassignment
40  */
41 #include "gmxpre.h"
42
43 #include "usergpuids.h"
44
45 #include <cctype>
46
47 #include <algorithm>
48 #include <sstream>
49 #include <string>
50 #include <vector>
51
52 #include "gromacs/gpu_utils/gpu_utils.h"
53 #include "gromacs/hardware/hw_info.h"
54 #include "gromacs/utility/exceptions.h"
55 #include "gromacs/utility/stringutil.h"
56
57 namespace gmx
58 {
59
60 std::vector<int>
61 parseUserGpuIds(const std::string &gpuIdString)
62 {
63     // An optional comma is used to separate GPU IDs assigned to the
64     // same type of task, which will be useful for any nodes that have
65     // more than ten GPUs.
66
67     std::vector<int> digits;
68     auto             foundCommaDelimiters = gpuIdString.find(',') != std::string::npos;
69     if (!foundCommaDelimiters)
70     {
71         for (const auto &c : gpuIdString)
72         {
73             if (std::isdigit(c) == 0)
74             {
75                 GMX_THROW(InvalidInputError(formatString("Invalid character in GPU ID string: \"%c\"\n", c)));
76             }
77             // Convert each character in the token to an integer
78             digits.push_back(c - '0');
79         }
80     }
81     else
82     {
83         if (gpuIdString[0] == ',')
84         {
85             GMX_THROW(InvalidInputError("Invalid use of leading comma in GPU ID string"));
86         }
87         std::istringstream ss(gpuIdString);
88         std::string        token;
89         digits.reserve(gpuIdString.length());
90         token.reserve(gpuIdString.length());
91         while (std::getline(ss, token, ','))
92         {
93             // Convert the whole token to an integer
94             if (token.empty())
95             {
96                 GMX_THROW(InvalidInputError("Invalid use of comma in GPU ID string"));
97             }
98             digits.push_back(std::stoi(token));
99         }
100     }
101     return digits;
102 }
103
104 std::vector<int>
105 makeGpuIds(ArrayRef<const int> compatibleGpus,
106            size_t              numGpuTasks)
107 {
108     std::vector<int> gpuIdsToUse;
109
110     gpuIdsToUse.reserve(numGpuTasks);
111
112     auto currentGpuId = compatibleGpus.begin();
113     for (size_t i = 0; i != numGpuTasks; ++i)
114     {
115         GMX_ASSERT(!compatibleGpus.empty(), "Must have compatible GPUs from which to build a list of GPU IDs to use");
116         gpuIdsToUse.push_back(*currentGpuId);
117         ++currentGpuId;
118         if (currentGpuId == compatibleGpus.end())
119         {
120             // Wrap around and assign tasks again.
121             currentGpuId = compatibleGpus.begin();
122         }
123     }
124     std::sort(gpuIdsToUse.begin(), gpuIdsToUse.end());
125     return gpuIdsToUse;
126 }
127
128 std::string
129 makeGpuIdString(const std::vector<int> &gpuIds,
130                 int                     totalNumberOfTasks)
131 {
132     auto resultGpuIds = makeGpuIds(gpuIds, totalNumberOfTasks);
133     return formatAndJoin(resultGpuIds, ",", StringFormatter("%d"));
134 }
135
136 void checkUserGpuIds(const gmx_gpu_info_t   &gpu_info,
137                      const std::vector<int> &compatibleGpus,
138                      const std::vector<int> &gpuIds)
139 {
140     bool        foundIncompatibleGpuIds = false;
141     std::string message
142         = "Some of the requested GPUs do not exist, behave strangely, or are not compatible:\n";
143
144     for (const auto &gpuId : gpuIds)
145     {
146         if (std::find(compatibleGpus.begin(), compatibleGpus.end(), gpuId) == compatibleGpus.end())
147         {
148             foundIncompatibleGpuIds = true;
149             message                += gmx::formatString("    GPU #%d: %s\n",
150                                                         gpuId,
151                                                         getGpuCompatibilityDescription(gpu_info, gpuId));
152         }
153     }
154     if (foundIncompatibleGpuIds)
155     {
156         GMX_THROW(InconsistentInputError(message));
157     }
158 }
159
160 } // namespace