@@ -532,40 +532,67 @@ get_backend(::Array) = CPU()
532
532
Adapt. adapt_storage (:: CPU , a:: Array ) = a
533
533
534
534
"""
535
- allocate(::Backend, Type, dims...)::AbstractArray
535
+ allocate(::Backend, Type, dims...; unified=false )::AbstractArray
536
536
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.
538
540
539
541
!!! note
540
542
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
+
544
556
545
557
"""
546
- zeros(::Backend, Type, dims...)::AbstractArray
558
+ zeros(::Backend, Type, dims...; unified=false )::AbstractArray
547
559
548
560
Allocate 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.
549
563
"""
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 ... )
553
567
fill! (data, zero (T))
554
568
return data
555
569
end
556
570
557
571
"""
558
- ones(::Backend, Type, dims...)::AbstractArray
572
+ ones(::Backend, Type, dims...; unified=false )::AbstractArray
559
573
560
574
Allocate 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.
561
577
"""
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 ... )
565
581
fill! (data, one (T))
566
582
return data
567
583
end
568
584
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
+
569
596
"""
570
597
supports_atomics(::Backend)::Bool
571
598
0 commit comments