Skip to content

Commit

Permalink
[L0] Fix counting event usage in the cache and discard event use cases
Browse files Browse the repository at this point in the history
Signed-off-by: Neil R. Spruit <neil.r.spruit@intel.com>
  • Loading branch information
nrspruit committed Mar 29, 2024
1 parent 6a817d3 commit f6c1083
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
6 changes: 5 additions & 1 deletion source/adapters/level_zero/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,14 +550,18 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
}

ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
bool HostVisible, bool WithProfiling, ur_device_handle_t Device) {
bool HostVisible, bool WithProfiling, ur_device_handle_t Device,
bool CounterBasedEventEnabled) {
std::scoped_lock<ur_mutex> Lock(EventCacheMutex);
auto Cache = getEventCache(HostVisible, WithProfiling, Device);
if (Cache->empty())
return nullptr;

auto It = Cache->begin();
ur_event_handle_t Event = *It;
if (Event->CounterBasedEventsEnabled != CounterBasedEventEnabled) {
return nullptr;
}
Cache->erase(It);
// We have to reset event before using it.
Event->reset();
Expand Down
3 changes: 2 additions & 1 deletion source/adapters/level_zero/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ struct ur_context_handle_t_ : _ur_object {
// Get ur_event_handle_t from cache.
ur_event_handle_t getEventFromContextCache(bool HostVisible,
bool WithProfiling,
ur_device_handle_t Device);
ur_device_handle_t Device,
bool CounterBasedEventEnabled);

// Add ur_event_handle_t to cache.
void addEventToContextCache(ur_event_handle_t);
Expand Down
2 changes: 1 addition & 1 deletion source/adapters/level_zero/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
}

if (auto CachedEvent = Context->getEventFromContextCache(
HostVisible, ProfilingEnabled, Device)) {
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled)) {
*RetEvent = CachedEvent;
return UR_RESULT_SUCCESS;
}
Expand Down
15 changes: 7 additions & 8 deletions source/adapters/level_zero/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,12 +1247,13 @@ bool ur_queue_handle_t_::doReuseDiscardedEvents() {

ur_result_t
ur_queue_handle_t_::resetDiscardedEvent(ur_command_list_ptr_t CommandList) {
if (!CounterBasedEventsEnabled && LastCommandEvent &&
LastCommandEvent->IsDiscarded) {
if (LastCommandEvent && LastCommandEvent->IsDiscarded) {
ZE2UR_CALL(zeCommandListAppendBarrier,
(CommandList->first, nullptr, 1, &(LastCommandEvent->ZeEvent)));
ZE2UR_CALL(zeCommandListAppendEventReset,
(CommandList->first, LastCommandEvent->ZeEvent));
if (!CounterBasedEventsEnabled) {
ZE2UR_CALL(zeCommandListAppendEventReset,
(CommandList->first, LastCommandEvent->ZeEvent));
}

// Create new ur_event_handle_t but with the same ze_event_handle_t. We are
// going to use this ur_event_handle_t for the next command with discarded
Expand Down Expand Up @@ -1896,10 +1897,8 @@ ur_result_t ur_queue_handle_t_::createCommandList(
std::tie(CommandList, std::ignore) = CommandListMap.insert(
std::pair<ze_command_list_handle_t, ur_command_list_info_t>(
ZeCommandList, {ZeFence, false, false, ZeCommandQueue, ZeQueueDesc}));
if (!CounterBasedEventsEnabled) {
UR_CALL(insertStartBarrierIfDiscardEventsMode(CommandList));
UR_CALL(insertActiveBarriers(CommandList, UseCopyEngine));
}
UR_CALL(insertStartBarrierIfDiscardEventsMode(CommandList));
UR_CALL(insertActiveBarriers(CommandList, UseCopyEngine));
return UR_RESULT_SUCCESS;
}

Expand Down

0 comments on commit f6c1083

Please sign in to comment.