Skip to content

Commit

Permalink
[L0] fixes cache map insert and add enum to make code more readable
Browse files Browse the repository at this point in the history
Signed-off-by: Winston Zhang <[email protected]>
  • Loading branch information
winstonzhang-intel committed Jul 10, 2024
1 parent caeceb4 commit 1768740
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
43 changes: 27 additions & 16 deletions source/adapters/level_zero/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,62 +304,73 @@ struct ur_context_handle_t_ : _ur_object {
bool isValidDevice(ur_device_handle_t Device) const;

private:
enum EventCacheType {
HostVisibleProfilingCacheType,
HostVisibleRegularCacheType,
HostInvisibleProfilingCacheType,
HostInvisibleRegularCacheType,
CounterBasedRegularCacheType,
CounterBasedImmediateCacheType
};
// Get the cache of events for a provided scope and profiling mode.
auto getEventCache(bool HostVisible, bool WithProfiling,
ur_device_handle_t Device) {
if (HostVisible) {
if (Device) {
auto EventCachesMap =
WithProfiling ? &EventCachesDeviceMap[0] : &EventCachesDeviceMap[1];
WithProfiling ? &EventCachesDeviceMap[HostVisibleProfilingCacheType]
: &EventCachesDeviceMap[HostVisibleRegularCacheType];
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
EventCaches.emplace_back();
EventCachesMap->insert(
std::make_pair(Device, EventCaches.size() - 1));
EventCaches.insert(std::make_pair(Device, EventCaches.size() - 1));
}
return &EventCaches[(*EventCachesMap)[Device]];
} else {
return WithProfiling ? &EventCaches[0] : &EventCaches[1];
return WithProfiling ? &EventCaches[HostVisibleProfilingCacheType]
: &EventCaches[HostVisibleRegularCacheType];
}
} else {
if (Device) {
auto EventCachesMap =
WithProfiling ? &EventCachesDeviceMap[2] : &EventCachesDeviceMap[3];
WithProfiling
? &EventCachesDeviceMap[HostInvisibleProfilingCacheType]
: &EventCachesDeviceMap[HostInvisibleRegularCacheType];
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
EventCaches.emplace_back();
EventCachesMap->insert(
std::make_pair(Device, EventCaches.size() - 1));
EventCaches.insert(std::make_pair(Device, EventCaches.size() - 1));
}
return &EventCaches[(*EventCachesMap)[Device]];
} else {
return WithProfiling ? &EventCaches[2] : &EventCaches[3];
return WithProfiling ? &EventCaches[HostInvisibleProfilingCacheType]
: &EventCaches[HostInvisibleRegularCacheType];
}
}
};
auto getCounterBasedEventCache(bool UsingImmediateCmdList,
ur_device_handle_t Device) {
if (UsingImmediateCmdList) {
if (Device) {
auto EventCachesMap = &EventCachesDeviceMap[4];
auto EventCachesMap =
&EventCachesDeviceMap[CounterBasedImmediateCacheType];
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
EventCaches.emplace_back();
EventCachesMap->insert(
std::make_pair(Device, EventCaches.size() - 1));
EventCaches.insert(std::make_pair(Device, EventCaches.size() - 1));
}
return &EventCaches[(*EventCachesMap)[Device]];
} else {
return &EventCaches[4];
return &EventCaches[CounterBasedImmediateCacheType];
}
} else {
if (Device) {
auto EventCachesMap = &EventCachesDeviceMap[5];
auto EventCachesMap =
&EventCachesDeviceMap[CounterBasedRegularCacheType];
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
EventCaches.emplace_back();
EventCachesMap->insert(
std::make_pair(Device, EventCaches.size() - 1));
EventCaches.insert(std::make_pair(Device, EventCaches.size() - 1));
}
return &EventCaches[(*EventCachesMap)[Device]];
} else {
return &EventCaches[5];
return &EventCaches[CounterBasedRegularCacheType];
}
}
}
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 @@ -872,7 +872,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventRetain(
UR_APIEXPORT ur_result_t UR_APICALL urEventRelease(
ur_event_handle_t Event ///< [in] handle of the event object
) {
if (Event->CounterBasedEventsEnabled && --Event->RefCountExternal == 0) {
if (--Event->RefCountExternal == 0 && Event->CounterBasedEventsEnabled) {
Event->Context->addEventToContextCache(Event);
} else {
UR_CALL(urEventReleaseInternal(Event));
Expand Down

0 comments on commit 1768740

Please sign in to comment.