Skip to content

Commit

Permalink
Add storage query to get an array's DataRef. (#567)
Browse files Browse the repository at this point in the history
This makes it possible to implement `unsafe_free!` and other calls
directly in GPUArrays.
  • Loading branch information
maleadt authored Oct 30, 2024
1 parent 44043dd commit b97643c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "GPUArrays"
uuid = "0c68f7d7-f131-5f86-a1c3-88cf8149b2d7"
version = "11.0.0"
version = "11.1.0"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
2 changes: 1 addition & 1 deletion lib/JLArrays/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
Adapt = "2.0, 3.0, 4.0"
GPUArrays = "11"
GPUArrays = "11.1"
Random = "1"
julia = "1.8"
2 changes: 1 addition & 1 deletion lib/JLArrays/src/JLArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ mutable struct JLArray{T, N} <: AbstractGPUArray{T, N}
end
end

unsafe_free!(a::JLArray) = GPUArrays.unsafe_free!(a.data)
GPUArrays.storage(a::JLArray) = a.data

# conversion of untyped data to a typed Array
function typed_data(x::JLArray{T}) where {T}
Expand Down
15 changes: 14 additions & 1 deletion src/host/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# storage handling

export DataRef
export DataRef, unsafe_free!

# DataRef provides a helper class to manage the storage of an array.
#
Expand Down Expand Up @@ -92,6 +92,19 @@ function unsafe_free!(ref::DataRef, args...)
return
end

# array methods

storage(x::AbstractGPUArray) = error("Not implemented") # COV_EXCL_LINE

"""
unsafe_free!(a::GPUArray)
Release the memory of an array for reuse by future allocations. This operation is
performed automatically by the GC when an array goes out of scope, but can be called
earlier to reduce pressure on the memory allocator.
"""
unsafe_free!(x::AbstractGPUArray) = unsafe_free!(storage(x))


# input/output

Expand Down
8 changes: 8 additions & 0 deletions test/testsuite/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ end
end

@testsuite "base" (AT, eltypes)->begin
if AT <: AbstractGPUArray
@testset "storage" begin
x = AT(rand(Float32, 10))
@test GPUArrays.storage(x) isa GPUArrays.DataRef
GPUArrays.unsafe_free!(x)
end
end

@testset "copy!" begin
for (dst, src,) in (
(rand(Float32, (10,)), rand(Float32, (10,))), # vectors
Expand Down

2 comments on commit b97643c

@maleadt
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/118363

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v11.1.0 -m "<description of version>" b97643cbe1f8ae5667d59dfb851661fff8d2f158
git push origin v11.1.0

Please sign in to comment.