Skip to content

Commit

Permalink
[L0] Refactoring of boolean event parameters
Browse files Browse the repository at this point in the history
new commit for rebase

Signed-off-by: Winston Zhang <[email protected]>
  • Loading branch information
winstonzhang-intel committed Aug 27, 2024
1 parent 76361a8 commit 4304208
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 80 deletions.
8 changes: 8 additions & 0 deletions source/adapters/level_zero/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@ enum {
UrL0SerializeBlock =
2, // blocking UR calls, where supported (usually in enqueue commands)
};
typedef uint32_t ur_event_flags_t;
typedef enum ur_event_flag_t {
COUNTER_BASED = UR_BIT(0),
USING_IMM_CMDLIST = UR_BIT(1),
HOST_VISIBLE = UR_BIT(2),
ENABLE_PROFILER = UR_BIT(3),
MULTIDEVICE = UR_BIT(4)
} ur_event_flag_t;

static const uint32_t UrL0Serialize = [] {
const char *ZeSerializeMode = std::getenv("ZE_SERIALIZE");
Expand Down
44 changes: 25 additions & 19 deletions source/adapters/level_zero/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,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 @@ -482,8 +481,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 @@ -514,14 +512,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 @@ -553,17 +551,17 @@ 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 != (Flags & COUNTER_BASED)) {
return nullptr;
}
Cache->erase(It);
Expand All @@ -580,8 +578,11 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
Device = Legacy(Event->UrQueue)->Device;
}

auto Cache = getEventCache(Event->isHostVisible(),
Event->isProfilingEnabled(), Device);
ur_event_flags_t Flags = static_cast<ur_event_flags_t>(
Event->isHostVisible() ? HOST_VISIBLE
: 0 | Event->isProfilingEnabled() ? ENABLE_PROFILER
: 0);
auto Cache = getEventCache(Flags, Device);
Cache->emplace_back(Event);
}

Expand All @@ -604,9 +605,14 @@ ur_context_handle_t_::decrementUnreleasedEventsInPool(ur_event_handle_t Event) {
ZeDevice = Legacy(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 = static_cast<ur_event_flags_t>(
Event->isHostVisible() ? HOST_VISIBLE
: 0 | Event->isProfilingEnabled() ? ENABLE_PROFILER
: 0 | Event->CounterBasedEventsEnabled ? COUNTER_BASED
: 0 | UsingImmediateCommandlists ? USING_IMM_CMDLIST
: 0);
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
70 changes: 18 additions & 52 deletions source/adapters/level_zero/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,12 @@ 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);
Expand All @@ -226,50 +221,20 @@ struct ur_context_handle_t_ : _ur_object {
};

std::list<ze_event_pool_handle_t> *
getZeEventPoolCache(bool HostVisible, bool WithProfiling,
bool CounterBasedEventEnabled, bool UsingImmediateCmdList,
ze_device_handle_t ZeDevice) {
EventPoolCacheType CacheType;

calculateCacheIndex(HostVisible, CounterBasedEventEnabled,
UsingImmediateCmdList, CacheType);
getZeEventPoolCache(ur_event_flags_t Flags, ze_device_handle_t ZeDevice) {
Flags = static_cast<ur_event_flag_t>(Flags & ~MULTIDEVICE);
if (ZeDevice) {
auto ZeEventPoolCacheMap =
WithProfiling ? &ZeEventPoolCacheDeviceMap[CacheType * 2]
: &ZeEventPoolCacheDeviceMap[CacheType * 2 + 1];
&ZeEventPoolCacheDeviceMap[static_cast<int>(Flags)];
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 &ZeEventPoolCache[static_cast<int>(Flags)];
}
return UR_RESULT_SUCCESS;
}

// Decrement number of events living in the pool upon event destroy
Expand Down Expand Up @@ -311,33 +276,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
23 changes: 14 additions & 9 deletions source/adapters/level_zero/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1238,15 +1238,21 @@ ur_result_t EventCreate(ur_context_handle_t Context,
bool ProfilingEnabled =
ForceDisableProfiling ? false : (!Queue || Queue->isProfilingEnabled());
bool UsingImmediateCommandlists = !Queue || Queue->UsingImmCmdLists;
ur_event_flags_t Flags = static_cast<ur_event_flags_t>(
ProfilingEnabled ? ENABLE_PROFILER
: 0 | UsingImmediateCommandlists ? USING_IMM_CMDLIST
: 0 | HostVisible ? HOST_VISIBLE
: 0 | IsMultiDevice ? MULTIDEVICE
: 0 | CounterBasedEventEnabled ? COUNTER_BASED
: 0);

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 @@ -1256,16 +1262,15 @@ ur_result_t EventCreate(ur_context_handle_t Context,

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 @@ -1290,8 +1295,8 @@ ur_result_t EventCreate(ur_context_handle_t Context,
} 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 4304208

Please sign in to comment.