Skip to content

Commit 9041386

Browse files
committed
[SYCL]: rebased
Signed-off-by: Sergey V Maslov <[email protected]>
1 parent e58ec28 commit 9041386

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -754,11 +754,25 @@ pi_result piextPlatformCreateWithNativeHandle(pi_native_handle NativeHandle,
754754
assert(Platform);
755755

756756
// Create PI platform from the given Level Zero driver handle.
757+
// TODO: get the platform from the platforms' cache.
757758
auto ZeDriver = pi_cast<ze_driver_handle_t>(NativeHandle);
758759
*Platform = new _pi_platform(ZeDriver);
759760
return PI_SUCCESS;
760761
}
761762

763+
// Get the cahched PI device created for the L0 device handle.
764+
// Return NULL if no such PI device found.
765+
pi_device _pi_platform::getDeviceFromNativeHandle(ze_device_handle_t ZeDevice) {
766+
767+
std::lock_guard<std::mutex> Lock(this->PiDevicesCacheMutex);
768+
auto it = std::find_if(PiDevicesCache.begin(), PiDevicesCache.end(),
769+
[&](pi_device &D) { return D->ZeDevice == ZeDevice; });
770+
if (it != PiDevicesCache.end()) {
771+
return *it;
772+
}
773+
return nullptr;
774+
}
775+
762776
pi_result piDevicesGet(pi_platform Platform, pi_device_type DeviceType,
763777
pi_uint32 NumEntries, pi_device *Devices,
764778
pi_uint32 *NumDevices) {
@@ -1396,6 +1410,7 @@ pi_result piextDeviceCreateWithNativeHandle(pi_native_handle NativeHandle,
13961410
assert(Platform);
13971411

13981412
// Create PI device from the given Level Zero device handle.
1413+
// TODO: get the device from the devices' cache.
13991414
auto ZeDevice = pi_cast<ze_device_handle_t>(NativeHandle);
14001415
*Device = new _pi_device(ZeDevice, Platform);
14011416
return (*Device)->initialize();
@@ -1861,6 +1876,9 @@ pi_result piMemImageCreate(pi_context Context, pi_mem_flags Flags,
18611876
// drivers to perform migration as necessary for sharing it across multiple
18621877
// devices in the context.
18631878
//
1879+
// TODO: figure out if we instead need explicit copying for acessing
1880+
// the image from other devices in the context.
1881+
//
18641882
pi_device Device = Context->Devices[0];
18651883
ze_image_handle_t ZeHImage;
18661884
ZE_CALL(zeImageCreate(Context->ZeContext, Device->ZeDevice, &ZeImageDesc,
@@ -3144,6 +3162,9 @@ pi_result piSamplerCreate(pi_context Context,
31443162
// drivers to perform migration as necessary for sharing it across multiple
31453163
// devices in the context.
31463164
//
3165+
// TODO: figure out if we instead need explicit copying for acessing
3166+
// the sampler from other devices in the context.
3167+
//
31473168
pi_device Device = Context->Devices[0];
31483169

31493170
ze_sampler_handle_t ZeSampler;
@@ -4274,28 +4295,20 @@ pi_result piextUSMFree(pi_context Context, void *Ptr) {
42744295
ze_memory_allocation_properties_t ZeMemoryAllocationProperties = {};
42754296

42764297
// Query memory type of the pointer we're freeing to determine the correct
4277-
// way to do it(directly or via the allocator)
4298+
// way to do it(directly or via an allocator)
42784299
ZE_CALL(zeMemGetAllocProperties(
42794300
Context->ZeContext, Ptr, &ZeMemoryAllocationProperties, &ZeDeviceHandle));
42804301

4281-
// TODO: when support for multiple devices is implemented, here
4282-
// we should do the following:
4283-
// - Find pi_device instance corresponding to ZeDeviceHandle we've just got if
4284-
// exist
4285-
// - Use that pi_device to find the right allocator context and free the
4286-
// pointer.
4287-
4288-
// The allocation doesn't belong to any device for which USM allocator is
4289-
// enabled.
4290-
if (Context->Device->ZeDevice != ZeDeviceHandle) {
4291-
return USMFreeImpl(Context, Ptr);
4292-
}
4302+
// All devices in the context are of the same platform.
4303+
auto Platform = Context->Devices[0]->Platform;
4304+
auto Device = Platform->getDeviceFromNativeHandle(ZeDeviceHandle);
4305+
assert(Device);
42934306

42944307
auto DeallocationHelper =
4295-
[Context,
4308+
[Context, Device,
42964309
Ptr](std::unordered_map<pi_device, USMAllocContext> &AllocContextMap) {
42974310
try {
4298-
auto It = AllocContextMap.find(Context->Device);
4311+
auto It = AllocContextMap.find(Device);
42994312
if (It == AllocContextMap.end())
43004313
return PI_INVALID_VALUE;
43014314

@@ -4554,14 +4567,13 @@ pi_result piextUSMGetMemAllocInfo(pi_context Context, const void *Ptr,
45544567
}
45554568
case PI_MEM_ALLOC_DEVICE:
45564569
if (ZeDeviceHandle) {
4557-
auto it = std::find_if(
4558-
Context->Devices.begin(), Context->Devices.end(),
4559-
[&](pi_device &D) { return D->ZeDevice == ZeDeviceHandle; });
4560-
if (it != Context->Devices.end()) {
4561-
ReturnValue(*it);
4562-
}
4570+
// All devices in the context are of the same platform.
4571+
auto Platform = Context->Devices[0]->Platform;
4572+
auto Device = Platform->getDeviceFromNativeHandle(ZeDeviceHandle);
4573+
return Device ? ReturnValue(Device) : PI_INVALID_VALUE;
4574+
} else {
4575+
return PI_INVALID_VALUE;
45634576
}
4564-
return PI_INVALID_VALUE;
45654577
case PI_MEM_ALLOC_BASE_PTR: {
45664578
void *Base;
45674579
ZE_CALL(zeMemGetAddressRange(Context->ZeContext, Ptr, &Base, nullptr));

sycl/plugins/level_zero/pi_level_zero.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ struct _pi_platform {
8080
// Cache pi_devices for reuse
8181
std::vector<pi_device> PiDevicesCache;
8282
std::mutex PiDevicesCacheMutex;
83+
pi_device getDeviceFromNativeHandle(ze_device_handle_t);
84+
8385
// Maximum Number of Command Lists that can be created.
8486
// This Value is initialized to 20000, but can be changed by the user
8587
// thru the environment variable SYCL_PI_LEVEL0_MAX_COMMAND_LIST_CACHE

0 commit comments

Comments
 (0)