@@ -141,7 +141,7 @@ struct ur_context_handle_t_ : _ur_object {
141141 // head.
142142 //
143143 // Cache of event pools to which host-visible events are added to.
144- std::vector<std::list<ze_event_pool_handle_t >> ZeEventPoolCache{ 4 } ;
144+ std::vector<std::list<ze_event_pool_handle_t > * > ZeEventPoolCache;
145145 std::vector<std::unordered_map<ze_device_handle_t ,
146146 std::list<ze_event_pool_handle_t > *>>
147147 ZeEventPoolCacheDeviceMap{4 };
@@ -165,7 +165,7 @@ struct ur_context_handle_t_ : _ur_object {
165165 ur_mutex EventCacheMutex;
166166
167167 // Caches for events.
168- std::vector<std::list<ur_event_handle_t >> EventCaches{ 4 } ;
168+ std::vector<std::list<ur_event_handle_t > * > EventCaches;
169169 std::vector<
170170 std::unordered_map<ur_device_handle_t , std::list<ur_event_handle_t > *>>
171171 EventCachesDeviceMap{4 };
@@ -207,31 +207,44 @@ struct ur_context_handle_t_ : _ur_object {
207207
208208 auto getZeEventPoolCache (bool HostVisible, bool WithProfiling,
209209 ze_device_handle_t ZeDevice) {
210+ // Adding 4 initial global caches for provided scope and profiling modes:
211+ // Host Scope, Device Scope, with Profiling, without Profiling.
212+ if (ZeEventPoolCache.empty ()) {
213+ for (int i = 0 ; i < 4 ; i++) {
214+ std::list<ze_event_pool_handle_t > *deviceZeEventPoolCache =
215+ new std::list<ze_event_pool_handle_t >;
216+ ZeEventPoolCache.push_back (deviceZeEventPoolCache);
217+ }
218+ }
210219 if (HostVisible) {
211220 if (ZeDevice) {
212221 auto ZeEventPoolCacheMap = WithProfiling
213222 ? &ZeEventPoolCacheDeviceMap[0 ]
214223 : &ZeEventPoolCacheDeviceMap[1 ];
215224 if (ZeEventPoolCacheMap->find (ZeDevice) == ZeEventPoolCacheMap->end ()) {
216- ZeEventPoolCache.emplace_back ();
217- (*ZeEventPoolCacheMap)[ZeDevice] = &ZeEventPoolCache.back ();
225+ std::list<ze_event_pool_handle_t > *deviceZeEventPoolCache =
226+ new std::list<ze_event_pool_handle_t >;
227+ ZeEventPoolCache.push_back (deviceZeEventPoolCache);
228+ (*ZeEventPoolCacheMap)[ZeDevice] = deviceZeEventPoolCache;
218229 }
219230 return (*ZeEventPoolCacheMap)[ZeDevice];
220231 } else {
221- return WithProfiling ? & ZeEventPoolCache[0 ] : & ZeEventPoolCache[1 ];
232+ return WithProfiling ? ZeEventPoolCache[0 ] : ZeEventPoolCache[1 ];
222233 }
223234 } else {
224235 if (ZeDevice) {
225236 auto ZeEventPoolCacheMap = WithProfiling
226237 ? &ZeEventPoolCacheDeviceMap[2 ]
227238 : &ZeEventPoolCacheDeviceMap[3 ];
228239 if (ZeEventPoolCacheMap->find (ZeDevice) == ZeEventPoolCacheMap->end ()) {
229- ZeEventPoolCache.emplace_back ();
230- (*ZeEventPoolCacheMap)[ZeDevice] = &ZeEventPoolCache.back ();
240+ std::list<ze_event_pool_handle_t > *deviceZeEventPoolCache =
241+ new std::list<ze_event_pool_handle_t >;
242+ ZeEventPoolCache.push_back (deviceZeEventPoolCache);
243+ (*ZeEventPoolCacheMap)[ZeDevice] = deviceZeEventPoolCache;
231244 }
232245 return (*ZeEventPoolCacheMap)[ZeDevice];
233246 } else {
234- return WithProfiling ? & ZeEventPoolCache[2 ] : & ZeEventPoolCache[3 ];
247+ return WithProfiling ? ZeEventPoolCache[2 ] : ZeEventPoolCache[3 ];
235248 }
236249 }
237250 }
@@ -274,29 +287,42 @@ struct ur_context_handle_t_ : _ur_object {
274287 // Get the cache of events for a provided scope and profiling mode.
275288 auto getEventCache (bool HostVisible, bool WithProfiling,
276289 ur_device_handle_t Device) {
290+ // Adding 4 initial global caches for provided scope and profiling modes:
291+ // Host Scope, Device Scope, with Profiling, without Profiling.
292+ if (EventCaches.empty ()) {
293+ for (int i = 0 ; i < 4 ; i++) {
294+ std::list<ur_event_handle_t > *deviceEventCache =
295+ new std::list<ur_event_handle_t >;
296+ EventCaches.push_back (deviceEventCache);
297+ }
298+ }
277299 if (HostVisible) {
278300 if (Device) {
279301 auto EventCachesMap =
280302 WithProfiling ? &EventCachesDeviceMap[0 ] : &EventCachesDeviceMap[1 ];
281303 if (EventCachesMap->find (Device) == EventCachesMap->end ()) {
282- EventCaches.emplace_back ();
283- (*EventCachesMap)[Device] = &EventCaches.back ();
304+ std::list<ur_event_handle_t > *deviceEventCache =
305+ new std::list<ur_event_handle_t >;
306+ EventCaches.push_back (deviceEventCache);
307+ (*EventCachesMap)[Device] = deviceEventCache;
284308 }
285309 return (*EventCachesMap)[Device];
286310 } else {
287- return WithProfiling ? & EventCaches[0 ] : & EventCaches[1 ];
311+ return WithProfiling ? EventCaches[0 ] : EventCaches[1 ];
288312 }
289313 } else {
290314 if (Device) {
291315 auto EventCachesMap =
292316 WithProfiling ? &EventCachesDeviceMap[2 ] : &EventCachesDeviceMap[3 ];
293317 if (EventCachesMap->find (Device) == EventCachesMap->end ()) {
294- EventCaches.emplace_back ();
295- (*EventCachesMap)[Device] = &EventCaches.back ();
318+ std::list<ur_event_handle_t > *deviceEventCache =
319+ new std::list<ur_event_handle_t >;
320+ EventCaches.push_back (deviceEventCache);
321+ (*EventCachesMap)[Device] = deviceEventCache;
296322 }
297323 return (*EventCachesMap)[Device];
298324 } else {
299- return WithProfiling ? & EventCaches[2 ] : & EventCaches[3 ];
325+ return WithProfiling ? EventCaches[2 ] : EventCaches[3 ];
300326 }
301327 }
302328 }
0 commit comments