Skip to content

[SYCL] Fix device handling during interop L0 context creation #12138

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
Dec 12, 2023
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
5 changes: 3 additions & 2 deletions sycl/source/backend/level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ __SYCL_EXPORT context make_context(const std::vector<device> &DeviceList,
NativeHandle, DeviceHandles.size(), DeviceHandles.data(), !KeepOwnership,
&PiContext);
// Construct the SYCL context from PI context.
return detail::createSyclObjFromImpl<context>(std::make_shared<context_impl>(
PiContext, detail::defaultAsyncHandler, Plugin, !KeepOwnership));
return detail::createSyclObjFromImpl<context>(
std::make_shared<context_impl>(PiContext, detail::defaultAsyncHandler,
Plugin, DeviceList, !KeepOwnership));
}

//----------------------------------------------------------------------------
Expand Down
51 changes: 28 additions & 23 deletions sycl/source/detail/context_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,36 @@ context_impl::context_impl(const std::vector<sycl::device> Devices,

context_impl::context_impl(sycl::detail::pi::PiContext PiContext,
async_handler AsyncHandler, const PluginPtr &Plugin,
const std::vector<sycl::device> &DeviceList,
bool OwnedByRuntime)
: MOwnedByRuntime(OwnedByRuntime), MAsyncHandler(AsyncHandler), MDevices(),
MContext(PiContext), MPlatform(), MHostContext(false),
MSupportBufferLocationByDevices(NotChecked) {

std::vector<sycl::detail::pi::PiDevice> DeviceIds;
uint32_t DevicesNum = 0;
// TODO catch an exception and put it to list of asynchronous exceptions
Plugin->call<PiApiKind::piContextGetInfo>(
MContext, PI_CONTEXT_INFO_NUM_DEVICES, sizeof(DevicesNum), &DevicesNum,
nullptr);
DeviceIds.resize(DevicesNum);
// TODO catch an exception and put it to list of asynchronous exceptions
Plugin->call<PiApiKind::piContextGetInfo>(
MContext, PI_CONTEXT_INFO_DEVICES,
sizeof(sycl::detail::pi::PiDevice) * DevicesNum, &DeviceIds[0], nullptr);

if (!DeviceIds.empty()) {
std::shared_ptr<detail::platform_impl> Platform =
platform_impl::getPlatformFromPiDevice(DeviceIds[0], Plugin);
for (sycl::detail::pi::PiDevice Dev : DeviceIds) {
MDevices.emplace_back(createSyclObjFromImpl<device>(
Platform->getOrMakeDeviceImpl(Dev, Platform)));
: MOwnedByRuntime(OwnedByRuntime), MAsyncHandler(AsyncHandler),
MDevices(DeviceList), MContext(PiContext), MPlatform(),
MHostContext(false), MSupportBufferLocationByDevices(NotChecked) {
if (!MDevices.empty()) {
MPlatform = detail::getSyclObjImpl(MDevices[0].get_platform());
} else {
std::vector<sycl::detail::pi::PiDevice> DeviceIds;
uint32_t DevicesNum = 0;
// TODO catch an exception and put it to list of asynchronous exceptions
Plugin->call<PiApiKind::piContextGetInfo>(
MContext, PI_CONTEXT_INFO_NUM_DEVICES, sizeof(DevicesNum), &DevicesNum,
nullptr);
DeviceIds.resize(DevicesNum);
// TODO catch an exception and put it to list of asynchronous exceptions
Plugin->call<PiApiKind::piContextGetInfo>(
MContext, PI_CONTEXT_INFO_DEVICES,
sizeof(sycl::detail::pi::PiDevice) * DevicesNum, &DeviceIds[0],
nullptr);

if (!DeviceIds.empty()) {
std::shared_ptr<detail::platform_impl> Platform =
platform_impl::getPlatformFromPiDevice(DeviceIds[0], Plugin);
for (sycl::detail::pi::PiDevice Dev : DeviceIds) {
MDevices.emplace_back(createSyclObjFromImpl<device>(
Platform->getOrMakeDeviceImpl(Dev, Platform)));
}
MPlatform = Platform;
}
MPlatform = Platform;
}
// TODO catch an exception and put it to list of asynchronous exceptions
// getPlugin() will be the same as the Plugin passed. This should be taken
Expand Down
1 change: 1 addition & 0 deletions sycl/source/detail/context_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class context_impl {
/// transferred to runtime
context_impl(sycl::detail::pi::PiContext PiContext,
async_handler AsyncHandler, const PluginPtr &Plugin,
const std::vector<sycl::device> &DeviceList = {},
bool OwnedByRuntime = true);

~context_impl();
Expand Down
60 changes: 60 additions & 0 deletions sycl/test-e2e/Basic/interop/ze_context_device.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// REQUIRES: level_zero, level_zero_dev_kit
// RUN: %{build} -o %t.out %level_zero_options
// RUN: %{run} %t.out

// This test checks that an interop Level Zero device is properly handled during
// interop context construction.
#include <sycl/ext/oneapi/backend/level_zero.hpp>
#include <sycl/sycl.hpp>

#include <level_zero/ze_api.h>

#include <cassert>
#include <iostream>
#include <vector>

int main(int argc, char *argv[]) {
int level0DriverIndex = 0;
int level0DeviceIndex = 0;

zeInit(0);
uint32_t level0NumDrivers = 0;
zeDriverGet(&level0NumDrivers, nullptr);

assert(level0NumDrivers > 0);

std::vector<ze_driver_handle_t> level0Drivers(level0NumDrivers);
zeDriverGet(&level0NumDrivers, level0Drivers.data());

ze_driver_handle_t level0Driver = level0Drivers[level0DriverIndex];
uint32_t level0NumDevices = 0;
zeDeviceGet(level0Driver, &level0NumDevices, nullptr);

assert(level0NumDevices > 0);

std::vector<ze_device_handle_t> level0Devices(level0NumDevices);
zeDeviceGet(level0Driver, &level0NumDevices, level0Devices.data());

ze_device_handle_t level0Device = level0Devices[level0DeviceIndex];
ze_context_handle_t level0Context = nullptr;
ze_context_desc_t level0ContextDesc = {};
level0ContextDesc.stype = ZE_STRUCTURE_TYPE_CONTEXT_DESC;
zeContextCreateEx(level0Driver, &level0ContextDesc, 1, &level0Device,
&level0Context);

sycl::device dev;
sycl::device interopDev =
sycl::make_device<sycl::backend::ext_oneapi_level_zero>(level0Device);
sycl::context interopCtx =
sycl::make_context<sycl::backend::ext_oneapi_level_zero>(
{level0Context,
{interopDev},
sycl::ext::oneapi::level_zero::ownership::keep});

assert(interopCtx.get_devices().size() == 1);
assert(interopCtx.get_devices()[0] == interopDev);
sycl::queue q{interopCtx, interopDev};

zeContextDestroy(level0Context);
return 0;
}