Skip to content

Commit af6e186

Browse files
[L0] Phase 2 of Counter-Based Event Implementation
Signed-off-by: Winston Zhang <winston.zhang@intel.com>
1 parent e3910da commit af6e186

File tree

4 files changed

+86
-29
lines changed

4 files changed

+86
-29
lines changed

source/adapters/level_zero/context.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,12 @@ 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);
566+
if (CounterBasedEventEnabled) {
567+
Cache = getCounterBasedEventCache(WithProfiling, UsingImmCmdList, Device);
568+
}
566569
if (Cache->empty())
567570
return nullptr;
568571

@@ -585,9 +588,17 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
585588
Device = Event->UrQueue->Device;
586589
}
587590

588-
auto Cache = getEventCache(Event->isHostVisible(),
589-
Event->isProfilingEnabled(), Device);
590-
Cache->emplace_back(Event);
591+
if (Event->CounterBasedEventsEnabled) {
592+
bool UsingImmediateCommandlists =
593+
!Event->UrQueue || Event->UrQueue->UsingImmCmdLists;
594+
auto Cache = getCounterBasedEventCache(Event->isProfilingEnabled(),
595+
UsingImmediateCommandlists, Device);
596+
Cache->emplace_back(Event);
597+
} else {
598+
auto Cache = getEventCache(Event->isHostVisible(),
599+
Event->isProfilingEnabled(), Device);
600+
Cache->emplace_back(Event);
601+
}
591602
}
592603

593604
ur_result_t

source/adapters/level_zero/context.hpp

Lines changed: 68 additions & 23 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 &Device : Devices) {
43+
for (int i = 0; i < EventCacheTypeCount; i++) {
44+
EventCaches.emplace_back();
45+
EventCachesDeviceMap[i].insert(
46+
std::make_pair(Device, 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::vector<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();
@@ -214,25 +222,39 @@ struct ur_context_handle_t_ : _ur_object {
214222
ur_event_handle_t getEventFromContextCache(bool HostVisible,
215223
bool WithProfiling,
216224
ur_device_handle_t Device,
217-
bool CounterBasedEventEnabled);
225+
bool CounterBasedEventEnabled,
226+
bool UsingImmCmdList);
218227

219228
// Add ur_event_handle_t to cache.
220229
void addEventToContextCache(ur_event_handle_t);
221230

222-
enum EventPoolCacheType {
231+
enum ZeEventPoolCacheType {
223232
HostVisibleCacheType,
224233
HostInvisibleCacheType,
225234
HostVisibleCounterBasedRegularCacheType,
226235
HostInvisibleCounterBasedRegularCacheType,
227236
HostVisibleCounterBasedImmediateCacheType,
228-
HostInvisibleCounterBasedImmediateCacheType
237+
HostInvisibleCounterBasedImmediateCacheType,
238+
ZeEventPoolCacheTypeCount
239+
};
240+
241+
enum EventCacheType {
242+
HostVisibleProfilingCacheType,
243+
HostVisibleRegularCacheType,
244+
HostInvisibleProfilingCacheType,
245+
HostInvisibleRegularCacheType,
246+
CounterBasedImmediateCacheType,
247+
CounterBasedRegularCacheType,
248+
CounterBasedImmediateProfilingCacheType,
249+
CounterBasedRegularProfilingCacheType,
250+
EventCacheTypeCount
229251
};
230252

231253
std::list<ze_event_pool_handle_t> *
232254
getZeEventPoolCache(bool HostVisible, bool WithProfiling,
233255
bool CounterBasedEventEnabled, bool UsingImmediateCmdList,
234256
ze_device_handle_t ZeDevice) {
235-
EventPoolCacheType CacheType;
257+
ZeEventPoolCacheType CacheType;
236258

237259
calculateCacheIndex(HostVisible, CounterBasedEventEnabled,
238260
UsingImmediateCmdList, CacheType);
@@ -255,7 +277,7 @@ struct ur_context_handle_t_ : _ur_object {
255277
ur_result_t calculateCacheIndex(bool HostVisible,
256278
bool CounterBasedEventEnabled,
257279
bool UsingImmediateCmdList,
258-
EventPoolCacheType &CacheType) {
280+
ZeEventPoolCacheType &CacheType) {
259281
if (CounterBasedEventEnabled && HostVisible && !UsingImmediateCmdList) {
260282
CacheType = HostVisibleCounterBasedRegularCacheType;
261283
} else if (CounterBasedEventEnabled && !HostVisible &&
@@ -319,28 +341,51 @@ struct ur_context_handle_t_ : _ur_object {
319341
if (HostVisible) {
320342
if (Device) {
321343
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-
}
344+
WithProfiling ? &EventCachesDeviceMap[HostVisibleProfilingCacheType]
345+
: &EventCachesDeviceMap[HostVisibleRegularCacheType];
346+
return &EventCaches[(*EventCachesMap)[Device]];
347+
} else {
348+
return WithProfiling ? &EventCaches[HostVisibleProfilingCacheType]
349+
: &EventCaches[HostVisibleRegularCacheType];
350+
}
351+
} else {
352+
if (Device) {
353+
auto EventCachesMap =
354+
WithProfiling
355+
? &EventCachesDeviceMap[HostInvisibleProfilingCacheType]
356+
: &EventCachesDeviceMap[HostInvisibleRegularCacheType];
357+
return &EventCaches[(*EventCachesMap)[Device]];
358+
} else {
359+
return WithProfiling ? &EventCaches[HostInvisibleProfilingCacheType]
360+
: &EventCaches[HostInvisibleRegularCacheType];
361+
}
362+
}
363+
};
364+
auto getCounterBasedEventCache(bool WithProfiling, bool UsingImmediateCmdList,
365+
ur_device_handle_t Device) {
366+
if (UsingImmediateCmdList) {
367+
if (Device) {
368+
auto EventCachesMap =
369+
WithProfiling
370+
? &EventCachesDeviceMap[CounterBasedImmediateProfilingCacheType]
371+
: &EventCachesDeviceMap[CounterBasedImmediateCacheType];
328372
return &EventCaches[(*EventCachesMap)[Device]];
329373
} else {
330-
return WithProfiling ? &EventCaches[0] : &EventCaches[1];
374+
return WithProfiling
375+
? &EventCaches[CounterBasedImmediateProfilingCacheType]
376+
: &EventCaches[CounterBasedImmediateCacheType];
331377
}
332378
} else {
333379
if (Device) {
334380
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-
}
381+
WithProfiling
382+
? &EventCachesDeviceMap[CounterBasedRegularProfilingCacheType]
383+
: &EventCachesDeviceMap[CounterBasedRegularCacheType];
341384
return &EventCaches[(*EventCachesMap)[Device]];
342385
} else {
343-
return WithProfiling ? &EventCaches[2] : &EventCaches[3];
386+
return WithProfiling
387+
? &EventCaches[CounterBasedRegularProfilingCacheType]
388+
: &EventCaches[CounterBasedRegularCacheType];
344389
}
345390
}
346391
}

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)