Skip to content

Commit

Permalink
[SYCL][L0] make_device shouldn't need platform as an input (#4561)
Browse files Browse the repository at this point in the history
Signed-off-by: Sergey V Maslov <[email protected]>
  • Loading branch information
smaslov-intel authored Sep 14, 2021
1 parent 094ca47 commit e662166
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
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

0 comments on commit e662166

Please sign in to comment.