Skip to content

Commit 6582a8a

Browse files
[L0] Rebased against phase 2 of cb events
CI in LLVM/SYCL: intel/llvm#14536 Signed-off-by: Winston Zhang <[email protected]>
1 parent f01741a commit 6582a8a

File tree

5 files changed

+139
-88
lines changed

5 files changed

+139
-88
lines changed

source/adapters/level_zero/common.hpp

+13
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,19 @@ enum {
240240
2, // blocking UR calls, where supported (usually in enqueue commands)
241241
};
242242

243+
typedef uint32_t ur_event_flags_t;
244+
typedef enum ur_event_flag_t {
245+
USING_IMM_CMDLIST = UR_BIT(0),
246+
HOST_VISIBLE = UR_BIT(1),
247+
COUNTER_BASED = UR_BIT(2),
248+
ENABLE_PROFILER = UR_BIT(3),
249+
MULTIDEVICE = UR_BIT(4),
250+
/// @cond
251+
UR_EVENT_FLAG_FORCE_UINT32 = 0x7FFFFFFF
252+
/// @endcond
253+
254+
} ur_event_flag_t;
255+
243256
static const uint32_t UrL0Serialize = [] {
244257
const char *ZeSerializeMode = std::getenv("ZE_SERIALIZE");
245258
const char *UrL0SerializeMode = std::getenv("UR_L0_SERIALIZE");

source/adapters/level_zero/context.cpp

+41-21
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,8 @@ static const uint32_t MaxNumEventsPerPool = [] {
473473
}();
474474

475475
ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
476-
ze_event_pool_handle_t &Pool, size_t &Index, bool HostVisible,
477-
bool ProfilingEnabled, ur_device_handle_t Device,
478-
bool CounterBasedEventEnabled, bool UsingImmCmdList) {
476+
ze_event_pool_handle_t &Pool, size_t &Index, ur_event_flags_t Flags,
477+
ur_device_handle_t Device) {
479478
// Lock while updating event pool machinery.
480479
std::scoped_lock<ur_mutex> Lock(ZeEventPoolCacheMutex);
481480

@@ -485,8 +484,7 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
485484
ZeDevice = Device->ZeDevice;
486485
}
487486
std::list<ze_event_pool_handle_t> *ZePoolCache =
488-
getZeEventPoolCache(HostVisible, ProfilingEnabled,
489-
CounterBasedEventEnabled, UsingImmCmdList, ZeDevice);
487+
getZeEventPoolCache(Flags, ZeDevice);
490488

