Skip to content

Commit ae35020

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 <[email protected]>
1 parent d18d523 commit ae35020

File tree

5 files changed

+117
-117
lines changed

5 files changed

+117
-117
lines changed

source/adapters/level_zero/common.hpp

+12
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

+32-29
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,8 @@ static const uint32_t MaxNumEventsPerPool = [] {
491491
}();
492492

493493
ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
494-
ze_event_pool_handle_t &Pool, size_t &Index, bool HostVisible,
495-
bool ProfilingEnabled, ur_device_handle_t Device,
496-
bool CounterBasedEventEnabled, bool UsingImmCmdList,
497-
bool InterruptBasedEventEnabled) {
494+
ze_event_pool_handle_t &Pool, size_t &Index, ur_event_flags_t Flags,
495+
ur_device_handle_t Device) {
498496
// Lock while updating event pool machinery.
499497
std::scoped_lock<ur_mutex> Lock(ZeEventPoolCacheMutex);
500498

@@ -503,9 +501,8 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
503501
if (Device) {
504502
ZeDevice = Device->ZeDevice;
505503
}
506-
std::list<ze_event_pool_handle_t> *ZePoolCache = getZeEventPoolCache(
507-
HostVisible, ProfilingEnabled, CounterBasedEventEnabled, UsingImmCmdList,
508-
InterruptBasedEventEnabled, ZeDevice);
504+
std::list<ze_event_pool_handle_t> *ZePoolCache =
505+
getZeEventPoolCache(Flags, ZeDevice);
509506

510507
if (!ZePoolCache->empty()) {
511508
if (NumEventsAvailableInEventPool[ZePoolCache->front()] == 0) {
@@ -536,14 +533,14 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
536533
ZeEventPoolDesc.count = MaxNumEventsPerPool;
537534
ZeEventPoolDesc.flags = 0;
538535
ZeEventPoolDesc.pNext = nullptr;
539-
if (HostVisible)
536+
if (Flags & EVENT_FLAG_HOST_VISIBLE)
540537
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
541-
if (ProfilingEnabled)
538+
if (Flags & EVENT_FLAG_WITH_PROFILING)
542539
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
543540
logger::debug("ze_event_pool_desc_t flags set to: {}",
544541
ZeEventPoolDesc.flags);
545-
if (CounterBasedEventEnabled) {
546-
if (UsingImmCmdList) {
542+
if (Flags & EVENT_FLAG_COUNTER) {
543+
if (Flags & EVENT_FLAG_IMM_CMDLIST) {
547544
counterBasedExt.flags = ZE_EVENT_POOL_COUNTER_BASED_EXP_FLAG_IMMEDIATE;
548545
} else {
549546
counterBasedExt.flags =
@@ -553,7 +550,7 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
553550
counterBasedExt.flags);
554551
ZeEventPoolDesc.pNext = &counterBasedExt;
555552
}
556-
if (InterruptBasedEventEnabled) {
553+
if (Flags & EVENT_FLAG_INTERRUPT) {
557554
ze_intel_event_sync_mode_exp_desc_t eventSyncMode = {
558555
ZE_INTEL_STRUCTURE_TYPE_EVENT_SYNC_MODE_EXP_DESC, nullptr, 0};
559556
eventSyncMode.syncModeFlags =
@@ -585,18 +582,17 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
585582
return UR_RESULT_SUCCESS;
586583
}
587584

588-
ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
589-
bool HostVisible, bool WithProfiling, ur_device_handle_t Device,
590-
bool CounterBasedEventEnabled, bool InterruptBasedEventEnabled) {
585+
ur_event_handle_t
586+
ur_context_handle_t_::getEventFromContextCache(ur_event_flags_t Flags,
587+
ur_device_handle_t Device) {
591588
std::scoped_lock<ur_mutex> Lock(EventCacheMutex);
592-
auto Cache =
593-
getEventCache(HostVisible, WithProfiling, Device,
594-
CounterBasedEventEnabled, InterruptBasedEventEnabled);
589+
auto Cache = getEventCache(Flags, Device);
595590
if (Cache->empty()) {
596-
logger::info("Cache empty (Host Visible: {}, Profiling: {}, Counter: {}, "
597-
"Interrupt: {}, Device: {})",
598-
HostVisible, WithProfiling, CounterBasedEventEnabled,
599-
InterruptBasedEventEnabled, Device);
591+
logger::info(
592+
"Cache empty (Host Visible: {}, Profiling: {}, Counter: {}, "
593+
"Interrupt: {}, Device: {})",
594+
(Flags & EVENT_FLAG_HOST_VISIBLE), (Flags & EVENT_FLAG_WITH_PROFILING),
595+
(Flags & EVENT_FLAG_COUNTER), (Flags & EVENT_FLAG_INTERRUPT), Device);
600596
return nullptr;
601597
}
602598

@@ -624,9 +620,16 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
624620
Device = Event->UrQueue->Device;
625621
}
626622

627-
auto Cache = getEventCache(
628-
Event->isHostVisible(), Event->isProfilingEnabled(), Device,
629-
Event->CounterBasedEventsEnabled, Event->InterruptBasedEventsEnabled);
623+
ur_event_flags_t Flags = 0;
624+
if (Event->HostVisibleEvent)
625+
Flags |= EVENT_FLAG_HOST_VISIBLE;
626+
if (Event->isProfilingEnabled())
627+
Flags |= EVENT_FLAG_WITH_PROFILING;
628+
if (Event->CounterBasedEventsEnabled)
629+
Flags |= EVENT_FLAG_COUNTER;
630+
if (Event->InterruptBasedEventsEnabled)
631+
Flags |= EVENT_FLAG_INTERRUPT;
632+
auto Cache = getEventCache(Flags, Device);
630633
logger::info("Inserting {} event (Host Visible: {}, Profiling: {}, Counter: "
631634
"{}, Device: {}) into cache {}",
632635
Event, Event->HostVisibleEvent, Event->isProfilingEnabled(),
@@ -652,11 +655,11 @@ ur_context_handle_t_::decrementUnreleasedEventsInPool(ur_event_handle_t Event) {
652655
if (!Event->IsMultiDevice && Event->UrQueue) {
653656
ZeDevice = Event->UrQueue->Device->ZeDevice;
654657
}
658+
if (UsingImmediateCommandlists)
659+
Event->Flags |= EVENT_FLAG_IMM_CMDLIST;
655660

656-
std::list<ze_event_pool_handle_t> *ZePoolCache = getZeEventPoolCache(
657-
Event->isHostVisible(), Event->isProfilingEnabled(),
658-
Event->CounterBasedEventsEnabled, UsingImmediateCommandlists,
659-
Event->InterruptBasedEventsEnabled, ZeDevice);
661+
std::list<ze_event_pool_handle_t> *ZePoolCache =
662+
getZeEventPoolCache(Event->Flags, ZeDevice);
660663

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

source/adapters/level_zero/context.hpp

+54-81
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

+17-7
Original file line numberDiff line numberDiff line change
@@ -1308,16 +1308,27 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
13081308
bool ProfilingEnabled =
13091309
ForceDisableProfiling ? false : (!Queue || Queue->isProfilingEnabled());
13101310
bool UsingImmediateCommandlists = !Queue || Queue->UsingImmCmdLists;
1311+
ur_event_flags_t Flags = 0;
1312+
if (ProfilingEnabled)
1313+
Flags |= EVENT_FLAG_WITH_PROFILING;
1314+
if (UsingImmediateCommandlists)
1315+
Flags |= EVENT_FLAG_IMM_CMDLIST;
1316+
if (HostVisible)
1317+
Flags |= EVENT_FLAG_HOST_VISIBLE;
1318+
if (IsMultiDevice)
1319+
Flags |= EVENT_FLAG_MULTIDEVICE;
1320+
if (CounterBasedEventEnabled)
1321+
Flags |= EVENT_FLAG_COUNTER;
1322+
if (InterruptBasedEventEnabled)
1323+
Flags |= EVENT_FLAG_INTERRUPT;
13111324

13121325
ur_device_handle_t Device = nullptr;
13131326

13141327
if (!IsMultiDevice && Queue) {
13151328
Device = Queue->Device;
13161329
}
13171330

1318-
if (auto CachedEvent = Context->getEventFromContextCache(
1319-
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled,
1320-
InterruptBasedEventEnabled)) {
1331+
if (auto CachedEvent = Context->getEventFromContextCache(Flags, Device)) {
13211332
*RetEvent = CachedEvent;
13221333
return UR_RESULT_SUCCESS;
13231334
}
@@ -1327,10 +1338,8 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
13271338

13281339
size_t Index = 0;
13291340

1330-
if (auto Res = Context->getFreeSlotInExistingOrNewPool(
1331-
ZeEventPool, Index, HostVisible, ProfilingEnabled, Device,
1332-
CounterBasedEventEnabled, UsingImmediateCommandlists,
1333-
InterruptBasedEventEnabled))
1341+
if (auto Res = Context->getFreeSlotInExistingOrNewPool(ZeEventPool, Index,
1342+
Flags, Device))
13341343
return Res;
13351344

13361345
ZeStruct<ze_event_desc_t> ZeEventDesc;
@@ -1367,6 +1376,7 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
13671376
if (HostVisible)
13681377
(*RetEvent)->HostVisibleEvent =
13691378
reinterpret_cast<ur_event_handle_t>(*RetEvent);
1379+
(*RetEvent)->Flags = Flags;
13701380

13711381
return UR_RESULT_SUCCESS;
13721382
}

source/adapters/level_zero/event.hpp

+2
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)