Skip to content

Commit 854cb87

Browse files
[L0] Phase 2 of Counter-Based Event Implementation
-enable counter-based events for regular commandlist -counter-based events may be reused even though they are not done -when ref count goes to not used by external clients value it means that event may be reused by subsequent calls -move events that are no longer externally visible to re-usable pool and reuse those more aggressively Signed-off-by: Winston Zhang <winston.zhang@intel.com>
1 parent 4517290 commit 854cb87

File tree

4 files changed

+62
-21
lines changed

4 files changed

+62
-21
lines changed

source/adapters/level_zero/context.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,12 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
560560

561561
ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
562562
bool HostVisible, bool WithProfiling, ur_device_handle_t Device,
563-
bool CounterBasedEventEnabled) {
563+
bool CounterBasedEventEnabled, bool UsingImmCmdList) {
564564
std::scoped_lock<ur_mutex> Lock(EventCacheMutex);
565565
auto Cache = getEventCache(HostVisible, WithProfiling, Device);
566+
if (CounterBasedEventEnabled) {
567+
Cache = getCounterBasedEventCache(UsingImmCmdList, Device);
568+
}
566569
if (Cache->empty())
567570
return nullptr;
568571

@@ -573,7 +576,8 @@ ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
573576
}
574577
Cache->erase(It);
575578
// We have to reset event before using it.
576-
Event->reset();
579+
if (!CounterBasedEventEnabled)
580+
Event->reset();
577581
return Event;
578582
}
579583

@@ -585,9 +589,15 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
585589
Device = Event->UrQueue->Device;
586590
}
587591

588-
auto Cache = getEventCache(Event->isHostVisible(),
589-
Event->isProfilingEnabled(), Device);
590-
Cache->emplace_back(Event);
592+
if (!Event->CounterBasedEventsEnabled) {
593+
auto Cache = getCounterBasedEventCache(
594+
!Event->UrQueue || Event->UrQueue->UsingImmCmdLists, Device);
595+
Cache->emplace_back(Event);
596+
} else {
597+
auto Cache = getEventCache(Event->isHostVisible(),
598+
Event->isProfilingEnabled(), Device);
599+
Cache->emplace_back(Event);
600+
}
591601
}
592602

593603
ur_result_t

source/adapters/level_zero/context.hpp

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ struct ur_context_handle_t_ : _ur_object {
171171

172172
// Caches for events.
173173
using EventCache = std::vector<std::list<ur_event_handle_t>>;
174-
EventCache EventCaches{4};
174+
EventCache EventCaches{6};
175175
std::vector<std::unordered_map<ur_device_handle_t, size_t>>
176-
EventCachesDeviceMap{4};
176+
EventCachesDeviceMap{6};
177177

178178
// Initialize the PI context.
179179
ur_result_t initialize();
@@ -208,10 +208,10 @@ struct ur_context_handle_t_ : _ur_object {
208208
bool UsingImmCmdList);
209209

210210
// Get ur_event_handle_t from cache.
211-
ur_event_handle_t getEventFromContextCache(bool HostVisible,
212-
bool WithProfiling,
213-
ur_device_handle_t Device,
214-
bool CounterBasedEventEnabled);
211+
ur_event_handle_t
212+
getEventFromContextCache(bool HostVisible, bool WithProfiling,
213+
ur_device_handle_t Device,
214+
bool CounterBasedEventEnabled bool UsingImmCmdList);
215215

216216
// Add ur_event_handle_t to cache.
217217
void addEventToContextCache(ur_event_handle_t);
@@ -341,9 +341,36 @@ struct ur_context_handle_t_ : _ur_object {
341341
}
342342
}
343343
}
344-
};
344+
auto getCounterBasedEventCache(bool UsingImmediateCmdList,
345+
ur_device_handle_t Device) {
346+
if (UsingImmediateCmdList) {
347+
if (Device) {
348+
auto EventCachesMap = &EventCachesDeviceMap[4];
349+
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
350+
EventCaches.emplace_back();
351+
EventCachesMap->insert(
352+
std::make_pair(Device, EventCaches.size() - 1));
353+
}
354+
return &EventCaches[(*EventCachesMap)[Device]];
355+
} else {
356+
return &EventCaches[4];
357+
}
358+
} else {
359+
if (Device) {
360+
auto EventCachesMap = &EventCachesDeviceMap[5];
361+
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
362+
EventCaches.emplace_back();
363+
EventCachesMap->insert(
364+
std::make_pair(Device, EventCaches.size() - 1));
365+
}
366+
return &EventCaches[(*EventCachesMap)[Device]];
367+
} else {
368+
return &EventCaches[5];
369+
}
370+
}
371+
};
345372

346-
// Helper function to release the context, a caller must lock the platform-level
347-
// mutex guarding the container with contexts because the context can be removed
348-
// from the list of tracked contexts.
349-
ur_result_t ContextReleaseHelper(ur_context_handle_t Context);
373+
// Helper function to release the context, a caller must lock the
374+
// platform-level mutex guarding the container with contexts because the
375+
// context can be removed from the list of tracked contexts.
376+
ur_result_t ContextReleaseHelper(ur_context_handle_t Context);

source/adapters/level_zero/event.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -834,8 +834,11 @@ ur_result_t
834834
urEventRelease(ur_event_handle_t Event ///< [in] handle of the event object
835835
) {
836836
Event->RefCountExternal--;
837-
UR_CALL(urEventReleaseInternal(Event));
838-
837+
if (Event->CounterBasedEventsEnabled && Event->RefCountExternal == 0) {
838+
Event->Context->addEventToContextCache(Event);
839+
} else {
840+
UR_CALL(urEventReleaseInternal(Event));
841+
}
839842
return UR_RESULT_SUCCESS;
840843
}
841844

@@ -995,7 +998,7 @@ ur_result_t ur_event_handle_t_::getOrCreateHostVisibleEvent(
995998
}
996999

9971000
ur_result_t urEventReleaseInternal(ur_event_handle_t Event) {
998-
if (!Event->RefCount.decrementAndTest())
1001+
if (!Event->CounterBasedEventsEnabled && !Event->RefCount.decrementAndTest())
9991002
return UR_RESULT_SUCCESS;
10001003

10011004
if (Event->CommandType == UR_COMMAND_MEM_UNMAP && Event->CommandData) {
@@ -1257,7 +1260,8 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
12571260
}
12581261

12591262
if (auto CachedEvent = Context->getEventFromContextCache(
1260-
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled)) {
1263+
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled,
1264+
UsingImmediateCommandlists)) {
12611265
*RetEvent = CachedEvent;
12621266
return UR_RESULT_SUCCESS;
12631267
}

source/adapters/level_zero/queue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ ur_queue_handle_t_::ur_queue_handle_t_(
11871187
return std::atoi(UrRet) != 0;
11881188
}();
11891189
this->CounterBasedEventsEnabled =
1190-
UsingImmCmdLists && isInOrderQueue() && Device->useDriverInOrderLists() &&
1190+
isInOrderQueue() && Device->useDriverInOrderLists() &&
11911191
useDriverCounterBasedEvents &&
11921192
Device->Platform->ZeDriverEventPoolCountingEventsExtensionFound;
11931193
}

0 commit comments

Comments
 (0)