21 #include <condition_variable>
23 #include <type_traits>
25 #include <boost/unordered/unordered_flat_map.hpp>
26 #include <boost/unordered_map.hpp>
29 class MockKernelProgramCache;
32 inline namespace _V1 {
86 State.store(DesiredState);
100 this->
State.store(InitialState);
104 ur_result_t Err =
Plugin->call_nocheck(urProgramRelease,
Val);
116 std::pair<std::pair<SerializedObj, std::uintptr_t>, ur_device_handle_t>;
120 ::boost::unordered_map<ProgramCacheKeyT, ProgramBuildResultPtr>
Cache;
121 ::boost::unordered_multimap<CommonProgramKeyT, ProgramCacheKeyT>
KeyMap;
129 std::pair<ur_kernel_handle_t, const KernelArgMask *>;
137 ur_result_t Err =
Plugin->call_nocheck(urKernelRelease,
Val.first);
145 ::boost::unordered_map<std::string, KernelBuildResultPtr>;
147 ::boost::unordered_map<ur_program_handle_t, KernelByNameT>;
150 std::tuple<SerializedObj, ur_device_handle_t, std::string, std::string>;
152 std::tuple<ur_kernel_handle_t, std::mutex *,
const KernelArgMask *,
153 ur_program_handle_t>;
160 ::boost::unordered_flat_map<KernelFastCacheKeyT, KernelFastCacheValT>;
167 return {MCachedPrograms, MProgramCacheMutex};
171 return {MKernelsPerProgramCache, MKernelsPerProgramCacheMutex};
174 std::pair<ProgramBuildResultPtr, bool>
177 auto &ProgCache = LockedCache.get();
178 auto [It, DidInsert] = ProgCache.Cache.try_emplace(CacheKey,
nullptr);
180 It->second = std::make_shared<ProgramBuildResult>(getPlugin());
183 std::make_pair(CacheKey.first.second, CacheKey.second);
184 ProgCache.KeyMap.emplace(CommonKey, CacheKey);
186 return std::make_pair(It->second, DidInsert);
195 ur_program_handle_t Program) {
197 auto &ProgCache = LockedCache.get();
198 auto [It, DidInsert] = ProgCache.Cache.try_emplace(CacheKey,
nullptr);
200 It->second = std::make_shared<ProgramBuildResult>(getPlugin(),
202 It->second->Val = Program;
205 std::make_pair(CacheKey.first.second, CacheKey.second);
206 ProgCache.KeyMap.emplace(CommonKey, CacheKey);
211 std::pair<KernelBuildResultPtr, bool>
213 const std::string &KernelName) {
215 auto &Cache = LockedCache.get()[Program];
216 auto [It, DidInsert] = Cache.try_emplace(KernelName,
nullptr);
218 It->second = std::make_shared<KernelBuildResult>(getPlugin());
219 return std::make_pair(It->second, DidInsert);
222 template <
typename KeyT>
224 std::unique_lock<std::mutex> Lock(MKernelFastCacheMutex);
225 auto It = MKernelFastCache.find(CacheKey);
226 if (It != MKernelFastCache.end()) {
232 template <
typename KeyT,
typename ValT>
234 std::unique_lock<std::mutex> Lock(MKernelFastCacheMutex);
237 MKernelFastCache.emplace(CacheKey, CacheVal);
244 std::lock_guard<std::mutex> L1(MProgramCacheMutex);
245 std::lock_guard<std::mutex> L2(MKernelsPerProgramCacheMutex);
246 std::lock_guard<std::mutex> L3(MKernelFastCacheMutex);
274 template <errc Errc,
typename GetCachedBuildFT,
typename BuildFT>
275 auto getOrBuild(GetCachedBuildFT &&GetCachedBuild, BuildFT &&Build) {
277 constexpr
size_t MaxAttempts = 2;
278 for (
size_t AttemptCounter = 0;; ++AttemptCounter) {
279 auto Res = GetCachedBuild();
294 AttemptCounter + 1 == MaxAttempts) {
328 std::rethrow_exception(std::current_exception());
331 std::rethrow_exception(std::current_exception());
337 std::mutex MProgramCacheMutex;
338 std::mutex MKernelsPerProgramCacheMutex;
340 ProgramCache MCachedPrograms;
344 std::mutex MKernelFastCacheMutex;
346 friend class ::MockKernelProgramCache;
std::shared_ptr< KernelBuildResult > KernelBuildResultPtr
~KernelProgramCache()=default
void reset()
Clears cache state.
::boost::unordered_map< ur_program_handle_t, KernelByNameT > KernelCacheT
std::pair< std::uintptr_t, ur_device_handle_t > CommonProgramKeyT
std::shared_ptr< ProgramBuildResult > ProgramBuildResultPtr
Locked< ProgramCache > acquireCachedPrograms()
bool insertBuiltProgram(const ProgramCacheKeyT &CacheKey, ur_program_handle_t Program)
::boost::unordered_flat_map< KernelFastCacheKeyT, KernelFastCacheValT > KernelFastCacheT
BuildState
Denotes the state of a build.
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(ur_program_handle_t Program, const std::string &KernelName)
std::pair< std::pair< SerializedObj, std::uintptr_t >, ur_device_handle_t > ProgramCacheKeyT
std::pair< ur_kernel_handle_t, const KernelArgMask * > KernelArgMaskPairT
std::tuple< ur_kernel_handle_t, std::mutex *, const KernelArgMask *, ur_program_handle_t > KernelFastCacheValT
Locked< KernelCacheT > acquireKernelsPerProgramCache()
KernelFastCacheValT tryToGetKernelFast(KeyT &&CacheKey)
::boost::unordered_map< std::string, KernelBuildResultPtr > KernelByNameT
void saveKernel(KeyT &&CacheKey, ValT &&CacheVal)
std::pair< ProgramBuildResultPtr, bool > getOrInsertProgram(const ProgramCacheKeyT &CacheKey)
context_impl * ContextPtr
std::tuple< SerializedObj, ur_device_handle_t, std::string, std::string > KernelFastCacheKeyT
Represents a reference to value with appropriate lock acquired.
const char * what() const noexcept final
const std::error_code & code() const noexcept
std::vector< bool > KernelArgMask
std::shared_ptr< plugin > PluginPtr
constexpr tuple< Ts... > make_tuple(Ts... Args)
int32_t get_ur_error(const exception &e)
exception set_ur_error(exception &&e, int32_t ur_err)
std::error_code make_error_code(sycl::errc E) noexcept
Constructs an error code using e and sycl_category()
#define __SYCL_CHECK_OCL_CODE_NO_EXC(X)
_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)
ProgramBuildResult(const PluginPtr &Plugin, BuildState InitialState)
::boost::unordered_map< ProgramCacheKeyT, ProgramBuildResultPtr > Cache
size_t size() const noexcept
::boost::unordered_multimap< CommonProgramKeyT, ProgramCacheKeyT > KeyMap
C++ utilities for Unified Runtime integration.