Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 99 additions & 6 deletions unified-runtime/source/adapters/level_zero/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,15 +406,89 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
ze_event_pool_handle_t &Pool, size_t &Index, bool HostVisible,
bool ProfilingEnabled, ur_device_handle_t Device,
bool CounterBasedEventEnabled, bool UsingImmCmdList,
bool InterruptBasedEventEnabled) {
// Lock while updating event pool machinery.
std::scoped_lock<ur_mutex> Lock(ZeEventPoolCacheMutex);
bool InterruptBasedEventEnabled, ur_queue_handle_t Queue, bool IsInternal) {

ze_device_handle_t ZeDevice = nullptr;

if (Device) {
ZeDevice = Device->ZeDevice;
}

if (DisableEventsCaching) {
// Skip all cache handling, always create a new pool
ze_event_pool_counter_based_exp_desc_t counterBasedExt = {
ZE_STRUCTURE_TYPE_COUNTER_BASED_EVENT_POOL_EXP_DESC, nullptr, 0};

ze_intel_event_sync_mode_exp_desc_t eventSyncMode = {
ZE_INTEL_STRUCTURE_TYPE_EVENT_SYNC_MODE_EXP_DESC, nullptr, 0};
eventSyncMode.syncModeFlags =
ZE_INTEL_EVENT_SYNC_MODE_EXP_FLAG_LOW_POWER_WAIT |
ZE_INTEL_EVENT_SYNC_MODE_EXP_FLAG_SIGNAL_INTERRUPT;

ZeStruct<ze_event_pool_desc_t> ZeEventPoolDesc;
ZeEventPoolDesc.count = MaxNumEventsPerPool;
ZeEventPoolDesc.flags = 0;
ZeEventPoolDesc.pNext = nullptr;
if (HostVisible)
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
if (ProfilingEnabled)
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
UR_LOG(DEBUG, "ze_event_pool_desc_t flags set to: {}",
ZeEventPoolDesc.flags);
if (CounterBasedEventEnabled) {
if (UsingImmCmdList) {
counterBasedExt.flags = ZE_EVENT_POOL_COUNTER_BASED_EXP_FLAG_IMMEDIATE;
} else {
counterBasedExt.flags =
ZE_EVENT_POOL_COUNTER_BASED_EXP_FLAG_NON_IMMEDIATE;
}
UR_LOG(DEBUG, "ze_event_pool_desc_t counter based flags set to: {}",
counterBasedExt.flags);
if (InterruptBasedEventEnabled) {
counterBasedExt.pNext = &eventSyncMode;
}
ZeEventPoolDesc.pNext = &counterBasedExt;
} else if (InterruptBasedEventEnabled) {
ZeEventPoolDesc.pNext = &eventSyncMode;
}

std::vector<ze_device_handle_t> ZeDevices;
if (ZeDevice) {
ZeDevices.push_back(ZeDevice);
} else {
std::for_each(Devices.begin(), Devices.end(),
[&](const ur_device_handle_t &D) {
ZeDevices.push_back(D->ZeDevice);
});
}

ze_result_t Result = ZE_CALL_NOCHECK(
zeEventPoolCreate,
(ZeContext, &ZeEventPoolDesc, ZeDevices.size(), &ZeDevices[0], &Pool));
if (IsInternal && ze2urResult(Result) == UR_RESULT_ERROR_OUT_OF_RESOURCES &&
Queue) {
if (!Queue->isInOrderQueue()) {
if (Queue->UsingImmCmdLists) {
UR_CALL(CleanupEventsInImmCmdLists(Queue, true /*QueueLocked*/,
false /*QueueSynced*/,
nullptr /*CompletedEvent*/));
} else {
UR_CALL(resetCommandLists(Queue));
}
ZE2UR_CALL(zeEventPoolCreate, (ZeContext, &ZeEventPoolDesc,
ZeDevices.size(), &ZeDevices[0], &Pool));
}
} else if (ze2urResult(Result) != UR_RESULT_SUCCESS) {
return ze2urResult(Result);
}
Index = 0;
NumEventsAvailableInEventPool[Pool] = MaxNumEventsPerPool - 1;
NumEventsUnreleasedInEventPool[Pool] = 1;
return UR_RESULT_SUCCESS;
}

// --- Normal cache-based logic below ---
std::scoped_lock<ur_mutex> Lock(ZeEventPoolCacheMutex);

std::list<ze_event_pool_handle_t> *ZePoolCache = getZeEventPoolCache(
HostVisible, ProfilingEnabled, CounterBasedEventEnabled, UsingImmCmdList,
InterruptBasedEventEnabled, ZeDevice);
Expand All @@ -423,6 +497,7 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
if (NumEventsAvailableInEventPool[ZePoolCache->front()] == 0) {
if (DisableEventsCaching) {
// Remove full pool from the cache if events caching is disabled.
ZE_CALL_NOCHECK(zeEventPoolDestroy, (ZePoolCache->front()));
ZePoolCache->erase(ZePoolCache->begin());
} else {
// If event caching is enabled then we don't destroy events so there is
Expand Down Expand Up @@ -488,8 +563,26 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
});
}

