Skip to content

Commit

Permalink
[L0] avoid using pointers into event cache related vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
pbalcer committed Feb 28, 2024
1 parent 2aa10dd commit 8a5d6d3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
34 changes: 19 additions & 15 deletions source/adapters/level_zero/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ struct ur_context_handle_t_ : _ur_object {
//
// Cache of event pools to which host-visible events are added to.
std::vector<std::list<ze_event_pool_handle_t>> ZeEventPoolCache{4};
std::vector<std::unordered_map<ze_device_handle_t,
std::list<ze_event_pool_handle_t> *>>
std::vector<std::unordered_map<ze_device_handle_t, size_t>>
ZeEventPoolCacheDeviceMap{4};

// This map will be used to determine if a pool is full or not
Expand All @@ -165,9 +164,9 @@ struct ur_context_handle_t_ : _ur_object {
ur_mutex EventCacheMutex;

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

// Initialize the PI context.
Expand Down Expand Up @@ -205,18 +204,20 @@ struct ur_context_handle_t_ : _ur_object {
// Add ur_event_handle_t to cache.
void addEventToContextCache(ur_event_handle_t);

auto getZeEventPoolCache(bool HostVisible, bool WithProfiling,
ze_device_handle_t ZeDevice) {
std::list<ze_event_pool_handle_t> *
getZeEventPoolCache(bool HostVisible, bool WithProfiling,
ze_device_handle_t ZeDevice) {
if (HostVisible) {
if (ZeDevice) {
auto ZeEventPoolCacheMap = WithProfiling
? &ZeEventPoolCacheDeviceMap[0]
: &ZeEventPoolCacheDeviceMap[1];
if (ZeEventPoolCacheMap->find(ZeDevice) == ZeEventPoolCacheMap->end()) {
ZeEventPoolCache.emplace_back();
(*ZeEventPoolCacheMap)[ZeDevice] = &ZeEventPoolCache.back();
ZeEventPoolCacheMap->insert(
std::make_pair(ZeDevice, ZeEventPoolCache.size() - 1));
}
return (*ZeEventPoolCacheMap)[ZeDevice];
return &ZeEventPoolCache[(*ZeEventPoolCacheMap)[ZeDevice]];
} else {
return WithProfiling ? &ZeEventPoolCache[0] : &ZeEventPoolCache[1];
}
Expand All @@ -227,9 +228,10 @@ struct ur_context_handle_t_ : _ur_object {
: &ZeEventPoolCacheDeviceMap[3];
if (ZeEventPoolCacheMap->find(ZeDevice) == ZeEventPoolCacheMap->end()) {
ZeEventPoolCache.emplace_back();
(*ZeEventPoolCacheMap)[ZeDevice] = &ZeEventPoolCache.back();
ZeEventPoolCacheMap->insert(
std::make_pair(ZeDevice, ZeEventPoolCache.size() - 1));
}
return (*ZeEventPoolCacheMap)[ZeDevice];
return &ZeEventPoolCache[(*ZeEventPoolCacheMap)[ZeDevice]];
} else {
return WithProfiling ? &ZeEventPoolCache[2] : &ZeEventPoolCache[3];
}
Expand Down Expand Up @@ -280,9 +282,10 @@ struct ur_context_handle_t_ : _ur_object {
WithProfiling ? &EventCachesDeviceMap[0] : &EventCachesDeviceMap[1];
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
EventCaches.emplace_back();
(*EventCachesMap)[Device] = &EventCaches.back();
EventCachesMap->insert(
std::make_pair(Device, EventCaches.size() - 1));
}
return (*EventCachesMap)[Device];
return &EventCaches[(*EventCachesMap)[Device]];
} else {
return WithProfiling ? &EventCaches[0] : &EventCaches[1];
}
Expand All @@ -292,9 +295,10 @@ struct ur_context_handle_t_ : _ur_object {
WithProfiling ? &EventCachesDeviceMap[2] : &EventCachesDeviceMap[3];
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
EventCaches.emplace_back();
(*EventCachesMap)[Device] = &EventCaches.back();
EventCachesMap->insert(
std::make_pair(Device, EventCaches.size() - 1));
}
return (*EventCachesMap)[Device];
return &EventCaches[(*EventCachesMap)[Device]];
} else {
return WithProfiling ? &EventCaches[2] : &EventCaches[3];
}
Expand Down
12 changes: 7 additions & 5 deletions source/adapters/level_zero/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1273,9 +1273,9 @@ ur_result_t ur_queue_handle_t_::addEventToQueueCache(ur_event_handle_t Event) {
: &EventCachesDeviceMap[1];
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
EventCaches.emplace_back();
(*EventCachesMap)[Device] = &EventCaches.back();
EventCachesMap->insert(std::make_pair(Device, EventCaches.size() - 1));
}
(*EventCachesMap)[Device]->emplace_back(Event);
EventCaches[EventCachesMap->at(Device)].emplace_back(Event);
} else {
auto Cache = Event->isHostVisible() ? &EventCaches[0] : &EventCaches[1];
Cache->emplace_back(Event);
Expand All @@ -1301,9 +1301,11 @@ ur_result_t urQueueReleaseInternal(ur_queue_handle_t Queue) {
if (!UrQueue->RefCount.decrementAndTest())
return UR_RESULT_SUCCESS;

for (auto &Cache : UrQueue->EventCaches)
for (auto &Cache : UrQueue->EventCaches) {
for (auto &Event : Cache)
UR_CALL(urEventReleaseInternal(Event));
Cache.clear();
}

if (UrQueue->OwnZeCommandQueue) {
for (auto &QueueMap :
Expand Down Expand Up @@ -1462,8 +1464,8 @@ ur_event_handle_t ur_queue_handle_t_::getEventFromQueueCache(bool IsMultiDevice,

if (!IsMultiDevice) {
auto Device = this->Device;
Cache = HostVisible ? EventCachesDeviceMap[0][Device]
: EventCachesDeviceMap[1][Device];
Cache = HostVisible ? &EventCaches[EventCachesDeviceMap[0][Device]]
: &EventCaches[EventCachesDeviceMap[1][Device]];
if (!Cache) {
return nullptr;
}
Expand Down
3 changes: 1 addition & 2 deletions source/adapters/level_zero/queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,7 @@ struct ur_queue_handle_t_ : _ur_object {
// inside all command lists in the queue as described in the 2-event model.
// Leftover events in the cache are relased at the queue destruction.
std::vector<std::list<ur_event_handle_t>> EventCaches{2};
std::vector<
std::unordered_map<ur_device_handle_t, std::list<ur_event_handle_t> *>>
std::vector<std::unordered_map<ur_device_handle_t, size_t>>
EventCachesDeviceMap{2};

// adjust the queue's batch size, knowing that the current command list
Expand Down

0 comments on commit 8a5d6d3

Please sign in to comment.