Access the device status directly, remove the getter
[gromacs.git] / src / gromacs / hardware / device_management_common.cpp
blobad85eb05a667b73cd1949139ac4e0fad37e2614e
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 "gromacs/hardware/device_management.h"
52 #include "gromacs/utility/fatalerror.h"
54 #include "device_information.h"
56 bool canPerformDeviceDetection(std::string* errorMessage)
58 if (c_binarySupportsGpus && getenv("GMX_DISABLE_GPU_DETECTION") == nullptr)
60 return isDeviceDetectionFunctional(errorMessage);
62 else
64 return false;
68 bool canComputeOnDevice()
70 bool canComputeOnDevice = false;
71 if (canPerformDeviceDetection(nullptr))
73 std::vector<std::unique_ptr<DeviceInformation>> devInfos = findDevices();
74 canComputeOnDevice = !getCompatibleDevices(devInfos).empty();
76 return canComputeOnDevice;
79 std::vector<std::reference_wrapper<DeviceInformation>>
80 getCompatibleDevices(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList)
82 // Possible minor over-allocation here, but not important for anything
83 std::vector<std::reference_wrapper<DeviceInformation>> compatibleDeviceInfoList;
84 compatibleDeviceInfoList.reserve(deviceInfoList.size());
85 for (const auto& deviceInfo : deviceInfoList)
87 if (deviceInfo->status == DeviceStatus::Compatible)
89 compatibleDeviceInfoList.emplace_back(*deviceInfo);
92 return compatibleDeviceInfoList;
95 std::string getDeviceCompatibilityDescription(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList,
96 int deviceId)
98 return (deviceId >= static_cast<int>(deviceInfoList.size())
99 ? c_deviceStateString[DeviceStatus::Nonexistent]
100 : c_deviceStateString[deviceInfoList[deviceId]->status]);
103 void serializeDeviceInformations(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList,
104 gmx::ISerializer* serializer)
106 int numDevices = deviceInfoList.size();
107 serializer->doInt(&numDevices);
108 for (auto& deviceInfo : deviceInfoList)
110 serializer->doOpaque(reinterpret_cast<char*>(deviceInfo.get()), sizeof(DeviceInformation));
114 std::vector<std::unique_ptr<DeviceInformation>> deserializeDeviceInformations(gmx::ISerializer* serializer)
116 int numDevices = 0;
117 serializer->doInt(&numDevices);
118 std::vector<std::unique_ptr<DeviceInformation>> deviceInfoList(numDevices);
119 for (int i = 0; i < numDevices; i++)
121 deviceInfoList[i] = std::make_unique<DeviceInformation>();
122 serializer->doOpaque(reinterpret_cast<char*>(deviceInfoList[i].get()), sizeof(DeviceInformation));
124 return deviceInfoList;