@@ -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,44 @@ struct ur_context_handle_t_ : _ur_object {
313304 ze_context_handle_t getZeHandle () const ;
314305
315306private:
307+
308+ enum EventFlags {
309+ EVENT_FLAG_HOST_VISIBLE = UR_BIT (0 ),
310+ EVENT_FLAG_WITH_PROFILING = UR_BIT (1 ),
311+ EVENT_FLAG_COUNTER = UR_BIT (2 ),
312+ MAX_EVENT_FLAG_BITS = 3 , // this is used as an offset for embedding device id
313+ };
314+
315+ // Mutex to control operations on event caches.
316+ ur_mutex EventCacheMutex;
317+
318+ // Caches for events.
319+ using EventCache = std::list<ur_event_handle_t >;
320+ std::vector<EventCache> EventCaches;
321+
316322 // 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) {
323+ EventCache *getEventCache (bool HostVisible, bool WithProfiling,
324+ ur_device_handle_t Device, bool Counter) {
325+
326+ size_t index = 0 ;
319327 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- }
328+ index |= EVENT_FLAG_HOST_VISIBLE;
329+ }
330+ if (WithProfiling) {
331+ index |= EVENT_FLAG_WITH_PROFILING;
345332 }
333+ if (Counter) {
334+ index |= EVENT_FLAG_COUNTER;
335+ }
336+ if (Device) {
337+ index |= (*Device->Id << MAX_EVENT_FLAG_BITS);
338+ }
339+
340+ if (index >= EventCaches.size ()) {
341+ EventCaches.resize (index + 1 );
342+ }
343+
344+ return &EventCaches[index];
346345 }
347346};
348347
0 commit comments