1392020ce02b8660a382a2e75ea548aee14f196b
[alexxy/gromacs.git] / src / gromacs / hardware / device_management_common.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2016, by the GROMACS development team.
5  * Copyright (c) 2017,2018,2019,2020, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 /*! \internal \file
37  *  \brief Defines the implementations of device management functions that
38  *         are common for CPU, CUDA and OpenCL.
39  *
40  *  \author Anca Hamuraru <anca@streamcomputing.eu>
41  *  \author Dimitrios Karkoulis <dimitris.karkoulis@gmail.com>
42  *  \author Teemu Virolainen <teemu@streamcomputing.eu>
43  *  \author Mark Abraham <mark.j.abraham@gmail.com>
44  *  \author Szilárd Páll <pall.szilard@gmail.com>
45  *  \author Artem Zhmurov <zhmurov@gmail.com>
46  *
47  * \ingroup module_hardware
48  */
49 #include "gmxpre.h"
50
51 #include <algorithm>
52
53 #include "gromacs/hardware/device_management.h"
54 #include "gromacs/utility/exceptions.h"
55 #include "gromacs/utility/fatalerror.h"
56
57 #include "device_information.h"
58
59 bool canPerformDeviceDetection(std::string* errorMessage)
60 {
61     return isDeviceDetectionEnabled() && isDeviceDetectionFunctional(errorMessage);
62 }
63
64 bool isDeviceDetectionEnabled()
65 {
66     if (c_binarySupportsGpus)
67     {
68         return getenv("GMX_DISABLE_GPU_DETECTION") == nullptr;
69     }
70     else
71     {
72         return false;
73     }
74 }
75
76 DeviceVendor getDeviceVendor(const char* vendorName)
77 {
78     if (vendorName)
79     {
80         if (strstr(vendorName, "NVIDIA"))
81         {
82             return DeviceVendor::Nvidia;
83         }
84         else if (strstr(vendorName, "AMD") || strstr(vendorName, "Advanced Micro Devices"))
85         {
86             return DeviceVendor::Amd;
87         }
88         else if (strstr(vendorName, "Intel"))
89         {
90             return DeviceVendor::Intel;
91         }
92     }
93     return DeviceVendor::Unknown;
94 }
95
96
97 std::vector<std::reference_wrapper<DeviceInformation>>
98 getCompatibleDevices(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList)
99 {
100     // Possible minor over-allocation here, but not important for anything
101     std::vector<std::reference_wrapper<DeviceInformation>> compatibleDeviceInfoList;
102     compatibleDeviceInfoList.reserve(deviceInfoList.size());
103     for (const auto& deviceInfo : deviceInfoList)
104     {
105         if (deviceInfo->status == DeviceStatus::Compatible)
106         {
107             compatibleDeviceInfoList.emplace_back(*deviceInfo);
108         }
109     }
110     return compatibleDeviceInfoList;
111 }
112
113 std::vector<int> getCompatibleDeviceIds(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList)
114 {
115     // Possible minor over-allocation here, but not important for anything
116     std::vector<int> compatibleDeviceIds;
117     compatibleDeviceIds.reserve(deviceInfoList.size());
118     for (const auto& deviceInfo : deviceInfoList)
119     {
120         if (deviceInfo->status == DeviceStatus::Compatible)
121         {
122             compatibleDeviceIds.emplace_back(deviceInfo->id);
123         }
124     }
125     return compatibleDeviceIds;
126 }
127
128 bool deviceIdIsCompatible(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList,
129                           const int                                              deviceId)
130 {
131     auto foundIt = std::find_if(deviceInfoList.begin(), deviceInfoList.end(),
132                                 [deviceId](auto& deviceInfo) { return deviceInfo->id == deviceId; });
133     if (foundIt == deviceInfoList.end())
134     {
135         GMX_THROW(gmx::RangeError(gmx::formatString(
136                 "Device ID %d did not correspond to any of the %zu detected device(s)", deviceId,
137                 deviceInfoList.size())));
138     }
139     return (*foundIt)->status == DeviceStatus::Compatible;
140 }
141
142 std::string getDeviceCompatibilityDescription(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList,
143                                               int deviceId)
144 {
145     return (deviceId >= static_cast<int>(deviceInfoList.size())
146                     ? c_deviceStateString[DeviceStatus::Nonexistent]
147                     : c_deviceStateString[deviceInfoList[deviceId]->status]);
148 }
149
150 void serializeDeviceInformations(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList,
151                                  gmx::ISerializer*                                      serializer)
152 {
153     GMX_RELEASE_ASSERT(c_canSerializeDeviceInformation,
154                        "DeviceInformation for OpenCL/SYCL can not be serialized");
155     int numDevices = deviceInfoList.size();
156     serializer->doInt(&numDevices);
157     for (auto& deviceInfo : deviceInfoList)
158     {
159         serializer->doOpaque(reinterpret_cast<char*>(deviceInfo.get()), sizeof(DeviceInformation));
160     }
161 }
162
163 std::vector<std::unique_ptr<DeviceInformation>> deserializeDeviceInformations(gmx::ISerializer* serializer)
164 {
165     GMX_RELEASE_ASSERT(c_canSerializeDeviceInformation,
166                        "DeviceInformation for OpenCL/SYCL can not be deserialized");
167     int numDevices = 0;
168     serializer->doInt(&numDevices);
169     std::vector<std::unique_ptr<DeviceInformation>> deviceInfoList(numDevices);
170     for (int i = 0; i < numDevices; i++)
171     {
172         deviceInfoList[i] = std::make_unique<DeviceInformation>();
173         serializer->doOpaque(reinterpret_cast<char*>(deviceInfoList[i].get()), sizeof(DeviceInformation));
174     }
175     return deviceInfoList;
176 }