d505833d76124aa2b67a4dabb154c996d3451d90
[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 "gromacs/hardware/device_management.h"
52 #include "gromacs/utility/fatalerror.h"
53
54 #include "device_information.h"
55
56 bool canPerformDeviceDetection(std::string* errorMessage)
57 {
58     return isDeviceDetectionEnabled() && isDeviceDetectionFunctional(errorMessage);
59 }
60
61 bool isDeviceDetectionEnabled()
62 {
63     return c_binarySupportsGpus && getenv("GMX_DISABLE_GPU_DETECTION") == nullptr;
64 }
65
66 bool canComputeOnDevice()
67 {
68     bool canComputeOnDevice = false;
69     if (canPerformDeviceDetection(nullptr))
70     {
71         std::vector<std::unique_ptr<DeviceInformation>> devInfos = findDevices();
72         canComputeOnDevice = !getCompatibleDevices(devInfos).empty();
73     }
74     return canComputeOnDevice;
75 }
76
77 std::vector<std::reference_wrapper<DeviceInformation>>
78 getCompatibleDevices(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList)
79 {
80     // Possible minor over-allocation here, but not important for anything
81     std::vector<std::reference_wrapper<DeviceInformation>> compatibleDeviceInfoList;
82     compatibleDeviceInfoList.reserve(deviceInfoList.size());
83     for (const auto& deviceInfo : deviceInfoList)
84     {
85         if (deviceInfo->status == DeviceStatus::Compatible)
86         {
87             compatibleDeviceInfoList.emplace_back(*deviceInfo);
88         }
89     }
90     return compatibleDeviceInfoList;
91 }
92
93 std::string getDeviceCompatibilityDescription(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList,
94                                               int deviceId)
95 {
96     return (deviceId >= static_cast<int>(deviceInfoList.size())
97                     ? c_deviceStateString[DeviceStatus::Nonexistent]
98                     : c_deviceStateString[deviceInfoList[deviceId]->status]);
99 }
100
101 void serializeDeviceInformations(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList,
102                                  gmx::ISerializer*                                      serializer)
103 {
104     int numDevices = deviceInfoList.size();
105     serializer->doInt(&numDevices);
106     for (auto& deviceInfo : deviceInfoList)
107     {
108         serializer->doOpaque(reinterpret_cast<char*>(deviceInfo.get()), sizeof(DeviceInformation));
109     }
110 }
111
112 std::vector<std::unique_ptr<DeviceInformation>> deserializeDeviceInformations(gmx::ISerializer* serializer)
113 {
114     int numDevices = 0;
115     serializer->doInt(&numDevices);
116     std::vector<std::unique_ptr<DeviceInformation>> deviceInfoList(numDevices);
117     for (int i = 0; i < numDevices; i++)
118     {
119         deviceInfoList[i] = std::make_unique<DeviceInformation>();
120         serializer->doOpaque(reinterpret_cast<char*>(deviceInfoList[i].get()), sizeof(DeviceInformation));
121     }
122     return deviceInfoList;
123 }