@@ -168,15 +168,6 @@ struct ur_context_handle_t_ : _ur_object {
168168 // holding the current pool usage counts.
169169 ur_mutex ZeEventPoolCacheMutex;
170170
171- // Mutex to control operations on event caches.
172- ur_mutex EventCacheMutex;
173-
174- // Caches for events.
175- using EventCache = std::vector<std::list<ur_event_handle_t >>;
176- EventCache EventCaches{4 };
177- std::vector<std::unordered_map<ur_device_handle_t , size_t >>
178- EventCachesDeviceMap{4 };
179-
180171 // Initialize the PI context.
181172 ur_result_t initialize ();
182173
@@ -306,36 +297,45 @@ struct ur_context_handle_t_ : _ur_object {
306297 bool isValidDevice (ur_device_handle_t Device) const ;
307298
308299private:
300+ enum EventFlags {
301+ EVENT_FLAG_HOST_VISIBLE = UR_BIT (0 ),
302+ EVENT_FLAG_WITH_PROFILING = UR_BIT (1 ),
303+ EVENT_FLAG_COUNTER = UR_BIT (2 ),
304+ EVENT_FLAG_DEVICE = UR_BIT (3 ), // if set, subsequent bits are device id
305+ MAX_EVENT_FLAG_BITS =
306+ 4 , // this is used as an offset for embedding device id
307+ };
308+
309+ // Mutex to control operations on event caches.
310+ ur_mutex EventCacheMutex;
311+
312+ // Caches for events.
313+ using EventCache = std::list<ur_event_handle_t >;
314+ std::vector<EventCache> EventCaches;
315+
309316 // Get the cache of events for a provided scope and profiling mode.
310- auto getEventCache (bool HostVisible, bool WithProfiling,
311- ur_device_handle_t Device) {
317+ EventCache *getEventCache (bool HostVisible, bool WithProfiling,
318+ ur_device_handle_t Device, bool Counter) {
319+
320+ size_t index = 0 ;
312321 if (HostVisible) {
313- if (Device) {
314- auto EventCachesMap =
315- WithProfiling ? &EventCachesDeviceMap[0 ] : &EventCachesDeviceMap[1 ];
316- if (EventCachesMap->find (Device) == EventCachesMap->end ()) {
317- EventCaches.emplace_back ();
318- EventCachesMap->insert (
319- std::make_pair (Device, EventCaches.size () - 1 ));
320- }
321- return &EventCaches[(*EventCachesMap)[Device]];
322- } else {
323- return WithProfiling ? &EventCaches[0 ] : &EventCaches[1 ];
324- }
325- } else {
326- if (Device) {
327- auto EventCachesMap =
328- WithProfiling ? &EventCachesDeviceMap[2 ] : &EventCachesDeviceMap[3 ];
329- if (EventCachesMap->find (Device) == EventCachesMap->end ()) {
330- EventCaches.emplace_back ();
331- EventCachesMap->insert (
332- std::make_pair (Device, EventCaches.size () - 1 ));
333- }
334- return &EventCaches[(*EventCachesMap)[Device]];
335- } else {
336- return WithProfiling ? &EventCaches[2 ] : &EventCaches[3 ];
337- }
322+ index |= EVENT_FLAG_HOST_VISIBLE;
323+ }
324+ if (WithProfiling) {
325+ index |= EVENT_FLAG_WITH_PROFILING;
338326 }
327+ if (Counter) {
328+ index |= EVENT_FLAG_COUNTER;
329+ }
330+ if (Device) {
331+ index |= EVENT_FLAG_DEVICE | (*Device->Id << MAX_EVENT_FLAG_BITS);
332+ }
333+
334+ if (index >= EventCaches.size ()) {
335+ EventCaches.resize (index + 1 );
336+ }
337+
338+ return &EventCaches[index];
339339 }
340340};
341341
0 commit comments