Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sycl/include/sycl/detail/spinlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 9 additions & 5 deletions sycl/source/detail/kernel_program_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <sycl/detail/common.hpp>
#include <sycl/detail/locked.hpp>
#include <sycl/detail/os_util.hpp>
#include <sycl/detail/spinlock.hpp>
#include <sycl/detail/ur.hpp>
#include <sycl/detail/util.hpp>

Expand Down Expand Up @@ -421,7 +422,7 @@ class KernelProgramCache {

template <typename KeyT>
KernelFastCacheValT tryToGetKernelFast(KeyT &&CacheKey) {
std::unique_lock<std::mutex> Lock(MKernelFastCacheMutex);
KernelFastCacheReadLockT Lock(MKernelFastCacheMutex);
auto It = MKernelFastCache.find(CacheKey);
if (It != MKernelFastCache.end()) {
traceKernel("Kernel fetched.", CacheKey.second, true);
Expand All @@ -445,7 +446,7 @@ class KernelProgramCache {
return;
}
// Save reference between the program and the fast cache key.
std::unique_lock<std::mutex> Lock(MKernelFastCacheMutex);
KernelFastCacheWriteLockT Lock(MKernelFastCacheMutex);
MProgramToKernelFastCacheKeyMap[Program].emplace_back(CacheKey);

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

{
// Remove corresponding entries from KernelFastCache.
std::unique_lock<std::mutex> Lock(MKernelFastCacheMutex);
KernelFastCacheWriteLockT Lock(MKernelFastCacheMutex);
if (auto FastCacheKeyItr =
MProgramToKernelFastCacheKeyMap.find(NativePrg);
FastCacheKeyItr != MProgramToKernelFastCacheKeyMap.end()) {
Expand Down Expand Up @@ -630,7 +631,7 @@ class KernelProgramCache {
std::lock_guard<std::mutex> EvictionListLock(MProgramEvictionListMutex);
std::lock_guard<std::mutex> L1(MProgramCacheMutex);
std::lock_guard<std::mutex> L2(MKernelsPerProgramCacheMutex);
std::lock_guard<std::mutex> L3(MKernelFastCacheMutex);
KernelFastCacheWriteLockT L3(MKernelFastCacheMutex);
MCachedPrograms = ProgramCache{};
MKernelsPerProgramCache = KernelCacheT{};
MKernelFastCache = KernelFastCacheT{};
Expand Down Expand Up @@ -758,7 +759,10 @@ class KernelProgramCache {
KernelCacheT MKernelsPerProgramCache;
ContextPtr MParentContext;

std::mutex MKernelFastCacheMutex;
using KernelFastCacheMutexT = SpinLock;
using KernelFastCacheReadLockT = std::lock_guard<KernelFastCacheMutexT>;
using KernelFastCacheWriteLockT = std::lock_guard<KernelFastCacheMutexT>;
KernelFastCacheMutexT MKernelFastCacheMutex;
KernelFastCacheT MKernelFastCache;

// Map between fast kernel cache keys and program handle.
Expand Down