Skip to content

Commit c7bc3ce

Browse files
[L0] Phase 2 of Counter-Based Event Implementation
Signed-off-by: Winston Zhang <[email protected]>
1 parent a21a230 commit c7bc3ce

File tree

4 files changed

+84
-32
lines changed

4 files changed

+84
-32
lines changed

source/adapters/level_zero/context.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
560560

561561
ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
562562
bool HostVisible, bool WithProfiling, ur_device_handle_t Device,
563-
bool CounterBasedEventEnabled) {
563+
bool CounterBasedEventEnabled, bool UsingImmCmdList) {
564564
std::scoped_lock<ur_mutex> Lock(EventCacheMutex);
565565
auto Cache = getEventCache(HostVisible, WithProfiling, Device);
566566
if (Cache->empty())
@@ -585,9 +585,16 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
585585
Device = Event->UrQueue->Device;
586586
}
587587

588-
auto Cache = getEventCache(Event->isHostVisible(),
589-
Event->isProfilingEnabled(), Device);
590-
Cache->emplace_back(Event);
588+
if (Event->CounterBasedEventsEnabled) {
589+
auto Cache = getCounterBasedEventCache(
590+
Event->isProfilingEnabled(),
591+
!(Event - UrQueue) || (Event->UrQueue)->UsingImmCmdLists, Device);
592+
Cache->emplace_back(Event);
593+
} else {
594+
auto Cache = getEventCache(Event->isHostVisible(),
595+
Event->isProfilingEnabled(), Device);
596+
Cache->emplace_back(Event);
597+
}
591598
}
592599

593600
ur_result_t

source/adapters/level_zero/context.hpp

Lines changed: 70 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

source/adapters/level_zero/event.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,8 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
12721272
}
12731273

12741274
if (auto CachedEvent = Context->getEventFromContextCache(
1275-
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled)) {
1275+
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled,
1276+
UsingImmediateCommandlists)) {
12761277
*RetEvent = CachedEvent;
12771278
return UR_RESULT_SUCCESS;
12781279
}

source/adapters/level_zero/queue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ ur_queue_handle_t_::ur_queue_handle_t_(
11871187
return std::atoi(UrRet) != 0;
11881188
}();
11891189
this->CounterBasedEventsEnabled =
1190-
UsingImmCmdLists && isInOrderQueue() && Device->useDriverInOrderLists() &&
1190+
isInOrderQueue() && Device->useDriverInOrderLists() &&
11911191
useDriverCounterBasedEvents &&
11921192
Device->Platform->ZeDriverEventPoolCountingEventsExtensionFound;
11931193
}

0 commit comments

Comments
 (0)