Skip to content

Commit a9550cd

Browse files
committed
fix event caching
1 parent db83117 commit a9550cd

File tree

3 files changed

+49
-42
lines changed

3 files changed

+49
-42
lines changed

source/adapters/level_zero/context.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -565,18 +565,20 @@ ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
565565
bool HostVisible, bool WithProfiling, ur_device_handle_t Device,
566566
bool CounterBasedEventEnabled) {
567567
std::scoped_lock<ur_mutex> Lock(EventCacheMutex);
568-
auto Cache = getEventCache(HostVisible, WithProfiling, Device);
569-
if (Cache->empty())
568+
auto Cache = getEventCache(HostVisible, WithProfiling, Device, CounterBasedEventEnabled);
569+
if (Cache->empty()) {
570+
logger::info("Cache empty (Host Visible: {}, Profiling: {}, Counter: {}, Device: {})", HostVisible, WithProfiling, CounterBasedEventEnabled, Device);
570571
return nullptr;
572+
}
571573

572574
auto It = Cache->begin();
573575
ur_event_handle_t Event = *It;
574-
if (Event->CounterBasedEventsEnabled != CounterBasedEventEnabled) {
575-
return nullptr;
576-
}
577576
Cache->erase(It);
578577
// We have to reset event before using it.
579578
Event->reset();
579+
580+
logger::info("Using {} event (Host Visible: {}, Profiling: {}, Counter: {}, Device: {}) from cache {}", Event, Event->HostVisibleEvent, Event->isProfilingEnabled(), Event->CounterBasedEventsEnabled, Device, Cache);
581+
580582
return Event;
581583
}
582584

@@ -589,7 +591,8 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
589591
}
590592

591593
auto Cache = getEventCache(Event->isHostVisible(),
592-
Event->isProfilingEnabled(), Device);
594+
Event->isProfilingEnabled(), Device, Event->CounterBasedEventsEnabled);
595+
logger::info("Inserting {} event (Host Visible: {}, Profiling: {}, Counter: {}, Device: {}) into cache {}", Event, Event->HostVisibleEvent, Event->isProfilingEnabled(), Event->CounterBasedEventsEnabled, Device, Cache);
593596
Cache->emplace_back(Event);
594597
}
595598

source/adapters/level_zero/context.hpp

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,6 @@ struct ur_context_handle_t_ : _ur_object {
169169
// holding the current pool usage counts.
170170
ur_mutex ZeEventPoolCacheMutex;
171171

172-
// Mutex to control operations on event caches.
173-
ur_mutex EventCacheMutex;
174-
175-
// Caches for events.
176-
using EventCache = std::vector<std::list<ur_event_handle_t>>;
177-
EventCache EventCaches{4};
178-
std::vector<std::unordered_map<ur_device_handle_t, size_t>>
179-
EventCachesDeviceMap{4};
180-
181172
// Initialize the PI context.
182173
ur_result_t initialize();
183174

@@ -313,36 +304,44 @@ struct ur_context_handle_t_ : _ur_object {
313304
ze_context_handle_t getZeHandle() const;
314305

315306
private:
307+
308+
enum EventFlags {
309+
EVENT_FLAG_HOST_VISIBLE = UR_BIT(0),
310+
EVENT_FLAG_WITH_PROFILING = UR_BIT(1),
311+
EVENT_FLAG_COUNTER = UR_BIT(2),
312+
MAX_EVENT_FLAG_BITS = 3, // this is used as an offset for embedding device id
313+
};
314+
315+
// Mutex to control operations on event caches.
316+
ur_mutex EventCacheMutex;
317+
318+
// Caches for events.
319+
using EventCache = std::list<ur_event_handle_t>;
320+
std::vector<EventCache> EventCaches;
321+
316322
// Get the cache of events for a provided scope and profiling mode.
317-
auto getEventCache(bool HostVisible, bool WithProfiling,
318-
ur_device_handle_t Device) {
323+
EventCache *getEventCache(bool HostVisible, bool WithProfiling,
324+
ur_device_handle_t Device, bool Counter) {
325+
326+
size_t index = 0;
319327
if (HostVisible) {
320-
if (Device) {
321-
auto EventCachesMap =
322-
WithProfiling ? &EventCachesDeviceMap[0] : &EventCachesDeviceMap[1];
323-
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
324-
EventCaches.emplace_back();
325-
EventCachesMap->insert(
326-
std::make_pair(Device, EventCaches.size() - 1));
327-
}
328-
return &EventCaches[(*EventCachesMap)[Device]];
329-
} else {
330-
return WithProfiling ? &EventCaches[0] : &EventCaches[1];
331-
}
332-
} else {
333-
if (Device) {
334-
auto EventCachesMap =
335-
WithProfiling ? &EventCachesDeviceMap[2] : &EventCachesDeviceMap[3];
336-
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
337-
EventCaches.emplace_back();
338-
EventCachesMap->insert(
339-
std::make_pair(Device, EventCaches.size() - 1));
340-
}
341-
return &EventCaches[(*EventCachesMap)[Device]];
342-
} else {
343-
return WithProfiling ? &EventCaches[2] : &EventCaches[3];
344-
}
328+
index |= EVENT_FLAG_HOST_VISIBLE;
329+
}
330+
if (WithProfiling) {
331+
index |= EVENT_FLAG_WITH_PROFILING;
345332
}
333+
if (Counter) {
334+
index |= EVENT_FLAG_COUNTER;
335+
}
336+
if (Device) {
337+
index |= (*Device->Id << MAX_EVENT_FLAG_BITS);
338+
}
339+
340+
if (index >= EventCaches.size()) {
341+
EventCaches.resize(index + 1);
342+
}
343+
344+
return &EventCaches[index];
346345
}
347346
};
348347

test/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ set(UR_TEST_FUZZTESTS ON CACHE BOOL "Run fuzz tests if using clang and UR_DPCXX
1717
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
1818
set(INSTALL_GTEST OFF)
1919
FetchContent_MakeAvailable(googletest)
20+
21+
set_target_properties(gtest PROPERTIES POSITION_INDEPENDENT_CODE ON)
22+
set_target_properties(gtest_main PROPERTIES POSITION_INDEPENDENT_CODE ON)
23+
set_target_properties(gmock PROPERTIES POSITION_INDEPENDENT_CODE ON)
24+
2025
enable_testing()
2126

2227
# Conformance defines the generate_device_binaries target which should be

0 commit comments

Comments
 (0)