@@ -532,40 +532,67 @@ get_backend(::Array) = CPU()
532532Adapt. 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=true`
538+ allocates an array using unified memory if the backend supports it and throws otherwise.
539+ Use [`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)`
541- """
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)))
543+ Backend implementations **should** implement `allocate(::NewBackend, T, dims::Tuple; unified::Bool=false)`
544+ """
545+ allocate (backend:: Backend , T:: Type , dims... ; kwargs... ) = allocate (backend, T, dims; kwargs... )
546+ function allocate (backend:: Backend , T:: Type , dims:: Tuple ; unified:: Union{Nothing, Bool} = nothing )
547+ if isnothing (unified)
548+ throw (MethodError (allocate, (backend, T, dims)))
549+ elseif unified
550+ throw (ArgumentError (" `$(typeof (backend)) ` does not support unified memory. If you believe it does, please open a github issue." ))
551+ else
552+ return allocate (backend, T, dims)
553+ end
554+ end
555+
544556
545557"""
546- zeros(::Backend, Type, dims...)::AbstractArray
558+ zeros(::Backend, Type, dims...; unified=false )::AbstractArray
547559
548560Allocate a storage array appropriate for the computational backend filled with zeros.
561+ `unified=true` allocates an array using unified memory if the backend supports it and
562+ throws otherwise.
549563"""
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... )
564+ zeros (backend:: Backend , T:: Type , dims... ; kwargs ... ) = zeros (backend, T, dims; kwargs ... )
565+ function zeros (backend:: Backend , :: Type{T} , dims:: Tuple ; kwargs ... ) where {T}
566+ data = allocate (backend, T, dims... ; kwargs ... )
553567 fill! (data, zero (T))
554568 return data
555569end
556570
557571"""
558- ones(::Backend, Type, dims...)::AbstractArray
572+ ones(::Backend, Type, dims...; unified=false )::AbstractArray
559573
560574Allocate a storage array appropriate for the computational backend filled with ones.
575+ `unified=true` allocates an array using unified memory if the backend supports it and
576+ throws otherwise.
561577"""
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)
578+ ones (backend:: Backend , T:: Type , dims... ; kwargs ... ) = ones (backend, T, dims; kwargs ... )
579+ function ones (backend:: Backend , :: Type{T} , dims:: Tuple ; kwargs ... ) where {T}
580+ data = allocate (backend, T, dims; kwargs ... )
565581 fill! (data, one (T))
566582 return data
567583end
568584
585+ """
586+ supports_unified(::Backend)::Bool
587+
588+ Returns whether unified memory arrays are supported by the backend.
589+
590+ !!! note
591+ Backend implementations **should** implement this function
592+ only if they **do** support unified memory.
593+ """
594+ supports_unified (:: Backend ) = false
595+
569596"""
570597 supports_atomics(::Backend)::Bool
571598
0 commit comments