ZE2UR_CALL(zeEventPoolCreate, (ZeContext, &ZeEventPoolDesc,
ZeDevices.size(), &ZeDevices[0], ZePool));
ze_result_t Result = ZE_CALL_NOCHECK(
zeEventPoolCreate,
(ZeContext, &ZeEventPoolDesc, ZeDevices.size(), &ZeDevices[0], ZePool));
if (IsInternal && ze2urResult(Result) == UR_RESULT_ERROR_OUT_OF_RESOURCES &&
Queue) {
if (!Queue->isInOrderQueue()) {
if (Queue->UsingImmCmdLists) {
UR_CALL(CleanupEventsInImmCmdLists(Queue, true /*QueueLocked*/,
false /*QueueSynced*/,
nullptr /*CompletedEvent*/));
} else {
UR_CALL(resetCommandLists(Queue));
}
ZE2UR_CALL(zeEventPoolCreate,
(ZeContext, &ZeEventPoolDesc, ZeDevices.size(),
&ZeDevices[0], ZePool));
}
} else if (ze2urResult(Result) != UR_RESULT_SUCCESS) {
return ze2urResult(Result);
}
NumEventsAvailableInEventPool[*ZePool] = MaxNumEventsPerPool - 1;
NumEventsUnreleasedInEventPool[*ZePool] = 1;
} else {
Expand Down
13 changes: 6 additions & 7 deletions unified-runtime/source/adapters/level_zero/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,12 @@ struct ur_context_handle_t_ : ur_object {
// pool then create new one. The HostVisible parameter tells if we need a
// slot for a host-visible event. The ProfilingEnabled tells is we need a
// slot for an event with profiling capabilities.
ur_result_t getFreeSlotInExistingOrNewPool(ze_event_pool_handle_t &, size_t &,
bool HostVisible,
bool ProfilingEnabled,
ur_device_handle_t Device,
bool CounterBasedEventEnabled,
bool UsingImmCmdList,
bool InterruptBasedEventEnabled);
ur_result_t getFreeSlotInExistingOrNewPool(
ze_event_pool_handle_t &, size_t &, bool HostVisible,
bool ProfilingEnabled, ur_device_handle_t Device,
bool CounterBasedEventEnabled, bool UsingImmCmdList,
bool InterruptBasedEventEnabled, ur_queue_handle_t Queue,
bool IsInternal);

// Get ur_event_handle_t from cache.
ur_event_handle_t getEventFromContextCache(bool HostVisible,
Expand Down
16 changes: 9 additions & 7 deletions unified-runtime/source/adapters/level_zero/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1349,16 +1349,18 @@ ur_result_t CleanupCompletedEvent(ur_event_handle_t Event, bool QueueLocked,
// The "HostVisible" argument specifies if event needs to be allocated from
// a host-visible pool.
//
ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
bool IsMultiDevice, bool HostVisible,
ur_event_handle_t *RetEvent,
bool CounterBasedEventEnabled,
bool ForceDisableProfiling,
bool InterruptBasedEventEnabled) {
ur_result_t
EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
bool IsMultiDevice, bool HostVisible, ur_event_handle_t *RetEvent,
bool CounterBasedEventEnabled, bool ForceDisableProfiling,
bool InterruptBasedEventEnabled, std::optional<bool> IsInternal) {
bool ProfilingEnabled =
ForceDisableProfiling ? false : (!Queue || Queue->isProfilingEnabled());
bool UsingImmediateCommandlists = !Queue || Queue->UsingImmCmdLists;

// Handle optional IsInternal parameter - default to false if not provided
bool isInternalValue = IsInternal.value_or(false);

ur_device_handle_t Device = nullptr;

if (!IsMultiDevice && Queue) {
Expand All @@ -1380,7 +1382,7 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
if (auto Res = Context->getFreeSlotInExistingOrNewPool(
ZeEventPool, Index, HostVisible, ProfilingEnabled, Device,
CounterBasedEventEnabled, UsingImmediateCommandlists,
InterruptBasedEventEnabled))
InterruptBasedEventEnabled, Queue, isInternalValue))
return Res;

ZeStruct<ze_event_desc_t> ZeEventDesc;
Expand Down
3 changes: 2 additions & 1 deletion unified-runtime/source/adapters/level_zero/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
ur_event_handle_t *RetEvent,
bool CounterBasedEventEnabled,
bool ForceDisableProfiling,
bool InterruptBasedEventEnabled);
bool InterruptBasedEventEnabled,
std::optional<bool> IsInternal = std::nullopt);
} // extern "C"

// This is an experimental option that allows to disable caching of events in
Expand Down
2 changes: 1 addition & 1 deletion unified-runtime/source/adapters/level_zero/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,7 @@ ur_result_t createEventAndAssociateQueue(ur_queue_handle_t Queue,
UR_CALL(EventCreate(
Queue->Context, Queue, IsMultiDevice, HostVisible.value(), Event,
Queue->CounterBasedEventsEnabled, false /*ForceDisableProfiling*/,
Queue->InterruptBasedEventsEnabled));
Queue->InterruptBasedEventsEnabled, IsInternal));

(*Event)->UrQueue = Queue;
(*Event)->CommandType = CommandType;
Expand Down
Loading