Document unsafe_wrap(Array, ...) lifetimes, and support host buffers - #650
Merged
Merged
Conversation
The Array returned by unsafe_wrap(Array, ::oneArray) does not keep the oneArray alive, which wasn't documented. Add a prominent warning, as the other back-ends do. Also allow wrapping arrays backed by host buffers, which are just as accessible from the host as shared buffers.
Contributor
|
Your PR requires formatting changes to meet the project's style guidelines. Click here to view the suggested changes.diff --git a/src/array.jl b/src/array.jl
index 9df190c..ff2ca3f 100644
--- a/src/array.jl
+++ b/src/array.jl
@@ -613,8 +613,10 @@ a shared buffer (as created by `oneArray{T}(undef, ...)`) or a host buffer.
asynchronously, so call `synchronize()` before accessing the returned array after
using `arr` on the device.
"""
-function Base.unsafe_wrap(::Type{Array},
- arr::oneArray{T,N,<:Union{oneL0.SharedBuffer,oneL0.HostBuffer}}) where {T,N}
+function Base.unsafe_wrap(
+ ::Type{Array},
+ arr::oneArray{T, N, <:Union{oneL0.SharedBuffer, oneL0.HostBuffer}}
+ ) where {T, N}
# TODO: can we make this more convenient by increasing the buffer's refcount and using
# a finalizer on the Array? does that work when taking views etc of the Array?
ptr = reinterpret(Ptr{T}, pointer(arr))
diff --git a/test/array.jl b/test/array.jl
index f71d6ce..3989278 100644
--- a/test/array.jl
+++ b/test/array.jl
@@ -102,12 +102,12 @@ end
oneAPI.@sync copyto!(a, 2, [200], 1, 1)
@test b == [100, 200]
- # the same works for arrays backed by host memory
- c = oneVector{Int,oneL0.HostBuffer}([1, 2])
- d = unsafe_wrap(Array, c)
- @test d == [1, 2]
- d[1] = 100
- @test Array(c) == [100, 2]
+ # the same works for arrays backed by host memory
+ c = oneVector{Int, oneL0.HostBuffer}([1, 2])
+ d = unsafe_wrap(Array, c)
+ @test d == [1, 2]
+ d[1] = 100
+ @test Array(c) == [100, 2]
end
# https://github.com/JuliaGPU/CUDA.jl/issues/2191 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #650 +/- ##
=======================================
Coverage 80.87% 80.87%
=======================================
Files 56 56
Lines 4088 4088
=======================================
Hits 3306 3306
Misses 782 782 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
unsafe_wrap(Array, a::oneArray)gives you a CPUArraythat shares memory with aoneArray, without copying. The returnedArraydoes not keepaalive, though, which wasn't documented anywhere: ifais garbage collected, theArraysilently refers to freed memory. The docstring now has a prominent warning about that (and about synchronizing before touching the memory), matching what the other back-ends document for the same function.It also works for arrays backed by host buffers now, not only shared buffers, since those are just as accessible from the CPU:
Tested on an Iris Xe.