Skip to content

Commit c894462

Browse files
committed
merge/update for buffer-interop
Signed-off-by: Sergey V Maslov <[email protected]>
1 parent 456220c commit c894462

File tree

2 files changed

+117
-54
lines changed

2 files changed

+117
-54
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3017,7 +3017,7 @@ pi_result ContextReleaseHelper(pi_context Context) {
30173017

30183018
if (--(Context->RefCount) == 0) {
30193019
if (IndirectAccessTrackingEnabled) {
3020-
pi_platform Plt = Context->Devices[0]->Platform;
3020+
pi_platform Plt = Context->getPlatform();
30213021
auto &Contexts = Plt->Contexts;
30223022
auto It = std::find(Contexts.begin(), Contexts.end(), Context);
30233023
if (It != Contexts.end())
@@ -3047,7 +3047,7 @@ pi_result ContextReleaseHelper(pi_context Context) {
30473047
}
30483048

30493049
pi_result piContextRelease(pi_context Context) {
3050-
pi_platform Plt = Context->Devices[0]->Platform;
3050+
pi_platform Plt = Context->getPlatform();
30513051
std::unique_lock<std::mutex> ContextsLock(Plt->ContextsMutex,
30523052
std::defer_lock);
30533053
if (IndirectAccessTrackingEnabled)
@@ -3364,7 +3364,7 @@ static pi_result ZeDeviceMemAllocHelper(void **ResultPtr, pi_context Context,
33643364
// otherwise just calls zeMemAllocHost.
33653365
static pi_result ZeHostMemAllocHelper(void **ResultPtr, pi_context Context,
33663366
size_t Size) {
3367-
pi_platform Plt = Context->Devices[0]->Platform;
3367+
pi_platform Plt = Context->getPlatform();
33683368
std::unique_lock<std::mutex> ContextsLock(Plt->ContextsMutex,
33693369
std::defer_lock);
33703370
if (IndirectAccessTrackingEnabled) {
@@ -3439,7 +3439,7 @@ pi_result piMemBufferCreate(pi_context Context, pi_mem_flags Flags, size_t Size,
34393439
// If not shared of any type, we can import the ptr
34403440
if (ZeMemoryAllocationProperties.type == ZE_MEMORY_TYPE_UNKNOWN) {
34413441
// Promote the host ptr to USM host memory
3442-
ze_driver_handle_t driverHandle = Context->Devices[0]->Platform->ZeDriver;
3442+
ze_driver_handle_t driverHandle = Context->getPlatform()->ZeDriver;
34433443
ZeUSMImport.doZeUSMImport(driverHandle, HostPtr, Size);
34443444
HostPtrImported = true;
34453445
}
@@ -3449,8 +3449,7 @@ pi_result piMemBufferCreate(pi_context Context, pi_mem_flags Flags, size_t Size,
34493449
auto HostPtrOrNull =
34503450
(Flags & PI_MEM_FLAGS_HOST_PTR_USE) ? pi_cast<char *>(HostPtr) : nullptr;
34513451
try {
3452-
Buffer = new _pi_buffer(Context, Size, HostPtrOrNull,
3453-
true /* OwnZeMemHandle */, HostPtrImported);
3452+
Buffer = new _pi_buffer(Context, Size, HostPtrOrNull, HostPtrImported);
34543453
} catch (const std::bad_alloc &) {
34553454
return PI_OUT_OF_HOST_MEMORY;
34563455
} catch (...) {
@@ -3499,12 +3498,8 @@ pi_result piMemGetInfo(pi_mem Mem, pi_mem_info ParamName, size_t ParamValueSize,
34993498
return ReturnValue(Mem->Context);
35003499
case PI_MEM_SIZE: {
35013500
// Get size of the allocation
3502-
size_t Size;
3503-
ZE_CALL(zeMemGetAddressRange,
3504-
(Mem->Context->ZeContext, pi_cast<void *>(Mem->getZeHandle()),
3505-
nullptr, &Size));
3506-
3507-
return ReturnValue(Size);
3501+
auto Buffer = pi_cast<pi_buffer>(Mem);
3502+
return ReturnValue(size_t{Buffer->Size});
35083503
}
35093504
default:
35103505
die("piMemGetInfo: Parameter is not implemented");
@@ -3526,7 +3521,7 @@ pi_result piMemRetain(pi_mem Mem) {
35263521
// performed.
35273522
static pi_result ZeMemFreeHelper(pi_context Context, void *Ptr,
35283523
bool OwnZeMemHandle = true) {
3529-
pi_platform Plt = Context->Devices[0]->Platform;
3524+
pi_platform Plt = Context->getPlatform();
35303525
std::unique_lock<std::mutex> ContextsLock(Plt->ContextsMutex,
35313526
std::defer_lock);
35323527
if (IndirectAccessTrackingEnabled) {
@@ -3555,7 +3550,7 @@ static pi_result ZeMemFreeHelper(pi_context Context, void *Ptr,
35553550
}
35563551

35573552
static pi_result USMFreeHelper(pi_context Context, void *Ptr,
3558-
bool OwnZeMemHandle = true);
3553+
bool OwnZeMemHandle);
35593554

35603555
pi_result piMemRelease(pi_mem Mem) {
35613556
PI_ASSERT(Mem, PI_INVALID_MEM_OBJECT);
@@ -3729,7 +3724,7 @@ pi_result piMemImageCreate(pi_context Context, pi_mem_flags Flags,
37293724
(Flags & PI_MEM_FLAGS_HOST_PTR_USE) ? pi_cast<char *>(HostPtr) : nullptr;
37303725

37313726
try {
3732-
auto ZePIImage = new _pi_image(Context, ZeHImage, HostPtrOrNull, true);
3727+
auto ZePIImage = new _pi_image(Context, ZeHImage, HostPtrOrNull);
37333728

37343729
#ifndef NDEBUG
37353730
ZePIImage->ZeImageDesc = ZeImageDesc;
@@ -3767,25 +3762,24 @@ pi_result piextMemCreateWithNativeHandle(pi_native_handle NativeHandle,
37673762
PI_ASSERT(Mem, PI_INVALID_VALUE);
37683763
PI_ASSERT(NativeHandle, PI_INVALID_VALUE);
37693764
PI_ASSERT(Context, PI_INVALID_CONTEXT);
3770-
PI_ASSERT(Context->Devices.size() == 1, PI_INVALID_CONTEXT);
37713765

37723766
// Get base of the allocation
37733767
void *Base;
37743768
size_t Size;
37753769
void *Ptr = pi_cast<void *>(NativeHandle);
37763770
ZE_CALL(zeMemGetAddressRange, (Context->ZeContext, Ptr, &Base, &Size));
3777-
37783771
PI_ASSERT(Ptr == Base, PI_INVALID_VALUE);
37793772

3780-
// Check type of the allocation
37813773
ZeStruct<ze_memory_allocation_properties_t> ZeMemProps;
3782-
ze_device_handle_t ZeDevice;
3774+
ze_device_handle_t ZeDevice = nullptr;
37833775
ZE_CALL(zeMemGetAllocProperties,
37843776
(Context->ZeContext, Ptr, &ZeMemProps, &ZeDevice));
3785-
bool OnHost = false;
3777+
3778+
// Check type of the allocation
3779+
bool HostAllocation = false;
37863780
switch (ZeMemProps.type) {
37873781
case ZE_MEMORY_TYPE_HOST:
3788-
OnHost = true;
3782+
HostAllocation = true;
37893783
break;
37903784
case ZE_MEMORY_TYPE_SHARED:
37913785
case ZE_MEMORY_TYPE_DEVICE:
@@ -3797,12 +3791,21 @@ pi_result piextMemCreateWithNativeHandle(pi_native_handle NativeHandle,
37973791
die("Unexpected memory type");
37983792
}
37993793

3794+
pi_device Device = nullptr;
3795+
if (ZeDevice) {
3796+
Device = Context->getPlatform()->getDeviceFromNativeHandle(ZeDevice);
3797+
// Check that the device is present in this context.
3798+
if (std::find(Context->Devices.begin(), Context->Devices.end(), Device) ==
3799+
Context->Devices.end()) {
3800+
return PI_INVALID_CONTEXT;
3801+
}
3802+
}
3803+
38003804
try {
3801-
*Mem = new _pi_buffer(Context, pi_cast<char *>(NativeHandle),
3802-
nullptr /* HostPtr */, ownNativeHandle, nullptr, 0, 0,
3803-
OnHost);
3805+
*Mem = new _pi_buffer(Context, Size, Device, HostAllocation,
3806+
pi_cast<char *>(NativeHandle), ownNativeHandle);
38043807

3805-
pi_platform Plt = Context->Devices[0]->Platform;
3808+
pi_platform Plt = Context->getPlatform();
38063809
std::unique_lock<std::mutex> ContextsLock(Plt->ContextsMutex,
38073810
std::defer_lock);
38083811
if (IndirectAccessTrackingEnabled) {
@@ -3821,6 +3824,26 @@ pi_result piextMemCreateWithNativeHandle(pi_native_handle NativeHandle,
38213824
} catch (...) {
38223825
return PI_ERROR_UNKNOWN;
38233826
}
3827+
3828+
// Initialize the buffer as necessary
3829+
auto Buffer = pi_cast<pi_buffer>(*Mem);
3830+
if (Device) {
3831+
// If this allocation is on a device, then we re-use it for the buffer.
3832+
// Nothing to do.
3833+
} else if (HostAllocation && Buffer->OnHost) {
3834+
// If this is host allocation and buffer always stays on host there
3835+
// nothing more to do.
3836+
} else {
3837+
// In all other cases (shared allocation, or host allocation that cannot
3838+
// represent the buffer in this context) copy the data to a newly
3839+
// created device allocation.
3840+
char *ZeHandleDst;
3841+
PI_CALL(Buffer->getZeHandle(ZeHandleDst, _pi_mem::write_only));
3842+
ZE_CALL(zeCommandListAppendMemoryCopy,
3843+
(Context->ZeCommandListInit, ZeHandleDst, Ptr, Size, nullptr, 0,
3844+
nullptr));
3845+
}
3846+
38243847
return PI_SUCCESS;
38253848
}
38263849

@@ -4730,7 +4753,7 @@ pi_result piKernelRelease(pi_kernel Kernel) {
47304753
// then release referenced memory allocations. As a result, memory can be
47314754
// deallocated and context can be removed from container in the platform.
47324755
// That's why we need to lock a mutex here.
4733-
pi_platform Plt = Kernel->Program->Context->Devices[0]->Platform;
4756+
pi_platform Plt = Kernel->Program->Context->getPlatform();
47344757
std::lock_guard<std::mutex> ContextsLock(Plt->ContextsMutex);
47354758

47364759
if (--Kernel->SubmissionsCount == 0) {
@@ -5518,8 +5541,7 @@ pi_result piSamplerCreate(pi_context Context,
55185541
// border", i.e. logic is flipped. Starting from API version 1.3 this
55195542
// problem is going to be fixed. That's why check for API version to set
55205543
// an address mode.
5521-
ze_api_version_t ZeApiVersion =
5522-
Context->Devices[0]->Platform->ZeApiVersion;
5544+
ze_api_version_t ZeApiVersion = Context->getPlatform()->ZeApiVersion;
55235545
// TODO: add support for PI_SAMPLER_ADDRESSING_MODE_CLAMP_TO_EDGE
55245546
switch (CurValueAddressingMode) {
55255547
case PI_SAMPLER_ADDRESSING_MODE_NONE:
@@ -6746,7 +6768,7 @@ pi_result piMemBufferPartition(pi_mem Buffer, pi_mem_flags Flags,
67466768
void *BufferCreateInfo, pi_mem *RetMem) {
67476769

67486770
PI_ASSERT(Buffer && !Buffer->isImage() &&
6749-
!(static_cast<_pi_buffer *>(Buffer))->isSubBuffer(),
6771+
!(static_cast<pi_buffer>(Buffer))->isSubBuffer(),
67506772
PI_INVALID_MEM_OBJECT);
67516773

67526774
PI_ASSERT(BufferCreateType == PI_BUFFER_CREATE_TYPE_REGION &&
@@ -7211,7 +7233,7 @@ pi_result piextUSMHostAlloc(void **ResultPtr, pi_context Context,
72117233
if (Alignment > 65536)
72127234
return PI_INVALID_VALUE;
72137235

7214-
pi_platform Plt = Context->Devices[0]->Platform;
7236+
pi_platform Plt = Context->getPlatform();
72157237
std::unique_lock<std::mutex> ContextsLock(Plt->ContextsMutex,
72167238
std::defer_lock);
72177239
if (IndirectAccessTrackingEnabled) {
@@ -7332,7 +7354,7 @@ static pi_result USMFreeHelper(pi_context Context, void *Ptr,
73327354
PI_ASSERT(Device->ZeDevice == ZeDeviceHandle, PI_INVALID_DEVICE);
73337355
} else {
73347356
// All devices in the context are of the same platform.
7335-
auto Platform = Context->Devices[0]->Platform;
7357+
auto Platform = Context->getPlatform();
73367358
Device = Platform->getDeviceFromNativeHandle(ZeDeviceHandle);
73377359
PI_ASSERT(Device, PI_INVALID_DEVICE);
73387360
}
@@ -7382,12 +7404,12 @@ static pi_result USMFreeHelper(pi_context Context, void *Ptr,
73827404
}
73837405

73847406
pi_result piextUSMFree(pi_context Context, void *Ptr) {
7385-
pi_platform Plt = Context->Devices[0]->Platform;
7407+
pi_platform Plt = Context->getPlatform();
73867408
std::unique_lock<std::mutex> ContextsLock(Plt->ContextsMutex,
73877409
std::defer_lock);
73887410
if (IndirectAccessTrackingEnabled)
73897411
ContextsLock.lock();
7390-
return USMFreeHelper(Context, Ptr);
7412+
return USMFreeHelper(Context, Ptr, true /* OwnZeMemHandle */);
73917413
}
73927414

73937415
pi_result piextKernelSetArgPointer(pi_kernel Kernel, pi_uint32 ArgIndex,
@@ -7651,8 +7673,7 @@ pi_result piextUSMGetMemAllocInfo(pi_context Context, const void *Ptr,
76517673
}
76527674
case PI_MEM_ALLOC_DEVICE:
76537675
if (ZeDeviceHandle) {
7654-
// All devices in the context are of the same platform.
7655-
auto Platform = Context->Devices[0]->Platform;
7676+
auto Platform = Context->getPlatform();
76567677
auto Device = Platform->getDeviceFromNativeHandle(ZeDeviceHandle);
76577678
return Device ? ReturnValue(Device) : PI_INVALID_VALUE;
76587679
} else {
@@ -7961,23 +7982,31 @@ pi_result _pi_buffer::free() {
79617982
return PI_SUCCESS;
79627983
}
79637984
if (HostPtrImported) {
7964-
ze_driver_handle_t DriverHandle = Context->Devices[0]->Platform->ZeDriver;
7985+
ze_driver_handle_t DriverHandle = Context->getPlatform()->ZeDriver;
79657986
ZeUSMImport.doZeUSMRelease(DriverHandle, MapHostPtr);
79667987
if (OnHost) {
79677988
// We were using imported host pointer, so nothing to free.
79687989
return PI_SUCCESS;
79697990
}
79707991
}
79717992

7993+
pi_platform Plt = Context->getPlatform();
7994+
std::unique_lock<std::mutex> ContextsLock(Plt->ContextsMutex,
7995+
std::defer_lock);
7996+
if (IndirectAccessTrackingEnabled)
7997+
ContextsLock.lock();
7998+
79727999
for (auto &Alloc : Allocations) {
79738000
auto Device = Alloc.first;
79748001
if (Context->SingleRootDevice && Context->SingleRootDevice != Device) {
79758002
// These were re-using root-device allocations
79768003
}
79778004
// It is possible that the real allocation wasn't made if the buffer
79788005
// wasn't really used on this device.
7979-
if (Alloc.second.ZeHandle)
7980-
PI_CALL(piextUSMFree(Context, Alloc.second.ZeHandle));
8006+
if (Alloc.second.ZeHandle) {
8007+
PI_CALL(USMFreeHelper(Context, Alloc.second.ZeHandle,
8008+
Alloc.second.ZeHandle != NotOwnZeMemHandle));
8009+
}
79818010
}
79828011
return PI_SUCCESS;
79838012
}

0 commit comments

Comments
 (0)