|
| 1 | +//==------------------- findplatforms.hpp ----------------------------------==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +bool findPlatformAndDevice(cl_device_type deviceType, |
| 9 | + cl_platform_id &platformOut, cl_device_id &deviceOut, |
| 10 | + cl_int &errorCode) { |
| 11 | + cl_uint numPlatforms; |
| 12 | + bool foundDevice = false; |
| 13 | + |
| 14 | + errorCode = clGetPlatformIDs(0, nullptr, &numPlatforms); |
| 15 | + std::vector<cl_platform_id> platforms(numPlatforms); |
| 16 | + errorCode = clGetPlatformIDs(numPlatforms, platforms.data(), nullptr); |
| 17 | + |
| 18 | + for (auto platform : platforms) { |
| 19 | + if (!foundDevice) { |
| 20 | + cl_uint numDevices = 0; |
| 21 | + errorCode = |
| 22 | + clGetDeviceIDs(platform, deviceType, 0, nullptr, &numDevices); |
| 23 | + std::vector<cl_device_id> devices(numDevices); |
| 24 | + errorCode = clGetDeviceIDs(platform, deviceType, numDevices, |
| 25 | + devices.data(), nullptr); |
| 26 | + if (numDevices) { |
| 27 | + platformOut = platform; |
| 28 | + deviceOut = devices[0]; |
| 29 | + foundDevice = true; |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return foundDevice; |
| 35 | +} |
0 commit comments