Skip to content

Commit b97643c

Browse files
authored
Add storage query to get an array's DataRef. (#567)
This makes it possible to implement `unsafe_free!` and other calls directly in GPUArrays.
1 parent 44043dd commit b97643c

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "GPUArrays"
22
uuid = "0c68f7d7-f131-5f86-a1c3-88cf8149b2d7"
3-
version = "11.0.0"
3+
version = "11.1.0"
44

55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

lib/JLArrays/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1111

1212
[compat]
1313
Adapt = "2.0, 3.0, 4.0"
14-
GPUArrays = "11"
14+
GPUArrays = "11.1"
1515
Random = "1"
1616
julia = "1.8"

lib/JLArrays/src/JLArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ mutable struct JLArray{T, N} <: AbstractGPUArray{T, N}
105105
end
106106
end
107107

108-
unsafe_free!(a::JLArray) = GPUArrays.unsafe_free!(a.data)
108+
GPUArrays.storage(a::JLArray) = a.data
109109

110110
# conversion of untyped data to a typed Array
111111
function typed_data(x::JLArray{T}) where {T}

src/host/abstractarray.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# storage handling
55

6-
export DataRef
6+
export DataRef, unsafe_free!
77

88
# DataRef provides a helper class to manage the storage of an array.
99
#
@@ -92,6 +92,19 @@ function unsafe_free!(ref::DataRef, args...)
9292
return
9393
end
9494

95+
# array methods
96+
97+
storage(x::AbstractGPUArray) = error("Not implemented") # COV_EXCL_LINE
98+
99+
"""
100+
unsafe_free!(a::GPUArray)
101+
102+
Release the memory of an array for reuse by future allocations. This operation is
103+
performed automatically by the GC when an array goes out of scope, but can be called
104+
earlier to reduce pressure on the memory allocator.
105+
"""
106+
unsafe_free!(x::AbstractGPUArray) = unsafe_free!(storage(x))
107+
95108

96109
# input/output
97110

test/testsuite/base.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ end
2121
end
2222

2323
@testsuite "base" (AT, eltypes)->begin
24+
if AT <: AbstractGPUArray
25+
@testset "storage" begin
26+
x = AT(rand(Float32, 10))
27+
@test GPUArrays.storage(x) isa GPUArrays.DataRef
28+
GPUArrays.unsafe_free!(x)
29+
end
30+
end
31+
2432
@testset "copy!" begin
2533
for (dst, src,) in (
2634
(rand(Float32, (10,)), rand(Float32, (10,))), # vectors

0 commit comments

Comments
 (0)