@@ -141,9 +141,8 @@ struct ur_context_handle_t_ : _ur_object {
141
141
// head.
142
142
//
143
143
// Cache of event pools to which host-visible events are added to.
144
- std::vector<std::list<ze_event_pool_handle_t > *> ZeEventPoolCache;
145
- std::vector<std::unordered_map<ze_device_handle_t ,
146
- std::list<ze_event_pool_handle_t > *>>
144
+ std::vector<std::list<ze_event_pool_handle_t >> ZeEventPoolCache{4 };
145
+ std::vector<std::unordered_map<ze_device_handle_t , size_t >>
147
146
ZeEventPoolCacheDeviceMap{4 };
148
147
149
148
// This map will be used to determine if a pool is full or not
@@ -165,9 +164,9 @@ struct ur_context_handle_t_ : _ur_object {
165
164
ur_mutex EventCacheMutex;
166
165
167
166
// Caches for events.
168
- std::vector<std::list<ur_event_handle_t > *> EventCaches ;
169
- std::vector<
170
- std::unordered_map< ur_device_handle_t , std::list< ur_event_handle_t > * >>
167
+ using EventCache = std::vector<std::list<ur_event_handle_t >> ;
168
+ EventCache EventCaches{ 4 };
169
+ std::vector< std::unordered_map< ur_device_handle_t , size_t >>
171
170
EventCachesDeviceMap{4 };
172
171
173
172
// Initialize the PI context.
@@ -205,46 +204,36 @@ struct ur_context_handle_t_ : _ur_object {
205
204
// Add ur_event_handle_t to cache.
206
205
void addEventToContextCache (ur_event_handle_t );
207
206
208
- auto getZeEventPoolCache (bool HostVisible, bool WithProfiling,
209
- 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
- }
207
+ std::list<ze_event_pool_handle_t > *
208
+ getZeEventPoolCache (bool HostVisible, bool WithProfiling,
209
+ ze_device_handle_t ZeDevice) {
219
210
if (HostVisible) {
220
211
if (ZeDevice) {
221
212
auto ZeEventPoolCacheMap = WithProfiling
222
213
? &ZeEventPoolCacheDeviceMap[0 ]
223
214
: &ZeEventPoolCacheDeviceMap[1 ];
224
215
if (ZeEventPoolCacheMap->find (ZeDevice) == ZeEventPoolCacheMap->end ()) {
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;
216
+ ZeEventPoolCache.emplace_back ();
217
+ ZeEventPoolCacheMap->insert (
218
+ std::make_pair (ZeDevice, ZeEventPoolCache.size () - 1 ));
229
219
}
230
- return (*ZeEventPoolCacheMap)[ZeDevice];
220
+ return &ZeEventPoolCache[ (*ZeEventPoolCacheMap)[ZeDevice] ];
231
221
} else {
232
- return WithProfiling ? ZeEventPoolCache[0 ] : ZeEventPoolCache[1 ];
222
+ return WithProfiling ? & ZeEventPoolCache[0 ] : & ZeEventPoolCache[1 ];
233
223
}
234
224
} else {
235
225
if (ZeDevice) {
236
226
auto ZeEventPoolCacheMap = WithProfiling
237
227
? &ZeEventPoolCacheDeviceMap[2 ]
238
228
: &ZeEventPoolCacheDeviceMap[3 ];
239
229
if (ZeEventPoolCacheMap->find (ZeDevice) == ZeEventPoolCacheMap->end ()) {
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;
230
+ ZeEventPoolCache.emplace_back ();
231
+ ZeEventPoolCacheMap->insert (
232
+ std::make_pair (ZeDevice, ZeEventPoolCache.size () - 1 ));
244
233
}
245
- return (*ZeEventPoolCacheMap)[ZeDevice];
234
+ return &ZeEventPoolCache[ (*ZeEventPoolCacheMap)[ZeDevice] ];
246
235
} else {
247
- return WithProfiling ? ZeEventPoolCache[2 ] : ZeEventPoolCache[3 ];
236
+ return WithProfiling ? & ZeEventPoolCache[2 ] : & ZeEventPoolCache[3 ];
248
237
}
249
238
}
250
239
}
@@ -287,42 +276,31 @@ struct ur_context_handle_t_ : _ur_object {
287
276
// Get the cache of events for a provided scope and profiling mode.
288
277
auto getEventCache (bool HostVisible, bool WithProfiling,
289
278
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
- }
299
279
if (HostVisible) {
300
280
if (Device) {
301
281
auto EventCachesMap =
302
282
WithProfiling ? &EventCachesDeviceMap[0 ] : &EventCachesDeviceMap[1 ];
303
283
if (EventCachesMap->find (Device) == EventCachesMap->end ()) {
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;
284
+ EventCaches.emplace_back ();
285
+ EventCachesMap->insert (
286
+ std::make_pair (Device, EventCaches.size () - 1 ));
308
287
}
309
- return (*EventCachesMap)[Device];
288
+ return &EventCaches[ (*EventCachesMap)[Device] ];
310
289
} else {
311
- return WithProfiling ? EventCaches[0 ] : EventCaches[1 ];
290
+ return WithProfiling ? & EventCaches[0 ] : & EventCaches[1 ];
312
291
}
313
292
} else {
314
293
if (Device) {
315
294
auto EventCachesMap =
316
295
WithProfiling ? &EventCachesDeviceMap[2 ] : &EventCachesDeviceMap[3 ];
317
296
if (EventCachesMap->find (Device) == EventCachesMap->end ()) {
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;
297
+ EventCaches.emplace_back ();
298
+ EventCachesMap->insert (
299
+ std::make_pair (Device, EventCaches.size () - 1 ));
322
300
}
323
- return (*EventCachesMap)[Device];
301
+ return &EventCaches[ (*EventCachesMap)[Device] ];
324
302
} else {
325
- return WithProfiling ? EventCaches[2 ] : EventCaches[3 ];
303
+ return WithProfiling ? & EventCaches[2 ] : & EventCaches[3 ];
326
304
}
327
305
}
328
306
}
0 commit comments