Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preliminary fix for Base.unsafe_wrap #583

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,51 @@ end

# TODO docs
function Base.unsafe_wrap(
::Type{<:ROCArray}, ptr::Ptr{T}, dims::NTuple{N, <:Integer};
lock::Bool = true,
::Union{Type{ROCArray},Type{ROCArray{T}},Type{ROCArray{T,1}}}, ptr::Ptr{T}, dims::NTuple{N, <:Integer};
own::Bool = false,
) where {T,N}
@assert isbitstype(T) "Cannot wrap a non-bitstype pointer as a ROCArray"
buf = _unsafe_wrap_identify_buffer(ptr, dims, own)
ROCArray{T, N}(DataRef(own ? _free_buffer : (args...) -> (), buf), dims)
end

function Base.unsafe_wrap(::Type{ROCArray{T,N,B}}, ptr::Ptr{T}, dims::NTuple{N,<:Integer};
own::Bool=false,
) where {T,N,B}
buf = _unsafe_wrap_identify_buffer(ptr, dims, own)
if typeof(buf) !== B
throw(ArgumentError("Declared buffer type does not match inferred buffer type."))
end
ROCArray{T,N}(DataRef(own ? _free_buffer : (args...) -> (), buf), dims)
end

function _unsafe_wrap_identify_buffer(ptr::Ptr{T}, dims::NTuple{N, <:Integer}, own::Bool) where {T, N}
(status, attrs) = Mem.attributes(ptr)
if status != HIP.hipSuccess # The pointer is unknown to the HIP runtime
error("The HIP runtime could not identify the attributes of the ptr $ptr.")
end
device = HIPDevice(attrs.device + 1)
context = HIPContext(device)
sz = prod(dims) * sizeof(T)
buf = lock ?
Mem.HostBuffer(Ptr{Cvoid}(ptr), sz) :
Mem.HIPBuffer(Ptr{Cvoid}(ptr), sz)
ROCArray{T, N}(DataRef(_free_buf, buf), dims)
if attrs.memoryType == HIP.hipMemoryTypeHost
return Mem.HostBuffer(device, context, attrs.hostPointer, attrs.devicePointer, sz, own)
elseif attrs.memoryType == HIP.hipMemoryTypeDevice
return Mem.HIPBuffer(device, context, ptr, sz, own)
else
error("Memory type $(attrs.memoryType) is not supported by AMDGPU.jl")
end
end

# integer size input
function Base.unsafe_wrap(::Union{Type{ROCArray},Type{ROCArray{T}},Type{ROCArray{T,1}}},
p::Ptr{T}, dim::Int; own=false) where {T}
unsafe_wrap(ROCArray{T,1}, p, (dim,); own=own)
end
function Base.unsafe_wrap(::Type{ROCArray{T,1,B}}, p::Ptr{T}, dim::Int; own=false) where {T,B}
unsafe_wrap(ROCArray{T,1,B}, p, (dim,); own=own)
end

Base.unsafe_wrap(::Type{ROCArray{T}}, ptr::Ptr, dims; kwargs...) where T =
Base.unsafe_wrap(::Type{ROCArray{T}}, ptr::Ptr, dims::NTuple{N,<:Integer}; kwargs...) where {T, N} =
unsafe_wrap(ROCArray, Base.unsafe_convert(Ptr{T}, ptr), dims; kwargs...)

## interop with CPU arrays
Expand Down