Update instructions in containers.rst
[gromacs.git] / src / gromacs / hardware / device_management_common.cpp
blob1392020ce02b8660a382a2e75ea548aee14f196b
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
36 /*! \internal \file
37 * \brief Defines the implementations of device management functions that
38 * are common for CPU, CUDA and OpenCL.
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>
47 * \ingroup module_hardware
49 #include "gmxpre.h"
51 #include <algorithm>
53 #include "gromacs/hardware/device_management.h"
54 #include "gromacs/utility/exceptions.h"
55 #include "gromacs/utility/fatalerror.h"
57 #include "device_information.h"
59 bool canPerformDeviceDetection(std::string* errorMessage)
61 return isDeviceDetectionEnabled() && isDeviceDetectionFunctional(errorMessage);
64 bool isDeviceDetectionEnabled()
66 if (c_binarySupportsGpus)
68 return getenv("GMX_DISABLE_GPU_DETECTION") == nullptr;
70 else
72 return false;
76 DeviceVendor getDeviceVendor(const char* vendorName)
78 if (vendorName)
80 if (strstr(vendorName, "NVIDIA"))
82 return DeviceVendor::Nvidia;
84 else if (strstr(vendorName, "AMD") || strstr(vendorName, "Advanced Micro Devices"))
86 return DeviceVendor::Amd;
88 else if (strstr(vendorName, "Intel"))
90 return DeviceVendor::Intel;
93 return DeviceVendor::Unknown;
97 std::vector<std::reference_wrapper<DeviceInformation>>
98 getCompatibleDevices(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList)
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)
105 if (deviceInfo->status == DeviceStatus::Compatible)
107 compatibleDeviceInfoList.emplace_back(*deviceInfo);
110 return compatibleDeviceInfoList;
113 std::vector<int> getCompatibleDeviceIds(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList)
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)
120 if (deviceInfo->status == DeviceStatus::Compatible)
122 compatibleDeviceIds.emplace_back(deviceInfo->id);
125 return compatibleDeviceIds;
128 bool deviceIdIsCompatible(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList,
129 const int deviceId)
131 auto foundIt = std::find_if(deviceInfoList.begin(), deviceInfoList.end(),
132 [deviceId](auto& deviceInfo) { return deviceInfo->id == deviceId; });
133 if (foundIt == deviceInfoList.end())
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())));
139 return (*foundIt)->status == DeviceStatus::Compatible;
142 std::string getDeviceCompatibilityDescription(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList,
143 int deviceId)
145 return (deviceId >= static_cast<int>(deviceInfoList.size())
146 ? c_deviceStateString[DeviceStatus::Nonexistent]
147 : c_deviceStateString[deviceInfoList[deviceId]->status]);
150 void serializeDeviceInformations(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList,
151 gmx::ISerializer* serializer)
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)
159 serializer->doOpaque(reinterpret_cast<char*>(deviceInfo.get()), sizeof(DeviceInformation));
163 std::vector<std::unique_ptr<DeviceInformation>> deserializeDeviceInformations(gmx::ISerializer* serializer)
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++)
172 deviceInfoList[i] = std::make_unique<DeviceInformation>();
173 serializer->doOpaque(reinterpret_cast<char*>(deviceInfoList[i].get()), sizeof(DeviceInformation));
175 return deviceInfoList;