|
| 1 | +using ..GPUArrays |
| 2 | + |
| 3 | +@static if VERSION < v"1.11" |
| 4 | + using ScopedValues |
| 5 | +else |
| 6 | + using Base.ScopedValues |
| 7 | +end |
| 8 | + |
| 9 | +mutable struct AllocCache |
| 10 | + lock::ReentrantLock |
| 11 | + busy::Dict{UInt64, Vector{Any}} # hash(key) => GPUArray[] |
| 12 | + free::Dict{UInt64, Vector{Any}} |
| 13 | + |
| 14 | + function AllocCache() |
| 15 | + cache = new( |
| 16 | + ReentrantLock(), |
| 17 | + Dict{UInt64, Vector{Any}}(), |
| 18 | + Dict{UInt64, Vector{Any}}() |
| 19 | + ) |
| 20 | + return finalizer(unsafe_free!, cache) |
| 21 | + end |
| 22 | +end |
| 23 | + |
| 24 | +function get_pool!(cache::AllocCache, pool::Symbol, uid::UInt64) |
| 25 | + pool = getproperty(cache, pool) |
| 26 | + uid_pool = get(pool, uid, nothing) |
| 27 | + if uid_pool ≡ nothing |
| 28 | + uid_pool = Base.@lock cache.lock pool[uid] = Any[] |
| 29 | + end |
| 30 | + return uid_pool |
| 31 | +end |
| 32 | + |
| 33 | +function cached_alloc(f, key) |
| 34 | + cache = ALLOC_CACHE[] |
| 35 | + if cache === nothing |
| 36 | + return f() |
| 37 | + end |
| 38 | + |
| 39 | + x = nothing |
| 40 | + uid = hash(key) |
| 41 | + |
| 42 | + busy_pool = get_pool!(cache, :busy, uid) |
| 43 | + free_pool = get_pool!(cache, :free, uid) |
| 44 | + isempty(free_pool) && (x = f()) |
| 45 | + |
| 46 | + while !isempty(free_pool) && x ≡ nothing |
| 47 | + tmp = Base.@lock cache.lock pop!(free_pool) |
| 48 | + # Array was manually freed via `unsafe_free!`. |
| 49 | + GPUArrays.storage(tmp).freed && continue |
| 50 | + x = tmp |
| 51 | + end |
| 52 | + |
| 53 | + x ≡ nothing && (x = f()) |
| 54 | + Base.@lock cache.lock push!(busy_pool, x) |
| 55 | + return x |
| 56 | +end |
| 57 | + |
| 58 | +function free_busy!(cache::AllocCache) |
| 59 | + for uid in cache.busy.keys |
| 60 | + busy_pool = get_pool!(cache, :busy, uid) |
| 61 | + isempty(busy_pool) && continue |
| 62 | + |
| 63 | + Base.@lock cache.lock begin |
| 64 | + free_pool = get_pool!(cache, :free, uid) |
| 65 | + append!(free_pool, busy_pool) |
| 66 | + empty!(busy_pool) |
| 67 | + end |
| 68 | + end |
| 69 | + return |
| 70 | +end |
| 71 | + |
| 72 | +function unsafe_free!(cache::AllocCache) |
| 73 | + Base.@lock cache.lock begin |
| 74 | + for (_, pool) in cache.busy |
| 75 | + isempty(pool) || error( |
| 76 | + "Invalidating allocations cache that's currently in use. " * |
| 77 | + "Invalidating inside `@cached` is not allowed." |
| 78 | + ) |
| 79 | + end |
| 80 | + for (_, pool) in cache.free |
| 81 | + map(unsafe_free!, pool) |
| 82 | + end |
| 83 | + empty!(cache.free) |
| 84 | + end |
| 85 | + return |
| 86 | +end |
| 87 | + |
| 88 | +function Base.sizeof(cache::AllocCache) |
| 89 | + sz = UInt64(0) |
| 90 | + Base.@lock cache.lock begin |
| 91 | + for kind in (cache.free, cache.busy), (_, pool) in kind |
| 92 | + sz += sum(sizeof, pool; init = UInt64(0)) |
| 93 | + end |
| 94 | + end |
| 95 | + return sz |
| 96 | +end |
| 97 | + |
| 98 | +function Base.show(io::IO, cache::AllocCache) |
| 99 | + sz, n_free, n_busy = Base.@lock cache.lock begin |
| 100 | + sz = sizeof(cache) |
| 101 | + n_free = sum(p -> length(p[2]), cache.free; init = 0) |
| 102 | + n_busy = sum(p -> length(p[2]), cache.busy; init = 0) |
| 103 | + sz, n_free, n_busy |
| 104 | + end |
| 105 | + return print(io, "AllocCache(n_free=$n_free, n_busy=$n_busy, sizeof=$(Base.format_bytes(sz)))") |
| 106 | +end |
| 107 | + |
| 108 | +const ALLOC_CACHE = ScopedValue{Union{Nothing, AllocCache}}(nothing) |
| 109 | + |
| 110 | +""" |
| 111 | + @cached(cache, expr) |
| 112 | +
|
| 113 | +Evaluate `expr` using allocations cache `cache`. |
| 114 | +
|
| 115 | +When GPU memory is allocated during the execution of `expr`, `cache` will first be checked. |
| 116 | +If no memory is available in the cache, a new allocation will be requested. |
| 117 | +
|
| 118 | +After the execution of `expr`, all allocations made under the scope of `@cached` will be |
| 119 | +cached within `cache` for future use. This is useful to avoid relying on GC to free GPU |
| 120 | +memory in time. |
| 121 | +
|
| 122 | +Once `cache` goes out scope, or when the user calls `unsafe_free!` on it, all cached |
| 123 | +allocations will be freed. |
| 124 | +
|
| 125 | +# Example |
| 126 | +
|
| 127 | +In the following example, each iteration of the for-loop requires 8 GiB of GPU memory. |
| 128 | +Without caching those allocations, significant pressure would be put on the GC, resulting |
| 129 | +in high memory usage and latency. By using the allocator cache, the memory usage is stable: |
| 130 | +
|
| 131 | +```julia |
| 132 | +cache = GPUArrays.AllocCache() |
| 133 | +for i in 1:1000 |
| 134 | + GPUArrays.@cached cache begin |
| 135 | + sin.(CUDA.rand(Float32, 1024^3)) |
| 136 | + end |
| 137 | +end |
| 138 | +
|
| 139 | +# optionally: free the memory now, instead of waiting for the GC to collect `cache` |
| 140 | +GPUArrays.unsafe_free!(cache) |
| 141 | +``` |
| 142 | +
|
| 143 | +See [`@uncached`](@ref). |
| 144 | +""" |
| 145 | +macro cached(cache, expr) |
| 146 | + return quote |
| 147 | + res = @with $(esc(ALLOC_CACHE)) => $(esc(cache)) $(esc(expr)) |
| 148 | + free_busy!($(esc(cache))) |
| 149 | + res |
| 150 | + end |
| 151 | +end |
| 152 | + |
| 153 | +""" |
| 154 | + uncached(expr) |
| 155 | +
|
| 156 | +Evaluate expression `expr` without using the allocation. This is useful to call from within |
| 157 | +`@cached` to avoid caching some allocations, e.g., because they can be returned out of the |
| 158 | +`@cached` scope. |
| 159 | +""" |
| 160 | +macro uncached(expr) |
| 161 | + return quote |
| 162 | + @with $(esc(ALLOC_CACHE)) => nothing $(esc(expr)) |
| 163 | + end |
| 164 | +end |
0 commit comments