Draft: Fix compilation
[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     if (c_binarySupportsGpus)
64     {
65         return getenv("GMX_DISABLE_GPU_DETECTION") == nullptr;
66     }
67     else
68     {
69         return false;
70     }
71 }
72
73 bool canComputeOnDevice()
74 {
75     bool canComputeOnDevice = false;
76     if (canPerformDeviceDetection(nullptr))
77     {
78         std::vector<std::unique_ptr<DeviceInformation>> devInfos = findDevices();
79         canComputeOnDevice = !getCompatibleDevices(devInfos).empty();
80     }
81     return canComputeOnDevice;
82 }
83
84 std::vector<std::reference_wrapper<DeviceInformation>>
85 getCompatibleDevices(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList)
86 {
87     // Possible minor over-allocation here, but not important for anything
88     std::vector<std::reference_wrapper<DeviceInformation>> compatibleDeviceInfoList;
89     compatibleDeviceInfoList.reserve(deviceInfoList.size());
90     for (const auto& deviceInfo : deviceInfoList)
91     {
92         if (deviceInfo->status == DeviceStatus::Compatible)
93         {
94             compatibleDeviceInfoList.emplace_back(*deviceInfo);
95         }
96     }
97     return compatibleDeviceInfoList;
98 }
99
100 std::string getDeviceCompatibilityDescription(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList,
101                                               int deviceId)
102 {
103     return (deviceId >= static_cast<int>(deviceInfoList.size())
104                     ? c_deviceStateString[DeviceStatus::Nonexistent]
105                     : c_deviceStateString[deviceInfoList[deviceId]->status]);
106 }
107
108 void serializeDeviceInformations(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList,
109                                  gmx::ISerializer*                                      serializer)
110 {
111     GMX_RELEASE_ASSERT(c_canSerializeDeviceInformation,
112                        "DeviceInformation for OpenCL/SYCL can not be serialized");
113     int numDevices = deviceInfoList.size();
114     serializer->doInt(&numDevices);
115     for (auto& deviceInfo : deviceInfoList)
116     {
117         serializer->doOpaque(reinterpret_cast<char*>(deviceInfo.get()), sizeof(DeviceInformation));
118     }
119 }
120
121 std::vector<std::unique_ptr<DeviceInformation>> deserializeDeviceInformations(gmx::ISerializer* serializer)
122 {
123     GMX_RELEASE_ASSERT(c_canSerializeDeviceInformation,
124                        "DeviceInformation for OpenCL/SYCL can not be deserialized");
125     int numDevices = 0;
126     serializer->doInt(&numDevices);
127     std::vector<std::unique_ptr<DeviceInformation>> deviceInfoList(numDevices);
128     for (int i = 0; i < numDevices; i++)
129     {
130         deviceInfoList[i] = std::make_unique<DeviceInformation>();
131         serializer->doOpaque(reinterpret_cast<char*>(deviceInfoList[i].get()), sizeof(DeviceInformation));
132     }
133     return deviceInfoList;
134 }