Skip to content

Commit 4304208

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

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
@@ -239,6 +239,14 @@ enum {
239239
UrL0SerializeBlock =
240240
2, // blocking UR calls, where supported (usually in enqueue commands)
241241
};
242+
typedef uint32_t ur_event_flags_t;
243+
typedef enum ur_event_flag_t {
244+
COUNTER_BASED = UR_BIT(0),
245+
USING_IMM_CMDLIST = UR_BIT(1),
246+
HOST_VISIBLE = UR_BIT(2),
247+
ENABLE_PROFILER = UR_BIT(3),
248+
MULTIDEVICE = UR_BIT(4)
249+
} ur_event_flag_t;
242250

243251
static const uint32_t UrL0Serialize = [] {
244252
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
@@ -201,17 +201,12 @@ struct ur_context_handle_t_ : _ur_object {
201201
// slot for a host-visible event. The ProfilingEnabled tells is we need a
202202
// slot for an event with profiling capabilities.
203203
ur_result_t getFreeSlotInExistingOrNewPool(ze_event_pool_handle_t &, size_t &,
204-
bool HostVisible,
205-
bool ProfilingEnabled,
206-
ur_device_handle_t Device,
207-
bool CounterBasedEventEnabled,
208-
bool UsingImmCmdList);
204+
ur_event_flags_t Flags,
205+
ur_device_handle_t Device);
209206

210207
// Get ur_event_handle_t from cache.
211-
ur_event_handle_t getEventFromContextCache(bool HostVisible,
212-
bool WithProfiling,
213-
ur_device_handle_t Device,
214-
bool CounterBasedEventEnabled);
208+
ur_event_handle_t getEventFromContextCache(ur_event_flags_t Flags,
209+
ur_device_handle_t Device);
215210

216211
// Add ur_event_handle_t to cache.
217212
void addEventToContextCache(ur_event_handle_t);
@@ -226,50 +221,20 @@ struct ur_context_handle_t_ : _ur_object {
226221
};
227222

228223
std::list<ze_event_pool_handle_t> *
229-
getZeEventPoolCache(bool HostVisible, bool WithProfiling,
230-
bool CounterBasedEventEnabled, bool UsingImmediateCmdList,
231-
ze_device_handle_t ZeDevice) {
232-
EventPoolCacheType CacheType;
233-
234-
calculateCacheIndex(HostVisible, CounterBasedEventEnabled,
235-
UsingImmediateCmdList, CacheType);
224+
getZeEventPoolCache(ur_event_flags_t Flags, ze_device_handle_t ZeDevice) {
225+
Flags = static_cast<ur_event_flag_t>(Flags & ~MULTIDEVICE);
236226
if (ZeDevice) {
237227
auto ZeEventPoolCacheMap =
238-
WithProfiling ? &ZeEventPoolCacheDeviceMap[CacheType * 2]
239-
: &ZeEventPoolCacheDeviceMap[CacheType * 2 + 1];
228+
&ZeEventPoolCacheDeviceMap[static_cast<int>(Flags)];
240229
if (ZeEventPoolCacheMap->find(ZeDevice) == ZeEventPoolCacheMap->end()) {
241230
ZeEventPoolCache.emplace_back();
242231
ZeEventPoolCacheMap->insert(
243232
std::make_pair(ZeDevice, ZeEventPoolCache.size() - 1));
244233
}
245234
return &ZeEventPoolCache[(*ZeEventPoolCacheMap)[ZeDevice]];
246235
} else {
247-
return WithProfiling ? &ZeEventPoolCache[CacheType * 2]
248-
: &ZeEventPoolCache[CacheType * 2 + 1];
249-
}
250-
}
251-
252-
ur_result_t calculateCacheIndex(bool HostVisible,
253-
bool CounterBasedEventEnabled,
254-
bool UsingImmediateCmdList,
255-
EventPoolCacheType &CacheType) {
256-
if (CounterBasedEventEnabled && HostVisible && !UsingImmediateCmdList) {
257-
CacheType = HostVisibleCounterBasedRegularCacheType;
258-
} else if (CounterBasedEventEnabled && !HostVisible &&
259-
!UsingImmediateCmdList) {
260-
CacheType = HostInvisibleCounterBasedRegularCacheType;
261-
} else if (CounterBasedEventEnabled && HostVisible &&
262-
UsingImmediateCmdList) {
263-
CacheType = HostVisibleCounterBasedImmediateCacheType;
264-
} else if (CounterBasedEventEnabled && !HostVisible &&
265-
UsingImmediateCmdList) {
266-
CacheType = HostInvisibleCounterBasedImmediateCacheType;
267-
} else if (!CounterBasedEventEnabled && HostVisible) {
268-
CacheType = HostVisibleCacheType;
269-
} else {
270-
CacheType = HostInvisibleCacheType;
236+
return &ZeEventPoolCache[static_cast<int>(Flags)];
271237
}
272-
return UR_RESULT_SUCCESS;
273238
}
274239

275240
// Decrement number of events living in the pool upon event destroy
@@ -311,33 +276,34 @@ struct ur_context_handle_t_ : _ur_object {
311276

312277
private:
313278
// Get the cache of events for a provided scope and profiling mode.
314-
auto getEventCache(bool HostVisible, bool WithProfiling,
315-
ur_device_handle_t Device) {
316-
if (HostVisible) {
279+
auto getEventCache(ur_event_flags_t Flags, ur_device_handle_t Device) {
280+
if (Flags & HOST_VISIBLE) {
317281
if (Device) {
318-
auto EventCachesMap =
319-
WithProfiling ? &EventCachesDeviceMap[0] : &EventCachesDeviceMap[1];
282+
auto EventCachesMap = Flags & ENABLE_PROFILER
283+
? &EventCachesDeviceMap[0]
284+
: &EventCachesDeviceMap[1];
320285
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
321286
EventCaches.emplace_back();
322287
EventCachesMap->insert(
323288
std::make_pair(Device, EventCaches.size() - 1));
324289
}
325290
return &EventCaches[(*EventCachesMap)[Device]];
326291
} else {
327-
return WithProfiling ? &EventCaches[0] : &EventCaches[1];
292+
return Flags & ENABLE_PROFILER ? &EventCaches[0] : &EventCaches[1];
328293
}
329294
} else {
330295
if (Device) {
331-
auto EventCachesMap =
332-
WithProfiling ? &EventCachesDeviceMap[2] : &EventCachesDeviceMap[3];
296+
auto EventCachesMap = Flags & ENABLE_PROFILER
297+
? &EventCachesDeviceMap[2]
298+
: &EventCachesDeviceMap[3];
333299
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
334300
EventCaches.emplace_back();
335301
EventCachesMap->insert(
336302
std::make_pair(Device, EventCaches.size() - 1));
337303
}
338304
return &EventCaches[(*EventCachesMap)[Device]];
339305
} else {
340-
return WithProfiling ? &EventCaches[2] : &EventCaches[3];
306+
return Flags & ENABLE_PROFILER ? &EventCaches[2] : &EventCaches[3];
341307
}
342308
}
343309
}

source/adapters/level_zero/event.cpp

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

12421249
ur_device_handle_t Device = nullptr;
12431250

1244-
if (!IsMultiDevice && Queue) {
1251+
if (Flags & MULTIDEVICE && Queue) {
12451252
Device = Queue->Device;
12461253
}
12471254

1248-
if (auto CachedEvent = Context->getEventFromContextCache(
1249-
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled)) {
1255+
if (auto CachedEvent = Context->getEventFromContextCache(Flags, Device)) {
12501256
*RetEvent = CachedEvent;
12511257
return UR_RESULT_SUCCESS;
12521258
}
@@ -1256,16 +1262,15 @@ ur_result_t EventCreate(ur_context_handle_t Context,
12561262

12571263
size_t Index = 0;
12581264

1259-
if (auto Res = Context->getFreeSlotInExistingOrNewPool(
1260-
ZeEventPool, Index, HostVisible, ProfilingEnabled, Device,
1261-
CounterBasedEventEnabled, UsingImmediateCommandlists))
1265+
if (auto Res = Context->getFreeSlotInExistingOrNewPool(ZeEventPool, Index,
1266+
Flags, Device))
12621267
return Res;
12631268

12641269
ZeStruct<ze_event_desc_t> ZeEventDesc;
12651270
ZeEventDesc.index = Index;
12661271
ZeEventDesc.wait = 0;
12671272

1268-
if (HostVisible || CounterBasedEventEnabled) {
1273+
if (Flags & USING_IMM_CMDLIST || Flags & COUNTER_BASED) {
12691274
ZeEventDesc.signal = ZE_EVENT_SCOPE_FLAG_HOST;
12701275
} else {
12711276
//
@@ -1290,8 +1295,8 @@ ur_result_t EventCreate(ur_context_handle_t Context,
12901295
} catch (...) {
12911296
return UR_RESULT_ERROR_UNKNOWN;
12921297
}
1293-
(*RetEvent)->CounterBasedEventsEnabled = CounterBasedEventEnabled;
1294-
if (HostVisible)
1298+
(*RetEvent)->CounterBasedEventsEnabled = Flags & COUNTER_BASED;
1299+
if (Flags & HOST_VISIBLE)
12951300
(*RetEvent)->HostVisibleEvent =
12961301
reinterpret_cast<ur_event_handle_t>(*RetEvent);
12971302

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)