Skip to content

Commit e5cb418

Browse files
committed
Ban fpga devices in OpenCL adapter
1 parent 467f2cf commit e5cb418

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

unified-runtime/source/adapters/opencl/platform.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ static bool isBannedOpenCLPlatform(cl_platform_id platform) {
5959
return isBanned;
6060
}
6161

62+
static bool isBannedOpenCLDevice(cl_device_id device) {
63+
cl_device_type deviceType = 0;
64+
cl_int res = clGetDeviceInfo(device, CL_DEVICE_TYPE, sizeof(cl_device_type),
65+
&deviceType, nullptr);
66+
if (res != CL_SUCCESS) {
67+
return false;
68+
}
69+
70+
// Filter out FPGA accelerator devices as their usage with OpenCL adapter is deprecated
71+
bool isBanned = (deviceType & CL_DEVICE_TYPE_ACCELERATOR) != 0;
72+
73+
return isBanned;
74+
}
75+
6276
UR_DLLEXPORT ur_result_t UR_APICALL
6377
urPlatformGetInfo(ur_platform_handle_t hPlatform, ur_platform_info_t propName,
6478
size_t propSize, void *pPropValue, size_t *pSizeRet) {
@@ -143,9 +157,12 @@ urPlatformGet(ur_adapter_handle_t, uint32_t NumEntries,
143157
for (auto &Platform : FilteredPlatforms) {
144158
auto URPlatform = std::make_unique<ur_platform_handle_t_>(Platform);
145159
UR_RETURN_ON_FAILURE(URPlatform->InitDevices());
146-
Adapter->URPlatforms.emplace_back(URPlatform.release());
160+
// Only add platforms that have devices, especially in case all devices are banned
161+
if (!URPlatform->Devices.empty()) {
162+
Adapter->URPlatforms.emplace_back(URPlatform.release());
163+
}
147164
}
148-
Adapter->NumPlatforms = static_cast<uint32_t>(FilteredPlatforms.size());
165+
Adapter->NumPlatforms = static_cast<uint32_t>(Adapter->URPlatforms.size());
149166
} catch (std::bad_alloc &) {
150167
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
151168
} catch (...) {
@@ -253,11 +270,19 @@ ur_result_t ur_platform_handle_t_::InitDevices() {
253270

254271
CL_RETURN_ON_FAILURE(Res);
255272

273+
// Filter out banned devices
274+
std::vector<cl_device_id> FilteredDevices;
275+
for (uint32_t i = 0; i < DeviceNum; i++) {
276+
if (!isBannedOpenCLDevice(CLDevices[i])) {
277+
FilteredDevices.push_back(CLDevices[i]);
278+
}
279+
}
280+
256281
try {
257-
Devices.resize(DeviceNum);
258-
for (size_t i = 0; i < DeviceNum; i++) {
282+
Devices.resize(FilteredDevices.size());
283+
for (size_t i = 0; i < FilteredDevices.size(); i++) {
259284
Devices[i] =
260-
std::make_unique<ur_device_handle_t_>(CLDevices[i], this, nullptr);
285+
std::make_unique<ur_device_handle_t_>(FilteredDevices[i], this, nullptr);
261286
}
262287
} catch (std::bad_alloc &) {
263288
return UR_RESULT_ERROR_OUT_OF_RESOURCES;

0 commit comments

Comments
 (0)