Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "[L0] Phase 2 of Counter-Based Event Implementation" #2113

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions source/adapters/level_zero/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,9 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(

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

Expand All @@ -588,16 +585,9 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
Device = Event->UrQueue->Device;
}

if (Event->CounterBasedEventsEnabled) {
auto Cache = getCounterBasedEventCache(
Event->isProfilingEnabled(),
!(Event->UrQueue) || (Event->UrQueue)->UsingImmCmdLists, Device);
Cache->emplace_back(Event);
} else {
auto Cache = getEventCache(Event->isHostVisible(),
Event->isProfilingEnabled(), Device);
Cache->emplace_back(Event);
}
auto Cache = getEventCache(Event->isHostVisible(),
Event->isProfilingEnabled(), Device);
Cache->emplace_back(Event);
}

ur_result_t
Expand Down
97 changes: 26 additions & 71 deletions source/adapters/level_zero/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ struct ur_context_handle_t_ : _ur_object {
: ZeContext{ZeContext}, Devices{Devs, Devs + NumDevices},
NumDevices{NumDevices} {
OwnNativeHandle = OwnZeContext;
for (const auto &Device : Devices) {
for (int i = 0; i < EventCacheTypeCount; i++) {
EventCaches.emplace_back();
EventCachesDeviceMap[i].insert(
std::make_pair(Device, EventCaches.size() - 1));
}
}
}

ur_context_handle_t_(ze_context_handle_t ZeContext) : ZeContext{ZeContext} {}
Expand Down Expand Up @@ -154,10 +147,9 @@ struct ur_context_handle_t_ : _ur_object {
// head.
//
// Cache of event pools to which host-visible events are added to.
std::vector<std::list<ze_event_pool_handle_t>> ZeEventPoolCache{
ZeEventPoolCacheTypeCount * 2};
std::vector<std::list<ze_event_pool_handle_t>> ZeEventPoolCache{12};
std::vector<std::unordered_map<ze_device_handle_t, size_t>>
ZeEventPoolCacheDeviceMap{ZeEventPoolCacheTypeCount * 2};
ZeEventPoolCacheDeviceMap{12};

// This map will be used to determine if a pool is full or not
// by storing number of empty slots available in the pool.
Expand All @@ -179,9 +171,9 @@ struct ur_context_handle_t_ : _ur_object {

// Caches for events.
using EventCache = std::vector<std::list<ur_event_handle_t>>;
EventCache EventCaches{EventCacheTypeCount};
EventCache EventCaches{4};
std::vector<std::unordered_map<ur_device_handle_t, size_t>>
EventCachesDeviceMap{EventCacheTypeCount};
EventCachesDeviceMap{4};

// Initialize the PI context.
ur_result_t initialize();
Expand Down Expand Up @@ -219,39 +211,25 @@ struct ur_context_handle_t_ : _ur_object {
ur_event_handle_t getEventFromContextCache(bool HostVisible,
bool WithProfiling,
ur_device_handle_t Device,
bool CounterBasedEventEnabled,
bool UsingImmCmdList);
bool CounterBasedEventEnabled);

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

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

enum EventCacheType {
HostVisibleProfilingCacheType,
HostVisibleRegularCacheType,
HostInvisibleProfilingCacheType,
HostInvisibleRegularCacheType,
CounterBasedImmediateCacheType,
CounterBasedRegularCacheType,
CounterBasedImmediateProfilingCacheType,
CounterBasedRegularProfilingCacheType,
EventCacheTypeCount
HostInvisibleCounterBasedImmediateCacheType
};

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

calculateCacheIndex(HostVisible, CounterBasedEventEnabled,
UsingImmediateCmdList, CacheType);
Expand All @@ -274,7 +252,7 @@ struct ur_context_handle_t_ : _ur_object {
ur_result_t calculateCacheIndex(bool HostVisible,
bool CounterBasedEventEnabled,
bool UsingImmediateCmdList,
ZeEventPoolCacheType &CacheType) {
EventPoolCacheType &CacheType) {
if (CounterBasedEventEnabled && HostVisible && !UsingImmediateCmdList) {
CacheType = HostVisibleCounterBasedRegularCacheType;
} else if (CounterBasedEventEnabled && !HostVisible &&
Expand Down Expand Up @@ -338,57 +316,34 @@ struct ur_context_handle_t_ : _ur_object {
if (HostVisible) {
if (Device) {
auto EventCachesMap =
WithProfiling ? &EventCachesDeviceMap[HostVisibleProfilingCacheType]
: &EventCachesDeviceMap[HostVisibleRegularCacheType];
return &EventCaches[(*EventCachesMap)[Device]];
} else {
return WithProfiling ? &EventCaches[HostVisibleProfilingCacheType]
: &EventCaches[HostVisibleRegularCacheType];
}
} else {
if (Device) {
auto EventCachesMap =
WithProfiling
? &EventCachesDeviceMap[HostInvisibleProfilingCacheType]
: &EventCachesDeviceMap[HostInvisibleRegularCacheType];
return &EventCaches[(*EventCachesMap)[Device]];
} else {
return WithProfiling ? &EventCaches[HostInvisibleProfilingCacheType]
: &EventCaches[HostInvisibleRegularCacheType];
}
}
};
auto getCounterBasedEventCache(bool WithProfiling, bool UsingImmediateCmdList,
ur_device_handle_t Device) {
if (UsingImmediateCmdList) {
if (Device) {
auto EventCachesMap =
WithProfiling
? &EventCachesDeviceMap[CounterBasedImmediateProfilingCacheType]
: &EventCachesDeviceMap[CounterBasedImmediateCacheType];
WithProfiling ? &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[CounterBasedImmediateProfilingCacheType]
: &EventCaches[CounterBasedImmediateCacheType];
return WithProfiling ? &EventCaches[0] : &EventCaches[1];
}
} else {
if (Device) {
auto EventCachesMap =
WithProfiling
? &EventCachesDeviceMap[CounterBasedRegularProfilingCacheType]
: &EventCachesDeviceMap[CounterBasedRegularCacheType];
WithProfiling ? &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[CounterBasedRegularProfilingCacheType]
: &EventCaches[CounterBasedRegularCacheType];
return WithProfiling ? &EventCaches[2] : &EventCaches[3];
}
}
}
};

// Helper function to release the context, a caller must lock the
// platform-level mutex guarding the container with contexts because the
// context can be removed from the list of tracked contexts.
// Helper function to release the context, a caller must lock the platform-level
// mutex guarding the container with contexts because the context can be removed
// from the list of tracked contexts.
ur_result_t ContextReleaseHelper(ur_context_handle_t Context);
4 changes: 2 additions & 2 deletions source/adapters/level_zero/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ urEventRelease(ur_event_handle_t Event ///< [in] handle of the event object
) {
Event->RefCountExternal--;
UR_CALL(urEventReleaseInternal(Event));

return UR_RESULT_SUCCESS;
}

Expand Down Expand Up @@ -1256,8 +1257,7 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
}

if (auto CachedEvent = Context->getEventFromContextCache(
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled,
UsingImmediateCommandlists)) {
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled)) {
*RetEvent = CachedEvent;
return UR_RESULT_SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion source/adapters/level_zero/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ ur_queue_handle_t_::ur_queue_handle_t_(
return std::atoi(UrRet) != 0;
}();
this->CounterBasedEventsEnabled =
isInOrderQueue() && Device->useDriverInOrderLists() &&
UsingImmCmdLists && isInOrderQueue() && Device->useDriverInOrderLists() &&
useDriverCounterBasedEvents &&
Device->Platform->ZeDriverEventPoolCountingEventsExtensionFound;
}
Expand Down
Loading