99#pragma once
1010
1111#include " sycl/exception.hpp"
12+ #include < detail/config.hpp>
1213#include < detail/kernel_arg_mask.hpp>
1314#include < detail/platform_impl.hpp>
1415#include < sycl/detail/common.hpp>
1920
2021#include < atomic>
2122#include < condition_variable>
23+ #include < iomanip>
2224#include < mutex>
2325#include < set>
26+ #include < thread>
2427#include < type_traits>
2528
2629#include < boost/unordered/unordered_flat_map.hpp>
@@ -176,6 +179,48 @@ class KernelProgramCache {
176179
177180 void setContextPtr (const ContextPtr &AContext) { MParentContext = AContext; }
178181
182+ /* Sends message to std:cerr stream when SYCL_CACHE_TRACE environemnt is
183+ * set.*/
184+ static void traceProgram (const std::string &Msg,
185+ const ProgramCacheKeyT &CacheKey) {
186+ static const bool traceEnabled =
187+ SYCLConfig<SYCL_CACHE_TRACE>::isTraceInMemCache ();
188+ if (traceEnabled) {
189+
190+ int ImageId = CacheKey.first .second ;
191+ std::stringstream DeviceList;
192+ for (const auto &Device : CacheKey.second )
193+ DeviceList << " 0x" << std::setbase (16 )
194+ << reinterpret_cast <uintptr_t >(Device) << " ," ;
195+
196+ std::string Identifier = " [Key:{imageId = " + std::to_string (ImageId) +
197+ " ,urDevice = " + DeviceList.str () + " ]}: " ;
198+
199+ // Get TID of current thread.
200+ thread_local std::thread::id this_id = std::this_thread::get_id ();
201+ std::cerr << " [In-Memory Cache][Thread Id:" << this_id
202+ << " ][Program Cache]" << Identifier << Msg << std::endl;
203+ }
204+ }
205+
206+ /* Sends message to std:cerr stream when SYCL_CACHE_TRACE environemnt is
207+ * set.*/
208+ static void traceKernel (const std::string &Msg, const std::string &KernelName,
209+ bool IsKernelFastCache = false ) {
210+ static const bool traceEnabled =
211+ SYCLConfig<SYCL_CACHE_TRACE>::isTraceInMemCache ();
212+ if (traceEnabled) {
213+ std::string Identifier =
214+ " [IsFastCache: " + std::to_string (IsKernelFastCache)
215+ + " ][Key:{Name = " + KernelName + " ]}: " ;
216+
217+ // Get TID of current thread.
218+ thread_local std::thread::id this_id = std::this_thread::get_id ();
219+ std::cerr << " [In-Memory Cache][Thread Id:" << this_id
220+ << " ][Kernel Cache]" << Identifier << Msg << std::endl;
221+ }
222+ }
223+
179224 Locked<ProgramCache> acquireCachedPrograms () {
180225 return {MCachedPrograms, MProgramCacheMutex};
181226 }
@@ -195,7 +240,10 @@ class KernelProgramCache {
195240 CommonProgramKeyT CommonKey =
196241 std::make_pair (CacheKey.first .second , CacheKey.second );
197242 ProgCache.KeyMap .emplace (CommonKey, CacheKey);
243+ traceProgram (" Program inserted." , CacheKey);
198244 }
245+ else
246+ traceProgram (" Program fetched." , CacheKey);
199247 return std::make_pair (It->second , DidInsert);
200248 }
201249
@@ -217,7 +265,10 @@ class KernelProgramCache {
217265 CommonProgramKeyT CommonKey =
218266 std::make_pair (CacheKey.first .second , CacheKey.second );
219267 ProgCache.KeyMap .emplace (CommonKey, CacheKey);
268+ traceProgram (" Program inserted." , CacheKey);
220269 }
270+ else
271+ traceProgram (" Program fetched." , CacheKey);
221272 return DidInsert;
222273 }
223274
@@ -227,8 +278,12 @@ class KernelProgramCache {
227278 auto LockedCache = acquireKernelsPerProgramCache ();
228279 auto &Cache = LockedCache.get ()[Program];
229280 auto [It, DidInsert] = Cache.try_emplace (KernelName, nullptr );
230- if (DidInsert)
281+ if (DidInsert) {
231282 It->second = std::make_shared<KernelBuildResult>(getAdapter ());
283+ traceKernel (" Kernel inserted." , KernelName);
284+ }
285+ else
286+ traceKernel (" Kernel fetched" , KernelName);
232287 return std::make_pair (It->second , DidInsert);
233288 }
234289
@@ -237,6 +292,7 @@ class KernelProgramCache {
237292 std::unique_lock<std::mutex> Lock (MKernelFastCacheMutex);
238293 auto It = MKernelFastCache.find (CacheKey);
239294 if (It != MKernelFastCache.end ()) {
295+ traceKernel (" Kernel fetched" , std::get<3 >(CacheKey), true );
240296 return It->second ;
241297 }
242298 return std::make_tuple (nullptr , nullptr , nullptr , nullptr );
@@ -247,6 +303,7 @@ class KernelProgramCache {
247303 std::unique_lock<std::mutex> Lock (MKernelFastCacheMutex);
248304 // if no insertion took place, thus some other thread has already inserted
249305 // smth in the cache
306+ traceKernel (" Kernel inserted." , std::get<3 >(CacheKey), true );
250307 MKernelFastCache.emplace (CacheKey, CacheVal);
251308 }
252309
0 commit comments