Skip to content

Commit 09298d3

Browse files
fix: pass USM alloc to external CB event if possible
Signed-off-by: Bartosz Dunajski <[email protected]>
1 parent e91480c commit 09298d3

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

level_zero/core/source/event/event.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,15 @@ void Event::resetInOrderTimestampNode(NEO::TagNodeBase *newNode) {
673673
inOrderTimestampNode = newNode;
674674
}
675675

676+
NEO::GraphicsAllocation *Event::getExternalCounterAllocationFromAddress(uint64_t *address) const {
677+
NEO::SvmAllocationData *allocData = nullptr;
678+
if (!address || !device->getDriverHandle()->findAllocationDataForRange(address, sizeof(uint64_t), allocData)) {
679+
return nullptr;
680+
}
681+
682+
return allocData->gpuAllocations.getGraphicsAllocation(device->getRootDeviceIndex());
683+
}
684+
676685
ze_result_t Event::enableExtensions(const EventDescriptor &eventDescriptor) {
677686
bool interruptMode = false;
678687
bool kmdWaitMode = false;
@@ -699,31 +708,31 @@ ze_result_t Event::enableExtensions(const EventDescriptor &eventDescriptor) {
699708
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
700709
}
701710

702-
NEO::SvmAllocationData *externalHostAllocData = nullptr;
703-
if (!device->getDriverHandle()->findAllocationDataForRange(externalSyncAllocProperties->hostAddress, sizeof(uint64_t), externalHostAllocData)) {
711+
auto deviceAlloc = getExternalCounterAllocationFromAddress(externalSyncAllocProperties->deviceAddress);
712+
auto hostAlloc = getExternalCounterAllocationFromAddress(externalSyncAllocProperties->hostAddress);
713+
714+
if (!hostAlloc) {
704715
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
705716
}
706717

707-
auto allocation = externalHostAllocData->gpuAllocations.getGraphicsAllocation(device->getRootDeviceIndex());
708-
auto inOrderExecInfo = NEO::InOrderExecInfo::createFromExternalAllocation(*device->getNEODevice(), nullptr, castToUint64(externalSyncAllocProperties->deviceAddress),
709-
allocation, externalSyncAllocProperties->hostAddress, externalSyncAllocProperties->completionValue, 1, 1);
718+
auto inOrderExecInfo = NEO::InOrderExecInfo::createFromExternalAllocation(*device->getNEODevice(), deviceAlloc, castToUint64(externalSyncAllocProperties->deviceAddress),
719+
hostAlloc, externalSyncAllocProperties->hostAddress, externalSyncAllocProperties->completionValue, 1, 1);
710720
updateInOrderExecState(inOrderExecInfo, externalSyncAllocProperties->completionValue, 0);
711721
} else if (extendedDesc->stype == ZEX_STRUCTURE_COUNTER_BASED_EVENT_EXTERNAL_STORAGE_ALLOC_PROPERTIES) {
712722
auto externalStorageProperties = reinterpret_cast<const zex_counter_based_event_external_storage_properties_t *>(extendedDesc);
713723

714-
NEO::SvmAllocationData *externalDeviceAllocData = nullptr;
715-
if (!externalStorageProperties->deviceAddress || !device->getDriverHandle()->findAllocationDataForRange(externalStorageProperties->deviceAddress, sizeof(uint64_t), externalDeviceAllocData) ||
716-
externalStorageProperties->incrementValue == 0) {
724+
auto deviceAlloc = getExternalCounterAllocationFromAddress(externalStorageProperties->deviceAddress);
725+
726+
if (!deviceAlloc || externalStorageProperties->incrementValue == 0) {
717727
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
718728
}
719729

720-
auto allocation = externalDeviceAllocData->gpuAllocations.getGraphicsAllocation(device->getRootDeviceIndex());
721-
auto offset = ptrDiff(externalStorageProperties->deviceAddress, allocation->getGpuAddress());
730+
auto offset = ptrDiff(externalStorageProperties->deviceAddress, deviceAlloc->getGpuAddress());
722731

723-
auto hostAddress = ptrOffset(device->getNEODevice()->getMemoryManager()->lockResource(allocation), offset);
732+
auto hostAddress = ptrOffset(device->getNEODevice()->getMemoryManager()->lockResource(deviceAlloc), offset);
724733

725-
auto inOrderExecInfo = NEO::InOrderExecInfo::createFromExternalAllocation(*device->getNEODevice(), allocation, castToUint64(externalStorageProperties->deviceAddress),
726-
allocation, reinterpret_cast<uint64_t *>(hostAddress), externalStorageProperties->completionValue, 1, 1);
734+
auto inOrderExecInfo = NEO::InOrderExecInfo::createFromExternalAllocation(*device->getNEODevice(), deviceAlloc, castToUint64(externalStorageProperties->deviceAddress),
735+
deviceAlloc, reinterpret_cast<uint64_t *>(hostAddress), externalStorageProperties->completionValue, 1, 1);
727736

728737
updateInOrderExecState(inOrderExecInfo, externalStorageProperties->completionValue, 0);
729738

level_zero/core/source/event/event.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ struct Event : _ze_event_handle_t {
341341
Event(int index, Device *device) : device(device), index(index) {}
342342

343343
ze_result_t enableExtensions(const EventDescriptor &eventDescriptor);
344+
NEO::GraphicsAllocation *getExternalCounterAllocationFromAddress(uint64_t *address) const;
344345

345346
void unsetCmdQueue();
346347
void releaseTempInOrderTimestampNodes();

level_zero/core/test/unit_tests/sources/cmdlist/test_in_order_cmdlist_1.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5237,19 +5237,51 @@ HWTEST2_F(InOrderCmdListTests, givenCorrectInputParamsWhenCreatingCbEvent2ThenRe
52375237
EXPECT_EQ(counterValue, eventObj->getInOrderExecInfo()->getCounterValue());
52385238
EXPECT_EQ(hostAddress, eventObj->getInOrderExecInfo()->getBaseHostAddress());
52395239
EXPECT_EQ(castToUint64(gpuAddress), eventObj->getInOrderExecInfo()->getBaseDeviceAddress());
5240+
EXPECT_EQ(nullptr, eventObj->getInOrderExecInfo()->getDeviceCounterAllocation());
52405241

5241-
uint64_t addresss = 0;
5242+
uint64_t address = 0;
52425243
uint64_t value = 0;
5243-
zexEventGetDeviceAddress(handle, &value, &addresss);
5244+
zexEventGetDeviceAddress(handle, &value, &address);
52445245

5245-
EXPECT_EQ(addresss, eventObj->getInOrderExecInfo()->getBaseDeviceAddress());
5246+
EXPECT_EQ(address, eventObj->getInOrderExecInfo()->getBaseDeviceAddress());
52465247
EXPECT_EQ(value, counterValue);
52475248

52485249
zeEventDestroy(handle);
52495250

52505251
context->freeMem(hostAddress);
52515252
}
52525253

5254+
HWTEST2_F(InOrderCmdListTests, givenUsmDeviceAllocationWhenCreatingCbEventFromExternalMemoryThenAssignGraphicsAllocation, MatchAny) {
5255+
auto hostAddress = reinterpret_cast<uint64_t *>(allocHostMem(sizeof(uint64_t)));
5256+
auto deviceAddress = reinterpret_cast<uint64_t *>(allocDeviceMem(sizeof(uint64_t)));
5257+
5258+
zex_counter_based_event_external_sync_alloc_properties_t externalSyncAllocProperties = {ZEX_STRUCTURE_COUNTER_BASED_EVENT_EXTERNAL_SYNC_ALLOC_PROPERTIES}; // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange), NEO-12901
5259+
externalSyncAllocProperties.completionValue = 2;
5260+
externalSyncAllocProperties.deviceAddress = deviceAddress;
5261+
externalSyncAllocProperties.hostAddress = hostAddress;
5262+
5263+
zex_counter_based_event_desc_t counterBasedDesc = {ZEX_STRUCTURE_COUNTER_BASED_EVENT_DESC}; // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange), NEO-12901
5264+
counterBasedDesc.flags = ZEX_COUNTER_BASED_EVENT_FLAG_IMMEDIATE | ZEX_COUNTER_BASED_EVENT_FLAG_NON_IMMEDIATE;
5265+
counterBasedDesc.pNext = &externalSyncAllocProperties;
5266+
5267+
ze_event_handle_t handle = nullptr;
5268+
5269+
EXPECT_EQ(ZE_RESULT_SUCCESS, zexCounterBasedEventCreate2(context, device, &counterBasedDesc, &handle));
5270+
5271+
auto eventObj = Event::fromHandle(handle);
5272+
5273+
ASSERT_NE(nullptr, eventObj);
5274+
ASSERT_NE(nullptr, eventObj->getInOrderExecInfo().get());
5275+
5276+
EXPECT_EQ(castToUint64(deviceAddress), eventObj->getInOrderExecInfo()->getBaseDeviceAddress());
5277+
EXPECT_NE(nullptr, eventObj->getInOrderExecInfo()->getDeviceCounterAllocation());
5278+
5279+
zeEventDestroy(handle);
5280+
5281+
context->freeMem(hostAddress);
5282+
context->freeMem(deviceAddress);
5283+
}
5284+
52535285
HWTEST2_F(InOrderCmdListTests, givenExternalSyncStorageWhenCreatingCounterBasedEventThenSetAllParams, MatchAny) {
52545286
uint64_t counterValue = 4;
52555287
uint64_t incValue = 2;

0 commit comments

Comments
 (0)