DPC++ Runtime
Runtime libraries for oneAPI Data Parallel C++
kernel_program_cache.hpp
Go to the documentation of this file.
1 //==--- kernel_program_cache.hpp - Cache for kernel and program -*- C++-*---==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
14 #include <CL/sycl/detail/pi.hpp>
15 #include <CL/sycl/detail/util.hpp>
16 #include <detail/platform_impl.hpp>
17 
18 #include <atomic>
19 #include <condition_variable>
20 #include <map>
21 #include <mutex>
22 #include <type_traits>
23 
24 // For testing purposes
25 class MockKernelProgramCache;
26 
28 namespace sycl {
29 namespace detail {
30 class context_impl;
32 public:
35  struct BuildError {
36  std::string Msg;
38 
39  bool isFilledIn() const {
40  return !Msg.empty();
41  }
42  };
43 
48  template <typename T> struct BuildResult {
49  std::atomic<T *> Ptr;
50  std::atomic<int> State;
52 
63  std::condition_variable MBuildCV;
65  std::mutex MBuildResultMutex;
66 
67  BuildResult(T* P, int S) : Ptr{P}, State{S}, Error{"", 0} {}
68  };
69 
70  using PiProgramT = std::remove_pointer<RT::PiProgram>::type;
71  using PiProgramPtrT = std::atomic<PiProgramT *>;
73  using ProgramCacheKeyT = std::pair<std::pair<SerializedObj, KernelSetId>,
74  std::pair<RT::PiDevice, std::string>>;
75  using ProgramCacheT = std::map<ProgramCacheKeyT, ProgramWithBuildStateT>;
77 
78  using PiKernelT = std::remove_pointer<RT::PiKernel>::type;
79 
80  using PiKernelPtrT = std::atomic<PiKernelT *>;
82  using KernelByNameT = std::map<std::string, KernelWithBuildStateT>;
83  using KernelCacheT = std::map<RT::PiProgram, KernelByNameT>;
84 
85  using KernelFastCacheKeyT =
86  std::tuple<SerializedObj, OSModuleHandle, RT::PiDevice, std::string,
87  std::string>;
88  using KernelFastCacheValT =
89  std::tuple<RT::PiKernel, std::mutex *, RT::PiProgram>;
90  using KernelFastCacheT = std::map<KernelFastCacheKeyT, KernelFastCacheValT>;
91 
93 
94  void setContextPtr(const ContextPtr &AContext) { MParentContext = AContext; }
95 
97  return {MCachedPrograms, MProgramCacheMutex};
98  }
99 
101  return {MKernelsPerProgramCache, MKernelsPerProgramCacheMutex};
102  }
103 
104  template <typename T, class Predicate>
105  void waitUntilBuilt(BuildResult<T> &BR, Predicate Pred) const {
106  std::unique_lock<std::mutex> Lock(BR.MBuildResultMutex);
107 
108  BR.MBuildCV.wait(Lock, Pred);
109  }
110 
111  template <typename T> void notifyAllBuild(BuildResult<T> &BR) const {
112  BR.MBuildCV.notify_all();
113  }
114 
115  template <typename KeyT>
117  std::unique_lock<std::mutex> Lock(MKernelFastCacheMutex);
118  auto It = MKernelFastCache.find(CacheKey);
119  if (It != MKernelFastCache.end()) {
120  return It->second;
121  }
122  return std::make_tuple(nullptr, nullptr, nullptr);
123  }
124 
125  template <typename KeyT, typename ValT>
126  void saveKernel(KeyT &&CacheKey, ValT &&CacheVal) {
127  std::unique_lock<std::mutex> Lock(MKernelFastCacheMutex);
128  // if no insertion took place, thus some other thread has already inserted
129  // smth in the cache
130  MKernelFastCache.emplace(CacheKey, CacheVal);
131  }
132 
133 private:
134  std::mutex MProgramCacheMutex;
135  std::mutex MKernelsPerProgramCacheMutex;
136 
137  ProgramCacheT MCachedPrograms;
138  KernelCacheT MKernelsPerProgramCache;
139  ContextPtr MParentContext;
140 
141  std::mutex MKernelFastCacheMutex;
142  KernelFastCacheT MKernelFastCache;
143  friend class ::MockKernelProgramCache;
144 };
145 } // namespace detail
146 } // namespace sycl
147 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::detail::KernelProgramCache::BuildError::Code
pi_int32 Code
Definition: kernel_program_cache.hpp:37
cl::sycl::detail::KernelProgramCache::BuildResult
Denotes pointer to some entity with its general state and build error.
Definition: kernel_program_cache.hpp:48
cl::sycl::detail::Locked
Represents a reference to value with appropriate lock acquired.
Definition: locked.hpp:24
cl::sycl::detail::SerializedObj
std::vector< unsigned char > SerializedObj
Definition: util.hpp:56
cl::sycl::detail::KernelProgramCache::saveKernel
void saveKernel(KeyT &&CacheKey, ValT &&CacheVal)
Definition: kernel_program_cache.hpp:126
cl::sycl::detail::KernelProgramCache::KernelFastCacheT
std::map< KernelFastCacheKeyT, KernelFastCacheValT > KernelFastCacheT
Definition: kernel_program_cache.hpp:90
cl::sycl::detail::make_tuple
constexpr tuple< Ts... > make_tuple(Ts... Args)
Definition: tuple.hpp:36
cl::sycl::detail::KernelProgramCache
Definition: kernel_program_cache.hpp:31
cl::sycl::detail::KernelProgramCache::KernelByNameT
std::map< std::string, KernelWithBuildStateT > KernelByNameT
Definition: kernel_program_cache.hpp:82
cl::sycl::detail::KernelProgramCache::BuildError
Denotes build error data.
Definition: kernel_program_cache.hpp:35
cl::sycl::detail::pi::PiDevice
::pi_device PiDevice
Definition: pi.hpp:102
cl::sycl::detail::KernelProgramCache::notifyAllBuild
void notifyAllBuild(BuildResult< T > &BR) const
Definition: kernel_program_cache.hpp:111
os_util.hpp
cl::sycl::detail::KernelProgramCache::PiKernelT
std::remove_pointer< RT::PiKernel >::type PiKernelT
Definition: kernel_program_cache.hpp:78
util.hpp
cl::sycl::detail::KernelProgramCache::KernelCacheT
std::map< RT::PiProgram, KernelByNameT > KernelCacheT
Definition: kernel_program_cache.hpp:83
cl::sycl::detail::KernelProgramCache::BuildResult::Ptr
std::atomic< T * > Ptr
Definition: kernel_program_cache.hpp:49
pi.hpp
cl::sycl::detail::KernelProgramCache::BuildResult::BuildResult
BuildResult(T *P, int S)
Definition: kernel_program_cache.hpp:67
cl::sycl::detail::KernelProgramCache::BuildResult::MBuildCV
std::condition_variable MBuildCV
Condition variable to signal that build result is ready.
Definition: kernel_program_cache.hpp:63
cl::sycl::detail::KernelProgramCache::PiKernelPtrT
std::atomic< PiKernelT * > PiKernelPtrT
Definition: kernel_program_cache.hpp:80
platform_impl.hpp
cl::sycl::detail::KernelProgramCache::BuildError::isFilledIn
bool isFilledIn() const
Definition: kernel_program_cache.hpp:39
cl::sycl::detail::KernelProgramCache::BuildResult::MBuildResultMutex
std::mutex MBuildResultMutex
A mutex to be employed along with MBuildCV.
Definition: kernel_program_cache.hpp:65
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::detail::context_impl
Definition: context_impl.hpp:31
cl::sycl::detail::KernelProgramCache::ProgramCacheKeyT
std::pair< std::pair< SerializedObj, KernelSetId >, std::pair< RT::PiDevice, std::string > > ProgramCacheKeyT
Definition: kernel_program_cache.hpp:74
cl::sycl::detail::KernelProgramCache::BuildError::Msg
std::string Msg
Definition: kernel_program_cache.hpp:36
cl::sycl::detail::OSModuleHandle
intptr_t OSModuleHandle
Uniquely identifies an operating system module (executable or a dynamic library)
Definition: os_util.hpp:48
cl::sycl::detail::KernelProgramCache::ProgramCacheT
std::map< ProgramCacheKeyT, ProgramWithBuildStateT > ProgramCacheT
Definition: kernel_program_cache.hpp:75
cl::sycl::detail::KernelProgramCache::waitUntilBuilt
void waitUntilBuilt(BuildResult< T > &BR, Predicate Pred) const
Definition: kernel_program_cache.hpp:105
cl::sycl::detail::KernelProgramCache::BuildResult::Error
BuildError Error
Definition: kernel_program_cache.hpp:51
cl::sycl::detail::KernelProgramCache::acquireKernelsPerProgramCache
Locked< KernelCacheT > acquireKernelsPerProgramCache()
Definition: kernel_program_cache.hpp:100
P
#define P(n)
common.hpp
cl::sycl::detail::KernelProgramCache::acquireCachedPrograms
Locked< ProgramCacheT > acquireCachedPrograms()
Definition: kernel_program_cache.hpp:96
cl::sycl::detail::KernelProgramCache::KernelFastCacheValT
std::tuple< RT::PiKernel, std::mutex *, RT::PiProgram > KernelFastCacheValT
Definition: kernel_program_cache.hpp:89
cl::sycl::detail::KernelProgramCache::PiProgramPtrT
std::atomic< PiProgramT * > PiProgramPtrT
Definition: kernel_program_cache.hpp:71
cl::sycl::detail::KernelProgramCache::KernelFastCacheKeyT
std::tuple< SerializedObj, OSModuleHandle, RT::PiDevice, std::string, std::string > KernelFastCacheKeyT
Definition: kernel_program_cache.hpp:87
cl::sycl::detail::KernelProgramCache::PiProgramT
std::remove_pointer< RT::PiProgram >::type PiProgramT
Definition: kernel_program_cache.hpp:70
pi_int32
int32_t pi_int32
Definition: pi.h:67
cl::sycl::detail::KernelProgramCache::tryToGetKernelFast
KernelFastCacheValT tryToGetKernelFast(KeyT &&CacheKey)
Definition: kernel_program_cache.hpp:116
locked.hpp
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12
cl::sycl::detail::KernelProgramCache::setContextPtr
void setContextPtr(const ContextPtr &AContext)
Definition: kernel_program_cache.hpp:94
cl::sycl::detail::KernelProgramCache::BuildResult::State
std::atomic< int > State
Definition: kernel_program_cache.hpp:50