Skip to content

Commit e948e07

Browse files
authored
[SYCL] Optimize kernel cache lock (#17613)
This PR replaces `std::mutex` with the `sycl::detail::SpinLock` (that performs better) in `KernelProgramCache`.
1 parent 5a0d980 commit e948e07

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

sycl/include/sycl/detail/spinlock.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ namespace detail {
2626
/// std::mutex, that doesn't provide such guarantees).
2727
class SpinLock {
2828
public:
29+
bool try_lock() { return !MLock.test_and_set(std::memory_order_acquire); }
30+
2931
void lock() {
3032
while (MLock.test_and_set(std::memory_order_acquire))
3133
std::this_thread::yield();

sycl/source/detail/kernel_program_cache.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <sycl/detail/common.hpp>
1616
#include <sycl/detail/locked.hpp>
1717
#include <sycl/detail/os_util.hpp>
18+
#include <sycl/detail/spinlock.hpp>
1819
#include <sycl/detail/ur.hpp>
1920
#include <sycl/detail/util.hpp>
2021

@@ -421,7 +422,7 @@ class KernelProgramCache {
421422

422423
template <typename KeyT>
423424
KernelFastCacheValT tryToGetKernelFast(KeyT &&CacheKey) {
424-
std::unique_lock<std::mutex> Lock(MKernelFastCacheMutex);
425+
KernelFastCacheReadLockT Lock(MKernelFastCacheMutex);
425426
auto It = MKernelFastCache.find(CacheKey);
426427
if (It != MKernelFastCache.end()) {
427428
traceKernel("Kernel fetched.", CacheKey.second, true);
@@ -445,7 +446,7 @@ class KernelProgramCache {
445446
return;
446447
}
447448
// Save reference between the program and the fast cache key.
448-
std::unique_lock<std::mutex> Lock(MKernelFastCacheMutex);
449+
KernelFastCacheWriteLockT Lock(MKernelFastCacheMutex);
449450
MProgramToKernelFastCacheKeyMap[Program].emplace_back(CacheKey);
450451

451452
// if no insertion took place, thus some other thread has already inserted
@@ -483,7 +484,7 @@ class KernelProgramCache {
483484

484485
{
485486
// Remove corresponding entries from KernelFastCache.
486-
std::unique_lock<std::mutex> Lock(MKernelFastCacheMutex);
487+
KernelFastCacheWriteLockT Lock(MKernelFastCacheMutex);
487488
if (auto FastCacheKeyItr =
488489
MProgramToKernelFastCacheKeyMap.find(NativePrg);
489490
FastCacheKeyItr != MProgramToKernelFastCacheKeyMap.end()) {
@@ -630,7 +631,7 @@ class KernelProgramCache {
630631
std::lock_guard<std::mutex> EvictionListLock(MProgramEvictionListMutex);
631632
std::lock_guard<std::mutex> L1(MProgramCacheMutex);
632633
std::lock_guard<std::mutex> L2(MKernelsPerProgramCacheMutex);
633-
std::lock_guard<std::mutex> L3(MKernelFastCacheMutex);
634+
KernelFastCacheWriteLockT L3(MKernelFastCacheMutex);
634635
MCachedPrograms = ProgramCache{};
635636
MKernelsPerProgramCache = KernelCacheT{};
636637
MKernelFastCache = KernelFastCacheT{};
@@ -758,7 +759,10 @@ class KernelProgramCache {
758759
KernelCacheT MKernelsPerProgramCache;
759760
ContextPtr MParentContext;
760761

761-
std::mutex MKernelFastCacheMutex;
762+
using KernelFastCacheMutexT = SpinLock;
763+
using KernelFastCacheReadLockT = std::lock_guard<KernelFastCacheMutexT>;
764+
using KernelFastCacheWriteLockT = std::lock_guard<KernelFastCacheMutexT>;
765+
KernelFastCacheMutexT MKernelFastCacheMutex;
762766
KernelFastCacheT MKernelFastCache;
763767

764768
// Map between fast kernel cache keys and program handle.

0 commit comments

Comments
 (0)