Skip to content

Commit 697d908

Browse files
[L0] fixes cache map insert and add enum to make code more readable
Signed-off-by: Winston Zhang <winston.zhang@intel.com>
1 parent 3e07707 commit 697d908

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

source/adapters/level_zero/context.hpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -311,62 +311,73 @@ struct ur_context_handle_t_ : _ur_object {
311311
ze_context_handle_t getZeHandle() const;
312312

313313
private:
314+
enum EventCacheType {
315+
HostVisibleProfilingCacheType,
316+
HostVisibleRegularCacheType,
317+
HostInvisibleProfilingCacheType,
318+
HostInvisibleRegularCacheType,
319+
CounterBasedRegularCacheType,
320+
CounterBasedImmediateCacheType
321+
};
314322
// Get the cache of events for a provided scope and profiling mode.
315323
auto getEventCache(bool HostVisible, bool WithProfiling,
316324
ur_device_handle_t Device) {
317325
if (HostVisible) {
318326
if (Device) {
319327
auto EventCachesMap =
320-
WithProfiling ? &EventCachesDeviceMap[0] : &EventCachesDeviceMap[1];
328+
WithProfiling ? &EventCachesDeviceMap[HostVisibleProfilingCacheType]
329+
: &EventCachesDeviceMap[HostVisibleRegularCacheType];
321330
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
322331
EventCaches.emplace_back();
323-
EventCachesMap->insert(
324-
std::make_pair(Device, EventCaches.size() - 1));
332+
EventCaches.insert(std::make_pair(Device, EventCaches.size() - 1));
325333
}
326334
return &EventCaches[(*EventCachesMap)[Device]];
327335
} else {
328-
return WithProfiling ? &EventCaches[0] : &EventCaches[1];
336+
return WithProfiling ? &EventCaches[HostVisibleProfilingCacheType]
337+
: &EventCaches[HostVisibleRegularCacheType];
329338
}
330339
} else {
331340
if (Device) {
332341
auto EventCachesMap =
333-
WithProfiling ? &EventCachesDeviceMap[2] : &EventCachesDeviceMap[3];
342+
WithProfiling
343+
? &EventCachesDeviceMap[HostInvisibleProfilingCacheType]
344+
: &EventCachesDeviceMap[HostInvisibleRegularCacheType];
334345
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
335346
EventCaches.emplace_back();
336-
EventCachesMap->insert(
337-
std::make_pair(Device, EventCaches.size() - 1));
347+
EventCaches.insert(std::make_pair(Device, EventCaches.size() - 1));
338348
}
339349
return &EventCaches[(*EventCachesMap)[Device]];
340350
} else {
341-
return WithProfiling ? &EventCaches[2] : &EventCaches[3];
351+
return WithProfiling ? &EventCaches[HostInvisibleProfilingCacheType]
352+
: &EventCaches[HostInvisibleRegularCacheType];
342353
}
343354
}
344355
};
345356
auto getCounterBasedEventCache(bool UsingImmediateCmdList,
346357
ur_device_handle_t Device) {
347358
if (UsingImmediateCmdList) {
348359
if (Device) {
349-
auto EventCachesMap = &EventCachesDeviceMap[4];
360+
auto EventCachesMap =
361+
&EventCachesDeviceMap[CounterBasedImmediateCacheType];
350362
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
351363
EventCaches.emplace_back();
352-
EventCachesMap->insert(
353-
std::make_pair(Device, EventCaches.size() - 1));
364+
EventCaches.insert(std::make_pair(Device, EventCaches.size() - 1));
354365
}
355366
return &EventCaches[(*EventCachesMap)[Device]];
356367
} else {
357-
return &EventCaches[4];
368+
return &EventCaches[CounterBasedImmediateCacheType];
358369
}
359370
} else {
360371
if (Device) {
361-
auto EventCachesMap = &EventCachesDeviceMap[5];
372+
auto EventCachesMap =
373+
&EventCachesDeviceMap[CounterBasedRegularCacheType];
362374
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
363375
EventCaches.emplace_back();
364-
EventCachesMap->insert(
365-
std::make_pair(Device, EventCaches.size() - 1));
376+
EventCaches.insert(std::make_pair(Device, EventCaches.size() - 1));
366377
}
367378
return &EventCaches[(*EventCachesMap)[Device]];
368379
} else {
369-
return &EventCaches[5];
380+
return &EventCaches[CounterBasedRegularCacheType];
370381
}
371382
}
372383
}

source/adapters/level_zero/event.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ urEventRetain(ur_event_handle_t Event ///< [in] handle of the event object
833833
ur_result_t
834834
urEventRelease(ur_event_handle_t Event ///< [in] handle of the event object
835835
) {
836-
if (Event->CounterBasedEventsEnabled && --Event->RefCountExternal == 0) {
836+
if (--Event->RefCountExternal == 0 && Event->CounterBasedEventsEnabled) {
837837
Event->Context->addEventToContextCache(Event);
838838
} else {
839839
UR_CALL(urEventReleaseInternal(Event));

0 commit comments

Comments
 (0)