diff --git a/source/adapters/level_zero/context.cpp b/source/adapters/level_zero/context.cpp index 7c1c412ee4..a4e0512812 100644 --- a/source/adapters/level_zero/context.cpp +++ b/source/adapters/level_zero/context.cpp @@ -565,18 +565,20 @@ ur_event_handle_t ur_context_handle_t_::getEventFromContextCache( bool HostVisible, bool WithProfiling, ur_device_handle_t Device, bool CounterBasedEventEnabled) { std::scoped_lock Lock(EventCacheMutex); - auto Cache = getEventCache(HostVisible, WithProfiling, Device); - if (Cache->empty()) + auto Cache = getEventCache(HostVisible, WithProfiling, Device, CounterBasedEventEnabled); + if (Cache->empty()) { + logger::info("Cache empty (Host Visible: {}, Profiling: {}, Counter: {}, Device: {})", HostVisible, WithProfiling, CounterBasedEventEnabled, Device); return nullptr; + } auto It = Cache->begin(); ur_event_handle_t Event = *It; - if (Event->CounterBasedEventsEnabled != CounterBasedEventEnabled) { - return nullptr; - } Cache->erase(It); // We have to reset event before using it. Event->reset(); + + logger::info("Using {} event (Host Visible: {}, Profiling: {}, Counter: {}, Device: {}) from cache {}", Event, Event->HostVisibleEvent, Event->isProfilingEnabled(), Event->CounterBasedEventsEnabled, Device, Cache); + return Event; } @@ -589,7 +591,8 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) { } auto Cache = getEventCache(Event->isHostVisible(), - Event->isProfilingEnabled(), Device); + Event->isProfilingEnabled(), Device, Event->CounterBasedEventsEnabled); + logger::info("Inserting {} event (Host Visible: {}, Profiling: {}, Counter: {}, Device: {}) into cache {}", Event, Event->HostVisibleEvent, Event->isProfilingEnabled(), Event->CounterBasedEventsEnabled, Device, Cache); Cache->emplace_back(Event); } diff --git a/source/adapters/level_zero/context.hpp b/source/adapters/level_zero/context.hpp index 0d3b2846e2..9adbe9ecb9 100644 --- a/source/adapters/level_zero/context.hpp +++ b/source/adapters/level_zero/context.hpp @@ -169,15 +169,6 @@ struct ur_context_handle_t_ : _ur_object { // holding the current pool usage counts. ur_mutex ZeEventPoolCacheMutex; - // Mutex to control operations on event caches. - ur_mutex EventCacheMutex; - - // Caches for events. - using EventCache = std::vector>; - EventCache EventCaches{4}; - std::vector> - EventCachesDeviceMap{4}; - // Initialize the PI context. ur_result_t initialize(); @@ -313,36 +304,44 @@ struct ur_context_handle_t_ : _ur_object { ze_context_handle_t getZeHandle() const; private: + + enum EventFlags { + EVENT_FLAG_HOST_VISIBLE = UR_BIT(0), + EVENT_FLAG_WITH_PROFILING = UR_BIT(1), + EVENT_FLAG_COUNTER = UR_BIT(2), + MAX_EVENT_FLAG_BITS = 3, // this is used as an offset for embedding device id + }; + + // Mutex to control operations on event caches. + ur_mutex EventCacheMutex; + + // Caches for events. + using EventCache = std::list; + std::vector EventCaches; + // Get the cache of events for a provided scope and profiling mode. - auto getEventCache(bool HostVisible, bool WithProfiling, - ur_device_handle_t Device) { + EventCache *getEventCache(bool HostVisible, bool WithProfiling, + ur_device_handle_t Device, bool Counter) { + + size_t index = 0; if (HostVisible) { - if (Device) { - auto EventCachesMap = - 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[0] : &EventCaches[1]; - } - } else { - if (Device) { - auto EventCachesMap = - 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[2] : &EventCaches[3]; - } + index |= EVENT_FLAG_HOST_VISIBLE; + } + if (WithProfiling) { + index |= EVENT_FLAG_WITH_PROFILING; } + if (Counter) { + index |= EVENT_FLAG_COUNTER; + } + if (Device) { + index |= (*Device->Id << MAX_EVENT_FLAG_BITS); + } + + if (index >= EventCaches.size()) { + EventCaches.resize(index + 1); + } + + return &EventCaches[index]; } }; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e7514cefd8..b615e3557e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -17,6 +17,11 @@ set(UR_TEST_FUZZTESTS ON CACHE BOOL "Run fuzz tests if using clang and UR_DPCXX set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) set(INSTALL_GTEST OFF) FetchContent_MakeAvailable(googletest) + +set_target_properties(gtest PROPERTIES POSITION_INDEPENDENT_CODE ON) +set_target_properties(gtest_main PROPERTIES POSITION_INDEPENDENT_CODE ON) +set_target_properties(gmock PROPERTIES POSITION_INDEPENDENT_CODE ON) + enable_testing() # Conformance defines the generate_device_binaries target which should be