Skip to content

Commit 0856313

Browse files
[L0] Interrupt-based event implementation
Signed-off-by: Zhang, Winston <[email protected]>
1 parent d9576ca commit 0856313

File tree

6 files changed

+101
-44
lines changed

6 files changed

+101
-44
lines changed

source/adapters/level_zero/context.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,9 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
488488
if (Device) {
489489
ZeDevice = Device->ZeDevice;
490490
}
491-
std::list<ze_event_pool_handle_t> *ZePoolCache =
492-
getZeEventPoolCache(HostVisible, ProfilingEnabled,
493-
CounterBasedEventEnabled, UsingImmCmdList, ZeDevice);
491+
std::list<ze_event_pool_handle_t> *ZePoolCache = getZeEventPoolCache(
492+
HostVisible, ProfilingEnabled, CounterBasedEventEnabled, UsingImmCmdList,
493+
InterruptBasedEventEnabled, ZeDevice);
494494

495495
if (!ZePoolCache->empty()) {
496496
if (NumEventsAvailableInEventPool[ZePoolCache->front()] == 0) {
@@ -572,7 +572,7 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
572572

573573
ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
574574
bool HostVisible, bool WithProfiling, ur_device_handle_t Device,
575-
bool CounterBasedEventEnabled) {
575+
bool CounterBasedEventEnabled, bool InterruptBasedEventEnabled) {
576576
std::scoped_lock<ur_mutex> Lock(EventCacheMutex);
577577
auto Cache = getEventCache(HostVisible, WithProfiling, Device);
578578
if (Cache->empty())
@@ -583,6 +583,9 @@ ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
583583
if (Event->CounterBasedEventsEnabled != CounterBasedEventEnabled) {
584584
return nullptr;
585585
}
586+
if (Event->InterruptBasedEventsEnabled != InterruptBasedEventEnabled) {
587+
return nullptr;
588+
}
586589
Cache->erase(It);
587590
// We have to reset event before using it.
588591
Event->reset();
@@ -623,7 +626,8 @@ ur_context_handle_t_::decrementUnreleasedEventsInPool(ur_event_handle_t Event) {
623626

624627
std::list<ze_event_pool_handle_t> *ZePoolCache = getZeEventPoolCache(
625628
Event->isHostVisible(), Event->isProfilingEnabled(),
626-
Event->CounterBasedEventsEnabled, UsingImmediateCommandlists, ZeDevice);
629+
Event->CounterBasedEventsEnabled, UsingImmediateCommandlists,
630+
Event->InterruptBasedEventsEnabled, ZeDevice);
627631

628632
// Put the empty pool to the cache of the pools.
629633
if (NumEventsUnreleasedInEventPool[Event->ZeEventPool] == 0)

source/adapters/level_zero/context.hpp

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ struct ur_context_handle_t_ : _ur_object {
168168
// head.
169169
//
170170
// Cache of event pools to which host-visible events are added to.
171-
std::vector<std::list<ze_event_pool_handle_t>> ZeEventPoolCache{12};
171+
std::vector<std::list<ze_event_pool_handle_t>> ZeEventPoolCache{30};
172172
std::vector<std::unordered_map<ze_device_handle_t, size_t>>
173-
ZeEventPoolCacheDeviceMap{12};
173+
ZeEventPoolCacheDeviceMap{30};
174174

175175
// This map will be used to determine if a pool is full or not
176176
// by storing number of empty slots available in the pool.
@@ -233,7 +233,8 @@ struct ur_context_handle_t_ : _ur_object {
233233
ur_event_handle_t getEventFromContextCache(bool HostVisible,
234234
bool WithProfiling,
235235
ur_device_handle_t Device,
236-
bool CounterBasedEventEnabled);
236+
bool CounterBasedEventEnabled,
237+
bool InterruptBasedEventEnabled);
237238

238239
// Add ur_event_handle_t to cache.
239240
void addEventToContextCache(ur_event_handle_t);
@@ -244,17 +245,29 @@ struct ur_context_handle_t_ : _ur_object {
244245
HostVisibleCounterBasedRegularCacheType,
245246
HostInvisibleCounterBasedRegularCacheType,
246247
HostVisibleCounterBasedImmediateCacheType,
247-
HostInvisibleCounterBasedImmediateCacheType
248+
HostInvisibleCounterBasedImmediateCacheType,
249+
250+
HostVisibleInterruptBasedRegularCacheType,
251+
HostInvisibleInterruptBasedRegularCacheType,
252+
HostVisibleInterruptBasedImmediateCacheType,
253+
HostInvisibleInterruptBasedImmediateCacheType,
254+
255+
HostVisibleInterruptAndCounterBasedRegularCacheType,
256+
HostInvisibleInterruptAndCounterBasedRegularCacheType,
257+
HostVisibleInterruptAndCounterBasedImmediateCacheType,
258+
HostInvisibleInterruptAndCounterBasedImmediateCacheType
248259
};
249260

250261
std::list<ze_event_pool_handle_t> *
251262
getZeEventPoolCache(bool HostVisible, bool WithProfiling,
252263
bool CounterBasedEventEnabled, bool UsingImmediateCmdList,
264+
bool InterruptBasedEventEnabled,
253265
ze_device_handle_t ZeDevice) {
254266
EventPoolCacheType CacheType;
255267

256268
calculateCacheIndex(HostVisible, CounterBasedEventEnabled,
257-
UsingImmediateCmdList, CacheType);
269+
InterruptBasedEventEnabled, UsingImmediateCmdList,
270+
CacheType);
258271
if (ZeDevice) {
259272
auto ZeEventPoolCacheMap =
260273
WithProfiling ? &ZeEventPoolCacheDeviceMap[CacheType * 2]
@@ -274,23 +287,57 @@ struct ur_context_handle_t_ : _ur_object {
274287
ur_result_t calculateCacheIndex(bool HostVisible,
275288
bool CounterBasedEventEnabled,
276289
bool UsingImmediateCmdList,
290+
bool InterruptBasedEventEnabled,
277291
EventPoolCacheType &CacheType) {
278-
if (CounterBasedEventEnabled && HostVisible && !UsingImmediateCmdList) {
279-
CacheType = HostVisibleCounterBasedRegularCacheType;
280-
} else if (CounterBasedEventEnabled && !HostVisible &&
281-
!UsingImmediateCmdList) {
282-
CacheType = HostInvisibleCounterBasedRegularCacheType;
283-
} else if (CounterBasedEventEnabled && HostVisible &&
284-
UsingImmediateCmdList) {
285-
CacheType = HostVisibleCounterBasedImmediateCacheType;
286-
} else if (CounterBasedEventEnabled && !HostVisible &&
287-
UsingImmediateCmdList) {
288-
CacheType = HostInvisibleCounterBasedImmediateCacheType;
289-
} else if (!CounterBasedEventEnabled && HostVisible) {
290-
CacheType = HostVisibleCacheType;
292+
if (InterruptBasedEventEnabled) {
293+
if (CounterBasedEventEnabled) {
294+
if (HostVisible) {
295+
if (UsingImmediateCmdList) {
296+
CacheType = HostVisibleInterruptAndCounterBasedImmediateCacheType;
297+
} else {
298+
CacheType = HostVisibleInterruptAndCounterBasedRegularCacheType;
299+
}
300+
} else {
301+
if (UsingImmediateCmdList) {
302+
CacheType = HostInvisibleInterruptAndCounterBasedImmediateCacheType;
303+
} else {
304+
CacheType = HostInvisibleInterruptAndCounterBasedRegularCacheType;
305+
}
306+
}
307+
} else {
308+
if (HostVisible) {
309+
if (UsingImmediateCmdList) {
310+
CacheType = HostVisibleInterruptBasedImmediateCacheType;
311+
} else {
312+
CacheType = HostVisibleInterruptBasedRegularCacheType;
313+
}
314+
} else {
315+
if (UsingImmediateCmdList) {
316+
CacheType = HostInvisibleInterruptBasedImmediateCacheType;
317+
} else {
318+
CacheType = HostInvisibleInterruptBasedRegularCacheType;
319+
}
320+
}
321+
}
291322
} else {
292-
CacheType = HostInvisibleCacheType;
323+
if (CounterBasedEventEnabled && HostVisible && !UsingImmediateCmdList) {
324+
CacheType = HostVisibleCounterBasedRegularCacheType;
325+
} else if (CounterBasedEventEnabled && !HostVisible &&
326+
!UsingImmediateCmdList) {
327+
CacheType = HostInvisibleCounterBasedRegularCacheType;
328+
} else if (CounterBasedEventEnabled && HostVisible &&
329+
UsingImmediateCmdList) {
330+
CacheType = HostVisibleCounterBasedImmediateCacheType;
331+
} else if (CounterBasedEventEnabled && !HostVisible &&
332+
UsingImmediateCmdList) {
333+
CacheType = HostInvisibleCounterBasedImmediateCacheType;
334+
} else if (!CounterBasedEventEnabled && HostVisible) {
335+
CacheType = HostVisibleCacheType;
336+
} else {
337+
CacheType = HostInvisibleCacheType;
338+
}
293339
}
340+
294341
return UR_RESULT_SUCCESS;
295342
}
296343

source/adapters/level_zero/device.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ ur_result_t urDeviceGetInfo(
486486
// TODO: To find out correct value
487487
return ReturnValue("");
488488
case UR_DEVICE_INFO_LOW_POWER_EVENTS_EXP:
489-
return ReturnValue(UR_DEVICE_INFO_LOW_POWER_EVENTS_EXP);
489+
return ReturnValue(static_cast<ur_bool_t>(true));
490490
case UR_DEVICE_INFO_QUEUE_PROPERTIES:
491491
return ReturnValue(
492492
ur_queue_flag_t(UR_QUEUE_FLAG_OUT_OF_ORDER_EXEC_MODE_ENABLE |

source/adapters/level_zero/event.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ ur_result_t urEnqueueEventsWaitWithBarrier(
438438
ur_queue_handle_t, uint32_t, const ur_event_handle_t *,
439439
ur_event_handle_t *, bool)>(EnqueueEventsWaitWithBarrier)(
440440
Queue, NumEventsInWaitList, EventWaitList, OutEvent,
441-
Queue->interruptBasedEventsEnabled());
441+
Queue == nullptr ? false : Queue->InterruptBasedEventsEnabled);
442442
}
443443

444444
ur_result_t urEnqueueEventsWaitWithBarrierExt(
@@ -470,7 +470,7 @@ ur_result_t urEnqueueEventsWaitWithBarrierExt(
470470
ur_queue_handle_t, uint32_t, const ur_event_handle_t *,
471471
ur_event_handle_t *, bool)>(EnqueueEventsWaitWithBarrier)(
472472
Queue, NumEventsInWaitList, EventWaitList, OutEvent,
473-
Queue->interruptBasedEventsEnabled());
473+
Queue ? Queue->InterruptBasedEventsEnabled : false);
474474
}
475475
}
476476

@@ -1342,7 +1342,8 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
13421342
}
13431343

13441344
if (auto CachedEvent = Context->getEventFromContextCache(
1345-
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled)) {
1345+
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled,
1346+
InterruptBasedEventEnabled)) {
13461347
*RetEvent = CachedEvent;
13471348
return UR_RESULT_SUCCESS;
13481349
}
@@ -1355,7 +1356,7 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
13551356
if (auto Res = Context->getFreeSlotInExistingOrNewPool(
13561357
ZeEventPool, Index, HostVisible, ProfilingEnabled, Device,
13571358
CounterBasedEventEnabled, UsingImmediateCommandlists,
1358-
Queue->interruptBasedEventsEnabled()))
1359+
InterruptBasedEventEnabled))
13591360
return Res;
13601361

13611362
ZeStruct<ze_event_desc_t> ZeEventDesc;

source/adapters/level_zero/queue.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,10 +1192,20 @@ ur_queue_handle_t_::ur_queue_handle_t_(
11921192
}
11931193
return std::atoi(UrRet) != 0;
11941194
}();
1195+
static const bool useInterruptBasedEvents = [] {
1196+
const char *UrRet = std::getenv("UR_L0_USE_INTERRUPT_BASED_EVENTS");
1197+
if (!UrRet) {
1198+
return true;
1199+
}
1200+
return std::atoi(UrRet) != 0;
1201+
}();
11951202
this->CounterBasedEventsEnabled =
11961203
UsingImmCmdLists && isInOrderQueue() && Device->useDriverInOrderLists() &&
11971204
useDriverCounterBasedEvents &&
11981205
Device->Platform->ZeDriverEventPoolCountingEventsExtensionFound;
1206+
this->InterruptBasedEventsEnabled = useInterruptBasedEvents &&
1207+
isLowPowerEvents() && isInOrderQueue() &&
1208+
Device->useDriverInOrderLists();
11991209
}
12001210

