|
1 | 1 | @testsuite "Caching Allocator" (AT, eltypes) -> begin |
2 | 2 | device = GPUArrays.AllocCache.device(AT) |
| 3 | + pdcache = GPUArrays.AllocCache.cache_allocator(AT) |
| 4 | + named_cache = GPUArrays.AllocCache.named_cache_allocator!(pdcache, device, :cache) |
3 | 5 |
|
4 | | - @testset "free_immediately=false" begin |
5 | | - pdcache = GPUArrays.AllocCache.cache_allocator(AT) |
6 | | - pdcache.free_immediately = false |
7 | | - named_cache = GPUArrays.AllocCache.named_cache_allocator!(pdcache, device, :cache) |
| 6 | + T = Float32 |
| 7 | + dims = (1, 2, 3) |
8 | 8 |
|
9 | | - T = Float32 |
10 | | - dims = (1, 2, 3) |
11 | | - key = hash((T, dims)) |
| 9 | + GPUArrays.AllocCache.@enable AT :cache begin |
| 10 | + x1 = AT(zeros(T, dims)) |
| 11 | + end |
| 12 | + @test sizeof(pdcache, device, :cache) == sizeof(Float32) * prod(dims) |
| 13 | + @test length(named_cache.free) == 1 |
12 | 14 |
|
13 | | - GPUArrays.AllocCache.@enable AT :cache begin |
14 | | - x1 = AT(zeros(T, dims)) |
15 | | - end |
16 | | - @test sizeof(pdcache, device, :cache) == sizeof(Float32) * prod(dims) |
17 | | - @test length(named_cache.free[key]) == 1 |
18 | | - @test length(named_cache.busy[key]) == 0 |
19 | | - @test x1 === named_cache.free[key][1] |
| 15 | + key = first(keys(named_cache.free)) |
| 16 | + @test length(named_cache.free[key]) == 1 |
| 17 | + @test length(named_cache.busy[key]) == 0 |
| 18 | + @test x1 === named_cache.free[key][1] |
20 | 19 |
|
21 | | - # Second allocation does not allocate - cache stays the same in size. |
| 20 | + # Second allocation does not allocate - cache stays the same in size. |
22 | 21 |
|
23 | | - GPUArrays.AllocCache.@enable AT :cache begin |
24 | | - x2 = AT(zeros(T, dims)) |
| 22 | + GPUArrays.AllocCache.@enable AT :cache begin |
| 23 | + x2 = AT(zeros(T, dims)) |
25 | 24 |
|
26 | | - # Does not go to cache. |
27 | | - GPUArrays.AllocCache.@disable begin |
28 | | - x_free = AT(zeros(T, dims)) |
29 | | - end |
| 25 | + # Does not go to cache. |
| 26 | + GPUArrays.AllocCache.@disable begin |
| 27 | + x_free = AT(zeros(T, dims)) |
30 | 28 | end |
31 | | - @test sizeof(pdcache, device, :cache) == sizeof(Float32) * prod(dims) |
32 | | - @test length(named_cache.free[key]) == 1 |
33 | | - @test length(named_cache.busy[key]) == 0 |
34 | | - @test x2 === x1 |
35 | | - @test x2 === named_cache.free[key][1] |
36 | | - @test x_free !== x2 |
37 | | - |
38 | | - # Third allocation of different type - cache grows. |
39 | | - |
40 | | - T2 = Int32 |
41 | | - key2 = hash((T2, dims)) |
42 | | - GPUArrays.AllocCache.@enable AT :cache begin |
43 | | - x3 = AT(zeros(T2, dims)) |
44 | | - end |
45 | | - @test sizeof(pdcache, device, :cache) == (sizeof(Float32) + sizeof(Int32)) * prod(dims) |
46 | | - @test length(named_cache.free[key]) == 1 |
47 | | - @test length(named_cache.free[key2]) == 1 |
48 | | - @test x3 === named_cache.free[key2][1] |
49 | | - |
50 | | - # Freeing all memory held by cache. |
51 | | - |
52 | | - GPUArrays.AllocCache.invalidate!(AT, :cache) |
53 | | - @test sizeof(pdcache, device, :cache) == 0 |
54 | 29 | end |
| 30 | + @test sizeof(pdcache, device, :cache) == sizeof(Float32) * prod(dims) |
| 31 | + @test length(named_cache.free[key]) == 1 |
| 32 | + @test length(named_cache.busy[key]) == 0 |
| 33 | + @test x2 === x1 |
| 34 | + @test x2 === named_cache.free[key][1] |
| 35 | + @test x_free !== x2 |
| 36 | + |
| 37 | + # Third allocation of different type - cache grows. |
| 38 | + |
| 39 | + T2 = Int32 |
| 40 | + key2 = hash((T2, dims)) |
| 41 | + GPUArrays.AllocCache.@enable AT :cache begin |
| 42 | + x3 = AT(zeros(T2, dims)) |
| 43 | + end |
| 44 | + @test sizeof(pdcache, device, :cache) == (sizeof(Float32) + sizeof(Int32)) * prod(dims) |
55 | 45 |
|
56 | | - @testset "free_immediately=true" begin |
57 | | - pdcache = GPUArrays.AllocCache.cache_allocator(AT) |
58 | | - pdcache.free_immediately = true |
59 | | - named_cache = GPUArrays.AllocCache.named_cache_allocator!(pdcache, device, :cache2) |
60 | | - |
61 | | - T = Float32 |
62 | | - dims = (1, 2, 3) |
63 | | - key = hash((T, dims)) |
64 | | - |
65 | | - @test sizeof(pdcache, device, :cache2) == 0 |
66 | | - |
67 | | - GPUArrays.AllocCache.@enable AT :cache2 begin |
68 | | - x1 = AT(zeros(T, dims)) |
| 46 | + _keys = collect(keys(named_cache.free)) |
| 47 | + key2 = _keys[findfirst(i -> i != key, _keys)] |
| 48 | + @test length(named_cache.free[key]) == 1 |
| 49 | + @test length(named_cache.free[key2]) == 1 |
| 50 | + @test x3 === named_cache.free[key2][1] |
69 | 51 |
|
70 | | - @test !haskey(named_cache.free, key) |
71 | | - @test length(named_cache.busy[key]) == 1 |
72 | | - @test sizeof(pdcache, device, :cache2) == sizeof(Float32) * prod(dims) |
73 | | - end |
| 52 | + # Freeing all memory held by cache. |
74 | 53 |
|
75 | | - # `free` was never even used with `free_immediately=true`. |
76 | | - @test !haskey(named_cache.free, key) |
77 | | - @test length(named_cache.busy[key]) == 0 |
78 | | - @test sizeof(pdcache, device, :cache2) == 0 |
79 | | - end |
| 54 | + GPUArrays.AllocCache.invalidate!(AT, :cache) |
| 55 | + @test sizeof(pdcache, device, :cache) == 0 |
80 | 56 | end |
0 commit comments