2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2020, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
36 * \brief Defines the SYCL implementations of the device management.
38 * \author Paul Bauer <paul.bauer.q@gmail.com>
39 * \author Erik Lindahl <erik.lindahl@gmail.com>
40 * \author Artem Zhmurov <zhmurov@gmail.com>
41 * \author Andrey Alekseenko <al42and@gmail.com>
43 * \ingroup module_hardware
47 #include "gromacs/gpu_utils/gmxsycl.h"
48 #include "gromacs/hardware/device_management.h"
49 #include "gromacs/utility/fatalerror.h"
50 #include "gromacs/utility/stringutil.h"
52 #include "device_information.h"
55 bool isDeviceDetectionFunctional(std::string* errorMessage)
59 const std::vector<cl::sycl::platform> platforms = cl::sycl::platform::get_platforms();
60 // SYCL should always have the "host" platform, but just in case:
61 if (platforms.empty() && errorMessage != nullptr)
63 errorMessage->assign("No SYCL platforms found.");
65 return !platforms.empty();
67 catch (const std::exception& e)
69 if (errorMessage != nullptr)
72 gmx::formatString("Unable to get the list of SYCL platforms: %s", e.what()));
80 * \brief Checks that device \c deviceInfo is compatible with GROMACS.
82 * For now, only checks that the vendor is Intel and it is a GPU.
84 * \param[in] syclDevice The SYCL device pointer.
85 * \returns The status enumeration value for the checked device:
87 static DeviceStatus isDeviceCompatible(const cl::sycl::device& syclDevice)
89 if (getenv("GMX_GPU_DISABLE_COMPATIBILITY_CHECK") != nullptr)
91 // Assume the device is compatible because checking has been disabled.
92 return DeviceStatus::Compatible;
95 if (syclDevice.is_accelerator()) // FPGAs and FPGA emulators
97 return DeviceStatus::Incompatible;
101 return DeviceStatus::Compatible;
107 * \brief Checks that device \c deviceInfo is sane (ie can run a kernel).
109 * Compiles and runs a dummy kernel to determine whether the given
110 * SYCL device functions properly.
113 * \param[in] syclDevice The device info pointer.
114 * \param[out] errorMessage An error message related to a SYCL error.
115 * \throws std::bad_alloc When out of memory.
116 * \returns Whether the device passed sanity checks
118 static bool isDeviceFunctional(const cl::sycl::device& syclDevice, std::string* errorMessage)
120 static const int numThreads = 8;
121 cl::sycl::queue queue;
124 queue = cl::sycl::queue(syclDevice);
125 cl::sycl::buffer<int, 1> buffer(numThreads);
126 queue.submit([&](cl::sycl::handler& cgh) {
127 auto d_buffer = buffer.get_access<cl::sycl::access::mode::discard_write>(cgh);
128 cgh.parallel_for<class DummyKernel>(numThreads, [=](cl::sycl::id<1> threadId) {
129 d_buffer[threadId] = threadId.get(0);
133 const auto h_Buffer = buffer.get_access<cl::sycl::access::mode::read>();
134 for (int i = 0; i < numThreads; i++)
136 if (h_Buffer[i] != i)
138 if (errorMessage != nullptr)
140 errorMessage->assign("Dummy kernel produced invalid values");
146 catch (const std::exception& e)
148 if (errorMessage != nullptr)
150 errorMessage->assign(gmx::formatString(
151 "Unable to run dummy kernel on device %s: %s",
152 syclDevice.get_info<cl::sycl::info::device::name>().c_str(), e.what()));
161 * \brief Checks that device \c deviceInfo is compatible and functioning.
163 * Checks the given SYCL device for compatibility and runs a dummy kernel on it to determine
164 * whether the device functions properly.
167 * \param[in] deviceId Device number (internal to GROMACS).
168 * \param[in] deviceInfo The device info pointer.
169 * \returns The status of device.
171 static DeviceStatus checkDevice(size_t deviceId, const DeviceInformation& deviceInfo)
174 DeviceStatus supportStatus = isDeviceCompatible(deviceInfo.syclDevice);
175 if (supportStatus != DeviceStatus::Compatible)
177 return supportStatus;
180 std::string errorMessage;
181 if (!isDeviceFunctional(deviceInfo.syclDevice, &errorMessage))
183 gmx_warning("While sanity checking device #%zu, %s", deviceId, errorMessage.c_str());
184 return DeviceStatus::NonFunctional;
187 return DeviceStatus::Compatible;
190 std::vector<std::unique_ptr<DeviceInformation>> findDevices()
192 std::vector<std::unique_ptr<DeviceInformation>> deviceInfos(0);
193 const std::vector<cl::sycl::device> devices = cl::sycl::device::get_devices();
194 deviceInfos.reserve(devices.size());
195 for (const auto& syclDevice : devices)
197 deviceInfos.emplace_back(std::make_unique<DeviceInformation>());
199 size_t i = deviceInfos.size() - 1;
201 deviceInfos[i]->id = i;
202 deviceInfos[i]->syclDevice = syclDevice;
203 deviceInfos[i]->status = checkDevice(i, *deviceInfos[i]);
204 deviceInfos[i]->deviceVendor =
205 getDeviceVendor(syclDevice.get_info<sycl::info::device::vendor>());
210 void setActiveDevice(const DeviceInformation& /*deviceInfo*/) {}
212 void releaseDevice(DeviceInformation* /* deviceInfo */) {}
214 std::string getDeviceInformationString(const DeviceInformation& deviceInfo)
216 bool deviceExists = (deviceInfo.status != DeviceStatus::Nonexistent
217 && deviceInfo.status != DeviceStatus::NonFunctional);
221 return gmx::formatString("#%d: %s, status: %s", deviceInfo.id, "N/A",
222 c_deviceStateString[deviceInfo.status]);
226 return gmx::formatString(
227 "#%d: name: %s, vendor: %s, device version: %s, status: %s", deviceInfo.id,
228 deviceInfo.syclDevice.get_info<cl::sycl::info::device::name>().c_str(),
229 deviceInfo.syclDevice.get_info<cl::sycl::info::device::vendor>().c_str(),
230 deviceInfo.syclDevice.get_info<cl::sycl::info::device::version>().c_str(),
231 c_deviceStateString[deviceInfo.status]);