12011211
void ur_queue_handle_t_::adjustBatchSizeForFullBatch(bool IsCopy) {
@@ -1494,11 +1504,6 @@ bool ur_queue_handle_t_::doReuseDiscardedEvents() {
14941504
return ReuseDiscardedEvents && isInOrderQueue() && isDiscardEvents();
14951505
}
14961506

1497-
bool ur_queue_handle_t_::interruptBasedEventsEnabled() {
1498-
return isInOrderQueue() && Device->useDriverInOrderLists() &&
1499-
isLowPowerEvents();
1500-
}
1501-
15021507
ur_result_t
15031508
ur_queue_handle_t_::resetDiscardedEvent(ur_command_list_ptr_t CommandList) {
15041509
if (LastCommandEvent && LastCommandEvent->IsDiscarded) {
@@ -1877,10 +1882,12 @@ ur_result_t setSignalEvent(ur_queue_handle_t Queue, bool UseCopyEngine,
18771882
// visible pool.
18781883
// \param HostVisible tells if the event must be created in the
18791884
// host-visible pool. If not set then this function will decide.
1880-
ur_result_t createEventAndAssociateQueue(
1881-
ur_queue_handle_t Queue, ur_event_handle_t *Event, ur_command_t CommandType,
1882-
ur_command_list_ptr_t CommandList, bool IsInternal, bool IsMultiDevice,
1883-
std::optional<bool> HostVisible, std::optional<bool> InterruptBasedEvents) {
1885+
ur_result_t createEventAndAssociateQueue(ur_queue_handle_t Queue,
1886+
ur_event_handle_t *Event,
1887+
ur_command_t CommandType,
1888+
ur_command_list_ptr_t CommandList,
1889+
bool IsInternal, bool IsMultiDevice,
1890+
std::optional<bool> HostVisible) {
18841891

18851892
if (!HostVisible.has_value()) {
18861893
// Internal/discarded events do not need host-scope visibility.
@@ -1896,9 +1903,7 @@ ur_result_t createEventAndAssociateQueue(
18961903
UR_CALL(EventCreate(
18971904
Queue->Context, Queue, IsMultiDevice, HostVisible.value(), Event,
18981905
Queue->CounterBasedEventsEnabled, false /*ForceDisableProfiling*/,
1899-
InterruptBasedEvents.has_value()
1900-
? InterruptBasedEvents.value()
1901-
: Queue->interruptBasedEventsEnabled()));
1906+
Queue->InterruptBasedEventsEnabled));
19021907

19031908
(*Event)->UrQueue = Queue;
19041909
(*Event)->CommandType = CommandType;

source/adapters/level_zero/queue.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ struct ur_queue_handle_t_ : _ur_object {
375375
// Keeps track of whether we are using Counter-based Events
376376
bool CounterBasedEventsEnabled = false;
377377

378+
bool InterruptBasedEventsEnabled = false;
379+
378380
// Map of all command lists used in this queue.
379381
ur_command_list_map_t CommandListMap;
380382

@@ -533,8 +535,6 @@ struct ur_queue_handle_t_ : _ur_object {
533535
// queue.
534536
bool doReuseDiscardedEvents();
535537

536-
bool interruptBasedEventsEnabled();
537-
538538
// Append command to provided command list to wait and reset the last event if
539539
// it is discarded and create new ur_event_handle_t wrapper using the same
540540
// native event and put it to the cache. We call this method after each
@@ -713,7 +713,7 @@ struct ur_queue_handle_t_ : _ur_object {
713713
ur_result_t createEventAndAssociateQueue(
714714
ur_queue_handle_t Queue, ur_event_handle_t *Event, ur_command_t CommandType,
715715
ur_command_list_ptr_t CommandList, bool IsInternal, bool IsMultiDevice,
716-
std::optional<bool> HostVisible = std::nullopt, std::optional<bool> InterruptBasedEvents = std::nullopt);
716+
std::optional<bool> HostVisible = std::nullopt);
717717

718718
// This helper function checks to see if an event for a command can be included
719719
// at the end of a command list batch. This will only be true if the event does

0 commit comments

Comments
 (0)