491489
if (!ZePoolCache->empty()) {
492490
if (NumEventsAvailableInEventPool[ZePoolCache->front()] == 0) {
@@ -517,14 +515,14 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
517515
ZeEventPoolDesc.count = MaxNumEventsPerPool;
518516
ZeEventPoolDesc.flags = 0;
519517
ZeEventPoolDesc.pNext = nullptr;
520-
if (HostVisible)
518+
if (Flags & HOST_VISIBLE)
521519
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
522-
if (ProfilingEnabled)
520+
if (Flags & ENABLE_PROFILER)
523521
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
524522
logger::debug("ze_event_pool_desc_t flags set to: {}",
525523
ZeEventPoolDesc.flags);
526-
if (CounterBasedEventEnabled) {
527-
if (UsingImmCmdList) {
524+
if (Flags & COUNTER_BASED) {
525+
if (Flags & USING_IMM_CMDLIST) {
528526
counterBasedExt.flags = ZE_EVENT_POOL_COUNTER_BASED_EXP_FLAG_IMMEDIATE;
529527
} else {
530528
counterBasedExt.flags =
@@ -558,19 +556,24 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
558556
return UR_RESULT_SUCCESS;
559557
}
560558

561-
ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
562-
bool HostVisible, bool WithProfiling, ur_device_handle_t Device,
563-
bool CounterBasedEventEnabled) {
559+
ur_event_handle_t
560+
ur_context_handle_t_::getEventFromContextCache(ur_event_flags_t Flags,
561+
ur_device_handle_t Device) {
564562
std::scoped_lock<ur_mutex> Lock(EventCacheMutex);
565-
auto Cache = getEventCache(HostVisible, WithProfiling, Device);
563+
auto Cache = getEventCache(Flags, Device);
564+
if (Flags & COUNTER_BASED) {
565+
Cache = getCounterBasedEventCache(Flags, Device);
566+
}
566567
if (Cache->empty())
567568
return nullptr;
568569

569570
auto It = Cache->begin();
570571
ur_event_handle_t Event = *It;
571-
if (Event->CounterBasedEventsEnabled != CounterBasedEventEnabled) {
572+
if (Event->CounterBasedEventsEnabled !=
573+
static_cast<bool>(Flags & COUNTER_BASED)) {
572574
return nullptr;
573575
}
576+
574577
Cache->erase(It);
575578
// We have to reset event before using it.
576579
Event->reset();
@@ -584,10 +587,19 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
584587
if (!Event->IsMultiDevice && Event->UrQueue) {
585588
Device = Event->UrQueue->Device;
586589
}
587-
588-
auto Cache = getEventCache(Event->isHostVisible(),
589-
Event->isProfilingEnabled(), Device);
590-
Cache->emplace_back(Event);
590+
ur_event_flags_t Flags = 0;
591+
if (Event->isHostVisible())
592+
Flags |= HOST_VISIBLE;
593+
if (Event->isProfilingEnabled())
594+
Flags |= ENABLE_PROFILER;
595+
596+
if (Event->CounterBasedEventsEnabled) {
597+
auto Cache = getCounterBasedEventCache(Flags, Device);
598+
Cache->emplace_back(Event);
599+
} else {
600+
auto Cache = getEventCache(Flags, Device);
601+
Cache->emplace_back(Event);
602+
}
591603
}
592604

593605
ur_result_t
@@ -609,9 +621,17 @@ ur_context_handle_t_::decrementUnreleasedEventsInPool(ur_event_handle_t Event) {
609621
ZeDevice = Event->UrQueue->Device->ZeDevice;
610622
}
611623

612-
std::list<ze_event_pool_handle_t> *ZePoolCache = getZeEventPoolCache(
613-
Event->isHostVisible(), Event->isProfilingEnabled(),
614-
Event->CounterBasedEventsEnabled, UsingImmediateCommandlists, ZeDevice);
624+
ur_event_flags_t Flags = 0;
625+
if (Event->isHostVisible())
626+
Flags |= HOST_VISIBLE;
627+
if (Event->isProfilingEnabled())
628+
Flags |= ENABLE_PROFILER;
629+
if (Event->CounterBasedEventsEnabled)
630+
Flags |= COUNTER_BASED;
631+
if (UsingImmediateCommandlists)
632+
Flags |= USING_IMM_CMDLIST;
633+
std::list<ze_event_pool_handle_t> *ZePoolCache =
634+
getZeEventPoolCache(Flags, ZeDevice);
615635

616636
// Put the empty pool to the cache of the pools.
617637
if (NumEventsUnreleasedInEventPool[Event->ZeEventPool] == 0)

source/adapters/level_zero/context.hpp

+66-57
Original file line numberDiff line numberDiff line change
@@ -204,75 +204,49 @@ struct ur_context_handle_t_ : _ur_object {
204204
// slot for a host-visible event. The ProfilingEnabled tells is we need a
205205
// slot for an event with profiling capabilities.
206206
ur_result_t getFreeSlotInExistingOrNewPool(ze_event_pool_handle_t &, size_t &,
207-
bool HostVisible,
208-
bool ProfilingEnabled,
209-
ur_device_handle_t Device,
210-
bool CounterBasedEventEnabled,
211-
bool UsingImmCmdList);
207+
ur_event_flags_t Flags,
208+
ur_device_handle_t Device);
212209

213210
// 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);
211+
ur_event_handle_t getEventFromContextCache(ur_event_flags_t Flags,
212+
ur_device_handle_t Device);
218213

219214
// Add ur_event_handle_t to cache.
220215
void addEventToContextCache(ur_event_handle_t);
221216

222-
enum EventPoolCacheType {
223-
HostVisibleCacheType,
224-
HostInvisibleCacheType,
225-
HostVisibleCounterBasedRegularCacheType,
226-
HostInvisibleCounterBasedRegularCacheType,
227-
HostVisibleCounterBasedImmediateCacheType,
228-
HostInvisibleCounterBasedImmediateCacheType
217+
enum EventCacheType {
218+
HostVisibleProfilingCacheType,
219+
HostVisibleRegularCacheType,
220+
HostInvisibleProfilingCacheType,
221+
HostInvisibleRegularCacheType,
222+
CounterBasedImmediateCacheType,
223+
CounterBasedRegularCacheType,
224+
CounterBasedImmediateProfilingCacheType,
225+
CounterBasedRegularProfilingCacheType,
226+
EventCacheTypeCount
229227
};
230-
231228
std::list<ze_event_pool_handle_t> *
232-
getZeEventPoolCache(bool HostVisible, bool WithProfiling,
233-
bool CounterBasedEventEnabled, bool UsingImmediateCmdList,
234-
ze_device_handle_t ZeDevice) {
235-
EventPoolCacheType CacheType;
229+
getZeEventPoolCache(ur_event_flags_t Flags, ze_device_handle_t ZeDevice) {
230+
bool WithProfiling = Flags & ENABLE_PROFILER;
231+
Flags &= ~ENABLE_PROFILER;
232+
Flags &= ~MULTIDEVICE;
233+
int index = static_cast<int>(Flags);
234+
(Flags & COUNTER_BASED) ? index /= 2 : (index - 2);
236235

237-
calculateCacheIndex(HostVisible, CounterBasedEventEnabled,
238-
UsingImmediateCmdList, CacheType);
239236
if (ZeDevice) {
240237
auto ZeEventPoolCacheMap =
241-
WithProfiling ? &ZeEventPoolCacheDeviceMap[CacheType * 2]
242-
: &ZeEventPoolCacheDeviceMap[CacheType * 2 + 1];
238+
WithProfiling ? &ZeEventPoolCacheDeviceMap[index * 2]
239+
: &ZeEventPoolCacheDeviceMap[index * 2 + 1];
243240
if (ZeEventPoolCacheMap->find(ZeDevice) == ZeEventPoolCacheMap->end()) {
244241
ZeEventPoolCache.emplace_back();
245242
ZeEventPoolCacheMap->insert(
246243
std::make_pair(ZeDevice, ZeEventPoolCache.size() - 1));
247244
}
248245
return &ZeEventPoolCache[(*ZeEventPoolCacheMap)[ZeDevice]];
249246
} else {
250-
return WithProfiling ? &ZeEventPoolCache[CacheType * 2]
251-
: &ZeEventPoolCache[CacheType * 2 + 1];
252-
}
253-
}
254-
255-
ur_result_t calculateCacheIndex(bool HostVisible,
256-
bool CounterBasedEventEnabled,
257-
bool UsingImmediateCmdList,
258-
EventPoolCacheType &CacheType) {
259-
if (CounterBasedEventEnabled && HostVisible && !UsingImmediateCmdList) {
260-
CacheType = HostVisibleCounterBasedRegularCacheType;
261-
} else if (CounterBasedEventEnabled && !HostVisible &&
262-
!UsingImmediateCmdList) {
263-
CacheType = HostInvisibleCounterBasedRegularCacheType;
264-
} else if (CounterBasedEventEnabled && HostVisible &&
265-
UsingImmediateCmdList) {
266-
CacheType = HostVisibleCounterBasedImmediateCacheType;
267-
} else if (CounterBasedEventEnabled && !HostVisible &&
268-
UsingImmediateCmdList) {
269-
CacheType = HostInvisibleCounterBasedImmediateCacheType;
270-
} else if (!CounterBasedEventEnabled && HostVisible) {
271-
CacheType = HostVisibleCacheType;
272-
} else {
273-
CacheType = HostInvisibleCacheType;
247+
return WithProfiling ? &ZeEventPoolCache[index * 2]
248+
: &ZeEventPoolCache[index * 2 + 1];
274249
}
275-
return UR_RESULT_SUCCESS;
276250
}
277251

278252
// Decrement number of events living in the pool upon event destroy
@@ -314,33 +288,68 @@ struct ur_context_handle_t_ : _ur_object {
314288

315289
private:
316290
// 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) {
319-
if (HostVisible) {
291+
auto getEventCache(ur_event_flags_t Flags, ur_device_handle_t Device) {
292+
if (Flags & HOST_VISIBLE) {
320293
if (Device) {
321294
auto EventCachesMap =
322-
WithProfiling ? &EventCachesDeviceMap[0] : &EventCachesDeviceMap[1];
295+
(Flags & ENABLE_PROFILER)
296+
? &EventCachesDeviceMap[HostVisibleProfilingCacheType]
297+
: &EventCachesDeviceMap[HostVisibleRegularCacheType];
323298
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
324299
EventCaches.emplace_back();
325300
EventCachesMap->insert(
326301
std::make_pair(Device, EventCaches.size() - 1));
327302
}
328303
return &EventCaches[(*EventCachesMap)[Device]];
329304
} else {
330-
return WithProfiling ? &EventCaches[0] : &EventCaches[1];
305+
return (Flags & ENABLE_PROFILER)
306+
? &EventCaches[HostVisibleProfilingCacheType]
307+
: &EventCaches[HostVisibleRegularCacheType];
331308
}
332309
} else {
333310
if (Device) {
334311
auto EventCachesMap =
335-
WithProfiling ? &EventCachesDeviceMap[2] : &EventCachesDeviceMap[3];
312+
(Flags & ENABLE_PROFILER)
313+
? &EventCachesDeviceMap[HostInvisibleProfilingCacheType]
314+
: &EventCachesDeviceMap[HostInvisibleRegularCacheType];
336315
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
337316
EventCaches.emplace_back();
338317
EventCachesMap->insert(
339318
std::make_pair(Device, EventCaches.size() - 1));
340319
}
341320
return &EventCaches[(*EventCachesMap)[Device]];
342321
} else {
343-
return WithProfiling ? &EventCaches[2] : &EventCaches[3];
322+
return (Flags & ENABLE_PROFILER)
323+
? &EventCaches[HostInvisibleProfilingCacheType]
324+
: &EventCaches[HostInvisibleRegularCacheType];
325+
}
326+
}
327+
};
328+
auto getCounterBasedEventCache(ur_event_flags_t Flags,
329+
ur_device_handle_t Device) {
330+
if (Flags & USING_IMM_CMDLIST) {
331+
if (Device) {
332+
auto EventCachesMap =
333+
(Flags & ENABLE_PROFILER)
334+
? &EventCachesDeviceMap[CounterBasedImmediateProfilingCacheType]
335+
: &EventCachesDeviceMap[CounterBasedImmediateCacheType];
336+
return &EventCaches[(*EventCachesMap)[Device]];
337+
} else {
338+
return (Flags & ENABLE_PROFILER)
339+
? &EventCaches[CounterBasedImmediateProfilingCacheType]
340+
: &EventCaches[CounterBasedImmediateCacheType];
341+
}
342+
} else {
343+
if (Device) {
344+
auto EventCachesMap =
345+
(Flags & ENABLE_PROFILER)
346+
? &EventCachesDeviceMap[CounterBasedRegularProfilingCacheType]
347+
: &EventCachesDeviceMap[CounterBasedRegularCacheType];
348+
return &EventCaches[(*EventCachesMap)[Device]];
349+
} else {
350+
return (Flags & ENABLE_PROFILER)
351+
? &EventCaches[CounterBasedRegularProfilingCacheType]
352+
: &EventCaches[CounterBasedRegularCacheType];
344353
}
345354
}
346355
}

source/adapters/level_zero/event.cpp

+18-10
Original file line numberDiff line numberDiff line change
@@ -1277,15 +1277,24 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
12771277
bool ProfilingEnabled =
12781278
ForceDisableProfiling ? false : (!Queue || Queue->isProfilingEnabled());
12791279
bool UsingImmediateCommandlists = !Queue || Queue->UsingImmCmdLists;
1280+
ur_event_flags_t Flags = 0;
1281+
if (ProfilingEnabled)
1282+
Flags |= ENABLE_PROFILER;
1283+
if (UsingImmediateCommandlists)
1284+
Flags |= USING_IMM_CMDLIST;
1285+
if (HostVisible)
1286+
Flags |= HOST_VISIBLE;
1287+
if (IsMultiDevice)
1288+
Flags |= MULTIDEVICE;
1289+
if (CounterBasedEventEnabled)
1290+
Flags |= COUNTER_BASED;
12801291

12811292
ur_device_handle_t Device = nullptr;
1282-
1283-
if (!IsMultiDevice && Queue) {
1293+
if (!(Flags & MULTIDEVICE) && Queue) {
12841294
Device = Queue->Device;
12851295
}
12861296

1287-
if (auto CachedEvent = Context->getEventFromContextCache(
1288-
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled)) {
1297+
if (auto CachedEvent = Context->getEventFromContextCache(Flags, Device)) {
12891298
*RetEvent = CachedEvent;
12901299
return UR_RESULT_SUCCESS;
12911300
}
@@ -1295,16 +1304,15 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
12951304

12961305
size_t Index = 0;
12971306

1298-
if (auto Res = Context->getFreeSlotInExistingOrNewPool(
1299-
ZeEventPool, Index, HostVisible, ProfilingEnabled, Device,
1300-
CounterBasedEventEnabled, UsingImmediateCommandlists))
1307+
if (auto Res = Context->getFreeSlotInExistingOrNewPool(ZeEventPool, Index,
1308+
Flags, Device))
13011309
return Res;
13021310

13031311
ZeStruct<ze_event_desc_t> ZeEventDesc;
13041312
ZeEventDesc.index = Index;
13051313
ZeEventDesc.wait = 0;
13061314

1307-
if (HostVisible || CounterBasedEventEnabled) {
1315+
if ((Flags & USING_IMM_CMDLIST) || (Flags & COUNTER_BASED)) {
13081316
ZeEventDesc.signal = ZE_EVENT_SCOPE_FLAG_HOST;
13091317
} else {
13101318
//
@@ -1329,8 +1337,8 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
13291337
} catch (...) {
13301338
return UR_RESULT_ERROR_UNKNOWN;
13311339
}
1332-
(*RetEvent)->CounterBasedEventsEnabled = CounterBasedEventEnabled;
1333-
if (HostVisible)
1340+
(*RetEvent)->CounterBasedEventsEnabled = (Flags & COUNTER_BASED);
1341+
if (Flags & HOST_VISIBLE)
13341342
(*RetEvent)->HostVisibleEvent =
13351343
reinterpret_cast<ur_event_handle_t>(*RetEvent);
13361344

source/adapters/level_zero/event.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ struct ur_event_handle_t_ : _ur_object {
132132
OwnNativeHandle = OwnZeEvent;
133133
}
134134

135+
ur_event_flags_t flags;
135136
// Level Zero event handle.
136137
ze_event_handle_t ZeEvent;
137138

0 commit comments

Comments
 (0)