19 #include <condition_variable>
22 #include <type_traits>
25 class MockKernelProgramCache;
71 using PiProgramT = std::remove_pointer<RT::PiProgram>::type;
75 std::pair<RT::PiDevice, std::string>>;
79 std::map<ProgramCacheKeyT, ProgramWithBuildStateT>
Cache;
80 std::multimap<CommonProgramKeyT, ProgramCacheKeyT>
KeyMap;
82 size_t size() const noexcept {
return Cache.size(); }
87 using PiKernelT = std::remove_pointer<RT::PiKernel>::type;
98 std::tuple<RT::PiKernel, std::mutex *, RT::PiProgram>;
106 return {MCachedPrograms, MProgramCacheMutex};
110 return {MKernelsPerProgramCache, MKernelsPerProgramCacheMutex};
113 std::pair<ProgramWithBuildStateT *, bool>
115 auto LockedCache = acquireCachedPrograms();
116 auto &ProgCache = LockedCache.get();
117 auto Inserted = ProgCache.Cache.emplace(
118 std::piecewise_construct, std::forward_as_tuple(CacheKey),
119 std::forward_as_tuple(
nullptr, BS_InProgress));
120 if (Inserted.second) {
123 std::make_pair(CacheKey.first.second, CacheKey.second.first);
124 ProgCache.KeyMap.emplace(std::piecewise_construct,
125 std::forward_as_tuple(CommonKey),
126 std::forward_as_tuple(CacheKey));
128 return std::make_pair(&Inserted.first->second, Inserted.second);
131 std::pair<KernelWithBuildStateT *, bool>
133 auto LockedCache = acquireKernelsPerProgramCache();
134 auto &Cache = LockedCache.get()[Program];
135 auto Inserted = Cache.emplace(
136 std::piecewise_construct, std::forward_as_tuple(KernelName),
137 std::forward_as_tuple(
nullptr, BS_InProgress));
138 return std::make_pair(&Inserted.first->second, Inserted.second);
141 template <
typename T,
class Predicate>
143 std::unique_lock<std::mutex> Lock(BR.MBuildResultMutex);
145 BR.MBuildCV.wait(Lock, Pred);
148 template <
typename ExceptionT,
typename RetT>
154 return State == BuildState::BS_Done || State == BuildState::BS_Failed;
159 throw ExceptionT(Error.
Msg, Error.
Code);
166 BR.MBuildCV.notify_all();
169 template <
typename KeyT>
171 std::unique_lock<std::mutex> Lock(MKernelFastCacheMutex);
172 auto It = MKernelFastCache.find(CacheKey);
173 if (It != MKernelFastCache.end()) {
179 template <
typename KeyT,
typename ValT>
181 std::unique_lock<std::mutex> Lock(MKernelFastCacheMutex);
184 MKernelFastCache.emplace(CacheKey, CacheVal);
197 std::mutex MProgramCacheMutex;
198 std::mutex MKernelsPerProgramCacheMutex;
200 ProgramCache MCachedPrograms;
201 KernelCacheT MKernelsPerProgramCache;
202 ContextPtr MParentContext;
204 std::mutex MKernelFastCacheMutex;
205 KernelFastCacheT MKernelFastCache;
206 friend class ::MockKernelProgramCache;