Skip to content

Commit e1d74f2

Browse files
[L0] Refactoring of boolean event parameters
new commit for rebase Signed-off-by: Winston Zhang <[email protected]>
1 parent 167ddf9 commit e1d74f2

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
@@ -470,9 +470,8 @@ static const uint32_t MaxNumEventsPerPool = [] {
470470
}();
471471

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

@@ -482,8 +481,7 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
482481
ZeDevice = Device->ZeDevice;
483482
}
484483
std::list<ze_event_pool_handle_t> *ZePoolCache =
485-
getZeEventPoolCache(HostVisible, ProfilingEnabled,
486-
CounterBasedEventEnabled, UsingImmCmdList, ZeDevice);
484+
getZeEventPoolCache(Flags, ZeDevice);
487485

488486
if (!ZePoolCache->empty()) {
489487
if (NumEventsAvailableInEventPool[ZePoolCache->front()] == 0) {
@@ -514,14 +512,14 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
514512
ZeEventPoolDesc.count = MaxNumEventsPerPool;
515513
ZeEventPoolDesc.flags = 0;
516514
ZeEventPoolDesc.pNext = nullptr;
517-
if (HostVisible)
515+
if (Flags & HOST_VISIBLE)
518516
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
519-
if (ProfilingEnabled)
517+
if (Flags & ENABLE_PROFILER)
520518
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
521519
logger::debug("ze_event_pool_desc_t flags set to: {}",
522520
ZeEventPoolDesc.flags);
523-
if (CounterBasedEventEnabled) {
524-
if (UsingImmCmdList) {
521+
if (Flags & COUNTER_BASED) {
522+
if (Flags & USING_IMM_CMDLIST) {
525523
counterBasedExt.flags = ZE_EVENT_POOL_COUNTER_BASED_EXP_FLAG_IMMEDIATE;
526524
} else {
527525
counterBasedExt.flags =
@@ -553,17 +551,17 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
553551
return UR_RESULT_SUCCESS;
554552
}
555553

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

564562
auto It = Cache->begin();
565563
ur_event_handle_t Event = *It;
566-
if (Event->CounterBasedEventsEnabled != CounterBasedEventEnabled) {
564+
if (Event->CounterBasedEventsEnabled != (Flags & COUNTER_BASED)) {
567565
return nullptr;
568566
}
569567
Cache->erase(It);
@@ -580,8 +578,11 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
580578
Device = Legacy(Event->UrQueue)->Device;
581579
}
582580

583-
auto Cache = getEventCache(Event->isHostVisible(),
584-
Event->isProfilingEnabled(), Device);
581+
ur_event_flags_t Flags = static_cast<ur_event_flags_t>(
582+
Event->isHostVisible() ? HOST_VISIBLE
583+
: 0 | Event->isProfilingEnabled() ? ENABLE_PROFILER
584+
: 0);
585+
auto Cache = getEventCache(Flags, Device);
585586
Cache->emplace_back(Event);
586587
}
587588

@@ -604,9 +605,14 @@ ur_context_handle_t_::decrementUnreleasedEventsInPool(ur_event_handle_t Event) {
604605
ZeDevice = Legacy(Event->UrQueue)->Device->ZeDevice;
605606
}
606607

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

611617
// Put the empty pool to the cache of the pools.
612618
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)