Skip to content

Commit

Permalink
[L0] Refactoring of boolean event parameters
Browse files Browse the repository at this point in the history
CI in LLVM/SYCL: intel/llvm#14536

Signed-off-by: Winston Zhang <[email protected]>
  • Loading branch information
winstonzhang-intel committed Sep 18, 2024
1 parent 4517290 commit 689f323
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 89 deletions.
13 changes: 13 additions & 0 deletions source/adapters/level_zero/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,19 @@ enum {
2, // blocking UR calls, where supported (usually in enqueue commands)
};

typedef uint32_t ur_event_flags_t;
typedef enum ur_event_flag_t {
USING_IMM_CMDLIST = UR_BIT(0),
HOST_VISIBLE = UR_BIT(1),
COUNTER_BASED = UR_BIT(2),
ENABLE_PROFILER = UR_BIT(3),
MULTIDEVICE = UR_BIT(4),
/// @cond
UR_EVENT_FLAG_FORCE_UINT32 = 0x7FFFFFFF
/// @endcond

} ur_event_flag_t;

static const uint32_t UrL0Serialize = [] {
const char *ZeSerializeMode = std::getenv("ZE_SERIALIZE");
const char *UrL0SerializeMode = std::getenv("UR_L0_SERIALIZE");
Expand Down
50 changes: 31 additions & 19 deletions source/adapters/level_zero/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,8 @@ static const uint32_t MaxNumEventsPerPool = [] {
}();

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

Expand All @@ -485,8 +484,7 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
ZeDevice = Device->ZeDevice;
}
std::list<ze_event_pool_handle_t> *ZePoolCache =
getZeEventPoolCache(HostVisible, ProfilingEnabled,
CounterBasedEventEnabled, UsingImmCmdList, ZeDevice);
getZeEventPoolCache(Flags, ZeDevice);

