Skip to content

Commit dd16ed7

Browse files
winstonzhang-intelpbalcer
authored andcommitted
[L0] Refactoring of boolean event parameters
new commit for rebase Signed-off-by: Winston Zhang <[email protected]>
1 parent 3fc52b2 commit dd16ed7

File tree

5 files changed

+66
-80
lines changed

5 files changed

+66
-80
lines changed

source/adapters/level_zero/common.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ enum {
216216
UrL0SerializeBlock =
217217
2, // blocking UR calls, where supported (usually in enqueue commands)
218218
};
219+
typedef uint32_t ur_event_flags_t;
220+
typedef enum ur_event_flag_t {
221+
COUNTER_BASED = UR_BIT(0),
222+
USING_IMM_CMDLIST = UR_BIT(1),
223+
HOST_VISIBLE = UR_BIT(2),
224+
ENABLE_PROFILER = UR_BIT(3),
225+
MULTIDEVICE = UR_BIT(4)
226+
} ur_event_flag_t;
219227

220228
static const uint32_t UrL0Serialize = [] {
221229
const char *ZeSerializeMode = std::getenv("ZE_SERIALIZE");

source/adapters/level_zero/context.cpp

+25-19
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,8 @@ static const uint32_t MaxNumEventsPerPool = [] {
472472
}();
473473

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

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

490488
if (!ZePoolCache->empty()) {
491489
if (NumEventsAvailableInEventPool[ZePoolCache->front()] == 0) {
@@ -516,14 +514,14 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
516514
ZeEventPoolDesc.count = MaxNumEventsPerPool;
517515
ZeEventPoolDesc.flags = 0;
518516
ZeEventPoolDesc.pNext = nullptr;
519-
if (HostVisible)
517+
if (Flags & HOST_VISIBLE)
520518
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
521-
if (ProfilingEnabled)
519+
if (Flags & ENABLE_PROFILER)
522520
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
523521
logger::debug("ze_event_pool_desc_t flags set to: {}",
524522
ZeEventPoolDesc.flags);
525-
if (CounterBasedEventEnabled) {
526-
if (UsingImmCmdList) {
523+
if (Flags & COUNTER_BASED) {
524+
if (Flags & USING_IMM_CMDLIST) {
527525
counterBasedExt.flags = ZE_EVENT_POOL_COUNTER_BASED_EXP_FLAG_IMMEDIATE;
528526
} else {
529527
counterBasedExt.flags =
@@ -555,17 +553,17 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
555553
return UR_RESULT_SUCCESS;
556554
}
557555

558-
ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
559-
bool HostVisible, bool WithProfiling, ur_device_handle_t Device,
560-
bool CounterBasedEventEnabled) {
556+
ur_event_handle_t
557+
ur_context_handle_t_::getEventFromContextCache(ur_event_flags_t Flags,
558+
ur_device_handle_t Device) {
561559
std::scoped_lock<ur_mutex> Lock(EventCacheMutex);
562-
auto Cache = getEventCache(HostVisible, WithProfiling, Device);
560+
auto Cache = getEventCache(Flags, Device);
563561
if (Cache->empty())
564562
return nullptr;
565563

566564
auto It = Cache->begin();
567565
ur_event_handle_t Event = *It;
568-
if (Event->CounterBasedEventsEnabled != CounterBasedEventEnabled) {
566+
if (Event->CounterBasedEventsEnabled != (Flags & COUNTER_BASED)) {
569567
return nullptr;
570568
}
571569
Cache->erase(It);
@@ -582,8 +580,11 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
582580
Device = Legacy(Event->UrQueue)->Device;
583581
}
584582

585-
auto Cache = getEventCache(Event->isHostVisible(),
586-
Event->isProfilingEnabled(), Device);
583+
ur_event_flags_t Flags = static_cast<ur_event_flags_t>(
584+
Event->isHostVisible() ? HOST_VISIBLE
585+
: 0 | Event->isProfilingEnabled() ? ENABLE_PROFILER
586+
: 0);
587+
auto Cache = getEventCache(Flags, Device);
587588
Cache->emplace_back(Event);
588589
}
589590

@@ -606,9 +607,14 @@ ur_context_handle_t_::decrementUnreleasedEventsInPool(ur_event_handle_t Event) {
606607
ZeDevice = Legacy(Event->UrQueue)->Device->ZeDevice;
607608
}
608609

609-
std::list<ze_event_pool_handle_t> *ZePoolCache = getZeEventPoolCache(
610-
Event->isHostVisible(), Event->isProfilingEnabled(),
611-
Event->CounterBasedEventsEnabled, UsingImmediateCommandlists, ZeDevice);
610+
ur_event_flags_t Flags = static_cast<ur_event_flags_t>(
611+
Event->isHostVisible() ? HOST_VISIBLE
612+
: 0 | Event->isProfilingEnabled() ? ENABLE_PROFILER
613+
: 0 | Event->CounterBasedEventsEnabled ? COUNTER_BASED
614+
: 0 | UsingImmediateCommandlists ? USING_IMM_CMDLIST
615+
: 0);
616+
std::list<ze_event_pool_handle_t> *ZePoolCache =
617+
getZeEventPoolCache(Flags, ZeDevice);
612618

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

source/adapters/level_zero/context.hpp

+18-52
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,12 @@ struct ur_context_handle_t_ : _ur_object {
198198
// slot for a host-visible event. The ProfilingEnabled tells is we need a
199199
// slot for an event with profiling capabilities.
200200
ur_result_t getFreeSlotInExistingOrNewPool(ze_event_pool_handle_t &, size_t &,
201-
bool HostVisible,
202-
bool ProfilingEnabled,
203-
ur_device_handle_t Device,
204-
bool CounterBasedEventEnabled,
205-
bool UsingImmCmdList);
201+
ur_event_flags_t Flags,
202+
ur_device_handle_t Device);
206203

207204
// Get ur_event_handle_t from cache.
208-
ur_event_handle_t getEventFromContextCache(bool HostVisible,
209-
bool WithProfiling,
210-
ur_device_handle_t Device,
211-
bool CounterBasedEventEnabled);
205+
ur_event_handle_t getEventFromContextCache(ur_event_flags_t Flags,
206+
ur_device_handle_t Device);
212207

213208
// Add ur_event_handle_t to cache.
214209
void addEventToContextCache(ur_event_handle_t);
@@ -223,50 +218,20 @@ struct ur_context_handle_t_ : _ur_object {
223218
};
224219

225220
std::list<ze_event_pool_handle_t> *
226-
getZeEventPoolCache(bool HostVisible, bool WithProfiling,
227-
bool CounterBasedEventEnabled, bool UsingImmediateCmdList,
228-
ze_device_handle_t ZeDevice) {
229-
EventPoolCacheType CacheType;
230-
231-
calculateCacheIndex(HostVisible, CounterBasedEventEnabled,
232-
UsingImmediateCmdList, CacheType);
221+
getZeEventPoolCache(ur_event_flags_t Flags, ze_device_handle_t ZeDevice) {
222+
Flags = static_cast<ur_event_flag_t>(Flags & ~MULTIDEVICE);
233223
if (ZeDevice) {
234224
auto ZeEventPoolCacheMap =
235-
WithProfiling ? &ZeEventPoolCacheDeviceMap[CacheType * 2]
236-
: &ZeEventPoolCacheDeviceMap[CacheType * 2 + 1];
225+
&ZeEventPoolCacheDeviceMap[static_cast<int>(Flags)];
237226
if (ZeEventPoolCacheMap->find(ZeDevice) == ZeEventPoolCacheMap->end()) {
238227
ZeEventPoolCache.emplace_back();
239228
ZeEventPoolCacheMap->insert(
240229
std::make_pair(ZeDevice, ZeEventPoolCache.size() - 1));
241230
}
242231
return &ZeEventPoolCache[(*ZeEventPoolCacheMap)[ZeDevice]];
243232
} else {
244-
return WithProfiling ? &ZeEventPoolCache[CacheType * 2]
245-
: &ZeEventPoolCache[CacheType * 2 + 1];
246-
}
247-
}
248-
249-
ur_result_t calculateCacheIndex(bool HostVisible,
250-
bool CounterBasedEventEnabled,
251-
bool UsingImmediateCmdList,
252-
EventPoolCacheType &CacheType) {
253-
if (CounterBasedEventEnabled && HostVisible && !UsingImmediateCmdList) {
254-
CacheType = HostVisibleCounterBasedRegularCacheType;
255-
} else if (CounterBasedEventEnabled && !HostVisible &&
256-
!UsingImmediateCmdList) {
257-
CacheType = HostInvisibleCounterBasedRegularCacheType;
258-
} else if (CounterBasedEventEnabled && HostVisible &&
259-
UsingImmediateCmdList) {
260-
CacheType = HostVisibleCounterBasedImmediateCacheType;
261-
} else if (CounterBasedEventEnabled && !HostVisible &&
262-
UsingImmediateCmdList) {
263-
CacheType = HostInvisibleCounterBasedImmediateCacheType;
264-
} else if (!CounterBasedEventEnabled && HostVisible) {
265-
CacheType = HostVisibleCacheType;
266-
} else {
267-
CacheType = HostInvisibleCacheType;
233+
return &ZeEventPoolCache[static_cast<int>(Flags)];
268234
}
269-
return UR_RESULT_SUCCESS;
270235
}
271236

272237
// Decrement number of events living in the pool upon event destroy
@@ -305,33 +270,34 @@ struct ur_context_handle_t_ : _ur_object {
305270

306271
private:
307272
// Get the cache of events for a provided scope and profiling mode.
308-
auto getEventCache(bool HostVisible, bool WithProfiling,
309-
ur_device_handle_t Device) {
310-
if (HostVisible) {
273+
auto getEventCache(ur_event_flags_t Flags, ur_device_handle_t Device) {
274+
if (Flags & HOST_VISIBLE) {
311275
if (Device) {
312-
auto EventCachesMap =
313-
WithProfiling ? &EventCachesDeviceMap[0] : &EventCachesDeviceMap[1];
276+
auto EventCachesMap = Flags & ENABLE_PROFILER
277+
? &EventCachesDeviceMap[0]
278+
: &EventCachesDeviceMap[1];
314279
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
315280
EventCaches.emplace_back();
316281
EventCachesMap->insert(
317282
std::make_pair(Device, EventCaches.size() - 1));
318283
}
319284
return &EventCaches[(*EventCachesMap)[Device]];
320285
} else {
321-
return WithProfiling ? &EventCaches[0] : &EventCaches[1];
286+
return Flags & ENABLE_PROFILER ? &EventCaches[0] : &EventCaches[1];
322287
}
323288
} else {
324289
if (Device) {
325-
auto EventCachesMap =
326-
WithProfiling ? &EventCachesDeviceMap[2] : &EventCachesDeviceMap[3];
290+
auto EventCachesMap = Flags & ENABLE_PROFILER
291+
? &EventCachesDeviceMap[2]
292+
: &EventCachesDeviceMap[3];
327293
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
328294
EventCaches.emplace_back();
329295
EventCachesMap->insert(
330296
std::make_pair(Device, EventCaches.size() - 1));
331297
}
332298
return &EventCaches[(*EventCachesMap)[Device]];
333299
} else {
334-
return WithProfiling ? &EventCaches[2] : &EventCaches[3];
300+
return Flags & ENABLE_PROFILER ? &EventCaches[2] : &EventCaches[3];
335301
}
336302
}
337303
}

source/adapters/level_zero/event.cpp

+14-9
Original file line numberDiff line numberDiff line change
@@ -1236,15 +1236,21 @@ ur_result_t EventCreate(ur_context_handle_t Context,
12361236
bool ProfilingEnabled =
12371237
ForceDisableProfiling ? false : (!Queue || Queue->isProfilingEnabled());
12381238
bool UsingImmediateCommandlists = !Queue || Queue->UsingImmCmdLists;
1239+
ur_event_flags_t Flags = static_cast<ur_event_flags_t>(
1240+
ProfilingEnabled ? ENABLE_PROFILER
1241+
: 0 | UsingImmediateCommandlists ? USING_IMM_CMDLIST
1242+
: 0 | HostVisible ? HOST_VISIBLE
1243+
: 0 | IsMultiDevice ? MULTIDEVICE
1244+
: 0 | CounterBasedEventEnabled ? COUNTER_BASED
1245+
: 0);
12391246

12401247
ur_device_handle_t Device = nullptr;
12411248

1242-
if (!IsMultiDevice && Queue) {
1249+
if (Flags & MULTIDEVICE && Queue) {
12431250
Device = Queue->Device;
12441251
}
12451252

1246-
if (auto CachedEvent = Context->getEventFromContextCache(
1247-
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled)) {
1253+
if (auto CachedEvent = Context->getEventFromContextCache(Flags, Device)) {
12481254
*RetEvent = CachedEvent;
12491255
return UR_RESULT_SUCCESS;
12501256
}
@@ -1254,16 +1260,15 @@ ur_result_t EventCreate(ur_context_handle_t Context,
12541260

12551261
size_t Index = 0;
12561262

1257-
if (auto Res = Context->getFreeSlotInExistingOrNewPool(
1258-
ZeEventPool, Index, HostVisible, ProfilingEnabled, Device,
1259-
CounterBasedEventEnabled, UsingImmediateCommandlists))
1263+
if (auto Res = Context->getFreeSlotInExistingOrNewPool(ZeEventPool, Index,
1264+
Flags, Device))
12601265
return Res;
12611266

12621267
ZeStruct<ze_event_desc_t> ZeEventDesc;
12631268
ZeEventDesc.index = Index;
12641269
ZeEventDesc.wait = 0;
12651270

1266-
if (HostVisible || CounterBasedEventEnabled) {
1271+
if (Flags & USING_IMM_CMDLIST || Flags & COUNTER_BASED) {
12671272
ZeEventDesc.signal = ZE_EVENT_SCOPE_FLAG_HOST;
12681273
} else {
12691274
//
@@ -1288,8 +1293,8 @@ ur_result_t EventCreate(ur_context_handle_t Context,
12881293
} catch (...) {
12891294
return UR_RESULT_ERROR_UNKNOWN;
12901295
}
1291-
(*RetEvent)->CounterBasedEventsEnabled = CounterBasedEventEnabled;
1292-
if (HostVisible)
1296+
(*RetEvent)->CounterBasedEventsEnabled = Flags & COUNTER_BASED;
1297+
if (Flags & HOST_VISIBLE)
12931298
(*RetEvent)->HostVisibleEvent =
12941299
reinterpret_cast<ur_event_handle_t>(*RetEvent);
12951300

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)