@@ -169,15 +169,6 @@ struct ur_context_handle_t_ : _ur_object {
169169 // holding the current pool usage counts.
170170 ur_mutex ZeEventPoolCacheMutex;
171171
172- // Mutex to control operations on event caches.
173- ur_mutex EventCacheMutex;
174-
175- // Caches for events.
176- using EventCache = std::vector<std::list<ur_event_handle_t >>;
177- EventCache EventCaches{4 };
178- std::vector<std::unordered_map<ur_device_handle_t , size_t >>
179- EventCachesDeviceMap{4 };
180-
181172 // Initialize the PI context.
182173 ur_result_t initialize ();
183174
@@ -313,36 +304,45 @@ struct ur_context_handle_t_ : _ur_object {
313304 ze_context_handle_t getZeHandle () const ;
314305
315306private:
307+ enum EventFlags {
308+ EVENT_FLAG_HOST_VISIBLE = UR_BIT (0 ),
309+ EVENT_FLAG_WITH_PROFILING = UR_BIT (1 ),
310+ EVENT_FLAG_COUNTER = UR_BIT (2 ),
311+ EVENT_FLAG_DEVICE = UR_BIT (3 ), // if set, subsequent bits are device id
312+ MAX_EVENT_FLAG_BITS =
313+ 4 , // this is used as an offset for embedding device id
314+ };
315+
316+ // Mutex to control operations on event caches.
317+ ur_mutex EventCacheMutex;
318+
319+ // Caches for events.
320+ using EventCache = std::list<ur_event_handle_t >;
321+ std::vector<EventCache> EventCaches;
322+
316323 // Get the cache of events for a provided scope and profiling mode.
317- auto getEventCache (bool HostVisible, bool WithProfiling,
318- ur_device_handle_t Device) {
324+ EventCache *getEventCache (bool HostVisible, bool WithProfiling,
325+ ur_device_handle_t Device, bool Counter) {
326+
327+ size_t index = 0 ;
319328 if (HostVisible) {
320- if (Device) {
321- auto EventCachesMap =
322- WithProfiling ? &EventCachesDeviceMap[0 ] : &EventCachesDeviceMap[1 ];
323- if (EventCachesMap->find (Device) == EventCachesMap->end ()) {
324- EventCaches.emplace_back ();
325- EventCachesMap->insert (
326- std::make_pair (Device, EventCaches.size () - 1 ));
327- }
328- return &EventCaches[(*EventCachesMap)[Device]];
329- } else {
330- return WithProfiling ? &EventCaches[0 ] : &EventCaches[1 ];
331- }
332- } else {
333- if (Device) {
334- auto EventCachesMap =
335- WithProfiling ? &EventCachesDeviceMap[2 ] : &EventCachesDeviceMap[3 ];
336- if (EventCachesMap->find (Device) == EventCachesMap->end ()) {
337- EventCaches.emplace_back ();
338- EventCachesMap->insert (
339- std::make_pair (Device, EventCaches.size () - 1 ));
340- }
341- return &EventCaches[(*EventCachesMap)[Device]];
342- } else {
343- return WithProfiling ? &EventCaches[2 ] : &EventCaches[3 ];
344- }
329+ index |= EVENT_FLAG_HOST_VISIBLE;
330+ }
331+ if (WithProfiling) {
332+ index |= EVENT_FLAG_WITH_PROFILING;
345333 }
334+ if (Counter) {
335+ index |= EVENT_FLAG_COUNTER;
336+ }
337+ if (Device) {
338+ index |= EVENT_FLAG_DEVICE | (*Device->Id << MAX_EVENT_FLAG_BITS);
339+ }
340+
341+ if (index >= EventCaches.size ()) {
342+ EventCaches.resize (index + 1 );
343+ }
344+
345+ return &EventCaches[index];
346346 }
347347};
348348
0 commit comments