Skip to content

Commit 0f1de75

Browse files
[L0] further simplify context.hpp
Signed-off-by: Zhang, Winston <[email protected]>
1 parent ae35020 commit 0f1de75

File tree

2 files changed

+19
-97
lines changed

2 files changed

+19
-97
lines changed

source/adapters/level_zero/context.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ ur_result_t ur_context_handle_t_::finalize() {
432432
}
433433
{
434434
std::scoped_lock<ur_mutex> Lock(ZeEventPoolCacheMutex);
435-
for (auto &ZePoolCache : ZeEventPoolCache) {
435+
for (auto &ZePoolCache : ZeEventPoolCaches) {
436436
for (auto &ZePool : ZePoolCache) {
437437
auto ZeResult = ZE_CALL_NOCHECK(zeEventPoolDestroy, (ZePool));
438438
// Gracefully handle the case that L0 was already unloaded.

source/adapters/level_zero/context.hpp

+18-96
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,14 @@ 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{30};
172-
std::vector<std::unordered_map<ze_device_handle_t, size_t>>
173-
ZeEventPoolCacheDeviceMap{30};
171+
using ZeEventPoolCache = std::list<ze_event_pool_handle_t>;
172+
std::vector<ZeEventPoolCache> ZeEventPoolCaches;
173+
using ZeEventPoolCacheDeviceMap =
174+
std::unordered_map<ze_device_handle_t, size_t>;
175+
std::vector<ZeEventPoolCacheDeviceMap> ZeEventPoolCachesDeviceMap;
176+
// std::vector<std::list<ze_event_pool_handle_t>> ZeEventPoolCache{30};
177+
// std::vector<std::unordered_map<ze_device_handle_t, size_t>>
178+
// ZeEventPoolCacheDeviceMap{30};
174179

175180
// This map will be used to determine if a pool is full or not
176181
// by storing number of empty slots available in the pool.
@@ -223,99 +228,28 @@ struct ur_context_handle_t_ : _ur_object {
223228
// Add ur_event_handle_t to cache.
224229
void addEventToContextCache(ur_event_handle_t);
225230

226-
enum EventPoolCacheType {
227-
HostVisibleCacheType,
228-
HostInvisibleCacheType,
229-
HostVisibleCounterBasedRegularCacheType,
230-
HostInvisibleCounterBasedRegularCacheType,
231-
HostVisibleCounterBasedImmediateCacheType,
232-
HostInvisibleCounterBasedImmediateCacheType,
233-
234-
HostVisibleInterruptBasedRegularCacheType,
235-
HostInvisibleInterruptBasedRegularCacheType,
236-
HostVisibleInterruptBasedImmediateCacheType,
237-
HostInvisibleInterruptBasedImmediateCacheType,
238-
239-
HostVisibleInterruptAndCounterBasedRegularCacheType,
240-
HostInvisibleInterruptAndCounterBasedRegularCacheType,
241-
HostVisibleInterruptAndCounterBasedImmediateCacheType,
242-
HostInvisibleInterruptAndCounterBasedImmediateCacheType
243-
};
244-
245231
std::list<ze_event_pool_handle_t> *
246232
getZeEventPoolCache(ur_event_flags_t Flags, ze_device_handle_t ZeDevice) {
247-
EventPoolCacheType CacheType;
233+
size_t index = 0;
234+
index |= Flags;
248235
bool WithProfiling = Flags & EVENT_FLAG_WITH_PROFILING;
249236

250-
calculateCacheIndex(Flags, CacheType);
251237
if (ZeDevice) {
252238
auto ZeEventPoolCacheMap =
253-
WithProfiling ? &ZeEventPoolCacheDeviceMap[CacheType * 2]
254-
: &ZeEventPoolCacheDeviceMap[CacheType * 2 + 1];
239+
WithProfiling ? &ZeEventPoolCachesDeviceMap[index * 2]
240+
: &ZeEventPoolCachesDeviceMap[index * 2 + 1];
255241
if (ZeEventPoolCacheMap->find(ZeDevice) == ZeEventPoolCacheMap->end()) {
256-
ZeEventPoolCache.emplace_back();
242+
ZeEventPoolCaches.emplace_back();
257243
ZeEventPoolCacheMap->insert(
258-
std::make_pair(ZeDevice, ZeEventPoolCache.size() - 1));
244+
std::make_pair(ZeDevice, ZeEventPoolCaches.size() - 1));
259245
}
260-
return &ZeEventPoolCache[(*ZeEventPoolCacheMap)[ZeDevice]];
246+
return &ZeEventPoolCaches[(*ZeEventPoolCacheMap)[ZeDevice]];
261247
} else {
262-
return WithProfiling ? &ZeEventPoolCache[CacheType * 2]
263-
: &ZeEventPoolCache[CacheType * 2 + 1];
248+
return WithProfiling ? &ZeEventPoolCaches[index * 2]
249+
: &ZeEventPoolCaches[index * 2 + 1];
264250
}
265251
}
266252

267-
ur_result_t calculateCacheIndex(ur_event_flags_t Flags,
268-
EventPoolCacheType &CacheType) {
269-
bool InterruptBasedEventEnabled = Flags & EVENT_FLAG_INTERRUPT;
270-
bool CounterBasedEventEnabled = Flags & EVENT_FLAG_COUNTER;
271-
bool HostVisible = Flags & EVENT_FLAG_HOST_VISIBLE;
272-
bool UsingImmediateCmdList = Flags & EVENT_FLAG_IMM_CMDLIST;
273-
274-
if (InterruptBasedEventEnabled) {
275-
if (CounterBasedEventEnabled) {
276-
if (HostVisible) {
277-
CacheType =
278-
UsingImmediateCmdList
279-
? HostVisibleInterruptAndCounterBasedImmediateCacheType
280-
: HostVisibleInterruptAndCounterBasedRegularCacheType;
281-
} else {
282-
CacheType =
283-
UsingImmediateCmdList
284-
? HostInvisibleInterruptAndCounterBasedImmediateCacheType
285-
: HostInvisibleInterruptAndCounterBasedRegularCacheType;
286-
}
287-
} else {
288-
if (HostVisible) {
289-
CacheType = UsingImmediateCmdList
290-
? HostVisibleInterruptBasedImmediateCacheType
291-
: HostVisibleInterruptBasedRegularCacheType;
292-
} else {
293-
CacheType = UsingImmediateCmdList
294-
? HostInvisibleInterruptBasedImmediateCacheType
295-
: HostInvisibleInterruptBasedRegularCacheType;
296-
}
297-
}
298-
} else {
299-
if (CounterBasedEventEnabled) {
300-
if (HostVisible) {
301-
CacheType = UsingImmediateCmdList
302-
? HostVisibleCounterBasedImmediateCacheType
303-
: HostVisibleCounterBasedRegularCacheType;
304-
} else {
305-
CacheType = UsingImmediateCmdList
306-
? HostInvisibleCounterBasedImmediateCacheType
307-
: HostInvisibleCounterBasedRegularCacheType;
308-
}
309-
} else {
310-
CacheType = HostVisible ? HostVisibleCacheType : HostInvisibleCacheType;
311-
}
312-
}
313-
314-
return UR_RESULT_SUCCESS;
315-
316-
return UR_RESULT_SUCCESS;
317-
}
318-
319253
// Decrement number of events living in the pool upon event destroy
320254
// and return the pool to the cache if there are no unreleased events.
321255
ur_result_t decrementUnreleasedEventsInPool(ur_event_handle_t Event);
@@ -365,22 +299,10 @@ struct ur_context_handle_t_ : _ur_object {
365299
EventCache *getEventCache(ur_event_flags_t Flags, ur_device_handle_t Device) {
366300

367301
size_t index = 0;
368-
// if (HostVisible) {
369-
// index |= EVENT_FLAG_HOST_VISIBLE;
370-
//}
371-
// if (WithProfiling) {
372-
// index |= EVENT_FLAG_WITH_PROFILING;
373-
//}
374-
// if (Counter) {
375-
// index |= EVENT_FLAG_COUNTER;
376-
//}
377-
// if (Interrupt) {
378-
// index |= EVENT_FLAG_INTERRUPT;
379-
//}
302+
index |= Flags;
380303
if (Device) {
381304
index |= EVENT_FLAG_DEVICE | (*Device->Id << MAX_EVENT_FLAG_BITS);
382305
}
383-
index |= Flags;
384306

385307
if (index >= EventCaches.size()) {
386308
EventCaches.resize(index + 1);

0 commit comments

Comments
 (0)