if (!ZePoolCache->empty()) {
if (NumEventsAvailableInEventPool[ZePoolCache->front()] == 0) {
Expand Down Expand Up @@ -517,14 +515,14 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
ZeEventPoolDesc.count = MaxNumEventsPerPool;
ZeEventPoolDesc.flags = 0;
ZeEventPoolDesc.pNext = nullptr;
if (HostVisible)
if (Flags & HOST_VISIBLE)
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
if (ProfilingEnabled)
if (Flags & ENABLE_PROFILER)
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
logger::debug("ze_event_pool_desc_t flags set to: {}",
ZeEventPoolDesc.flags);
if (CounterBasedEventEnabled) {
if (UsingImmCmdList) {
if (Flags & COUNTER_BASED) {
if (Flags & USING_IMM_CMDLIST) {
counterBasedExt.flags = ZE_EVENT_POOL_COUNTER_BASED_EXP_FLAG_IMMEDIATE;
} else {
counterBasedExt.flags =
Expand Down Expand Up @@ -558,19 +556,21 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
return UR_RESULT_SUCCESS;
}

ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
bool HostVisible, bool WithProfiling, ur_device_handle_t Device,
bool CounterBasedEventEnabled) {
ur_event_handle_t
ur_context_handle_t_::getEventFromContextCache(ur_event_flags_t Flags,
ur_device_handle_t Device) {
std::scoped_lock<ur_mutex> Lock(EventCacheMutex);
auto Cache = getEventCache(HostVisible, WithProfiling, Device);
auto Cache = getEventCache(Flags, Device);
if (Cache->empty())
return nullptr;

auto It = Cache->begin();
ur_event_handle_t Event = *It;
if (Event->CounterBasedEventsEnabled != CounterBasedEventEnabled) {
if (Event->CounterBasedEventsEnabled !=
static_cast<bool>(Flags & COUNTER_BASED)) {
return nullptr;
}

Cache->erase(It);
// We have to reset event before using it.
Event->reset();
Expand All @@ -584,9 +584,13 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
if (!Event->IsMultiDevice && Event->UrQueue) {
Device = Event->UrQueue->Device;
}
ur_event_flags_t Flags = 0;
if (Event->isHostVisible())
Flags |= HOST_VISIBLE;
if (Event->isProfilingEnabled())
Flags |= ENABLE_PROFILER;

auto Cache = getEventCache(Event->isHostVisible(),
Event->isProfilingEnabled(), Device);
auto Cache = getEventCache(Flags, Device);
Cache->emplace_back(Event);
}

Expand All @@ -609,9 +613,17 @@ ur_context_handle_t_::decrementUnreleasedEventsInPool(ur_event_handle_t Event) {
ZeDevice = Event->UrQueue->Device->ZeDevice;
}

std::list<ze_event_pool_handle_t> *ZePoolCache = getZeEventPoolCache(
Event->isHostVisible(), Event->isProfilingEnabled(),
Event->CounterBasedEventsEnabled, UsingImmediateCommandlists, ZeDevice);
ur_event_flags_t Flags = 0;
if (Event->isHostVisible())
Flags |= HOST_VISIBLE;
if (Event->isProfilingEnabled())
Flags |= ENABLE_PROFILER;
if (Event->CounterBasedEventsEnabled)
Flags |= COUNTER_BASED;
if (UsingImmediateCommandlists)
Flags |= USING_IMM_CMDLIST;
std::list<ze_event_pool_handle_t> *ZePoolCache =
getZeEventPoolCache(Flags, ZeDevice);

// Put the empty pool to the cache of the pools.
if (NumEventsUnreleasedInEventPool[Event->ZeEventPool] == 0)
Expand Down
84 changes: 24 additions & 60 deletions source/adapters/level_zero/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,75 +201,38 @@ struct ur_context_handle_t_ : _ur_object {
// slot for a host-visible event. The ProfilingEnabled tells is we need a
// slot for an event with profiling capabilities.
ur_result_t getFreeSlotInExistingOrNewPool(ze_event_pool_handle_t &, size_t &,
bool HostVisible,
bool ProfilingEnabled,
ur_device_handle_t Device,
bool CounterBasedEventEnabled,
bool UsingImmCmdList);
ur_event_flags_t Flags,
ur_device_handle_t Device);

// Get ur_event_handle_t from cache.
ur_event_handle_t getEventFromContextCache(bool HostVisible,
bool WithProfiling,
ur_device_handle_t Device,
bool CounterBasedEventEnabled);
ur_event_handle_t getEventFromContextCache(ur_event_flags_t Flags,
ur_device_handle_t Device);

// Add ur_event_handle_t to cache.
void addEventToContextCache(ur_event_handle_t);

enum EventPoolCacheType {
HostVisibleCacheType,
HostInvisibleCacheType,
HostVisibleCounterBasedRegularCacheType,
HostInvisibleCounterBasedRegularCacheType,
HostVisibleCounterBasedImmediateCacheType,
HostInvisibleCounterBasedImmediateCacheType
};

std::list<ze_event_pool_handle_t> *
getZeEventPoolCache(bool HostVisible, bool WithProfiling,
bool CounterBasedEventEnabled, bool UsingImmediateCmdList,
ze_device_handle_t ZeDevice) {
EventPoolCacheType CacheType;
getZeEventPoolCache(ur_event_flags_t Flags, ze_device_handle_t ZeDevice) {
bool WithProfiling = Flags & ENABLE_PROFILER;
Flags &= ~ENABLE_PROFILER;
Flags &= ~MULTIDEVICE;
int index = static_cast<int>(Flags);
(Flags & COUNTER_BASED) ? index /= 2 : (index - 2);

calculateCacheIndex(HostVisible, CounterBasedEventEnabled,
UsingImmediateCmdList, CacheType);
if (ZeDevice) {
auto ZeEventPoolCacheMap =
WithProfiling ? &ZeEventPoolCacheDeviceMap[CacheType * 2]
: &ZeEventPoolCacheDeviceMap[CacheType * 2 + 1];
WithProfiling ? &ZeEventPoolCacheDeviceMap[index * 2]
: &ZeEventPoolCacheDeviceMap[index * 2 + 1];
if (ZeEventPoolCacheMap->find(ZeDevice) == ZeEventPoolCacheMap->end()) {
ZeEventPoolCache.emplace_back();
ZeEventPoolCacheMap->insert(
std::make_pair(ZeDevice, ZeEventPoolCache.size() - 1));
}
return &ZeEventPoolCache[(*ZeEventPoolCacheMap)[ZeDevice]];
} else {
return WithProfiling ? &ZeEventPoolCache[CacheType * 2]
: &ZeEventPoolCache[CacheType * 2 + 1];
}
}

ur_result_t calculateCacheIndex(bool HostVisible,
bool CounterBasedEventEnabled,
bool UsingImmediateCmdList,
EventPoolCacheType &CacheType) {
if (CounterBasedEventEnabled && HostVisible && !UsingImmediateCmdList) {
CacheType = HostVisibleCounterBasedRegularCacheType;
} else if (CounterBasedEventEnabled && !HostVisible &&
!UsingImmediateCmdList) {
CacheType = HostInvisibleCounterBasedRegularCacheType;
} else if (CounterBasedEventEnabled && HostVisible &&
UsingImmediateCmdList) {
CacheType = HostVisibleCounterBasedImmediateCacheType;
} else if (CounterBasedEventEnabled && !HostVisible &&
UsingImmediateCmdList) {
CacheType = HostInvisibleCounterBasedImmediateCacheType;
} else if (!CounterBasedEventEnabled && HostVisible) {
CacheType = HostVisibleCacheType;
} else {
CacheType = HostInvisibleCacheType;
return WithProfiling ? &ZeEventPoolCache[index * 2]
: &ZeEventPoolCache[index * 2 + 1];
}
return UR_RESULT_SUCCESS;
}

// Decrement number of events living in the pool upon event destroy
Expand Down Expand Up @@ -311,33 +274,34 @@ struct ur_context_handle_t_ : _ur_object {

private:
// Get the cache of events for a provided scope and profiling mode.
auto getEventCache(bool HostVisible, bool WithProfiling,
ur_device_handle_t Device) {
if (HostVisible) {
auto getEventCache(ur_event_flags_t Flags, ur_device_handle_t Device) {
if (Flags & HOST_VISIBLE) {
if (Device) {
auto EventCachesMap =
WithProfiling ? &EventCachesDeviceMap[0] : &EventCachesDeviceMap[1];
auto EventCachesMap = (Flags & ENABLE_PROFILER)
? &EventCachesDeviceMap[0]
: &EventCachesDeviceMap[1];
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
EventCaches.emplace_back();
EventCachesMap->insert(
std::make_pair(Device, EventCaches.size() - 1));
}
return &EventCaches[(*EventCachesMap)[Device]];
} else {
return WithProfiling ? &EventCaches[0] : &EventCaches[1];
return (Flags & ENABLE_PROFILER) ? &EventCaches[0] : &EventCaches[1];
}
} else {
if (Device) {
auto EventCachesMap =
WithProfiling ? &EventCachesDeviceMap[2] : &EventCachesDeviceMap[3];
auto EventCachesMap = (Flags & ENABLE_PROFILER)
? &EventCachesDeviceMap[2]
: &EventCachesDeviceMap[3];
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
EventCaches.emplace_back();
EventCachesMap->insert(
std::make_pair(Device, EventCaches.size() - 1));
}
return &EventCaches[(*EventCachesMap)[Device]];
} else {
return WithProfiling ? &EventCaches[2] : &EventCaches[3];
return (Flags & ENABLE_PROFILER) ? &EventCaches[2] : &EventCaches[3];
}
}
}
Expand Down
28 changes: 18 additions & 10 deletions source/adapters/level_zero/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,15 +1249,24 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
bool ProfilingEnabled =
ForceDisableProfiling ? false : (!Queue || Queue->isProfilingEnabled());
bool UsingImmediateCommandlists = !Queue || Queue->UsingImmCmdLists;
ur_event_flags_t Flags = 0;
if (ProfilingEnabled)
Flags |= ENABLE_PROFILER;
if (UsingImmediateCommandlists)
Flags |= USING_IMM_CMDLIST;
if (HostVisible)
Flags |= HOST_VISIBLE;
if (IsMultiDevice)
Flags |= MULTIDEVICE;
if (CounterBasedEventEnabled)
Flags |= COUNTER_BASED;

ur_device_handle_t Device = nullptr;

if (!IsMultiDevice && Queue) {
if (!(Flags & MULTIDEVICE) && Queue) {
Device = Queue->Device;
}

if (auto CachedEvent = Context->getEventFromContextCache(
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled)) {
if (auto CachedEvent = Context->getEventFromContextCache(Flags, Device)) {
*RetEvent = CachedEvent;
return UR_RESULT_SUCCESS;
}
Expand All @@ -1267,16 +1276,15 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,

size_t Index = 0;

if (auto Res = Context->getFreeSlotInExistingOrNewPool(
ZeEventPool, Index, HostVisible, ProfilingEnabled, Device,
CounterBasedEventEnabled, UsingImmediateCommandlists))
if (auto Res = Context->getFreeSlotInExistingOrNewPool(ZeEventPool, Index,
Flags, Device))
return Res;

ZeStruct<ze_event_desc_t> ZeEventDesc;
ZeEventDesc.index = Index;
ZeEventDesc.wait = 0;

if (HostVisible || CounterBasedEventEnabled) {
if ((Flags & USING_IMM_CMDLIST) || (Flags & COUNTER_BASED)) {
ZeEventDesc.signal = ZE_EVENT_SCOPE_FLAG_HOST;
} else {
//
Expand All @@ -1301,8 +1309,8 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
} catch (...) {
return UR_RESULT_ERROR_UNKNOWN;
}
(*RetEvent)->CounterBasedEventsEnabled = CounterBasedEventEnabled;
if (HostVisible)
(*RetEvent)->CounterBasedEventsEnabled = (Flags & COUNTER_BASED);
if (Flags & HOST_VISIBLE)
(*RetEvent)->HostVisibleEvent =
reinterpret_cast<ur_event_handle_t>(*RetEvent);

Expand Down
1 change: 1 addition & 0 deletions source/adapters/level_zero/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ struct ur_event_handle_t_ : _ur_object {
OwnNativeHandle = OwnZeEvent;
}

ur_event_flags_t flags;
// Level Zero event handle.
ze_event_handle_t ZeEvent;

Expand Down

0 comments on commit 689f323

Please sign in to comment.