Skip to content

Commit 9960b52

Browse files
committed
Simplify back-end interface.
1 parent 972b386 commit 9960b52

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

lib/JLArrays/src/JLArrays.jl

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,15 @@ mutable struct JLArray{T, N} <: AbstractGPUArray{T, N}
8989
check_eltype(T)
9090
maxsize = prod(dims) * sizeof(T)
9191

92-
function _alloc_f()
92+
GPUArrays.cached_alloc((JLArray, T, dims)) do
9393
data = Vector{UInt8}(undef, maxsize)
9494
ref = DataRef(data) do data
9595
resize!(data, 0)
9696
end
9797
obj = new{T, N}(ref, 0, dims)
98-
return finalizer(unsafe_free!, obj)
99-
end
100-
101-
cache = GPUArrays.ALLOC_CACHE[]
102-
return if cache nothing
103-
_alloc_f()
104-
else
105-
GPUArrays.alloc!(_alloc_f, cache, (JLArray, T, dims))::JLArray{T, N}
106-
end
98+
finalizer(unsafe_free!, obj)
99+
return obj
100+
end::JLArray{T,N}
107101
end
108102

109103
# low-level constructor for wrapping existing data
@@ -112,6 +106,7 @@ mutable struct JLArray{T, N} <: AbstractGPUArray{T, N}
112106
check_eltype(T)
113107
obj = new{T,N}(ref, offset, dims)
114108
finalizer(unsafe_free!, obj)
109+
return obj
115110
end
116111
end
117112

src/host/alloc_cache.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@ function get_pool!(cache::AllocCache{T}, pool::Symbol, uid::UInt64) where {T <:
3030
return uid_pool
3131
end
3232

33-
function alloc!(alloc_f, cache::AllocCache, key)
33+
function cached_alloc(f, key)
34+
cache = ALLOC_CACHE[]
35+
if cache === nothing
36+
return f()
37+
end
38+
3439
x = nothing
3540
uid = hash(key)
3641

3742
busy_pool = get_pool!(cache, :busy, uid)
3843
free_pool = get_pool!(cache, :free, uid)
39-
isempty(free_pool) && (x = alloc_f())
44+
isempty(free_pool) && (x = f())
4045

4146
while !isempty(free_pool) && x nothing
4247
tmp = Base.@lock cache.lock pop!(free_pool)
@@ -45,7 +50,7 @@ function alloc!(alloc_f, cache::AllocCache, key)
4550
x = tmp
4651
end
4752

48-
x nothing && (x = alloc_f())
53+
x nothing && (x = f())
4954
Base.@lock cache.lock push!(busy_pool, x)
5055
return x
5156
end

0 commit comments

Comments
 (0)