|
1 | 1 | # TODO: Add `ndims` type parameter.
|
2 | 2 | abstract type AbstractArrayInterface <: AbstractInterface end
|
| 3 | + |
| 4 | +function interface(::Type{<:Broadcast.AbstractArrayStyle}) |
| 5 | + return error("Not defined.") |
| 6 | +end |
| 7 | + |
| 8 | +function interface(::Type{<:Broadcast.Broadcasted{<:Style}}) where {Style} |
| 9 | + return interface(Style) |
| 10 | +end |
| 11 | + |
| 12 | +# TODO: Define as `Array{T}`. |
| 13 | +arraytype(::AbstractArrayInterface, T::Type) = error("Not implemented.") |
| 14 | + |
| 15 | +using ArrayLayouts: ArrayLayouts |
| 16 | + |
| 17 | +@interface ::AbstractArrayInterface function Base.getindex(a::AbstractArray, I...) |
| 18 | + return ArrayLayouts.layout_getindex(a, I...) |
| 19 | +end |
| 20 | + |
| 21 | +@interface ::AbstractArrayInterface function Base.getindex(a::AbstractArray, I::Int...) |
| 22 | + # TODO: Maybe define as `ArrayLayouts.layout_getindex(a, I...)` or |
| 23 | + # `invoke(getindex, Tuple{AbstractArray,Vararg{Any}}, a, I...)`. |
| 24 | + # TODO: Use `MethodError`? |
| 25 | + return error("Not implemented.") |
| 26 | +end |
| 27 | + |
| 28 | +@interface ::AbstractArrayInterface function Broadcast.BroadcastStyle(type::Type) |
| 29 | + return Broadcast.DefaultArrayStyle{ndims(type)}() |
| 30 | +end |
| 31 | + |
| 32 | +@interface interface::AbstractArrayInterface function Base.similar( |
| 33 | + a::AbstractArray, T::Type, size::Tuple{Vararg{Int}} |
| 34 | +) |
| 35 | + # TODO: Maybe define as `Array{T}(undef, size...)` or |
| 36 | + # `invoke(Base.similar, Tuple{AbstractArray,Type,Vararg{Int}}, a, T, size)`. |
| 37 | + # TODO: Use `MethodError`? |
| 38 | + return similar(arraytype(interface, T), size) |
| 39 | +end |
| 40 | + |
| 41 | +@interface ::AbstractArrayInterface function Base.copy(a::AbstractArray) |
| 42 | + a_dest = similar(a) |
| 43 | + return a_dest .= a |
| 44 | +end |
| 45 | + |
| 46 | +# TODO: Make this more general, handle mixtures of integers and ranges (`Union{Integer,Base.OneTo}`). |
| 47 | +@interface interface::AbstractArrayInterface function Base.similar( |
| 48 | + a::AbstractArray, T::Type, axes::Tuple{Base.OneTo,Vararg{Base.OneTo}} |
| 49 | +) |
| 50 | + # TODO: Use `Base.to_shape(axes)` or |
| 51 | + # `Base.invoke(similar, Tuple{AbstractArray,Type,Tuple{Union{Integer,Base.OneTo},Vararg{Union{Integer,Base.OneTo}}}}, a, T, axes)`. |
| 52 | + return @interface interface similar(a, T, Base.to_shape(axes)) |
| 53 | +end |
| 54 | + |
| 55 | +@interface interface::AbstractArrayInterface function Base.similar( |
| 56 | + bc::Broadcast.Broadcasted, T::Type, axes::Tuple |
| 57 | +) |
| 58 | + # `arraytype(::AbstractInterface)` determines the default array type associated with the interface. |
| 59 | + return similar(arraytype(interface, T), axes) |
| 60 | +end |
| 61 | + |
| 62 | +using BroadcastMapConversion: map_function, map_args |
| 63 | +# TODO: Turn this into an `@interface AbstractArrayInterface` function? |
| 64 | +# TODO: Look into `SparseArrays.capturescalars`: |
| 65 | +# https://github.com/JuliaSparse/SparseArrays.jl/blob/1beb0e4a4618b0399907b0000c43d9f66d34accc/src/higherorderfns.jl#L1092-L1102 |
| 66 | +@interface interface::AbstractArrayInterface function Base.copyto!( |
| 67 | + dest::AbstractArray, bc::Broadcast.Broadcasted |
| 68 | +) |
| 69 | + @interface interface map!(map_function(bc), dest, map_args(bc)...) |
| 70 | + return dest |
| 71 | +end |
| 72 | + |
| 73 | +# This is defined in this way so we can rely on the Broadcast logic |
| 74 | +# for determining the destination of the operation (element type, shape, etc.). |
| 75 | +@interface ::AbstractArrayInterface function Base.map(f, as::AbstractArray...) |
| 76 | + # TODO: Should this be `@interface interface ...`? That doesn't support |
| 77 | + # broadcasting yet. |
| 78 | + # Broadcasting is used here to determine the destination array but that |
| 79 | + # could be done manually here. |
| 80 | + return f.(as...) |
| 81 | +end |
| 82 | + |
| 83 | +@interface ::AbstractArrayInterface function Base.map!( |
| 84 | + f, dest::AbstractArray, as::AbstractArray... |
| 85 | +) |
| 86 | + # TODO: Maybe define as |
| 87 | + # `invoke(Base.map!, Tuple{Any,AbstractArray,Vararg{AbstractArray}}, f, dest, as...)`. |
| 88 | + # TODO: Use `MethodError`? |
| 89 | + return error("Not implemented.") |
| 90 | +end |
| 91 | + |
| 92 | +@interface ::AbstractArrayInterface function Base.permutedims!( |
| 93 | + a_dest::AbstractArray, a_src::AbstractArray, perm |
| 94 | +) |
| 95 | + # TODO: Should this be `@interface interface ...`? |
| 96 | + a_dest .= PermutedDimsArray(a_src, perm) |
| 97 | + return a_dest |
| 98 | +end |
| 99 | + |
| 100 | +using LinearAlgebra: LinearAlgebra |
| 101 | +# This then requires overloading: |
| 102 | +# function ArrayLayouts.materialize!( |
| 103 | +# m::MatMulMatAdd{<:AbstractSparseLayout,<:AbstractSparseLayout,<:AbstractSparseLayout} |
| 104 | +# ) |
| 105 | +# # Matmul implementation. |
| 106 | +# end |
| 107 | +@interface ::AbstractArrayInterface function LinearAlgebra.mul!( |
| 108 | + a_dest::AbstractVecOrMat, a1::AbstractVecOrMat, a2::AbstractVecOrMat, α::Number, β::Number |
| 109 | +) |
| 110 | + return ArrayLayouts.mul!(a_dest, a1, a2, α, β) |
| 111 | +end |
| 112 | + |
| 113 | +@interface ::AbstractArrayInterface function ArrayLayouts.MemoryLayout(type::Type) |
| 114 | + # TODO: Define as `UnknownLayout()`? |
| 115 | + # TODO: Use `MethodError`? |
| 116 | + return error("Not implemented.") |
| 117 | +end |
| 118 | + |
| 119 | +## TODO: Define `const AbstractMatrixInterface = AbstractArrayInterface{2}`, |
| 120 | +## requires adding `ndims` type parameter to `AbstractArrayInterface`. |
| 121 | +## @interface ::AbstractMatrixInterface function Base.*(a1, a2) |
| 122 | +## return ArrayLayouts.mul(a1, a2) |
| 123 | +## end |
0 commit comments