Skip to content

Commit 3dcd73f

Browse files
[L0] Further simplified getZeEventPoolCache
Signed-off-by: Zhang, Winston <[email protected]>
1 parent ce6afb4 commit 3dcd73f

File tree

2 files changed

+53
-26
lines changed

2 files changed

+53
-26
lines changed

source/adapters/level_zero/context.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,15 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
500500
std::scoped_lock<ur_mutex> Lock(ZeEventPoolCacheMutex);
501501

502502
ze_device_handle_t ZeDevice = nullptr;
503+
size_t DeviceId;
503504

504505
if (Device) {
505506
ZeDevice = Device->ZeDevice;
507+
DeviceId =
508+
Device->Id.has_value() ? static_cast<size_t>(Device->Id.value()) : 0;
506509
}
507510
std::list<ze_event_pool_handle_t> *ZePoolCache =
508-
getZeEventPoolCache(Flags, ZeDevice);
511+
getZeEventPoolCache(Flags, ZeDevice, DeviceId);
509512

510513
if (!ZePoolCache->empty()) {
511514
if (NumEventsAvailableInEventPool[ZePoolCache->front()] == 0) {
@@ -657,17 +660,30 @@ ur_context_handle_t_::decrementUnreleasedEventsInPool(ur_event_handle_t Event) {
657660
}
658661

659662
ze_device_handle_t ZeDevice = nullptr;
663+
size_t DeviceId;
664+
660665
bool UsingImmediateCommandlists =
661666
!Event->UrQueue || Event->UrQueue->UsingImmCmdLists;
662667

663668
if (!Event->IsMultiDevice && Event->UrQueue) {
664669
ZeDevice = Event->UrQueue->Device->ZeDevice;
670+
DeviceId = Event->UrQueue->Device->Id.has_value()
671+
? static_cast<size_t>(Event->UrQueue->Device->Id.value())
672+
: 0;
665673
}
674+
v2::event_flags_t Flags = 0;
666675
if (UsingImmediateCommandlists)
667-
Event->Flags |= v2::EVENT_FLAGS_IMM_CMDLIST;
668-
676+
Flags |= v2::EVENT_FLAGS_IMM_CMDLIST;
677+
if (Event->isHostVisible())
678+
Flags |= v2::EVENT_FLAGS_HOST_VISIBLE;
679+
if (Event->isProfilingEnabled())
680+
Flags |= v2::EVENT_FLAGS_PROFILING_ENABLED;
681+
if (Event->CounterBasedEventsEnabled)
682+
Flags |= v2::EVENT_FLAGS_COUNTER;
683+
if (Event->InterruptBasedEventsEnabled)
684+
Flags |= v2::EVENT_FLAGS_INTERRUPT;
669685
std::list<ze_event_pool_handle_t> *ZePoolCache =
670-
getZeEventPoolCache(Event->Flags, ZeDevice);
686+
getZeEventPoolCache(Flags, ZeDevice, DeviceId);
671687

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

source/adapters/level_zero/context.hpp

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,6 @@ struct ur_context_handle_t_ : _ur_object {
171171
// Cache of event pools to which host-visible events are added to.
172172
using ZeEventPoolCache = std::list<ze_event_pool_handle_t>;
173173
std::vector<ZeEventPoolCache> ZeEventPoolCaches;
174-
using ZeEventPoolCacheDeviceMap =
175-
std::unordered_map<ze_device_handle_t, size_t>;
176-
std::vector<ZeEventPoolCacheDeviceMap> ZeEventPoolCachesDeviceMap;
177-
// std::vector<std::list<ze_event_pool_handle_t>> ZeEventPoolCache{30};
178-
// std::vector<std::unordered_map<ze_device_handle_t, size_t>>
179-
// ZeEventPoolCacheDeviceMap{30};
180174

181175
// This map will be used to determine if a pool is full or not
182176
// by storing number of empty slots available in the pool.
@@ -230,27 +224,44 @@ struct ur_context_handle_t_ : _ur_object {
230224
void addEventToContextCache(ur_event_handle_t);
231225

232226
std::list<ze_event_pool_handle_t> *
233-
getZeEventPoolCache(v2::event_flags_t Flags, ze_device_handle_t ZeDevice) {
227+
getZeEventPoolCache(v2::event_flags_t Flags, ze_device_handle_t ZeDevice,
228+
size_t DeviceId) {
234229
size_t index = 0;
235-
index |= Flags;
236-
bool WithProfiling = Flags & v2::EVENT_FLAGS_PROFILING_ENABLED;
237-
230+
index |= uint64_t(Flags);
238231
if (ZeDevice) {
239-
auto ZeEventPoolCacheMap =
240-
WithProfiling ? &ZeEventPoolCachesDeviceMap[index * 2]
241-
: &ZeEventPoolCachesDeviceMap[index * 2 + 1];
242-
if (ZeEventPoolCacheMap->find(ZeDevice) == ZeEventPoolCacheMap->end()) {
243-
ZeEventPoolCaches.emplace_back();
244-
ZeEventPoolCacheMap->insert(
245-
std::make_pair(ZeDevice, ZeEventPoolCaches.size() - 1));
246-
}
247-
return &ZeEventPoolCaches[(*ZeEventPoolCacheMap)[ZeDevice]];
248-
} else {
249-
return WithProfiling ? &ZeEventPoolCaches[index * 2]
250-
: &ZeEventPoolCaches[index * 2 + 1];
232+
index |= v2::EVENT_FLAGS_DEVICE | (DeviceId << v2::MAX_EVENT_FLAG_BITS);
233+
}
234+
235+
if (index >= ZeEventPoolCaches.size()) {
236+
ZeEventPoolCaches.resize(index + 1);
251237
}
238+
return &ZeEventPoolCaches[index];
252239
}
253240

241+
/*
242+
std::list<ze_event_pool_handle_t> *
243+
getZeEventPoolCache(v2::event_flags_t Flags, ze_device_handle_t ZeDevice) {
244+
size_t index = 0;
245+
index |= Flags;
246+
bool WithProfiling = Flags & v2::EVENT_FLAGS_PROFILING_ENABLED;
247+
248+
if (ZeDevice) {
249+
auto ZeEventPoolCacheMap =
250+
WithProfiling ? &ZeEventPoolCachesDeviceMap[index * 2]
251+
: &ZeEventPoolCachesDeviceMap[index * 2 + 1];
252+
if (ZeEventPoolCacheMap->find(ZeDevice) == ZeEventPoolCacheMap->end()) {
253+
ZeEventPoolCaches.emplace_back();
254+
ZeEventPoolCacheMap->insert(
255+
std::make_pair(ZeDevice, ZeEventPoolCaches.size() - 1));
256+
}
257+
return &ZeEventPoolCaches[(*ZeEventPoolCacheMap)[ZeDevice]];
258+
} else {
259+
return WithProfiling ? &ZeEventPoolCaches[index * 2]
260+
: &ZeEventPoolCaches[index * 2 + 1];
261+
}
262+
}
263+
*/
264+
254265
// Decrement number of events living in the pool upon event destroy
255266
// and return the pool to the cache if there are no unreleased events.
256267
ur_result_t decrementUnreleasedEventsInPool(ur_event_handle_t Event);

0 commit comments

Comments
 (0)