From d4718d7da40b6d178caf6acdac620c74900f91a4 Mon Sep 17 00:00:00 2001 From: Sergei Vinogradov Date: Fri, 21 Mar 2025 16:07:52 +0100 Subject: [PATCH 1/2] Add try_lock method to the sycl::_V1::detail::SpinLock --- sycl/include/sycl/detail/spinlock.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sycl/include/sycl/detail/spinlock.hpp b/sycl/include/sycl/detail/spinlock.hpp index 5743e6ee5e797..c41397609c1d6 100644 --- a/sycl/include/sycl/detail/spinlock.hpp +++ b/sycl/include/sycl/detail/spinlock.hpp @@ -26,6 +26,8 @@ namespace detail { /// std::mutex, that doesn't provide such guarantees). class SpinLock { public: + bool try_lock() { return !MLock.test_and_set(std::memory_order_acquire); } + void lock() { while (MLock.test_and_set(std::memory_order_acquire)) std::this_thread::yield(); From 60eba4a83b8e1dd696c228cf5873bc72a3ff5896 Mon Sep 17 00:00:00 2001 From: Sergei Vinogradov Date: Fri, 21 Mar 2025 16:20:11 +0100 Subject: [PATCH 2/2] Use spinlock in the KernelProgramCache instead of std::mutex --- sycl/source/detail/kernel_program_cache.hpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/sycl/source/detail/kernel_program_cache.hpp b/sycl/source/detail/kernel_program_cache.hpp index d1832da1c59f6..1d55fb56b9dd4 100644 --- a/sycl/source/detail/kernel_program_cache.hpp +++ b/sycl/source/detail/kernel_program_cache.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -421,7 +422,7 @@ class KernelProgramCache { template KernelFastCacheValT tryToGetKernelFast(KeyT &&CacheKey) { - std::unique_lock Lock(MKernelFastCacheMutex); + KernelFastCacheReadLockT Lock(MKernelFastCacheMutex); auto It = MKernelFastCache.find(CacheKey); if (It != MKernelFastCache.end()) { traceKernel("Kernel fetched.", CacheKey.second, true); @@ -445,7 +446,7 @@ class KernelProgramCache { return; } // Save reference between the program and the fast cache key. - std::unique_lock Lock(MKernelFastCacheMutex); + KernelFastCacheWriteLockT Lock(MKernelFastCacheMutex); MProgramToKernelFastCacheKeyMap[Program].emplace_back(CacheKey); // if no insertion took place, thus some other thread has already inserted @@ -483,7 +484,7 @@ class KernelProgramCache { { // Remove corresponding entries from KernelFastCache. - std::unique_lock Lock(MKernelFastCacheMutex); + KernelFastCacheWriteLockT Lock(MKernelFastCacheMutex); if (auto FastCacheKeyItr = MProgramToKernelFastCacheKeyMap.find(NativePrg); FastCacheKeyItr != MProgramToKernelFastCacheKeyMap.end()) { @@ -630,7 +631,7 @@ class KernelProgramCache { std::lock_guard EvictionListLock(MProgramEvictionListMutex); std::lock_guard L1(MProgramCacheMutex); std::lock_guard L2(MKernelsPerProgramCacheMutex); - std::lock_guard L3(MKernelFastCacheMutex); + KernelFastCacheWriteLockT L3(MKernelFastCacheMutex); MCachedPrograms = ProgramCache{}; MKernelsPerProgramCache = KernelCacheT{}; MKernelFastCache = KernelFastCacheT{}; @@ -758,7 +759,10 @@ class KernelProgramCache { KernelCacheT MKernelsPerProgramCache; ContextPtr MParentContext; - std::mutex MKernelFastCacheMutex; + using KernelFastCacheMutexT = SpinLock; + using KernelFastCacheReadLockT = std::lock_guard; + using KernelFastCacheWriteLockT = std::lock_guard; + KernelFastCacheMutexT MKernelFastCacheMutex; KernelFastCacheT MKernelFastCache; // Map between fast kernel cache keys and program handle.