diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 1a5c762f31c69..1cd0e76244b73 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -255,8 +255,23 @@ platform_impl::get_devices(info::device_type DeviceType) const { MPlatform, pi::cast(DeviceType), 0, pi::cast(nullptr), &NumDevices); - if (NumDevices == 0) + if (NumDevices == 0) { + // If platform doesn't have devices (even without filter) + // LastDeviceIds[PlatformId] stay 0 that affects next platform devices num + // analysis. Doing adjustment by simple copy of last device num from + // previous platform. + // Needs non const plugin reference. + std::vector &Plugins = RT::initialize(); + auto It = std::find_if(Plugins.begin(), Plugins.end(), + [&Platform = MPlatform](plugin &Plugin) { + return Plugin.containsPiPlatform(Platform); + }); + if (It != Plugins.end()) { + std::lock_guard Guard(*(It->getPluginMutex())); + (*It).adjustLastDeviceId(MPlatform); + } return Res; + } std::vector PiDevices(NumDevices); // TODO catch an exception and put it to list of asynchronous exceptions diff --git a/sycl/source/detail/plugin.hpp b/sycl/source/detail/plugin.hpp index 0a5851175711c..8275bd8fddf41 100644 --- a/sycl/source/detail/plugin.hpp +++ b/sycl/source/detail/plugin.hpp @@ -238,6 +238,16 @@ class plugin { LastDeviceIds[PlatformId] = Id; } + // Adjust the id of the last device for the given platform. + // Involved when there is no device on that platform at all. + // The function is expected to be called in a thread safe manner. + void adjustLastDeviceId(RT::PiPlatform Platform) { + int PlatformId = getPlatformId(Platform); + if (PlatformId > 0 && + LastDeviceIds[PlatformId] < LastDeviceIds[PlatformId - 1]) + LastDeviceIds[PlatformId] = LastDeviceIds[PlatformId - 1]; + } + bool containsPiPlatform(RT::PiPlatform Platform) { auto It = std::find(PiPlatforms.begin(), PiPlatforms.end(), Platform); return It != PiPlatforms.end();