Skip to content

[SYCL] Fix LastDeviceIds assignment for Platform w/o device #5695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion sycl/source/detail/platform_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,23 @@ platform_impl::get_devices(info::device_type DeviceType) const {
MPlatform, pi::cast<RT::PiDeviceType>(DeviceType), 0,
pi::cast<RT::PiDevice *>(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<plugin> &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<std::mutex> Guard(*(It->getPluginMutex()));
(*It).adjustLastDeviceId(MPlatform);
}
return Res;
}

std::vector<RT::PiDevice> PiDevices(NumDevices);
// TODO catch an exception and put it to list of asynchronous exceptions
Expand Down
10 changes: 10 additions & 0 deletions sycl/source/detail/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down