Skip to content

Commit 435f199

Browse files
[L0] Refactoring of boolean event parameters
Reverted deleted section. Signed-off-by: Winston Zhang <[email protected]>
1 parent ea00936 commit 435f199

File tree

5 files changed

+78
-66
lines changed

5 files changed

+78
-66
lines changed

source/adapters/level_zero/common.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ enum {
218218
UrL0SerializeBlock =
219219
2, // blocking UR calls, where supported (usually in enqueue commands)
220220
};
221+
typedef enum {
222+
COUNTER_BASED = 1 << 0,
223+
USING_IMM_CMDLIST = 1 << 1,
224+
HOST_VISIBLE = 1 << 2,
225+
ENABLE_PROFILER = 1 << 3,
226+
MULTIDEVICE = 1 << 4
227+
} EventFlags;
221228

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

source/adapters/level_zero/context.cpp

Lines changed: 25 additions & 19 deletions
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, EventFlags 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(EventFlags 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 = Event->UrQueue->Device;
581579
}
582580

583-
auto Cache = getEventCache(Event->isHostVisible(),
584-
Event->isProfilingEnabled(), Device);
581+
EventFlags Flags = static_cast<EventFlags>(
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 = 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+
EventFlags Flags = static_cast<EventFlags>(
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

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,12 @@ struct ur_context_handle_t_ : _ur_object {
197197
// slot for a host-visible event. The ProfilingEnabled tells is we need a
198198
// slot for an event with profiling capabilities.
199199
ur_result_t getFreeSlotInExistingOrNewPool(ze_event_pool_handle_t &, size_t &,
200-
bool HostVisible,
201-
bool ProfilingEnabled,
202-
ur_device_handle_t Device,
203-
bool CounterBasedEventEnabled,
204-
bool UsingImmCmdList);
200+
EventFlags Flags,
201+
ur_device_handle_t Device);
205202

206203
// Get ur_event_handle_t from cache.
207-
ur_event_handle_t getEventFromContextCache(bool HostVisible,
208-
bool WithProfiling,
209-
ur_device_handle_t Device,
210-
bool CounterBasedEventEnabled);
204+
ur_event_handle_t getEventFromContextCache(EventFlags Flags,
205+
ur_device_handle_t Device);
211206

212207
// Add ur_event_handle_t to cache.
213208
void addEventToContextCache(ur_event_handle_t);
@@ -222,45 +217,42 @@ struct ur_context_handle_t_ : _ur_object {
222217
};
223218

224219
std::list<ze_event_pool_handle_t> *
225-
getZeEventPoolCache(bool HostVisible, bool WithProfiling,
226-
bool CounterBasedEventEnabled, bool UsingImmediateCmdList,
227-
ze_device_handle_t ZeDevice) {
220+
getZeEventPoolCache(EventFlags Flags, ze_device_handle_t ZeDevice) {
228221
EventPoolCacheType CacheType;
229222

230-
calculateCacheIndex(HostVisible, CounterBasedEventEnabled,
231-
UsingImmediateCmdList, CacheType);
223+
calculateCacheIndex(Flags, CacheType);
232224
if (ZeDevice) {
233225
auto ZeEventPoolCacheMap =
234-
WithProfiling ? &ZeEventPoolCacheDeviceMap[CacheType * 2]
235-
: &ZeEventPoolCacheDeviceMap[CacheType * 2 + 1];
226+
Flags & ENABLE_PROFILER
227+
? &ZeEventPoolCacheDeviceMap[CacheType * 2]
228+
: &ZeEventPoolCacheDeviceMap[CacheType * 2 + 1];
236229
if (ZeEventPoolCacheMap->find(ZeDevice) == ZeEventPoolCacheMap->end()) {
237230
ZeEventPoolCache.emplace_back();
238231
ZeEventPoolCacheMap->insert(
239232
std::make_pair(ZeDevice, ZeEventPoolCache.size() - 1));
240233
}
241234
return &ZeEventPoolCache[(*ZeEventPoolCacheMap)[ZeDevice]];
242235
} else {
243-
return WithProfiling ? &ZeEventPoolCache[CacheType * 2]
244-
: &ZeEventPoolCache[CacheType * 2 + 1];
236+
return Flags & ENABLE_PROFILER ? &ZeEventPoolCache[CacheType * 2]
237+
: &ZeEventPoolCache[CacheType * 2 + 1];
245238
}
246239
}
247240

248-
ur_result_t calculateCacheIndex(bool HostVisible,
249-
bool CounterBasedEventEnabled,
250-
bool UsingImmediateCmdList,
241+
ur_result_t calculateCacheIndex(EventFlags Flags,
251242
EventPoolCacheType &CacheType) {
252-
if (CounterBasedEventEnabled && HostVisible && !UsingImmediateCmdList) {
243+
if (Flags & COUNTER_BASED && Flags & HOST_VISIBLE &&
244+
!(Flags & USING_IMM_CMDLIST)) {
253245
CacheType = HostVisibleCounterBasedRegularCacheType;
254-
} else if (CounterBasedEventEnabled && !HostVisible &&
255-
!UsingImmediateCmdList) {
246+
} else if (Flags & COUNTER_BASED && !(Flags & HOST_VISIBLE) &&
247+
!(Flags & USING_IMM_CMDLIST)) {
256248
CacheType = HostInvisibleCounterBasedRegularCacheType;
257-
} else if (CounterBasedEventEnabled && HostVisible &&
258-
UsingImmediateCmdList) {
249+
} else if (Flags & COUNTER_BASED && Flags & HOST_VISIBLE &&
250+
Flags & USING_IMM_CMDLIST) {
259251
CacheType = HostVisibleCounterBasedImmediateCacheType;
260-
} else if (CounterBasedEventEnabled && !HostVisible &&
261-
UsingImmediateCmdList) {
252+
} else if (Flags & COUNTER_BASED && !Flags & HOST_VISIBLE &&
253+
Flags & USING_IMM_CMDLIST) {
262254
CacheType = HostInvisibleCounterBasedImmediateCacheType;
263-
} else if (!CounterBasedEventEnabled && HostVisible) {
255+
} else if (!(Flags & COUNTER_BASED) && Flags & HOST_VISIBLE) {
264256
CacheType = HostVisibleCacheType;
265257
} else {
266258
CacheType = HostInvisibleCacheType;
@@ -304,33 +296,34 @@ struct ur_context_handle_t_ : _ur_object {
304296

305297
private:
306298
// Get the cache of events for a provided scope and profiling mode.
307-
auto getEventCache(bool HostVisible, bool WithProfiling,
308-
ur_device_handle_t Device) {
309-
if (HostVisible) {
299+
auto getEventCache(EventFlags Flags, ur_device_handle_t Device) {
300+
if (Flags & HOST_VISIBLE) {
310301
if (Device) {
311-
auto EventCachesMap =
312-
WithProfiling ? &EventCachesDeviceMap[0] : &EventCachesDeviceMap[1];
302+
auto EventCachesMap = Flags & ENABLE_PROFILER
303+
? &EventCachesDeviceMap[0]
304+
: &EventCachesDeviceMap[1];
313305
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
314306
EventCaches.emplace_back();
315307
EventCachesMap->insert(
316308
std::make_pair(Device, EventCaches.size() - 1));
317309
}
318310
return &EventCaches[(*EventCachesMap)[Device]];
319311
} else {
320-
return WithProfiling ? &EventCaches[0] : &EventCaches[1];
312+
return Flags & ENABLE_PROFILER ? &EventCaches[0] : &EventCaches[1];
321313
}
322314
} else {
323315
if (Device) {
324-
auto EventCachesMap =
325-
WithProfiling ? &EventCachesDeviceMap[2] : &EventCachesDeviceMap[3];
316+
auto EventCachesMap = Flags & ENABLE_PROFILER
317+
? &EventCachesDeviceMap[2]
318+
: &EventCachesDeviceMap[3];
326319
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
327320
EventCaches.emplace_back();
328321
EventCachesMap->insert(
329322
std::make_pair(Device, EventCaches.size() - 1));
330323
}
331324
return &EventCaches[(*EventCachesMap)[Device]];
332325
} else {
333-
return WithProfiling ? &EventCaches[2] : &EventCaches[3];
326+
return Flags & ENABLE_PROFILER ? &EventCaches[2] : &EventCaches[3];
334327
}
335328
}
336329
}

source/adapters/level_zero/event.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,15 +1229,21 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
12291229
bool ProfilingEnabled =
12301230
ForceDisableProfiling ? false : (!Queue || Queue->isProfilingEnabled());
12311231
bool UsingImmediateCommandlists = !Queue || Queue->UsingImmCmdLists;
1232+
EventFlags Flags = static_cast<EventFlags>(
1233+
ProfilingEnabled ? ENABLE_PROFILER
1234+
: 0 | UsingImmediateCommandlists ? USING_IMM_CMDLIST
1235+
: 0 | HostVisible ? HOST_VISIBLE
1236+
: 0 | IsMultiDevice ? MULTIDEVICE
1237+
: 0 | CounterBasedEventEnabled ? COUNTER_BASED
1238+
: 0);
12321239

12331240
ur_device_handle_t Device = nullptr;
12341241

1235-
if (!IsMultiDevice && Queue) {
1242+
if (Flags & MULTIDEVICE && Queue) {
12361243
Device = Queue->Device;
12371244
}
12381245

1239-
if (auto CachedEvent = Context->getEventFromContextCache(
1240-
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled)) {
1246+
if (auto CachedEvent = Context->getEventFromContextCache(Flags, Device)) {
12411247
*RetEvent = CachedEvent;
12421248
return UR_RESULT_SUCCESS;
12431249
}
@@ -1247,16 +1253,15 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
12471253

12481254
size_t Index = 0;
12491255

1250-
if (auto Res = Context->getFreeSlotInExistingOrNewPool(
1251-
ZeEventPool, Index, HostVisible, ProfilingEnabled, Device,
1252-
CounterBasedEventEnabled, UsingImmediateCommandlists))
1256+
if (auto Res = Context->getFreeSlotInExistingOrNewPool(ZeEventPool, Index,
1257+
Flags, Device))
12531258
return Res;
12541259

12551260
ZeStruct<ze_event_desc_t> ZeEventDesc;
12561261
ZeEventDesc.index = Index;
12571262
ZeEventDesc.wait = 0;
12581263

1259-
if (HostVisible || CounterBasedEventEnabled) {
1264+
if (Flags & USING_IMM_CMDLIST || Flags & COUNTER_BASED) {
12601265
ZeEventDesc.signal = ZE_EVENT_SCOPE_FLAG_HOST;
12611266
} else {
12621267
//
@@ -1281,8 +1286,8 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
12811286
} catch (...) {
12821287
return UR_RESULT_ERROR_UNKNOWN;
12831288
}
1284-
(*RetEvent)->CounterBasedEventsEnabled = CounterBasedEventEnabled;
1285-
if (HostVisible)
1289+
(*RetEvent)->CounterBasedEventsEnabled = Flags & COUNTER_BASED;
1290+
if (Flags & HOST_VISIBLE)
12861291
(*RetEvent)->HostVisibleEvent =
12871292
reinterpret_cast<ur_event_handle_t>(*RetEvent);
12881293

source/adapters/level_zero/event.hpp

Lines changed: 1 addition & 0 deletions
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+
EventFlags flags;
135136
// Level Zero event handle.
136137
ze_event_handle_t ZeEvent;
137138

0 commit comments

Comments
 (0)