Skip to content

Commit 32558fe

Browse files
committed
POC supports_unified
1 parent 434d55a commit 32558fe

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/KernelAbstractions.jl

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -532,40 +532,55 @@ get_backend(::Array) = CPU()
532532
Adapt.adapt_storage(::CPU, a::Array) = a
533533

534534
"""
535-
allocate(::Backend, Type, dims...)::AbstractArray
535+
allocate(::Backend, Type, dims...; unified=false)::AbstractArray
536536
537-
Allocate a storage array appropriate for the computational backend.
537+
Allocate a storage array appropriate for the computational backend. `unified`
538+
allocates an array using unified memory if the backend supports it. Use
539+
[`supports_unified`](@ref) to determine whether it is supported by a backend.
538540
539541
!!! note
540542
Backend implementations **must** implement `allocate(::NewBackend, T, dims::Tuple)`
541543
"""
542-
allocate(backend::Backend, T::Type, dims...) = allocate(backend, T, dims)
543-
allocate(backend::Backend, T::Type, dims::Tuple) = throw(MethodError(allocate, (backend, T, dims)))
544+
allocate(backend::Backend, T::Type, dims...; unified=false) = allocate(backend, T, dims; unified)
545+
allocate(backend::Backend, T::Type, dims::Tuple; unified=false) = throw(MethodError(allocate, (backend, T, dims)))
544546

545547
"""
546-
zeros(::Backend, Type, dims...)::AbstractArray
548+
zeros(::Backend, Type, dims...; unified=false)::AbstractArray
547549
548550
Allocate a storage array appropriate for the computational backend filled with zeros.
551+
`unified` allocates an array using unified memory if the backend supports it.
549552
"""
550-
zeros(backend::Backend, T::Type, dims...) = zeros(backend, T, dims)
551-
function zeros(backend::Backend, ::Type{T}, dims::Tuple) where {T}
552-
data = allocate(backend, T, dims...)
553+
zeros(backend::Backend, T::Type, dims...; kwargs...) = zeros(backend, T, dims; kwargs...)
554+
function zeros(backend::Backend, ::Type{T}, dims::Tuple; unified=false) where {T}
555+
data = allocate(backend, T, dims...; unified)
553556
fill!(data, zero(T))
554557
return data
555558
end
556559

557560
"""
558-
ones(::Backend, Type, dims...)::AbstractArray
561+
ones(::Backend, Type, dims...; unified=false)::AbstractArray
559562
560563
Allocate a storage array appropriate for the computational backend filled with ones.
564+
`unified` allocates an array using unified memory if the backend supports it.
561565
"""
562-
ones(backend::Backend, T::Type, dims...) = ones(backend, T, dims)
563-
function ones(backend::Backend, ::Type{T}, dims::Tuple) where {T}
564-
data = allocate(backend, T, dims)
566+
ones(backend::Backend, T::Type, dims...; kwargs...) = ones(backend, T, dims; kwargs...)
567+
function ones(backend::Backend, ::Type{T}, dims::Tuple; unified=false) where {T}
568+
data = allocate(backend, T, dims; unified)
565569
fill!(data, one(T))
566570
return data
567571
end
568572

573+
"""
574+
supports_unified(::Backend)::Bool
575+
576+
Returns whether unified memory arrays are supported by the backend.
577+
578+
!!! note
579+
Backend implementations **must** implement this function
580+
only if they **do not** support unified memory.
581+
"""
582+
supports_unified(::Backend) = true
583+
569584
"""
570585
supports_atomics(::Backend)::Bool
571586

test/test.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ function unittest_testsuite(Backend, backend_str, backend_mod, BackendArrayT; sk
7878
backendT = typeof(backend).name.wrapper # To look through CUDABackend{true, false}
7979
@test backend isa backendT
8080

81+
unified = supports_unified(backend)
82+
@test unified isa Bool
83+
U = allocate(backend, Float32, 5; unified)
84+
if unified
85+
@test U[3] isa Float32
86+
else
87+
@test_throws U[3]
88+
end
89+
8190
x = allocate(backend, Float32, 5)
8291
A = allocate(backend, Float32, 5, 5)
8392
@test @inferred(KernelAbstractions.get_backend(A)) isa backendT

0 commit comments

Comments
 (0)