|
5 | 5 | # first allocation populates the cache |
6 | 6 | T, dims = Float32, (1, 2, 3) |
7 | 7 | GPUArrays.@cached cache begin |
8 | | - x1 = AT(zeros(T, dims)) |
| 8 | + cached1 = AT(zeros(T, dims)) |
9 | 9 | end |
10 | | - @test sizeof(cache) == sizeof(T) * prod(dims) |
| 10 | + @test sizeof(cache) == sizeof(cached1) |
11 | 11 | key = first(keys(cache.free)) |
12 | 12 | @test length(cache.free[key]) == 1 |
13 | 13 | @test length(cache.busy[key]) == 0 |
14 | | - @test cache.free[key][1] === GPUArrays.storage(x1) |
| 14 | + @test cache.free[key][1] === GPUArrays.storage(cached1) |
15 | 15 |
|
16 | 16 | # second allocation hits the cache |
17 | 17 | GPUArrays.@cached cache begin |
18 | | - x2 = AT(zeros(T, dims)) |
| 18 | + cached2 = AT(zeros(T, dims)) |
19 | 19 |
|
20 | 20 | # explicitly uncached ones don't |
21 | | - GPUArrays.@uncached x_free = AT(zeros(T, dims)) |
| 21 | + GPUArrays.@uncached uncached = AT(zeros(T, dims)) |
22 | 22 | end |
23 | | - @test sizeof(cache) == sizeof(T) * prod(dims) |
| 23 | + @test sizeof(cache) == sizeof(cached2) |
24 | 24 | key = first(keys(cache.free)) |
25 | 25 | @test length(cache.free[key]) == 1 |
26 | 26 | @test length(cache.busy[key]) == 0 |
27 | | - @test cache.free[key][1] === GPUArrays.storage(x2) |
28 | | - @test x_free !== x2 |
| 27 | + @test cache.free[key][1] === GPUArrays.storage(cached2) |
| 28 | + @test uncached !== cached2 |
29 | 29 |
|
30 | 30 | # compatible shapes should also hit the cache |
31 | 31 | dims = (3, 2, 1) |
32 | 32 | GPUArrays.@cached cache begin |
33 | | - x3 = AT(zeros(T, dims)) |
| 33 | + cached3 = AT(zeros(T, dims)) |
34 | 34 | end |
35 | | - @test sizeof(cache) == sizeof(T) * prod(dims) |
| 35 | + @test sizeof(cache) == sizeof(cached3) |
36 | 36 | key = first(keys(cache.free)) |
37 | 37 | @test length(cache.free[key]) == 1 |
38 | 38 | @test length(cache.busy[key]) == 0 |
39 | | - @test cache.free[key][1] === GPUArrays.storage(x3) |
| 39 | + @test cache.free[key][1] === GPUArrays.storage(cached3) |
40 | 40 |
|
41 | 41 | # as should compatible eltypes |
42 | 42 | T = Int32 |
43 | 43 | GPUArrays.@cached cache begin |
44 | | - x4 = AT(zeros(T, dims)) |
| 44 | + cached4 = AT(zeros(T, dims)) |
45 | 45 | end |
46 | | - @test sizeof(cache) == sizeof(T) * prod(dims) |
| 46 | + @test sizeof(cache) == sizeof(cached4) |
47 | 47 | key = first(keys(cache.free)) |
48 | 48 | @test length(cache.free[key]) == 1 |
49 | 49 | @test length(cache.busy[key]) == 0 |
50 | | - @test cache.free[key][1] === GPUArrays.storage(x4) |
| 50 | + @test cache.free[key][1] === GPUArrays.storage(cached4) |
51 | 51 |
|
52 | 52 | # different shapes should trigger a new allocation |
53 | 53 | dims = (2, 2) |
54 | 54 | GPUArrays.@cached cache begin |
55 | | - x5 = AT(zeros(T, dims)) |
| 55 | + cached5 = AT(zeros(T, dims)) |
56 | 56 |
|
57 | | - # we're allowed to early free arrays, which shouldn't release the underlying data |
58 | | - GPUArrays.unsafe_free!(x5) |
| 57 | + # we're allowed to early free arrays, which should be a no-op for cached data |
| 58 | + GPUArrays.unsafe_free!(cached5) |
59 | 59 | end |
| 60 | + @test sizeof(cache) == sizeof(cached4) + sizeof(cached5) |
60 | 61 | _keys = collect(keys(cache.free)) |
61 | 62 | key2 = _keys[findfirst(i -> i != key, _keys)] |
62 | 63 | @test length(cache.free[key]) == 1 |
63 | 64 | @test length(cache.free[key2]) == 1 |
64 | | - @test cache.free[key2][1] === GPUArrays.storage(x5) |
| 65 | + @test cache.free[key2][1] === GPUArrays.storage(cached5) |
| 66 | + |
| 67 | + # we should be able to re-use the early-freed |
| 68 | + GPUArrays.@cached cache begin |
| 69 | + cached5 = AT(zeros(T, dims)) |
| 70 | + end |
65 | 71 |
|
66 | 72 | # freeing all memory held by cache should free all allocations |
67 | | - @test !GPUArrays.storage(x1).freed |
68 | | - @test GPUArrays.storage(x5).freed |
69 | | - @test GPUArrays.storage(x5).rc.count[] == 1 # the ref appears freed, but the data isn't |
70 | | - @test !GPUArrays.storage(x_free).freed |
| 73 | + @test !GPUArrays.storage(cached1).freed |
| 74 | + @test GPUArrays.storage(cached1).cached |
| 75 | + @test !GPUArrays.storage(cached5).freed |
| 76 | + @test GPUArrays.storage(cached5).cached |
| 77 | + @test !GPUArrays.storage(uncached).freed |
| 78 | + @test !GPUArrays.storage(uncached).cached |
71 | 79 | GPUArrays.unsafe_free!(cache) |
72 | 80 | @test sizeof(cache) == 0 |
73 | | - @test GPUArrays.storage(x1).freed |
74 | | - @test GPUArrays.storage(x1).rc.count[] == 0 |
75 | | - @test GPUArrays.storage(x5).freed |
76 | | - @test GPUArrays.storage(x5).rc.count[] == 0 |
77 | | - @test !GPUArrays.storage(x_free).freed |
| 81 | + @test GPUArrays.storage(cached1).freed |
| 82 | + @test !GPUArrays.storage(cached1).cached |
| 83 | + @test GPUArrays.storage(cached5).freed |
| 84 | + @test !GPUArrays.storage(cached5).cached |
| 85 | + @test !GPUArrays.storage(uncached).freed |
| 86 | + ## test that the underlying data was freed as well |
| 87 | + @test GPUArrays.storage(cached1).rc.count[] == 0 |
| 88 | + @test GPUArrays.storage(cached5).rc.count[] == 0 |
| 89 | + @test GPUArrays.storage(uncached).rc.count[] == 1 |
78 | 90 | end |
79 | 91 | end |
0 commit comments