Skip to content

Commit 002de2d

Browse files
committed
POC supports_unified
1 parent a73cf97 commit 002de2d

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
@@ -524,40 +524,55 @@ end
524524
# adapt_storage(::Backend, a::BackendArray) = a
525525

526526
"""
527-
allocate(::Backend, Type, dims...)::AbstractArray
527+
allocate(::Backend, Type, dims...; unified=false)::AbstractArray
528528
529-
Allocate a storage array appropriate for the computational backend.
529+
Allocate a storage array appropriate for the computational backend. `unified`
530+
allocates an array using unified memory if the backend supports it. Use
531+
[`supports_unified`](@ref) to determine whether it is supported by a backend.
530532
531533
!!! note
532534
Backend implementations **must** implement `allocate(::NewBackend, T, dims::Tuple)`
533535
"""
534-
allocate(backend::Backend, T::Type, dims...) = allocate(backend, T, dims)
535-
allocate(backend::Backend, T::Type, dims::Tuple) = throw(MethodError(allocate, (backend, T, dims)))
536+
allocate(backend::Backend, T::Type, dims...; unified=false) = allocate(backend, T, dims; unified)
537+
allocate(backend::Backend, T::Type, dims::Tuple; unified=false) = throw(MethodError(allocate, (backend, T, dims)))
536538

537539
"""
538-
zeros(::Backend, Type, dims...)::AbstractArray
540+
zeros(::Backend, Type, dims...; unified=false)::AbstractArray
539541
540542
Allocate a storage array appropriate for the computational backend filled with zeros.
543+
`unified` allocates an array using unified memory if the backend supports it.
541544
"""
542-
zeros(backend::Backend, T::Type, dims...) = zeros(backend, T, dims)
543-
function zeros(backend::Backend, ::Type{T}, dims::Tuple) where {T}
544-
data = allocate(backend, T, dims...)
545+
zeros(backend::Backend, T::Type, dims...; kwargs...) = zeros(backend, T, dims; kwargs...)
546+
function zeros(backend::Backend, ::Type{T}, dims::Tuple; unified=false) where {T}
547+
data = allocate(backend, T, dims...; unified)
545548
fill!(data, zero(T))
546549
return data
547550
end
548551

549552
"""
550-
ones(::Backend, Type, dims...)::AbstractArray
553+
ones(::Backend, Type, dims...; unified=false)::AbstractArray
551554
552555
Allocate a storage array appropriate for the computational backend filled with ones.
556+
`unified` allocates an array using unified memory if the backend supports it.
553557
"""
554-
ones(backend::Backend, T::Type, dims...) = ones(backend, T, dims)
555-
function ones(backend::Backend, ::Type{T}, dims::Tuple) where {T}
556-
data = allocate(backend, T, dims)
558+
ones(backend::Backend, T::Type, dims...; kwargs...) = ones(backend, T, dims; kwargs...)
559+
function ones(backend::Backend, ::Type{T}, dims::Tuple; unified=false) where {T}
560+
data = allocate(backend, T, dims; unified)
557561
fill!(data, one(T))
558562
return data
559563
end
560564

565+
"""
566+
supports_unified(::Backend)::Bool
567+
568+
Returns whether unified memory arrays are supported by the backend.
569+
570+
!!! note
571+
Backend implementations **must** implement this function
572+
only if they **do not** support unified memory.
573+
"""
574+
supports_unified(::Backend) = true
575+
561576
"""
562577
supports_atomics(::Backend)::Bool
563578

test/test.jl

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

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

0 commit comments

Comments
 (0)