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

unsafe_wrap: unconditionally use the memory type provided by the user. #2372

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
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
54 changes: 28 additions & 26 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,42 +212,44 @@ unsafe_wrap
function Base.unsafe_wrap(::Union{Type{CuArray},Type{CuArray{T}},Type{CuArray{T,N}}},
ptr::CuPtr{T}, dims::NTuple{N,Int};
own::Bool=false, ctx::CuContext=context()) where {T,N}
mem = _unsafe_wrap_managed(T, ptr, dims, ctx)
data = DataRef(own ? pool_free : Returns(nothing), Managed(mem))
CuArray{T,N}(data, dims)
end
function Base.unsafe_wrap(::Type{CuArray{T,N,M}},
ptr::CuPtr{T}, dims::NTuple{N,Int};
own::Bool=false, ctx::CuContext=context()) where {T,N,M}
mem = _unsafe_wrap_managed(T, ptr, dims, ctx)
if typeof(mem) !== M
throw(ArgumentError("Declared memory type does not match inferred memory type."))
end
data = DataRef(own ? pool_free : Returns(nothing), Managed(mem))
CuArray{T,N}(data, dims)
end
function _unsafe_wrap_managed(::Type{T}, ptr::CuPtr{T}, dims::NTuple{N,Int},
ctx::CuContext) where {T,N}
isbitstype(T) || throw(ArgumentError("Can only unsafe_wrap a pointer to a bits type"))
sz = prod(dims) * sizeof(T)

# identify the memory
mem = try
# identify the memory type
M = try
typ = memory_type(ptr)
if is_managed(ptr)
UnifiedMemory(ctx, ptr, sz)
UnifiedMemory
elseif typ == CU_MEMORYTYPE_DEVICE
# TODO: can we identify whether this pointer was allocated asynchronously?
DeviceMemory(device(ctx), ctx, ptr, sz, false)
DeviceMemory
elseif typ == CU_MEMORYTYPE_HOST
HostMemory(ctx, host_pointer(ptr), sz)
HostMemory
else
error("Unknown memory type; please file an issue.")
end
catch err
throw(ArgumentError("Could not identify the memory type; are you passing a valid CUDA pointer to unsafe_wrap?"))
end
return mem

unsafe_wrap(CuArray{T,N,M}, ptr, dims; own, ctx)
end
function Base.unsafe_wrap(::Type{CuArray{T,N,M}},
ptr::CuPtr{T}, dims::NTuple{N,Int};
own::Bool=false, ctx::CuContext=context()) where {T,N,M}
isbitstype(T) || throw(ArgumentError("Can only unsafe_wrap a pointer to a bits type"))
sz = prod(dims) * sizeof(T)

# create a memory object
mem = if M == UnifiedMemory
UnifiedMemory(ctx, ptr, sz)
elseif M == DeviceMemory
# TODO: can we identify whether this pointer was allocated asynchronously?
DeviceMemory(device(ctx), ctx, ptr, sz, false)
elseif M == HostMemory
HostMemory(ctx, host_pointer(ptr), sz)
else
throw(ArgumentError("Unknown memory type $M"))
end

data = DataRef(own ? pool_free : Returns(nothing), Managed(mem))
CuArray{T,N}(data, dims)
end
# integer size input
function Base.unsafe_wrap(::Union{Type{CuArray},Type{CuArray{T}},Type{CuArray{T,1}}},
Expand Down
1 change: 0 additions & 1 deletion test/base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ end
# errors
let a = cu([1]; device=true)
@test_throws ArgumentError unsafe_wrap(Array, a)
@test_throws ArgumentError unsafe_wrap(CuArray{Int,1,CUDA.UnifiedMemory}, pointer(a), 1)
end
let a = [1]
@test_throws ArgumentError unsafe_wrap(CuArray{Int,1,CUDA.DeviceMemory}, a)
Expand Down