Skip to content

Commit 301e39e

Browse files
[L0] Refactor of boolean variables for Event
This now includes the interrupt changes and is rebased on top of getEventCache changes. Signed-off-by: Zhang, Winston <winston.zhang@intel.com>
1 parent 2a38201 commit 301e39e

File tree

5 files changed

+118
-118
lines changed

5 files changed

+118
-118
lines changed

source/adapters/level_zero/common.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,18 @@ enum {
240240
2, // blocking UR calls, where supported (usually in enqueue commands)
241241
};
242242

243+
using ur_event_flags_t = uint32_t;
244+
enum ur_event_flag_t {
245+
EVENT_FLAG_HOST_VISIBLE = UR_BIT(0),
246+
EVENT_FLAG_WITH_PROFILING = UR_BIT(1),
247+
EVENT_FLAG_COUNTER = UR_BIT(2),
248+
EVENT_FLAG_INTERRUPT = UR_BIT(3),
249+
EVENT_FLAG_IMM_CMDLIST = UR_BIT(4),
250+
EVENT_FLAG_MULTIDEVICE = UR_BIT(6),
251+
EVENT_FLAG_DEVICE = UR_BIT(7), // if set, subsequent bits are device id
252+
MAX_EVENT_FLAG_BITS = 8,
253+
};
254+
243255
static const uint32_t UrL0Serialize = [] {
244256
const char *ZeSerializeMode = std::getenv("ZE_SERIALIZE");
245257
const char *UrL0SerializeMode = std::getenv("UR_L0_SERIALIZE");

source/adapters/level_zero/context.cpp

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -494,10 +494,8 @@ static const uint32_t MaxNumEventsPerPool = [] {
494494
}();
495495

496496
ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
497-
ze_event_pool_handle_t &Pool, size_t &Index, bool HostVisible,
498-
bool ProfilingEnabled, ur_device_handle_t Device,
499-
bool CounterBasedEventEnabled, bool UsingImmCmdList,
500-
bool InterruptBasedEventEnabled) {
497+
ze_event_pool_handle_t &Pool, size_t &Index, ur_event_flags_t Flags,
498+
ur_device_handle_t Device) {
501499
// Lock while updating event pool machinery.
502500
std::scoped_lock<ur_mutex> Lock(ZeEventPoolCacheMutex);
503501

@@ -506,9 +504,8 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
506504
if (Device) {
507505
ZeDevice = Device->ZeDevice;
508506
}
509-
std::list<ze_event_pool_handle_t> *ZePoolCache = getZeEventPoolCache(
510-
HostVisible, ProfilingEnabled, CounterBasedEventEnabled, UsingImmCmdList,
511-
InterruptBasedEventEnabled, ZeDevice);
507+
std::list<ze_event_pool_handle_t> *ZePoolCache =
508+
getZeEventPoolCache(Flags, ZeDevice);
512509

513510
if (!ZePoolCache->empty()) {
514511
if (NumEventsAvailableInEventPool[ZePoolCache->front()] == 0) {
@@ -546,26 +543,26 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
546543
ZeEventPoolDesc.count = MaxNumEventsPerPool;
547544
ZeEventPoolDesc.flags = 0;
548545
ZeEventPoolDesc.pNext = nullptr;
549-
if (HostVisible)
546+
if (Flags & EVENT_FLAG_HOST_VISIBLE)
550547
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
551-
if (ProfilingEnabled)
548+
if (Flags & EVENT_FLAG_WITH_PROFILING)
552549
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
553550
logger::debug("ze_event_pool_desc_t flags set to: {}",
554551
ZeEventPoolDesc.flags);
555-
if (CounterBasedEventEnabled) {
556-
if (UsingImmCmdList) {
552+
if (Flags & EVENT_FLAG_COUNTER) {
553+
if (Flags & EVENT_FLAG_IMM_CMDLIST) {
557554
counterBasedExt.flags = ZE_EVENT_POOL_COUNTER_BASED_EXP_FLAG_IMMEDIATE;
558555
} else {
559556
counterBasedExt.flags =
560557
ZE_EVENT_POOL_COUNTER_BASED_EXP_FLAG_NON_IMMEDIATE;
561558
}
562559
logger::debug("ze_event_pool_desc_t counter based flags set to: {}",
563560
counterBasedExt.flags);
564-
if (InterruptBasedEventEnabled) {
561+
if (Flags & EVENT_FLAG_INTERRUPT) {
565562
counterBasedExt.pNext = &eventSyncMode;
566563
}
567564
ZeEventPoolDesc.pNext = &counterBasedExt;
568-
} else if (InterruptBasedEventEnabled) {
565+
} else if (Flags & EVENT_FLAG_INTERRUPT) {
569566
ZeEventPoolDesc.pNext = &eventSyncMode;
570567
}
571568

@@ -592,18 +589,17 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
592589
return UR_RESULT_SUCCESS;
593590
}
594591

595-
ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
596-
bool HostVisible, bool WithProfiling, ur_device_handle_t Device,
597-
bool CounterBasedEventEnabled, bool InterruptBasedEventEnabled) {
592+
ur_event_handle_t
593+
ur_context_handle_t_::getEventFromContextCache(ur_event_flags_t Flags,
594+
ur_device_handle_t Device) {
598595
std::scoped_lock<ur_mutex> Lock(EventCacheMutex);
599-
auto Cache =
600-
getEventCache(HostVisible, WithProfiling, Device,
601-
CounterBasedEventEnabled, InterruptBasedEventEnabled);
596+
auto Cache = getEventCache(Flags, Device);
602597
if (Cache->empty()) {
603-
logger::info("Cache empty (Host Visible: {}, Profiling: {}, Counter: {}, "
604-
"Interrupt: {}, Device: {})",
605-
HostVisible, WithProfiling, CounterBasedEventEnabled,
606-
InterruptBasedEventEnabled, Device);
598+
logger::info(
599+
"Cache empty (Host Visible: {}, Profiling: {}, Counter: {}, "
600+
"Interrupt: {}, Device: {})",
601+
(Flags & EVENT_FLAG_HOST_VISIBLE), (Flags & EVENT_FLAG_WITH_PROFILING),
602+
(Flags & EVENT_FLAG_COUNTER), (Flags & EVENT_FLAG_INTERRUPT), Device);
607603
return nullptr;
608604
}
609605

@@ -631,9 +627,16 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
631627
Device = Event->UrQueue->Device;
632628
}
633629

634-
auto Cache = getEventCache(
635-
Event->isHostVisible(), Event->isProfilingEnabled(), Device,
636-
Event->CounterBasedEventsEnabled, Event->InterruptBasedEventsEnabled);
630+
ur_event_flags_t Flags = 0;
631+
if (Event->HostVisibleEvent)
632+
Flags |= EVENT_FLAG_HOST_VISIBLE;
633+
if (Event->isProfilingEnabled())
634+
Flags |= EVENT_FLAG_WITH_PROFILING;
635+
if (Event->CounterBasedEventsEnabled)
636+
Flags |= EVENT_FLAG_COUNTER;
637+
if (Event->InterruptBasedEventsEnabled)
638+
Flags |= EVENT_FLAG_INTERRUPT;
639+
auto Cache = getEventCache(Flags, Device);
637640
logger::info("Inserting {} event (Host Visible: {}, Profiling: {}, Counter: "
638641
"{}, Device: {}) into cache {}",
639642
Event, Event->HostVisibleEvent, Event->isProfilingEnabled(),
@@ -659,11 +662,11 @@ ur_context_handle_t_::decrementUnreleasedEventsInPool(ur_event_handle_t Event) {
659662
if (!Event->IsMultiDevice && Event->UrQueue) {
660663
ZeDevice = Event->UrQueue->Device->ZeDevice;
661664
}
665+
if (UsingImmediateCommandlists)
666+
Event->Flags |= EVENT_FLAG_IMM_CMDLIST;
662667

663-
std::list<ze_event_pool_handle_t> *ZePoolCache = getZeEventPoolCache(
664-
Event->isHostVisible(), Event->isProfilingEnabled(),
665-
Event->CounterBasedEventsEnabled, UsingImmediateCommandlists,
666-
Event->InterruptBasedEventsEnabled, ZeDevice);
668+
std::list<ze_event_pool_handle_t> *ZePoolCache =
669+
getZeEventPoolCache(Event->Flags, ZeDevice);
667670

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

source/adapters/level_zero/context.hpp

Lines changed: 54 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,12 @@ struct ur_context_handle_t_ : _ur_object {
213213
// slot for a host-visible event. The ProfilingEnabled tells is we need a
214214
// slot for an event with profiling capabilities.
215215
ur_result_t getFreeSlotInExistingOrNewPool(ze_event_pool_handle_t &, size_t &,
216-
bool HostVisible,
217-
bool ProfilingEnabled,
218-
ur_device_handle_t Device,
219-
bool CounterBasedEventEnabled,
220-
bool UsingImmCmdList,
221-
bool InterruptBasedEventEnabled);
216+
ur_event_flags_t Flags,
217+
ur_device_handle_t Device);
222218

223219
// Get ur_event_handle_t from cache.
224-
ur_event_handle_t getEventFromContextCache(bool HostVisible,
225-
bool WithProfiling,
226-
ur_device_handle_t Device,
227-
bool CounterBasedEventEnabled,
228-
bool InterruptBasedEventEnabled);
220+
ur_event_handle_t getEventFromContextCache(ur_event_flags_t Flags,
221+
ur_device_handle_t Device);
229222

230223
// Add ur_event_handle_t to cache.
231224
void addEventToContextCache(ur_event_handle_t);
@@ -250,15 +243,11 @@ struct ur_context_handle_t_ : _ur_object {
250243
};
251244

252245
std::list<ze_event_pool_handle_t> *
253-
getZeEventPoolCache(bool HostVisible, bool WithProfiling,
254-
bool CounterBasedEventEnabled, bool UsingImmediateCmdList,
255-
bool InterruptBasedEventEnabled,
256-
ze_device_handle_t ZeDevice) {
246+
getZeEventPoolCache(ur_event_flags_t Flags, ze_device_handle_t ZeDevice) {
257247
EventPoolCacheType CacheType;
248+
bool WithProfiling = Flags & EVENT_FLAG_WITH_PROFILING;
258249

259-
calculateCacheIndex(HostVisible, CounterBasedEventEnabled,
260-
UsingImmediateCmdList, InterruptBasedEventEnabled,
261-
CacheType);
250+
calculateCacheIndex(Flags, CacheType);
262251
if (ZeDevice) {
263252
auto ZeEventPoolCacheMap =
264253
WithProfiling ? &ZeEventPoolCacheDeviceMap[CacheType * 2]
@@ -275,61 +264,56 @@ struct ur_context_handle_t_ : _ur_object {
275264
}
276265
}
277266

278-
ur_result_t calculateCacheIndex(bool HostVisible,
279-
bool CounterBasedEventEnabled,
280-
bool UsingImmediateCmdList,
281-
bool InterruptBasedEventEnabled,
267+
ur_result_t calculateCacheIndex(ur_event_flags_t Flags,
282268
EventPoolCacheType &CacheType) {
269+
bool InterruptBasedEventEnabled = Flags & EVENT_FLAG_INTERRUPT;
270+
bool CounterBasedEventEnabled = Flags & EVENT_FLAG_COUNTER;
271+
bool HostVisible = Flags & EVENT_FLAG_HOST_VISIBLE;
272+
bool UsingImmediateCmdList = Flags & EVENT_FLAG_IMM_CMDLIST;
273+
283274
if (InterruptBasedEventEnabled) {
284275
if (CounterBasedEventEnabled) {
285276
if (HostVisible) {
286-
if (UsingImmediateCmdList) {
287-
CacheType = HostVisibleInterruptAndCounterBasedImmediateCacheType;
288-
} else {
289-
CacheType = HostVisibleInterruptAndCounterBasedRegularCacheType;
290-
}
277+
CacheType =
278+
UsingImmediateCmdList
279+
? HostVisibleInterruptAndCounterBasedImmediateCacheType
280+
: HostVisibleInterruptAndCounterBasedRegularCacheType;
291281
} else {
292-
if (UsingImmediateCmdList) {
293-
CacheType = HostInvisibleInterruptAndCounterBasedImmediateCacheType;
294-
} else {
295-
CacheType = HostInvisibleInterruptAndCounterBasedRegularCacheType;
296-
}
282+
CacheType =
283+
UsingImmediateCmdList
284+
? HostInvisibleInterruptAndCounterBasedImmediateCacheType
285+
: HostInvisibleInterruptAndCounterBasedRegularCacheType;
297286
}
298287
} else {
299288
if (HostVisible) {
300-
if (UsingImmediateCmdList) {
301-
CacheType = HostVisibleInterruptBasedImmediateCacheType;
302-
} else {
303-
CacheType = HostVisibleInterruptBasedRegularCacheType;
304-
}
289+
CacheType = UsingImmediateCmdList
290+
? HostVisibleInterruptBasedImmediateCacheType
291+
: HostVisibleInterruptBasedRegularCacheType;
305292
} else {
306-
if (UsingImmediateCmdList) {
307-
CacheType = HostInvisibleInterruptBasedImmediateCacheType;
308-
} else {
309-
CacheType = HostInvisibleInterruptBasedRegularCacheType;
310-
}
293+
CacheType = UsingImmediateCmdList
294+
? HostInvisibleInterruptBasedImmediateCacheType
295+
: HostInvisibleInterruptBasedRegularCacheType;
311296
}
312297
}
313298
} else {
314-
if (CounterBasedEventEnabled && HostVisible && !UsingImmediateCmdList) {
315-
CacheType = HostVisibleCounterBasedRegularCacheType;
316-
} else if (CounterBasedEventEnabled && !HostVisible &&
317-
!UsingImmediateCmdList) {
318-
CacheType = HostInvisibleCounterBasedRegularCacheType;
319-
} else if (CounterBasedEventEnabled && HostVisible &&
320-
UsingImmediateCmdList) {
321-
CacheType = HostVisibleCounterBasedImmediateCacheType;
322-
} else if (CounterBasedEventEnabled && !HostVisible &&
323-
UsingImmediateCmdList) {
324-
CacheType = HostInvisibleCounterBasedImmediateCacheType;
325-
} else if (!CounterBasedEventEnabled && HostVisible) {
326-
CacheType = HostVisibleCacheType;
299+
if (CounterBasedEventEnabled) {
300+
if (HostVisible) {
301+
CacheType = UsingImmediateCmdList
302+
? HostVisibleCounterBasedImmediateCacheType
303+
: HostVisibleCounterBasedRegularCacheType;
304+
} else {
305+
CacheType = UsingImmediateCmdList
306+
? HostInvisibleCounterBasedImmediateCacheType
307+
: HostInvisibleCounterBasedRegularCacheType;
308+
}
327309
} else {
328-
CacheType = HostInvisibleCacheType;
310+
CacheType = HostVisible ? HostVisibleCacheType : HostInvisibleCacheType;
329311
}
330312
}
331313

332314
return UR_RESULT_SUCCESS;
315+
316+
return UR_RESULT_SUCCESS;
333317
}
334318

335319
// Decrement number of events living in the pool upon event destroy
@@ -370,16 +354,6 @@ struct ur_context_handle_t_ : _ur_object {
370354
ze_context_handle_t getZeHandle() const;
371355

372356
private:
373-
enum EventFlags {
374-
EVENT_FLAG_HOST_VISIBLE = UR_BIT(0),
375-
EVENT_FLAG_WITH_PROFILING = UR_BIT(1),
376-
EVENT_FLAG_COUNTER = UR_BIT(2),
377-
EVENT_FLAG_INTERRUPT = UR_BIT(3),
378-
EVENT_FLAG_DEVICE = UR_BIT(4), // if set, subsequent bits are device id
379-
MAX_EVENT_FLAG_BITS =
380-
5, // this is used as an offset for embedding device id
381-
};
382-
383357
// Mutex to control operations on event caches.
384358
ur_mutex EventCacheMutex;
385359

@@ -388,26 +362,25 @@ struct ur_context_handle_t_ : _ur_object {
388362
std::vector<EventCache> EventCaches;
389363

390364
// Get the cache of events for a provided scope and profiling mode.
391-
EventCache *getEventCache(bool HostVisible, bool WithProfiling,
392-
ur_device_handle_t Device, bool Counter,
393-
bool Interrupt) {
365+
EventCache *getEventCache(ur_event_flags_t Flags, ur_device_handle_t Device) {
394366

395367
size_t index = 0;
396-
if (HostVisible) {
397-
index |= EVENT_FLAG_HOST_VISIBLE;
398-
}
399-
if (WithProfiling) {
400-
index |= EVENT_FLAG_WITH_PROFILING;
401-
}
402-
if (Counter) {
403-
index |= EVENT_FLAG_COUNTER;
404-
}
405-
if (Interrupt) {
406-
index |= EVENT_FLAG_INTERRUPT;
407-
}
368+
// if (HostVisible) {
369+
// index |= EVENT_FLAG_HOST_VISIBLE;
370+
//}
371+
// if (WithProfiling) {
372+
// index |= EVENT_FLAG_WITH_PROFILING;
373+
//}
374+
// if (Counter) {
375+
// index |= EVENT_FLAG_COUNTER;
376+
//}
377+
// if (Interrupt) {
378+
// index |= EVENT_FLAG_INTERRUPT;
379+
//}
408380
if (Device) {
409381
index |= EVENT_FLAG_DEVICE | (*Device->Id << MAX_EVENT_FLAG_BITS);
410382
}
383+
index |= Flags;
411384

412385
if (index >= EventCaches.size()) {
413386
EventCaches.resize(index + 1);

source/adapters/level_zero/event.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,16 +1334,27 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
13341334
bool ProfilingEnabled =
13351335
ForceDisableProfiling ? false : (!Queue || Queue->isProfilingEnabled());
13361336
bool UsingImmediateCommandlists = !Queue || Queue->UsingImmCmdLists;
1337+
ur_event_flags_t Flags = 0;
1338+
if (ProfilingEnabled)
1339+
Flags |= EVENT_FLAG_WITH_PROFILING;
1340+
if (UsingImmediateCommandlists)
1341+
Flags |= EVENT_FLAG_IMM_CMDLIST;
1342+
if (HostVisible)
1343+
Flags |= EVENT_FLAG_HOST_VISIBLE;
1344+
if (IsMultiDevice)
1345+
Flags |= EVENT_FLAG_MULTIDEVICE;
1346+
if (CounterBasedEventEnabled)
1347+
Flags |= EVENT_FLAG_COUNTER;
1348+
if (InterruptBasedEventEnabled)
1349+
Flags |= EVENT_FLAG_INTERRUPT;
13371350

13381351
ur_device_handle_t Device = nullptr;
13391352

13401353
if (!IsMultiDevice && Queue) {
13411354
Device = Queue->Device;
13421355
}
13431356

1344-
if (auto CachedEvent = Context->getEventFromContextCache(
1345-
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled,
1346-
InterruptBasedEventEnabled)) {
1357+
if (auto CachedEvent = Context->getEventFromContextCache(Flags, Device)) {
13471358
*RetEvent = CachedEvent;
13481359
return UR_RESULT_SUCCESS;
13491360
}
@@ -1353,10 +1364,8 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
13531364

13541365
size_t Index = 0;
13551366

1356-
if (auto Res = Context->getFreeSlotInExistingOrNewPool(
1357-
ZeEventPool, Index, HostVisible, ProfilingEnabled, Device,
1358-
CounterBasedEventEnabled, UsingImmediateCommandlists,
1359-
InterruptBasedEventEnabled))
1367+
if (auto Res = Context->getFreeSlotInExistingOrNewPool(ZeEventPool, Index,
1368+
Flags, Device))
13601369
return Res;
13611370

13621371
ZeStruct<ze_event_desc_t> ZeEventDesc;
@@ -1393,6 +1402,7 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
13931402
if (HostVisible)
13941403
(*RetEvent)->HostVisibleEvent =
13951404
reinterpret_cast<ur_event_handle_t>(*RetEvent);
1405+
(*RetEvent)->Flags = Flags;
13961406

13971407
return UR_RESULT_SUCCESS;
13981408
}

source/adapters/level_zero/event.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ struct ur_event_handle_t_ : _ur_object {
139139
// Level Zero event pool handle.
140140
ze_event_pool_handle_t ZeEventPool;
141141

142+
ur_event_flags_t Flags;
143+
142144
// In case we use device-only events this holds their host-visible
143145
// counterpart. If this event is itself host-visble then HostVisibleEvent
144146
// points to this event. If this event is not host-visible then this field can

0 commit comments

Comments
 (0)