Skip to content

Commit

Permalink
Rename enable to cached
Browse files Browse the repository at this point in the history
  • Loading branch information
pxl-th committed Jan 9, 2025
1 parent 2709994 commit 6c0962e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions docs/src/interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ There are numerous examples of potential interfaces for GPUArrays, such as with
## Caching Allocator

```@docs
GPUArrays.@enable
GPUArrays.@disable
GPUArrays.@cached
GPUArrays.@uncached
```
18 changes: 9 additions & 9 deletions src/host/alloc_cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function unsafe_free!(cache::AllocCache)
for (_, pool) in cache.busy
isempty(pool) || error(
"Invalidating allocations cache that's currently in use. " *
"Invalidating inside `@enable` is not allowed."
"Invalidating inside `@cached` is not allowed."
)
end
for (_, pool) in cache.free
Expand All @@ -93,15 +93,15 @@ end
const ALLOC_CACHE = ScopedValue{Union{Nothing, AllocCache}}(nothing)

"""
@enable(cache, expr)
@cached(cache, expr)
Evaluate expression `expr` using allocations cache `cache`.
When gpu allocation is requested during execution of `expr`,
it will first check if there's "free" cache instead of performing an actual allocation.
If no "free" allocation exists, an actual allocation is performed.
Before returning allocation to the user, it is marked as busy and
will not be used by allocation in the scope defined by `@enable`.
will not be used by allocation in the scope defined by `@cached`.
**After** the execution of `expr` all "busy" allocations are marked as "free"
thus they can be re-used next time the program enters this scope.
Expand All @@ -120,7 +120,7 @@ With caching allocator, memory usage stays at exactly `8 GiB`.
cache = GPUArrays.AllocCache(CuArray)
n = 1024^3
for i in 1:1000
GPUArrays.@enable cache begin
GPUArrays.@cached cache begin
sin.(CUDA.rand(Float32, n))
end
end
Expand All @@ -129,9 +129,9 @@ end
GPUArrays.unsafe_free!(cache)
```
See [`@disable`](@ref).
See [`@uncached`](@ref).
"""
macro enable(cache, expr)
macro cached(cache, expr)
return quote
res = @with $(esc(ALLOC_CACHE)) => $(esc(cache)) $(esc(expr))
free_busy!($(esc(cache)))
Expand All @@ -140,12 +140,12 @@ macro enable(cache, expr)
end

"""
disable(expr)
uncached(expr)
Evaluate expression `expr` without using allocations cache.
This is useful to call from within `@enable` to avoid caching some allocations.
This is useful to call from within `@cached` to avoid caching some allocations.
"""
macro disable(expr)
macro uncached(expr)
return quote
@with $(esc(ALLOC_CACHE)) => nothing $(esc(expr))
end
Expand Down
8 changes: 4 additions & 4 deletions test/testsuite/caching_allocator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
cache = GPUArrays.AllocCache(AT)

T, dims = Float32, (1, 2, 3)
GPUArrays.@enable cache begin
GPUArrays.@cached cache begin
x1 = AT(zeros(T, dims))
end
@test sizeof(cache) == sizeof(T) * prod(dims)
Expand All @@ -13,10 +13,10 @@
@test x1 === cache.free[key][1]

# Second allocation hits cache.
GPUArrays.@enable cache begin
GPUArrays.@cached cache begin
x2 = AT(zeros(T, dims))
# Does not hit the cache.
GPUArrays.@disable x_free = AT(zeros(T, dims))
GPUArrays.@uncached x_free = AT(zeros(T, dims))
end
@test sizeof(cache) == sizeof(T) * prod(dims)
key = first(keys(cache.free))
Expand All @@ -27,7 +27,7 @@

# Third allocation is of different shape - allocates.
dims = (2, 2)
GPUArrays.@enable cache begin
GPUArrays.@cached cache begin
x3 = AT(zeros(T, dims))
end
_keys = collect(keys(cache.free))
Expand Down

0 comments on commit 6c0962e

Please sign in to comment.