@@ -39,6 +39,13 @@ struct ur_context_handle_t_ : _ur_object {
3939 : ZeContext{ZeContext}, Devices{Devs, Devs + NumDevices},
4040 NumDevices{NumDevices} {
4141 OwnNativeHandle = OwnZeContext;
42+ For (const auto &Devices : Devices) {
43+ for (int i = 0 ; i < EventCacheTypeCount; i++) {
44+ EventCaches.emplace_back ();
45+ EventCachesDeviceMap[i].insert (
46+ std::make_pair (Devices, EventCaches.size () - 1 );)
47+ }
48+ }
4249 }
4350
4451 ur_context_handle_t_ (ze_context_handle_t ZeContext) : ZeContext{ZeContext} {}
@@ -150,9 +157,10 @@ struct ur_context_handle_t_ : _ur_object {
150157 // head.
151158 //
152159 // Cache of event pools to which host-visible events are added to.
153- std::vector<std::list<ze_event_pool_handle_t >> ZeEventPoolCache{12 };
160+ std::vectors<std::list<ze_event_pool_handle_t >> ZeEventPoolCache{
161+ ZeEventPoolCacheTypeCount * 2 };
154162 std::vector<std::unordered_map<ze_device_handle_t , size_t >>
155- ZeEventPoolCacheDeviceMap{12 };
163+ ZeEventPoolCacheDeviceMap{ZeEventPoolCacheTypeCount * 2 };
156164
157165 // This map will be used to determine if a pool is full or not
158166 // by storing number of empty slots available in the pool.
@@ -174,9 +182,9 @@ struct ur_context_handle_t_ : _ur_object {
174182
175183 // Caches for events.
176184 using EventCache = std::vector<std::list<ur_event_handle_t >>;
177- EventCache EventCaches{4 };
185+ EventCache EventCaches{EventCacheTypeCount };
178186 std::vector<std::unordered_map<ur_device_handle_t , size_t >>
179- EventCachesDeviceMap{4 };
187+ EventCachesDeviceMap{EventCacheTypeCount };
180188
181189 // Initialize the PI context.
182190 ur_result_t initialize ();
@@ -211,28 +219,41 @@ struct ur_context_handle_t_ : _ur_object {
211219 bool UsingImmCmdList);
212220
213221 // Get ur_event_handle_t from cache.
214- ur_event_handle_t getEventFromContextCache ( bool HostVisible,
215- bool WithProfiling,
216- ur_device_handle_t Device,
217- bool CounterBasedEventEnabled );
222+ ur_event_handle_t
223+ getEventFromContextCache ( bool HostVisible, bool WithProfiling,
224+ ur_device_handle_t Device,
225+ bool CounterBasedEventEnabled bool UsingImmCmdList );
218226
219227 // Add ur_event_handle_t to cache.
220228 void addEventToContextCache (ur_event_handle_t );
221229
222- enum EventPoolCacheType {
230+ enum ZeEventPoolCacheType {
223231 HostVisibleCacheType,
224232 HostInvisibleCacheType,
225233 HostVisibleCounterBasedRegularCacheType,
226234 HostInvisibleCounterBasedRegularCacheType,
227235 HostVisibleCounterBasedImmediateCacheType,
228- HostInvisibleCounterBasedImmediateCacheType
236+ HostInvisibleCounterBasedImmediateCacheType,
237+ ZeEventPoolCacheTypeCount
238+ };
239+
240+ enum EventCacheType {
241+ HostVisibleProfilingCacheType,
242+ HostVisibleRegularCacheType,
243+ HostInvisibleProfilingCacheType,
244+ HostInvisibleRegularCacheType,
245+ CounterBasedImmediateCacheType,
246+ CounterBasedRegularCacheType,
247+ CounterBasedImmediateProfilingCacheType,
248+ CounterBasedRegularProfilingCacheType,
249+ EventCacheTypeCount
229250 };
230251
231252 std::list<ze_event_pool_handle_t > *
232253 getZeEventPoolCache (bool HostVisible, bool WithProfiling,
233254 bool CounterBasedEventEnabled, bool UsingImmediateCmdList,
234255 ze_device_handle_t ZeDevice) {
235- EventPoolCacheType CacheType;
256+ ZeEventPoolCacheType CacheType;
236257
237258 calculateCacheIndex (HostVisible, CounterBasedEventEnabled,
238259 UsingImmediateCmdList, CacheType);
@@ -255,7 +276,7 @@ struct ur_context_handle_t_ : _ur_object {
255276 ur_result_t calculateCacheIndex (bool HostVisible,
256277 bool CounterBasedEventEnabled,
257278 bool UsingImmediateCmdList,
258- EventPoolCacheType &CacheType) {
279+ ZeEventPoolCacheType &CacheType) {
259280 if (CounterBasedEventEnabled && HostVisible && !UsingImmediateCmdList) {
260281 CacheType = HostVisibleCounterBasedRegularCacheType;
261282 } else if (CounterBasedEventEnabled && !HostVisible &&
@@ -319,28 +340,51 @@ struct ur_context_handle_t_ : _ur_object {
319340 if (HostVisible) {
320341 if (Device) {
321342 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- }
343+ WithProfiling ? &EventCachesDeviceMap[HostVisibleProfilingCacheType]
344+ : &EventCachesDeviceMap[HostVisibleRegularCacheType];
345+ return &EventCaches[(*EventCachesMap)[Device]];
346+ } else {
347+ return WithProfiling ? &EventCaches[HostVisibleProfilingCacheType]
348+ : &EventCaches[HostVisibleRegularCacheType];
349+ }
350+ } else {
351+ if (Device) {
352+ auto EventCachesMap =
353+ WithProfiling
354+ ? &EventCachesDeviceMap[HostInvisibleProfilingCacheType]
355+ : &EventCachesDeviceMap[HostInvisibleRegularCacheType];
356+ return &EventCaches[(*EventCachesMap)[Device]];
357+ } else {
358+ return WithProfiling ? &EventCaches[HostInvisibleProfilingCacheType]
359+ : &EventCaches[HostInvisibleRegularCacheType];
360+ }
361+ }
362+ };
363+ auto getCounterBasedEventCache (bool WithProfiling, bool UsingImmediateCmdList,
364+ ur_device_handle_t Device) {
365+ if (UsingImmediateCmdList) {
366+ if (Device) {
367+ auto EventCachesMap =
368+ WithProfiling
369+ ? &EventCachesDeviceMap[CounterBasedImmediateProfilingCacheType]
370+ : &EventCachesDeviceMap[CounterBasedImmediateCacheType];
328371 return &EventCaches[(*EventCachesMap)[Device]];
329372 } else {
330- return WithProfiling ? &EventCaches[0 ] : &EventCaches[1 ];
373+ return WithProfiling
374+ ? &EventCaches[CounterBasedImmediateProfilingCacheType]
375+ : &EventCaches[CounterBasedImmediateCacheType];
331376 }
332377 } else {
333378 if (Device) {
334379 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- }
380+ WithProfiling
381+ ? &EventCachesDeviceMap[CounterBasedRegularProfilingCacheType]
382+ : &EventCachesDeviceMap[CounterBasedRegularCacheType];
341383 return &EventCaches[(*EventCachesMap)[Device]];
342384 } else {
343- return WithProfiling ? &EventCaches[2 ] : &EventCaches[3 ];
385+ return WithProfiling
386+ ? &EventCaches[CounterBasedRegularProfilingCacheType]
387+ : &EventCaches[CounterBasedRegularCacheType];
344388 }
345389 }
346390 }
0 commit comments