20 #include <condition_variable>
22 #include <type_traits>
24 #include <boost/unordered/unordered_flat_map.hpp>
25 #include <boost/unordered_map.hpp>
28 class MockKernelProgramCache;
31 inline namespace _V1 {
85 State.store(DesiredState);
113 std::pair<std::uintptr_t, sycl::detail::pi::PiDevice>;
116 ::boost::unordered_map<ProgramCacheKeyT, ProgramBuildResultPtr>
Cache;
117 ::boost::unordered_multimap<CommonProgramKeyT, ProgramCacheKeyT>
KeyMap;
125 std::pair<sycl::detail::pi::PiKernel, const KernelArgMask *>;
142 ::boost::unordered_map<std::string, KernelBuildResultPtr>;
144 ::boost::unordered_map<sycl::detail::pi::PiProgram, KernelByNameT>;
158 ::boost::unordered_flat_map<KernelFastCacheKeyT, KernelFastCacheValT>;
165 return {MCachedPrograms, MProgramCacheMutex};
169 return {MKernelsPerProgramCache, MKernelsPerProgramCacheMutex};
172 std::pair<ProgramBuildResultPtr, bool>
175 auto &ProgCache = LockedCache.get();
176 auto [It, DidInsert] = ProgCache.Cache.try_emplace(CacheKey,
nullptr);
178 It->second = std::make_shared<ProgramBuildResult>(getPlugin());
181 std::make_pair(CacheKey.first.second, CacheKey.second);
182 ProgCache.KeyMap.emplace(CommonKey, CacheKey);
184 return std::make_pair(It->second, DidInsert);
187 std::pair<KernelBuildResultPtr, bool>
189 const std::string &KernelName) {
191 auto &Cache = LockedCache.get()[Program];
192 auto [It, DidInsert] = Cache.try_emplace(KernelName,
nullptr);
194 It->second = std::make_shared<KernelBuildResult>(getPlugin());
195 return std::make_pair(It->second, DidInsert);
198 template <
typename KeyT>
200 std::unique_lock<std::mutex> Lock(MKernelFastCacheMutex);
201 auto It = MKernelFastCache.find(CacheKey);
202 if (It != MKernelFastCache.end()) {
208 template <
typename KeyT,
typename ValT>
210 std::unique_lock<std::mutex> Lock(MKernelFastCacheMutex);
213 MKernelFastCache.emplace(CacheKey, CacheVal);
220 std::lock_guard<std::mutex> L1(MProgramCacheMutex);
221 std::lock_guard<std::mutex> L2(MKernelsPerProgramCacheMutex);
222 std::lock_guard<std::mutex> L3(MKernelFastCacheMutex);
250 template <
typename ExceptionT,
typename GetCachedBuildFT,
typename BuildFT>
251 auto getOrBuild(GetCachedBuildFT &&GetCachedBuild, BuildFT &&Build) {
253 constexpr
size_t MaxAttempts = 2;
254 for (
size_t AttemptCounter = 0;; ++AttemptCounter) {
255 auto Res = GetCachedBuild();
270 AttemptCounter + 1 == MaxAttempts) {
300 std::rethrow_exception(std::current_exception());
303 std::rethrow_exception(std::current_exception());
309 std::mutex MProgramCacheMutex;
310 std::mutex MKernelsPerProgramCacheMutex;
312 ProgramCache MCachedPrograms;
316 std::mutex MKernelFastCacheMutex;
318 friend class ::MockKernelProgramCache;
std::shared_ptr< KernelBuildResult > KernelBuildResultPtr
~KernelProgramCache()=default
void reset()
Clears cache state.
std::shared_ptr< ProgramBuildResult > ProgramBuildResultPtr
Locked< ProgramCache > acquireCachedPrograms()
::boost::unordered_flat_map< KernelFastCacheKeyT, KernelFastCacheValT > KernelFastCacheT
BuildState
Denotes the state of a build.
std::pair< std::pair< SerializedObj, std::uintptr_t >, sycl::detail::pi::PiDevice > ProgramCacheKeyT
void setContextPtr(const ContextPtr &AContext)
auto getOrBuild(GetCachedBuildFT &&GetCachedBuild, BuildFT &&Build)
Try to fetch entity (kernel or program) from cache.
std::pair< KernelBuildResultPtr, bool > getOrInsertKernel(sycl::detail::pi::PiProgram Program, const std::string &KernelName)
Locked< KernelCacheT > acquireKernelsPerProgramCache()
::boost::unordered_map< sycl::detail::pi::PiProgram, KernelByNameT > KernelCacheT
KernelFastCacheValT tryToGetKernelFast(KeyT &&CacheKey)
std::tuple< SerializedObj, sycl::detail::pi::PiDevice, std::string, std::string > KernelFastCacheKeyT
std::pair< std::uintptr_t, sycl::detail::pi::PiDevice > CommonProgramKeyT
::boost::unordered_map< std::string, KernelBuildResultPtr > KernelByNameT
void saveKernel(KeyT &&CacheKey, ValT &&CacheVal)
std::tuple< sycl::detail::pi::PiKernel, std::mutex *, const KernelArgMask *, sycl::detail::pi::PiProgram > KernelFastCacheValT
std::pair< ProgramBuildResultPtr, bool > getOrInsertProgram(const ProgramCacheKeyT &CacheKey)
std::pair< sycl::detail::pi::PiKernel, const KernelArgMask * > KernelArgMaskPairT
context_impl * ContextPtr
Represents a reference to value with appropriate lock acquired.
const char * what() const noexcept final
cl_int get_cl_code() const
#define __SYCL_CHECK_OCL_CODE_NO_EXC(X)
std::vector< bool > KernelArgMask
std::shared_ptr< plugin > PluginPtr
constexpr tuple< Ts... > make_tuple(Ts... Args)
std::vector< unsigned char > SerializedObj
pi_result piKernelRelease(pi_kernel kernel)
pi_result piProgramRelease(pi_program program)
C++ wrapper of extern "C" PI interfaces.
_Abi const simd< _Tp, _Abi > & noexcept
Denotes build error data.
Denotes pointer to some entity with its general state and build error.
std::mutex MBuildResultMutex
A mutex to be employed along with MBuildCV.
void updateAndNotify(BuildState DesiredState)
std::atomic< BuildState > State
BuildState waitUntilTransition(BuildState From=BuildState::BS_InProgress)
std::condition_variable MBuildCV
Condition variable to signal that build result is ready.
KernelBuildResult(const PluginPtr &Plugin)
ProgramBuildResult(const PluginPtr &Plugin)
::boost::unordered_map< ProgramCacheKeyT, ProgramBuildResultPtr > Cache
size_t size() const noexcept
::boost::unordered_multimap< CommonProgramKeyT, ProgramCacheKeyT > KeyMap