Skip to content

Commit bc855cd

Browse files
committed
Add tests
1 parent 518c758 commit bc855cd

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

sycl/source/detail/kernel_program_cache.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ class KernelProgramCache {
386386
}
387387

388388
// Evict programs from cache to free up space.
389-
void evictPrograms(int DesiredCacheSize, int CurrentCacheSize) {
389+
void evictPrograms(size_t DesiredCacheSize, int CurrentCacheSize) {
390390

391391
// Figure out how many programs from the beginning we need to evict.
392392
// [FIXME] Will the race on MCachedPrograms.Cache.empty() be a problem?
@@ -400,7 +400,7 @@ class KernelProgramCache {
400400

401401
// Traverse the eviction list and remove the LRU programs.
402402
// The LRU programs will be at the front of the list.
403-
while (EvictSize > 0 && !MEvictionList.empty()) {
403+
while (EvictSize > DesiredCacheSize && !MEvictionList.empty()) {
404404
ProgramCacheKeyT CacheKey = MEvictionList.MProgramEvictionList.front();
405405
auto LockedCache = acquireCachedPrograms();
406406
auto &ProgCache = LockedCache.get();
@@ -418,6 +418,12 @@ class KernelProgramCache {
418418
ur_program_handle_t NativePrg = It->second->Val;
419419
{
420420
auto LockedCacheKP = acquireKernelsPerProgramCache();
421+
// List kernels that are to be removed from the cache, if tracing is
422+
// enabled.
423+
if (SYCLConfig<SYCL_CACHE_TRACE>::isTraceInMemCache()) {
424+
for (const auto &Kernel : LockedCacheKP.get()[NativePrg])
425+
traceKernel("Kernel evicted.", Kernel.first);
426+
}
421427
LockedCacheKP.get().erase(NativePrg);
422428
}
423429

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Tests eviction for in-memory program cache.
2+
3+
// Future optimizations in device code reduction can cause this test to fail.
4+
// Therefore, adding O0.
5+
// RUN: %{build} %O0 -o %t.out
6+
7+
// RUN: env SYCL_CACHE_TRACE=2 SYCL_IN_MEM_CACHE_EVICTION_THRESHOLD=20000 %{run} %t.out 2> %t.trace3
8+
// RUN: FileCheck %s --input-file=%t.trace3 --check-prefix=CHECK-CACHE-TRACE
9+
10+
#include <sycl/detail/core.hpp>
11+
12+
#include <sycl/specialization_id.hpp>
13+
#include <sycl/usm.hpp>
14+
15+
using namespace sycl;
16+
17+
constexpr specialization_id<int> spec_id;
18+
19+
int main() {
20+
queue q;
21+
22+
// The first time the kernel is used, it will be stored in kernel and fast
23+
// kernel cache. Then next two times it will be fetched from fast kernel
24+
// cache.
25+
// CHECK-CACHE-TRACE: [In-Memory Cache][Thread Id:{{.*}}][Program Cache][Key:[[KEY1:.*]]]: Program inserted.
26+
// CHECK-CACHE-TRACE: [In-Memory Cache][Thread Id:{{.*}}][Program Cache][Key:[[KEY1]]]: Program added to the end of eviction list.
27+
// CHECK-CACHE-TRACE: [In-Memory Cache][Thread Id:{{.*}}][Kernel Cache][IsFastCache: 0][Key:{Name = [[KERNELNAME1:.*]]]: Kernel inserted.
28+
// CHECK-CACHE-TRACE: [In-Memory Cache][Thread Id:{{.*}}][Kernel Cache][IsFastCache: 1][Key:{Name = [[KERNELNAME1:.*]]]: Kernel inserted.
29+
// CHECK-CACHE-TRACE: [In-Memory Cache][Thread Id:{{.*}}][Kernel Cache][IsFastCache: 1][Key:{Name = [[KERNELNAME1:.*]]]: Kernel fetched.
30+
// CHECK-CACHE-TRACE: [In-Memory Cache][Thread Id:{{.*}}][Kernel Cache][IsFastCache: 1][Key:{Name = [[KERNELNAME1:.*]]]: Kernel fetched.
31+
for (int i = 0; i < 3; i++)
32+
q.single_task([] {}).wait();
33+
34+
auto *p = malloc_device<int>(1, q);
35+
36+
// Added program and kernel for first loop iteration.
37+
// CHECK-CACHE-TRACE: [In-Memory Cache][Thread Id:{{.*}}][Program Cache][Key:[[KEY2:.*]]]: Program inserted.
38+
// CHECK-CACHE-TRACE: [In-Memory Cache][Thread Id:{{.*}}][Program Cache][Key:[[KEY2]]]: Program added to the end of eviction list.
39+
// CHECK-CACHE-TRACE: [In-Memory Cache][Thread Id:{{.*}}][Kernel Cache][IsFastCache: 0][Key:{Name = [[KERNELNAME2:.*]]]: Kernel inserted.
40+
41+
// Added program and kernel for second loop iteration.
42+
// CHECK-CACHE-TRACE: [In-Memory Cache][Thread Id:{{.*}}][Program Cache][Key:[[KEY3:.*]]]: Program inserted.
43+
// CHECK-CACHE-TRACE: [In-Memory Cache][Thread Id:{{.*}}][Program Cache][Key:[[KEY3]]]: Program added to the end of eviction list.
44+
45+
// Eviction triggered. The first program will be evicted from cache.
46+
// CHECK-CACHE-TRACE: [In-Memory Cache][Thread Id:{{.*}}][Kernel Cache][IsFastCache: 0][Key:{Name = [[KERNELNAME1]]]: Kernel evicted.
47+
// CHECK-CACHE-TRACE: [In-Memory Cache][Thread Id:{{.*}}][Kernel Cache][IsFastCache: 1][Key:{Name = [[KERNELNAME1]]]: Kernel evicted.
48+
// CHECK-CACHE-TRACE: [In-Memory Cache][Thread Id:{{.*}}][Program Cache][Key:[[KEY1]]]: Program evicted.
49+
50+
// Kernel for second loop iteration will be inserted into kernel cache.
51+
// CHECK-CACHE-TRACE: [In-Memory Cache][Thread Id:{{.*}}][Kernel Cache][IsFastCache: 0][Key:{Name = [[KERNELNAME3:.*]]]: Kernel inserted.
52+
53+
for (int i = 0; i < 2; ++i)
54+
q.submit([&](handler &cgh) {
55+
cgh.set_specialization_constant<spec_id>(i);
56+
cgh.parallel_for(1, [=](auto, kernel_handler kh) {
57+
*p = kh.get_specialization_constant<spec_id>();
58+
});
59+
}).wait();
60+
61+
free(p, q);
62+
}

0 commit comments

Comments
 (0)