From ad1118285579afd66897133df96aea2ad9872ed2 Mon Sep 17 00:00:00 2001 From: Winston Zhang Date: Wed, 18 Sep 2024 10:47:04 -0700 Subject: [PATCH] [L0] Preallocate EventCachesDeviceMap on context creation Signed-off-by: Winston Zhang --- source/adapters/level_zero/context.hpp | 27 +++++++------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/source/adapters/level_zero/context.hpp b/source/adapters/level_zero/context.hpp index 1e35dfcc11..097b9e4b50 100644 --- a/source/adapters/level_zero/context.hpp +++ b/source/adapters/level_zero/context.hpp @@ -39,6 +39,13 @@ 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} {} @@ -333,11 +340,6 @@ struct ur_context_handle_t_ : _ur_object { auto EventCachesMap = WithProfiling ? &EventCachesDeviceMap[HostVisibleProfilingCacheType] : &EventCachesDeviceMap[HostVisibleRegularCacheType]; - 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[HostVisibleProfilingCacheType] @@ -349,11 +351,6 @@ struct ur_context_handle_t_ : _ur_object { WithProfiling ? &EventCachesDeviceMap[HostInvisibleProfilingCacheType] : &EventCachesDeviceMap[HostInvisibleRegularCacheType]; - 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[HostInvisibleProfilingCacheType] @@ -369,11 +366,6 @@ struct ur_context_handle_t_ : _ur_object { WithProfiling ? &EventCachesDeviceMap[CounterBasedImmediateProfilingCacheType] : &EventCachesDeviceMap[CounterBasedImmediateCacheType]; - 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 @@ -386,11 +378,6 @@ struct ur_context_handle_t_ : _ur_object { WithProfiling ? &EventCachesDeviceMap[CounterBasedRegularProfilingCacheType] : &EventCachesDeviceMap[CounterBasedRegularCacheType]; - 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