Skip to content

Commit 9d222e8

Browse files
[L0] Refactoring of boolean event parameters
CI in LLVM/SYCL: intel/llvm#14536 Signed-off-by: Winston Zhang <[email protected]>
1 parent 77187f6 commit 9d222e8

File tree

5 files changed

+112
-115
lines changed

5 files changed

+112
-115
lines changed

source/adapters/level_zero/common.hpp

+13
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,19 @@ enum {
240240
2, // blocking UR calls, where supported (usually in enqueue commands)
241241
};
242242

243+
typedef uint32_t ur_event_flags_t;
244+
typedef enum ur_event_flag_t {
245+
USING_IMM_CMDLIST = UR_BIT(0),
246+
HOST_VISIBLE = UR_BIT(1),
247+
COUNTER_BASED = UR_BIT(2),
248+
ENABLE_PROFILER = UR_BIT(3),
249+
MULTIDEVICE = UR_BIT(4),
250+
/// @cond
251+
UR_EVENT_FLAG_FORCE_UINT32 = 0x7FFFFFFF
252+
/// @endcond
253+
254+
} ur_event_flag_t;
255+
243256
static const uint32_t UrL0Serialize = [] {
244257
const char *ZeSerializeMode = std::getenv("ZE_SERIALIZE");
245258
const char *UrL0SerializeMode = std::getenv("UR_L0_SERIALIZE");

source/adapters/level_zero/context.cpp

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

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

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

491489
if (!ZePoolCache->empty()) {
492490
if (NumEventsAvailableInEventPool[ZePoolCache->front()] == 0) {
@@ -517,14 +515,14 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
517515
ZeEventPoolDesc.count = MaxNumEventsPerPool;
518516
ZeEventPoolDesc.flags = 0;
519517
ZeEventPoolDesc.pNext = nullptr;
520-
if (HostVisible)
518+
if (Flags & HOST_VISIBLE)
521519
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
522-
if (ProfilingEnabled)
520+
if (Flags & ENABLE_PROFILER)
523521
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
524522
logger::debug("ze_event_pool_desc_t flags set to: {}",
525523
ZeEventPoolDesc.flags);
526-
if (CounterBasedEventEnabled) {
527-
if (UsingImmCmdList) {
524+
if (Flags & COUNTER_BASED) {
525+
if (Flags & USING_IMM_CMDLIST) {
528526
counterBasedExt.flags = ZE_EVENT_POOL_COUNTER_BASED_EXP_FLAG_IMMEDIATE;
529527
} else {
530528
counterBasedExt.flags =
@@ -558,22 +556,25 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
558556
return UR_RESULT_SUCCESS;
559557
}
560558

561-
ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
562-
bool HostVisible, bool WithProfiling, ur_device_handle_t Device,
563-
bool CounterBasedEventEnabled, bool UsingImmCmdList) {
559+
560+
ur_event_handle_t
561+
ur_context_handle_t_::getEventFromContextCache(ur_event_flags_t Flags,
562+
ur_device_handle_t Device) {
564563
std::scoped_lock<ur_mutex> Lock(EventCacheMutex);
565-
auto Cache = getEventCache(HostVisible, WithProfiling, Device);
566-
if (CounterBasedEventEnabled) {
567-
Cache = getCounterBasedEventCache(WithProfiling, UsingImmCmdList, Device);
568-
}
564+
auto Cache = getEventCache(Flags, Device);
565+
if (Flags & COUNTER_BASED) {
566+
Cache = getCounterBasedEventCache(Flags, Device);
567+
}
569568
if (Cache->empty())
570569
return nullptr;
571570

572571
auto It = Cache->begin();
573572
ur_event_handle_t Event = *It;
574-
if (Event->CounterBasedEventsEnabled != CounterBasedEventEnabled) {
573+
if (Event->CounterBasedEventsEnabled !=
574+
static_cast<bool>(Flags & COUNTER_BASED)) {
575575
return nullptr;
576576
}
577+
577578
Cache->erase(It);
578579
// We have to reset event before using it.
579580
Event->reset();
@@ -587,15 +588,17 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
587588
if (!Event->IsMultiDevice && Event->UrQueue) {
588589
Device = Event->UrQueue->Device;
589590
}
591+
ur_event_flags_t Flags = 0;
592+
if (Event->isHostVisible())
593+
Flags |= HOST_VISIBLE;
594+
if (Event->isProfilingEnabled())
595+
Flags |= ENABLE_PROFILER;
590596

591597
if (Event->CounterBasedEventsEnabled) {
592-
auto Cache = getCounterBasedEventCache(
593-
Event->isProfilingEnabled(),
594-
!(Event->UrQueue) || (Event->UrQueue)->UsingImmCmdLists, Device);
598+
auto Cache = getCounterBasedEventCache(Flags, Device);
595599
Cache->emplace_back(Event);
596600
} else {
597-
auto Cache = getEventCache(Event->isHostVisible(),
598-
Event->isProfilingEnabled(), Device);
601+
auto Cache = getEventCache(Flags, Device);
599602
Cache->emplace_back(Event);
600603
}
601604
}
@@ -619,9 +622,17 @@ ur_context_handle_t_::decrementUnreleasedEventsInPool(ur_event_handle_t Event) {
619622
ZeDevice = Event->UrQueue->Device->ZeDevice;
620623
}
621624

622-
std::list<ze_event_pool_handle_t> *ZePoolCache = getZeEventPoolCache(
623-
Event->isHostVisible(), Event->isProfilingEnabled(),
624-
Event->CounterBasedEventsEnabled, UsingImmediateCommandlists, ZeDevice);
625+
ur_event_flags_t Flags = 0;
626+
if (Event->isHostVisible())
627+
Flags |= HOST_VISIBLE;
628+
if (Event->isProfilingEnabled())
629+
Flags |= ENABLE_PROFILER;
630+
if (Event->CounterBasedEventsEnabled)
631+
Flags |= COUNTER_BASED;
632+
if (UsingImmediateCommandlists)
633+
Flags |= USING_IMM_CMDLIST;
634+
std::list<ze_event_pool_handle_t> *ZePoolCache =
635+
getZeEventPoolCache(Flags, ZeDevice);
625636

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

source/adapters/level_zero/context.hpp

+44-79
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,9 @@ struct ur_context_handle_t_ : _ur_object {
154154
// head.
155155
//
156156
// Cache of event pools to which host-visible events are added to.
157-
std::vector<std::list<ze_event_pool_handle_t>> ZeEventPoolCache{
158-
ZeEventPoolCacheTypeCount * 2};
157+
std::vector<std::list<ze_event_pool_handle_t>> ZeEventPoolCache{12};
159158
std::vector<std::unordered_map<ze_device_handle_t, size_t>>
160-
ZeEventPoolCacheDeviceMap{ZeEventPoolCacheTypeCount * 2};
159+
ZeEventPoolCacheDeviceMap{12};
161160

162161
// This map will be used to determine if a pool is full or not
163162
// by storing number of empty slots available in the pool.
@@ -209,33 +208,17 @@ struct ur_context_handle_t_ : _ur_object {
209208
// slot for a host-visible event. The ProfilingEnabled tells is we need a
210209
// slot for an event with profiling capabilities.
211210
ur_result_t getFreeSlotInExistingOrNewPool(ze_event_pool_handle_t &, size_t &,
212-
bool HostVisible,
213-
bool ProfilingEnabled,
214-
ur_device_handle_t Device,
215-
bool CounterBasedEventEnabled,
216-
bool UsingImmCmdList);
211+
ur_event_flags_t Flags,
212+
ur_device_handle_t Device);
217213

218214
// Get ur_event_handle_t from cache.
219-
ur_event_handle_t getEventFromContextCache(bool HostVisible,
220-
bool WithProfiling,
221-
ur_device_handle_t Device,
222-
bool CounterBasedEventEnabled,
223-
bool UsingImmCmdList);
215+
ur_event_handle_t getEventFromContextCache(ur_event_flags_t Flags,
216+
ur_device_handle_t Device);
224217

225218
// Add ur_event_handle_t to cache.
226219
void addEventToContextCache(ur_event_handle_t);
227220

228-
enum ZeEventPoolCacheType {
229-
HostVisibleCacheType,
230-
HostInvisibleCacheType,
231-
HostVisibleCounterBasedRegularCacheType,
232-
HostInvisibleCounterBasedRegularCacheType,
233-
HostVisibleCounterBasedImmediateCacheType,
234-
HostInvisibleCounterBasedImmediateCacheType,
235-
ZeEventPoolCacheTypeCount
236-
};
237-
238-
enum EventCacheType {
221+
enum EventCacheType {
239222
HostVisibleProfilingCacheType,
240223
HostVisibleRegularCacheType,
241224
HostInvisibleProfilingCacheType,
@@ -245,53 +228,29 @@ struct ur_context_handle_t_ : _ur_object {
245228
CounterBasedImmediateProfilingCacheType,
246229
CounterBasedRegularProfilingCacheType,
247230
EventCacheTypeCount
248-
};
249-
231+
};
250232
std::list<ze_event_pool_handle_t> *
251-
getZeEventPoolCache(bool HostVisible, bool WithProfiling,
252-
bool CounterBasedEventEnabled, bool UsingImmediateCmdList,
253-
ze_device_handle_t ZeDevice) {
254-
ZeEventPoolCacheType CacheType;
233+
getZeEventPoolCache(ur_event_flags_t Flags, ze_device_handle_t ZeDevice) {
234+
bool WithProfiling = Flags & ENABLE_PROFILER;
235+
Flags &= ~ENABLE_PROFILER;
236+
Flags &= ~MULTIDEVICE;
237+
int index = static_cast<int>(Flags);
238+
(Flags & COUNTER_BASED) ? index /= 2 : (index - 2);
255239

256-
calculateCacheIndex(HostVisible, CounterBasedEventEnabled,
257-
UsingImmediateCmdList, CacheType);
258240
if (ZeDevice) {
259241
auto ZeEventPoolCacheMap =
260-
WithProfiling ? &ZeEventPoolCacheDeviceMap[CacheType * 2]
261-
: &ZeEventPoolCacheDeviceMap[CacheType * 2 + 1];
242+
WithProfiling ? &ZeEventPoolCacheDeviceMap[index * 2]
243+
: &ZeEventPoolCacheDeviceMap[index * 2 + 1];
262244
if (ZeEventPoolCacheMap->find(ZeDevice) == ZeEventPoolCacheMap->end()) {
263245
ZeEventPoolCache.emplace_back();
264246
ZeEventPoolCacheMap->insert(
265247
std::make_pair(ZeDevice, ZeEventPoolCache.size() - 1));
266248
}
267249
return &ZeEventPoolCache[(*ZeEventPoolCacheMap)[ZeDevice]];
268250
} else {
269-
return WithProfiling ? &ZeEventPoolCache[CacheType * 2]
270-
: &ZeEventPoolCache[CacheType * 2 + 1];
271-
}
272-
}
273-
274-
ur_result_t calculateCacheIndex(bool HostVisible,
275-
bool CounterBasedEventEnabled,
276-
bool UsingImmediateCmdList,
277-
ZeEventPoolCacheType &CacheType) {
278-
if (CounterBasedEventEnabled && HostVisible && !UsingImmediateCmdList) {
279-
CacheType = HostVisibleCounterBasedRegularCacheType;
280-
} else if (CounterBasedEventEnabled && !HostVisible &&
281-
!UsingImmediateCmdList) {
282-
CacheType = HostInvisibleCounterBasedRegularCacheType;
283-
} else if (CounterBasedEventEnabled && HostVisible &&
284-
UsingImmediateCmdList) {
285-
CacheType = HostVisibleCounterBasedImmediateCacheType;
286-
} else if (CounterBasedEventEnabled && !HostVisible &&
287-
UsingImmediateCmdList) {
288-
CacheType = HostInvisibleCounterBasedImmediateCacheType;
289-
} else if (!CounterBasedEventEnabled && HostVisible) {
290-
CacheType = HostVisibleCacheType;
291-
} else {
292-
CacheType = HostInvisibleCacheType;
251+
return WithProfiling ? &ZeEventPoolCache[index * 2]
252+
: &ZeEventPoolCache[index * 2 + 1];
293253
}
294-
return UR_RESULT_SUCCESS;
295254
}
296255

297256
// Decrement number of events living in the pool upon event destroy
@@ -333,54 +292,60 @@ struct ur_context_handle_t_ : _ur_object {
333292

334293
private:
335294
// Get the cache of events for a provided scope and profiling mode.
336-
auto getEventCache(bool HostVisible, bool WithProfiling,
337-
ur_device_handle_t Device) {
338-
if (HostVisible) {
295+
auto getEventCache(ur_event_flags_t Flags, ur_device_handle_t Device) {
296+
if (Flags & HOST_VISIBLE) {
339297
if (Device) {
340-
auto EventCachesMap =
341-
WithProfiling ? &EventCachesDeviceMap[HostVisibleProfilingCacheType]
342-
: &EventCachesDeviceMap[HostVisibleRegularCacheType];
298+
auto EventCachesMap = (Flags & ENABLE_PROFILER)
299+
? &EventCachesDeviceMap[HostVisibleProfilingCacheType]
300+
: &EventCachesDeviceMap[HostVisibleRegularCacheType];
301+
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
302+
EventCaches.emplace_back();
303+
EventCachesMap->insert(
304+
std::make_pair(Device, EventCaches.size() - 1));
305+
}
343306
return &EventCaches[(*EventCachesMap)[Device]];
344307
} else {
345-
return WithProfiling ? &EventCaches[HostVisibleProfilingCacheType]
346-
: &EventCaches[HostVisibleRegularCacheType];
308+
return (Flags & ENABLE_PROFILER) ? &EventCaches[HostVisibleProfilingCacheType] : &EventCaches[HostVisibleRegularCacheType];
347309
}
348310
} else {
349311
if (Device) {
350-
auto EventCachesMap =
351-
WithProfiling
352-
? &EventCachesDeviceMap[HostInvisibleProfilingCacheType]
353-
: &EventCachesDeviceMap[HostInvisibleRegularCacheType];
312+
auto EventCachesMap = (Flags & ENABLE_PROFILER)
313+
? &EventCachesDeviceMap[HostInvisibleProfilingCacheType]
314+
: &EventCachesDeviceMap[HostInvisibleRegularCacheType];
315+
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
316+
EventCaches.emplace_back();
317+
EventCachesMap->insert(
318+
std::make_pair(Device, EventCaches.size() - 1));
319+
}
354320
return &EventCaches[(*EventCachesMap)[Device]];
355321
} else {
356-
return WithProfiling ? &EventCaches[HostInvisibleProfilingCacheType]
357-
: &EventCaches[HostInvisibleRegularCacheType];
322+
return (Flags & ENABLE_PROFILER) ? &EventCaches[HostInvisibleProfilingCacheType] : &EventCaches[HostInvisibleRegularCacheType];
358323
}
359324
}
360325
};
361-
auto getCounterBasedEventCache(bool WithProfiling, bool UsingImmediateCmdList,
326+
auto getCounterBasedEventCache(ur_event_flags_t Flags,
362327
ur_device_handle_t Device) {
363-
if (UsingImmediateCmdList) {
328+
if (Flags & USING_IMM_CMDLIST) {
364329
if (Device) {
365330
auto EventCachesMap =
366-
WithProfiling
331+
(Flags & ENABLE_PROFILER)
367332
? &EventCachesDeviceMap[CounterBasedImmediateProfilingCacheType]
368333
: &EventCachesDeviceMap[CounterBasedImmediateCacheType];
369334
return &EventCaches[(*EventCachesMap)[Device]];
370335
} else {
371-
return WithProfiling
336+
return (Flags & ENABLE_PROFILER)
372337
? &EventCaches[CounterBasedImmediateProfilingCacheType]
373338
: &EventCaches[CounterBasedImmediateCacheType];
374339
}
375340
} else {
376341
if (Device) {
377342
auto EventCachesMap =
378-
WithProfiling
343+
(Flags & ENABLE_PROFILER)
379344
? &EventCachesDeviceMap[CounterBasedRegularProfilingCacheType]
380345
: &EventCachesDeviceMap[CounterBasedRegularCacheType];
381346
return &EventCaches[(*EventCachesMap)[Device]];
382347
} else {
383-
return WithProfiling
348+
return (Flags & ENABLE_PROFILER)
384349
? &EventCaches[CounterBasedRegularProfilingCacheType]
385350
: &EventCaches[CounterBasedRegularCacheType];
386351
}

source/adapters/level_zero/event.cpp

+18-11
Original file line numberDiff line numberDiff line change
@@ -1248,16 +1248,24 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
12481248
bool ProfilingEnabled =
12491249
ForceDisableProfiling ? false : (!Queue || Queue->isProfilingEnabled());
12501250
bool UsingImmediateCommandlists = !Queue || Queue->UsingImmCmdLists;
1251+
ur_event_flags_t Flags = 0;
1252+
if (ProfilingEnabled)
1253+
Flags |= ENABLE_PROFILER;
1254+
if (UsingImmediateCommandlists)
1255+
Flags |= USING_IMM_CMDLIST;
1256+
if (HostVisible)
1257+
Flags |= HOST_VISIBLE;
1258+
if (IsMultiDevice)
1259+
Flags |= MULTIDEVICE;
1260+
if (CounterBasedEventEnabled)
1261+
Flags |= COUNTER_BASED;
12511262

12521263
ur_device_handle_t Device = nullptr;
1253-
1254-
if (!IsMultiDevice && Queue) {
1264+
if (!(Flags & MULTIDEVICE) && Queue) {
12551265
Device = Queue->Device;
12561266
}
12571267

1258-
if (auto CachedEvent = Context->getEventFromContextCache(
1259-
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled,
1260-
UsingImmediateCommandlists)) {
1268+
if (auto CachedEvent = Context->getEventFromContextCache(Flags, Device)) {
12611269
*RetEvent = CachedEvent;
12621270
return UR_RESULT_SUCCESS;
12631271
}
@@ -1267,16 +1275,15 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
12671275

12681276
size_t Index = 0;
12691277

1270-
if (auto Res = Context->getFreeSlotInExistingOrNewPool(
1271-
ZeEventPool, Index, HostVisible, ProfilingEnabled, Device,
1272-
CounterBasedEventEnabled, UsingImmediateCommandlists))
1278+
if (auto Res = Context->getFreeSlotInExistingOrNewPool(ZeEventPool, Index,
1279+
Flags, Device))
12731280
return Res;
12741281

12751282
ZeStruct<ze_event_desc_t> ZeEventDesc;
12761283
ZeEventDesc.index = Index;
12771284
ZeEventDesc.wait = 0;
12781285

1279-
if (HostVisible || CounterBasedEventEnabled) {
1286+
if ((Flags & USING_IMM_CMDLIST) || (Flags & COUNTER_BASED)) {
12801287
ZeEventDesc.signal = ZE_EVENT_SCOPE_FLAG_HOST;
12811288
} else {
12821289
//
@@ -1301,8 +1308,8 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
13011308
} catch (...) {
13021309
return UR_RESULT_ERROR_UNKNOWN;
13031310
}
1304-
(*RetEvent)->CounterBasedEventsEnabled = CounterBasedEventEnabled;
1305-
if (HostVisible)
1311+
(*RetEvent)->CounterBasedEventsEnabled = (Flags & COUNTER_BASED);
1312+
if (Flags & HOST_VISIBLE)
13061313
(*RetEvent)->HostVisibleEvent =
13071314
reinterpret_cast<ur_event_handle_t>(*RetEvent);
13081315

0 commit comments

Comments
 (0)