Skip to content

Commit 27e2c32

Browse files
Use device allocation for events if host visibility not set
Change-Id: I244cfd617ae77a88dd2f4e5818d39ac7e658b216 Signed-off-by: Aravind Gopalakrishnan <[email protected]>
1 parent bc3e09c commit 27e2c32

File tree

14 files changed

+315
-43
lines changed

14 files changed

+315
-43
lines changed

level_zero/core/source/cmdlist/cmdlist_hw.inl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,11 @@ ze_result_t CommandListCoreFamily<gfxCoreFamily>::appendEventReset(ze_event_hand
215215
if (event->isTimestampEvent) {
216216
eventOffset = offsetof(KernelTimestampEvent, contextEnd);
217217
}
218-
commandContainer.addToResidencyContainer(&event->getAllocation());
218+
219+
if (!event->allocOnDevice) {
220+
commandContainer.addToResidencyContainer(&event->getAllocation());
221+
}
222+
219223
if (isCopyOnly()) {
220224
NEO::EncodeMiFlushDW<GfxFamily>::programMiFlushDw(*commandContainer.getCommandStream(), event->getGpuAddress(), Event::STATE_CLEARED, false, true);
221225
} else {
@@ -1359,7 +1363,10 @@ ze_result_t CommandListCoreFamily<gfxCoreFamily>::appendSignalEvent(ze_event_han
13591363
using GfxFamily = typename NEO::GfxFamilyMapper<gfxCoreFamily>::GfxFamily;
13601364
auto event = Event::fromHandle(hEvent);
13611365

1362-
commandContainer.addToResidencyContainer(&event->getAllocation());
1366+
if (!event->allocOnDevice) {
1367+
commandContainer.addToResidencyContainer(&event->getAllocation());
1368+
}
1369+
13631370
uint64_t baseAddr = event->getGpuAddress();
13641371
size_t eventSignalOffset = 0;
13651372
if (event->isTimestampEvent) {
@@ -1394,7 +1401,9 @@ ze_result_t CommandListCoreFamily<gfxCoreFamily>::appendWaitOnEvents(uint32_t nu
13941401

13951402
for (uint32_t i = 0; i < numEvents; i++) {
13961403
auto event = Event::fromHandle(phEvent[i]);
1397-
commandContainer.addToResidencyContainer(&event->getAllocation());
1404+
if (!event->allocOnDevice) {
1405+
commandContainer.addToResidencyContainer(&event->getAllocation());
1406+
}
13981407

13991408
gpuAddr = event->getGpuAddress();
14001409
if (event->isTimestampEvent) {
@@ -1434,7 +1443,10 @@ void CommandListCoreFamily<gfxCoreFamily>::appendEventForProfiling(ze_event_hand
14341443
return;
14351444
}
14361445

1437-
commandContainer.addToResidencyContainer(&event->getAllocation());
1446+
if (!event->allocOnDevice) {
1447+
commandContainer.addToResidencyContainer(&event->getAllocation());
1448+
}
1449+
14381450
auto baseAddr = event->getGpuAddress();
14391451

14401452
if (beforeWalker) {

level_zero/core/source/event/event.cpp

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "shared/source/memory_manager/memory_operations_handler.h"
2020
#include "shared/source/utilities/cpuintrinsics.h"
2121

22+
#include "level_zero/core/source/cmdlist/cmdlist.h"
23+
#include "level_zero/core/source/cmdqueue/cmdqueue.h"
2224
#include "level_zero/core/source/device/device.h"
2325
#include "level_zero/core/source/device/device_imp.h"
2426
#include "level_zero/tools/source/metrics/metric.h"
@@ -56,25 +58,57 @@ ze_result_t EventPoolImp::initialize(DriverHandle *driver, uint32_t numDevices,
5658
maxRootDeviceIndex = rootDeviceIndices[0];
5759
}
5860

61+
if (this->devices.size() > 1) {
62+
this->allocOnDevice = false;
63+
}
64+
65+
if (allocOnDevice) {
66+
ze_command_queue_desc_t cmdQueueDesc = {};
67+
cmdQueueDesc.ordinal = 0;
68+
cmdQueueDesc.index = 0;
69+
cmdQueueDesc.flags = 0;
70+
cmdQueueDesc.stype = ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC;
71+
cmdQueueDesc.mode = ZE_COMMAND_QUEUE_MODE_SYNCHRONOUS;
72+
ze_result_t returnValue = ZE_RESULT_SUCCESS;
73+
eventPoolCommandList =
74+
CommandList::createImmediate(
75+
static_cast<DeviceImp *>(this->devices[0])->neoDevice->getHardwareInfo().platform.eProductFamily,
76+
this->devices[0],
77+
&cmdQueueDesc,
78+
true,
79+
NEO::EngineGroupType::RenderCompute,
80+
returnValue);
81+
82+
if (!this->eventPoolCommandList) {
83+
this->allocOnDevice = false;
84+
}
85+
}
86+
5987
eventPoolAllocations = new NEO::MultiGraphicsAllocation(maxRootDeviceIndex);
6088

6189
uint32_t rootDeviceIndex = rootDeviceIndices.at(0);
6290

63-
NEO::SVMAllocsManager::UnifiedMemoryProperties memoryProperties(InternalMemoryType::HOST_UNIFIED_MEMORY,
64-
devices[0]->getNEODevice()->getDeviceBitfield());
91+
auto subDeviceBitField = devices[0]->getNEODevice()->getDeviceBitfield();
92+
auto internalMemoryType = InternalMemoryType::HOST_UNIFIED_MEMORY;
93+
auto allocationType = isEventPoolUsedForTimestamp ? NEO::GraphicsAllocation::AllocationType::TIMESTAMP_PACKET_TAG_BUFFER : NEO::GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY;
94+
if (this->allocOnDevice) {
95+
internalMemoryType = InternalMemoryType::DEVICE_UNIFIED_MEMORY;
96+
allocationType = NEO::GraphicsAllocation::AllocationType::BUFFER;
97+
}
98+
99+
NEO::SVMAllocsManager::UnifiedMemoryProperties memoryProperties(internalMemoryType, subDeviceBitField);
65100

66-
NEO::AllocationProperties unifiedMemoryProperties{rootDeviceIndex,
67-
true,
68-
alignUp<size_t>(numEvents * eventSize, MemoryConstants::pageSize64k),
69-
isEventPoolUsedForTimestamp ? NEO::GraphicsAllocation::AllocationType::TIMESTAMP_PACKET_TAG_BUFFER
70-
: NEO::GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY,
71-
memoryProperties.subdeviceBitfield.count() > 1,
72-
memoryProperties.subdeviceBitfield.count() > 1,
73-
memoryProperties.subdeviceBitfield};
74-
unifiedMemoryProperties.alignment = eventAlignment;
101+
NEO::AllocationProperties eventPoolAllocationProperties{rootDeviceIndex,
102+
true,
103+
alignUp<size_t>(numEvents * eventSize, MemoryConstants::pageSize64k),
104+
allocationType,
105+
memoryProperties.subdeviceBitfield.count() > 1,
106+
memoryProperties.subdeviceBitfield.count() > 1,
107+
memoryProperties.subdeviceBitfield};
108+
eventPoolAllocationProperties.alignment = eventAlignment;
75109

76110
void *eventPoolPtr = driver->getMemoryManager()->createMultiGraphicsAllocation(rootDeviceIndices,
77-
unifiedMemoryProperties,
111+
eventPoolAllocationProperties,
78112
*eventPoolAllocations);
79113
if (!eventPoolPtr) {
80114
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
@@ -90,6 +124,11 @@ EventPoolImp::~EventPoolImp() {
90124
}
91125
delete eventPoolAllocations;
92126
eventPoolAllocations = nullptr;
127+
128+
if (eventPoolCommandList) {
129+
eventPoolCommandList->destroy();
130+
eventPoolCommandList = nullptr;
131+
}
93132
}
94133

95134
ze_result_t EventPoolImp::getIpcHandle(ze_ipc_event_pool_handle_t *pIpcHandle) {
@@ -122,6 +161,9 @@ Event *Event::create(EventPool *eventPool, const ze_event_desc_t *desc, Device *
122161
if (eventPool->isEventPoolUsedForTimestamp) {
123162
event->isTimestampEvent = true;
124163
}
164+
if (eventPool->allocOnDevice) {
165+
event->allocOnDevice = true;
166+
}
125167

126168
auto alloc = eventPool->getAllocation().getGraphicsAllocation(device->getNEODevice()->getRootDeviceIndex());
127169

@@ -135,6 +177,10 @@ Event *Event::create(EventPool *eventPool, const ze_event_desc_t *desc, Device *
135177

136178
event->reset();
137179

180+
if (event->allocOnDevice) {
181+
eventPool->eventPoolCommandList->appendEventReset(event->toHandle());
182+
}
183+
138184
return event;
139185
}
140186

@@ -144,6 +190,10 @@ NEO::GraphicsAllocation &Event::getAllocation() {
144190
return *eventImp->eventPool->getAllocation().getGraphicsAllocation(eventImp->device->getNEODevice()->getRootDeviceIndex());
145191
}
146192

193+
uint64_t Event::getGpuAddress() {
194+
return gpuAddress;
195+
}
196+
147197
ze_result_t Event::destroy() {
148198
delete this;
149199
return ZE_RESULT_SUCCESS;
@@ -165,7 +215,7 @@ ze_result_t EventImp::queryStatus() {
165215
return queryVal == Event::STATE_CLEARED ? ZE_RESULT_NOT_READY : ZE_RESULT_SUCCESS;
166216
}
167217

168-
ze_result_t EventImp::hostEventSetValueTimestamps(uint32_t eventVal) {
218+
void EventImp::hostEventSetValueTimestamps(uint32_t eventVal) {
169219

170220
auto baseAddr = reinterpret_cast<uint64_t>(hostAddress);
171221
auto signalScopeFlag = this->signalScope;
@@ -182,13 +232,11 @@ ze_result_t EventImp::hostEventSetValueTimestamps(uint32_t eventVal) {
182232
eventTsSetFunc(baseAddr + offsetof(KernelTimestampEvent, globalStart));
183233
eventTsSetFunc(baseAddr + offsetof(KernelTimestampEvent, contextEnd));
184234
eventTsSetFunc(baseAddr + offsetof(KernelTimestampEvent, globalEnd));
185-
186-
return ZE_RESULT_SUCCESS;
187235
}
188236

189237
ze_result_t EventImp::hostEventSetValue(uint32_t eventVal) {
190238
if (isTimestampEvent) {
191-
return hostEventSetValueTimestamps(eventVal);
239+
hostEventSetValueTimestamps(eventVal);
192240
}
193241

194242
auto hostAddr = static_cast<uint64_t *>(hostAddress);
@@ -243,6 +291,11 @@ ze_result_t EventImp::hostSynchronize(uint64_t timeout) {
243291
}
244292

245293
ze_result_t EventImp::reset() {
294+
295+
if (allocOnDevice) {
296+
return ZE_RESULT_SUCCESS;
297+
}
298+
246299
return hostEventSetValue(Event::STATE_INITIAL);
247300
}
248301

level_zero/core/source/event/event.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ struct Event : _ze_event_handle_t {
4444

4545
virtual NEO::GraphicsAllocation &getAllocation();
4646

47-
uint64_t getGpuAddress() { return gpuAddress; }
47+
virtual uint64_t getGpuAddress();
4848

4949
void *hostAddress = nullptr;
50-
uint64_t gpuAddress;
50+
uint64_t gpuAddress = 0u;
5151

5252
ze_event_scope_flags_t signalScope = 0u;
5353
ze_event_scope_flags_t waitScope = 0u;
5454

5555
bool isTimestampEvent = false;
56+
bool allocOnDevice = false;
5657

5758
// Metric streamer instance associated with the event.
5859
MetricStreamer *metricStreamer = nullptr;
@@ -84,7 +85,7 @@ struct EventImp : public Event {
8485

8586
protected:
8687
ze_result_t hostEventSetValue(uint32_t eventValue);
87-
ze_result_t hostEventSetValueTimestamps(uint32_t eventVal);
88+
void hostEventSetValueTimestamps(uint32_t eventVal);
8889
void makeAllocationResident();
8990
};
9091

@@ -112,9 +113,12 @@ struct EventPool : _ze_event_pool_handle_t {
112113

113114
virtual NEO::MultiGraphicsAllocation &getAllocation() { return *eventPoolAllocations; }
114115

115-
virtual uint32_t getEventSize() = 0;
116+
virtual size_t getEventSize() = 0;
116117

117118
bool isEventPoolUsedForTimestamp = false;
119+
bool allocOnDevice = false;
120+
121+
CommandList *eventPoolCommandList = nullptr;
118122

119123
protected:
120124
NEO::MultiGraphicsAllocation *eventPoolAllocations = nullptr;
@@ -125,6 +129,10 @@ struct EventPoolImp : public EventPool {
125129
if (flags & ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP) {
126130
isEventPoolUsedForTimestamp = true;
127131
}
132+
133+
if (!(flags & ZE_EVENT_POOL_FLAG_HOST_VISIBLE)) {
134+
allocOnDevice = true;
135+
}
128136
}
129137

130138
ze_result_t initialize(DriverHandle *driver,
@@ -142,7 +150,7 @@ struct EventPoolImp : public EventPool {
142150

143151
ze_result_t createEvent(const ze_event_desc_t *desc, ze_event_handle_t *phEvent) override;
144152

145-
uint32_t getEventSize() override { return eventSize; }
153+
size_t getEventSize() override { return eventSize; }
146154
size_t getNumEvents() { return numEvents; }
147155

148156
Device *getDevice() override { return devices[0]; }

level_zero/core/test/unit_tests/gen12lp/test_events_gen12lp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct TimestampEvent : public Test<DeviceFixture> {
2020
DeviceFixture::SetUp();
2121
ze_event_pool_desc_t eventPoolDesc = {};
2222
eventPoolDesc.count = 1;
23-
eventPoolDesc.flags = ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
23+
eventPoolDesc.flags = ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP | ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
2424

2525
ze_event_desc_t eventDesc = {};
2626
eventDesc.index = 0;

level_zero/core/test/unit_tests/mocks/mock_event.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ struct Mock<EventPool> : public EventPool {
7272
MOCK_METHOD2(reserveEventFromPool, ze_result_t(int index, ::L0::Event *event));
7373
MOCK_METHOD1(releaseEventToPool, ze_result_t(::L0::Event *event));
7474
MOCK_METHOD0(getDevice, Device *());
75-
MOCK_METHOD0(getEventSize, uint32_t());
75+
MOCK_METHOD0(getEventSize, size_t());
7676

7777
std::vector<int> pool;
7878

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ HWTEST2_F(CommandListCreate, givenCopyCommandListWhenProfilingBeforeCommandForCo
879879
commandList->initialize(device, NEO::EngineGroupType::Copy);
880880
ze_event_pool_desc_t eventPoolDesc = {};
881881
eventPoolDesc.count = 1;
882-
eventPoolDesc.flags = ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
882+
eventPoolDesc.flags = ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP | ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
883883

884884
ze_event_desc_t eventDesc = {};
885885
eventDesc.index = 0;
@@ -912,7 +912,7 @@ HWTEST2_F(CommandListCreate, givenCopyCommandListWhenProfilingAfterCommandForCop
912912
commandList->initialize(device, NEO::EngineGroupType::Copy);
913913
ze_event_pool_desc_t eventPoolDesc = {};
914914
eventPoolDesc.count = 1;
915-
eventPoolDesc.flags = ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
915+
eventPoolDesc.flags = ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP | ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
916916

917917
ze_event_desc_t eventDesc = {};
918918
eventDesc.index = 0;
@@ -938,6 +938,29 @@ HWTEST2_F(CommandListCreate, givenCopyCommandListWhenProfilingAfterCommandForCop
938938
EXPECT_EQ(cmd->getMemoryAddress(), ptrOffset(baseAddr, contextOffset));
939939
}
940940

941+
HWTEST2_F(CommandListCreate, givenComputeCommandListWhenProfilingDeviceOnlyEventThenEventNotAddedToResidencyContainer, Platforms) {
942+
auto commandList = std::make_unique<WhiteBox<::L0::CommandListCoreFamily<gfxCoreFamily>>>();
943+
commandList->initialize(device, NEO::EngineGroupType::RenderCompute);
944+
ze_event_pool_desc_t eventPoolDesc = {};
945+
eventPoolDesc.count = 1;
946+
eventPoolDesc.flags = ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
947+
948+
ze_event_desc_t eventDesc = {};
949+
eventDesc.index = 0;
950+
auto eventPool = std::unique_ptr<L0::EventPool>(L0::EventPool::create(driverHandle.get(), 0, nullptr, &eventPoolDesc));
951+
auto event = std::unique_ptr<L0::Event>(L0::Event::create(eventPool.get(), &eventDesc, device));
952+
953+
commandList->appendEventForProfiling(event->toHandle(), false);
954+
955+
auto &residencyContainer = commandList->commandContainer.getResidencyContainer();
956+
auto eventPoolAlloc = &eventPool->getAllocation();
957+
for (auto alloc : eventPoolAlloc->getGraphicsAllocations()) {
958+
auto itor =
959+
std::find(std::begin(residencyContainer), std::end(residencyContainer), alloc);
960+
EXPECT_EQ(itor, std::end(residencyContainer));
961+
}
962+
}
963+
941964
HWTEST2_F(CommandListCreate, givenNullEventWhenAppendEventAfterWalkerThenNothingAddedToStream, Platforms) {
942965
using GfxFamily = typename NEO::GfxFamilyMapper<gfxCoreFamily>::GfxFamily;
943966
using MI_STORE_REGISTER_MEM = typename GfxFamily::MI_STORE_REGISTER_MEM;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ HWTEST2_F(CommandListCreate, givenCommandListWhenTimestampPassedToMemoryCopyThen
468468

469469
ze_event_pool_desc_t eventPoolDesc = {};
470470
eventPoolDesc.count = 1;
471-
eventPoolDesc.flags = ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
471+
eventPoolDesc.flags = ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP | ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
472472

473473
ze_event_desc_t eventDesc = {};
474474
eventDesc.index = 0;
@@ -987,7 +987,7 @@ HWTEST2_F(CommandListCreate, givenCopyCommandListWhenTimestampPassedToMemoryCopy
987987
void *srcPtr = reinterpret_cast<void *>(0x1234);
988988
void *dstPtr = reinterpret_cast<void *>(0x2345);
989989
ze_event_pool_desc_t eventPoolDesc = {};
990-
eventPoolDesc.flags = ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
990+
eventPoolDesc.flags = ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP | ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
991991
eventPoolDesc.count = 1;
992992

993993
ze_event_desc_t eventDesc = {};

0 commit comments

Comments
 (0)