diff --git a/source/adapters/level_zero/context.cpp b/source/adapters/level_zero/context.cpp index 910f5e8071..296e3e98d5 100644 --- a/source/adapters/level_zero/context.cpp +++ b/source/adapters/level_zero/context.cpp @@ -560,12 +560,9 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool( ur_event_handle_t ur_context_handle_t_::getEventFromContextCache( bool HostVisible, bool WithProfiling, ur_device_handle_t Device, - bool CounterBasedEventEnabled, bool UsingImmCmdList) { + bool CounterBasedEventEnabled) { std::scoped_lock Lock(EventCacheMutex); auto Cache = getEventCache(HostVisible, WithProfiling, Device); - if (CounterBasedEventEnabled) { - Cache = getCounterBasedEventCache(WithProfiling, UsingImmCmdList, Device); - } if (Cache->empty()) return nullptr; @@ -588,17 +585,9 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) { Device = Event->UrQueue->Device; } - if (Event->CounterBasedEventsEnabled) { - bool UsingImmediateCommandlists = - !Event->UrQueue || Event->UrQueue->UsingImmCmdLists; - auto Cache = getCounterBasedEventCache(Event->isProfilingEnabled(), - UsingImmediateCommandlists, Device); - Cache->emplace_back(Event); - } else { - auto Cache = getEventCache(Event->isHostVisible(), - Event->isProfilingEnabled(), Device); - Cache->emplace_back(Event); - } + auto Cache = getEventCache(Event->isHostVisible(), + Event->isProfilingEnabled(), Device); + Cache->emplace_back(Event); } ur_result_t diff --git a/source/adapters/level_zero/context.hpp b/source/adapters/level_zero/context.hpp index a5866f891f..e7c0d784a0 100644 --- a/source/adapters/level_zero/context.hpp +++ b/source/adapters/level_zero/context.hpp @@ -39,13 +39,6 @@ struct ur_context_handle_t_ : _ur_object { : ZeContext{ZeContext}, Devices{Devs, Devs + NumDevices}, NumDevices{NumDevices} { OwnNativeHandle = OwnZeContext; - for (const auto &Device : Devices) { - for (int i = 0; i < EventCacheTypeCount; i++) { - EventCaches.emplace_back(); - EventCachesDeviceMap[i].insert( - std::make_pair(Device, EventCaches.size() - 1)); - } - } } ur_context_handle_t_(ze_context_handle_t ZeContext) : ZeContext{ZeContext} {} @@ -157,10 +150,9 @@ struct ur_context_handle_t_ : _ur_object { // head. // // Cache of event pools to which host-visible events are added to. - std::vector> ZeEventPoolCache{ - ZeEventPoolCacheTypeCount * 2}; + std::vector> ZeEventPoolCache{12}; std::vector> - ZeEventPoolCacheDeviceMap{ZeEventPoolCacheTypeCount * 2}; + ZeEventPoolCacheDeviceMap{12}; // This map will be used to determine if a pool is full or not // by storing number of empty slots available in the pool. @@ -182,9 +174,9 @@ struct ur_context_handle_t_ : _ur_object { // Caches for events. using EventCache = std::vector>; - EventCache EventCaches{EventCacheTypeCount}; + EventCache EventCaches{4}; std::vector> - EventCachesDeviceMap{EventCacheTypeCount}; + EventCachesDeviceMap{4}; // Initialize the PI context. ur_result_t initialize(); @@ -222,39 +214,25 @@ struct ur_context_handle_t_ : _ur_object { ur_event_handle_t getEventFromContextCache(bool HostVisible, bool WithProfiling, ur_device_handle_t Device, - bool CounterBasedEventEnabled, - bool UsingImmCmdList); + bool CounterBasedEventEnabled); // Add ur_event_handle_t to cache. void addEventToContextCache(ur_event_handle_t); - enum ZeEventPoolCacheType { + enum EventPoolCacheType { HostVisibleCacheType, HostInvisibleCacheType, HostVisibleCounterBasedRegularCacheType, HostInvisibleCounterBasedRegularCacheType, HostVisibleCounterBasedImmediateCacheType, - HostInvisibleCounterBasedImmediateCacheType, - ZeEventPoolCacheTypeCount - }; - - enum EventCacheType { - HostVisibleProfilingCacheType, - HostVisibleRegularCacheType, - HostInvisibleProfilingCacheType, - HostInvisibleRegularCacheType, - CounterBasedImmediateCacheType, - CounterBasedRegularCacheType, - CounterBasedImmediateProfilingCacheType, - CounterBasedRegularProfilingCacheType, - EventCacheTypeCount + HostInvisibleCounterBasedImmediateCacheType }; std::list * getZeEventPoolCache(bool HostVisible, bool WithProfiling, bool CounterBasedEventEnabled, bool UsingImmediateCmdList, ze_device_handle_t ZeDevice) { - ZeEventPoolCacheType CacheType; + EventPoolCacheType CacheType; calculateCacheIndex(HostVisible, CounterBasedEventEnabled, UsingImmediateCmdList, CacheType); @@ -277,7 +255,7 @@ struct ur_context_handle_t_ : _ur_object { ur_result_t calculateCacheIndex(bool HostVisible, bool CounterBasedEventEnabled, bool UsingImmediateCmdList, - ZeEventPoolCacheType &CacheType) { + EventPoolCacheType &CacheType) { if (CounterBasedEventEnabled && HostVisible && !UsingImmediateCmdList) { CacheType = HostVisibleCounterBasedRegularCacheType; } else if (CounterBasedEventEnabled && !HostVisible && @@ -341,51 +319,28 @@ struct ur_context_handle_t_ : _ur_object { if (HostVisible) { if (Device) { auto EventCachesMap = - WithProfiling ? &EventCachesDeviceMap[HostVisibleProfilingCacheType] - : &EventCachesDeviceMap[HostVisibleRegularCacheType]; - return &EventCaches[(*EventCachesMap)[Device]]; - } else { - return WithProfiling ? &EventCaches[HostVisibleProfilingCacheType] - : &EventCaches[HostVisibleRegularCacheType]; - } - } else { - if (Device) { - auto EventCachesMap = - WithProfiling - ? &EventCachesDeviceMap[HostInvisibleProfilingCacheType] - : &EventCachesDeviceMap[HostInvisibleRegularCacheType]; - return &EventCaches[(*EventCachesMap)[Device]]; - } else { - return WithProfiling ? &EventCaches[HostInvisibleProfilingCacheType] - : &EventCaches[HostInvisibleRegularCacheType]; - } - } - }; - auto getCounterBasedEventCache(bool WithProfiling, bool UsingImmediateCmdList, - ur_device_handle_t Device) { - if (UsingImmediateCmdList) { - if (Device) { - auto EventCachesMap = - WithProfiling - ? &EventCachesDeviceMap[CounterBasedImmediateProfilingCacheType] - : &EventCachesDeviceMap[CounterBasedImmediateCacheType]; + WithProfiling ? &EventCachesDeviceMap[0] : &EventCachesDeviceMap[1]; + if (EventCachesMap->find(Device) == EventCachesMap->end()) { + EventCaches.emplace_back(); + EventCachesMap->insert( + std::make_pair(Device, EventCaches.size() - 1)); + } return &EventCaches[(*EventCachesMap)[Device]]; } else { - return WithProfiling - ? &EventCaches[CounterBasedImmediateProfilingCacheType] - : &EventCaches[CounterBasedImmediateCacheType]; + return WithProfiling ? &EventCaches[0] : &EventCaches[1]; } } else { if (Device) { auto EventCachesMap = - WithProfiling - ? &EventCachesDeviceMap[CounterBasedRegularProfilingCacheType] - : &EventCachesDeviceMap[CounterBasedRegularCacheType]; + WithProfiling ? &EventCachesDeviceMap[2] : &EventCachesDeviceMap[3]; + if (EventCachesMap->find(Device) == EventCachesMap->end()) { + EventCaches.emplace_back(); + EventCachesMap->insert( + std::make_pair(Device, EventCaches.size() - 1)); + } return &EventCaches[(*EventCachesMap)[Device]]; } else { - return WithProfiling - ? &EventCaches[CounterBasedRegularProfilingCacheType] - : &EventCaches[CounterBasedRegularCacheType]; + return WithProfiling ? &EventCaches[2] : &EventCaches[3]; } } } diff --git a/source/adapters/level_zero/event.cpp b/source/adapters/level_zero/event.cpp index 43e728ed33..2bd3011b4b 100644 --- a/source/adapters/level_zero/event.cpp +++ b/source/adapters/level_zero/event.cpp @@ -1272,8 +1272,7 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue, } if (auto CachedEvent = Context->getEventFromContextCache( - HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled, - UsingImmediateCommandlists)) { + HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled)) { *RetEvent = CachedEvent; return UR_RESULT_SUCCESS; } diff --git a/source/adapters/level_zero/queue.cpp b/source/adapters/level_zero/queue.cpp index b498992bdd..978547df10 100644 --- a/source/adapters/level_zero/queue.cpp +++ b/source/adapters/level_zero/queue.cpp @@ -1187,7 +1187,7 @@ ur_queue_handle_t_::ur_queue_handle_t_( return std::atoi(UrRet) != 0; }(); this->CounterBasedEventsEnabled = - isInOrderQueue() && Device->useDriverInOrderLists() && + UsingImmCmdLists && isInOrderQueue() && Device->useDriverInOrderLists() && useDriverCounterBasedEvents && Device->Platform->ZeDriverEventPoolCountingEventsExtensionFound; }