Skip to content

[SYCL][L0] make_device shouldn't need platform as an input #4561

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 1 commit into from
Sep 14, 2021
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
2 changes: 1 addition & 1 deletion sycl/include/CL/sycl/detail/pi.h
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ piextDeviceGetNativeHandle(pi_device device, pi_native_handle *nativeHandle);
/// NOTE: The created PI object takes ownership of the native handle.
///
/// \param nativeHandle is the native handle to create PI device from.
/// \param platform is the platform of the device.
/// \param platform is the platform of the device (optional).
/// \param device is the PI device created from the native handle.
__SYCL_EXPORT pi_result piextDeviceCreateWithNativeHandle(
pi_native_handle nativeHandle, pi_platform platform, pi_device *device);
Expand Down
9 changes: 6 additions & 3 deletions sycl/include/sycl/ext/oneapi/backend/level_zero.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,24 @@ template <> struct BackendInput<backend::level_zero, context> {
using type = struct {
interop<backend::level_zero, context>::type NativeHandle;
std::vector<device> DeviceList;
ext::oneapi::level_zero::ownership Ownership;
ext::oneapi::level_zero::ownership Ownership{
ext::oneapi::level_zero::ownership::transfer};
};
};

template <> struct BackendInput<backend::level_zero, queue> {
using type = struct {
interop<backend::level_zero, queue>::type NativeHandle;
ext::oneapi::level_zero::ownership Ownership;
ext::oneapi::level_zero::ownership Ownership{
ext::oneapi::level_zero::ownership::transfer};
};
};

template <> struct BackendInput<backend::level_zero, event> {
using type = struct {
interop<backend::level_zero, event>::type NativeHandle;
ext::oneapi::level_zero::ownership Ownership;
ext::oneapi::level_zero::ownership Ownership{
ext::oneapi::level_zero::ownership::transfer};
};
};

Expand Down
21 changes: 18 additions & 3 deletions sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2528,18 +2528,33 @@ pi_result piextDeviceCreateWithNativeHandle(pi_native_handle NativeHandle,
pi_device *Device) {
PI_ASSERT(Device, PI_INVALID_DEVICE);
PI_ASSERT(NativeHandle, PI_INVALID_VALUE);
PI_ASSERT(Platform, PI_INVALID_PLATFORM);

auto ZeDevice = pi_cast<ze_device_handle_t>(NativeHandle);

// The SYCL spec requires that the set of devices must remain fixed for the
// duration of the application's execution. We assume that we found all of the
// Level Zero devices when we initialized the device cache, so the
// Level Zero devices when we initialized the platforms/devices cache, so the
// "NativeHandle" must already be in the cache. If it is not, this must not be
// a valid Level Zero device.
pi_device Dev = Platform->getDeviceFromNativeHandle(ZeDevice);
//
// TODO: maybe we should populate cache of platforms if it wasn't already.
// For now assert that is was populated.
PI_ASSERT(PiPlatformCachePopulated, PI_INVALID_VALUE);
const std::lock_guard<sycl::detail::SpinLock> Lock{*PiPlatformsCacheMutex};

pi_device Dev = nullptr;
for (auto &ThePlatform : *PiPlatformsCache) {
Dev = ThePlatform->getDeviceFromNativeHandle(ZeDevice);
if (Dev) {
// Check that the input Platform, if was given, matches the found one.
PI_ASSERT(!Platform || Platform == ThePlatform, PI_INVALID_PLATFORM);
break;
}
}

if (Dev == nullptr)
return PI_INVALID_VALUE;

*Device = Dev;
return PI_SUCCESS;
}
Expand Down