|
| 1 | +# Minimal interface for `SparseArrayInterface`. |
| 2 | +# TODO: Define default definitions for these based |
| 3 | +# on the dense case. |
| 4 | +storedvalues(a) = error() |
| 5 | +isstored(a, I::Int...) = error() |
| 6 | +eachstoredindex(a) = error() |
| 7 | +getstoredindex(a, I::Int...) = error() |
| 8 | +getunstoredindex(a, I::Int...) = error() |
| 9 | +setstoredindex!(a, value, I::Int...) = error() |
| 10 | +setunstoredindex!(a, value, I::Int...) = error() |
| 11 | + |
| 12 | +# Derived interface. |
| 13 | +storedlength(a) = length(storedvalues(a)) |
| 14 | +storedpairs(a) = map(I -> I => getstoredindex(a, I), eachstoredindex(a)) |
| 15 | + |
| 16 | +function eachstoredindex(a1, a2, a_rest...) |
| 17 | + # TODO: Make this more customizable, say with a function |
| 18 | + # `combine/promote_storedindices(a1, a2)`. |
| 19 | + return union(eachstoredindex.((a1, a2, a_rest...))...) |
| 20 | +end |
| 21 | + |
| 22 | +# TODO: Add `ndims` type parameter. |
| 23 | +# TODO: Define `AbstractSparseArrayInterface`, make this a subtype. |
| 24 | +using Derive: Derive, @interface, AbstractArrayInterface |
| 25 | +struct SparseArrayInterface <: AbstractArrayInterface end |
| 26 | + |
| 27 | +# Convenient shorthand to refer to the sparse interface. |
| 28 | +const sparse = SparseArrayInterface() |
| 29 | + |
| 30 | +# TODO: Use `ArrayLayouts.layout_getindex`, `ArrayLayouts.sub_materialize` |
| 31 | +# to handle slicing (implemented by copying SubArray). |
| 32 | +@interface sparse function Base.getindex(a, I::Int...) |
| 33 | + !isstored(a, I...) && return getunstoredindex(a, I...) |
| 34 | + return getstoredindex(a, I...) |
| 35 | +end |
| 36 | + |
| 37 | +@interface sparse function Base.setindex!(a, value, I::Int...) |
| 38 | + iszero(value) && return a |
| 39 | + if !isstored(a, I...) |
| 40 | + setunstoredindex!(a, value, I...) |
| 41 | + return a |
| 42 | + end |
| 43 | + setstoredindex!(a, value, I...) |
| 44 | + return a |
| 45 | +end |
| 46 | + |
| 47 | +# TODO: This may need to be defined in `sparsearraydok.jl`, after `SparseArrayDOK` |
| 48 | +# is defined. And/or define `default_type(::SparseArrayStyle, T::Type) = SparseArrayDOK{T}`. |
| 49 | +@interface sparse function Base.similar(a, T::Type, size::Tuple{Vararg{Int}}) |
| 50 | + return SparseArrayDOK{T}(size...) |
| 51 | +end |
| 52 | + |
| 53 | +## TODO: Make this more general, handle mixtures of integers and ranges. |
| 54 | +## TODO: Make this logic generic to all `similar(::AbstractInterface, ...)`. |
| 55 | +## @interface sparse function Base.similar(a, T::Type, dims::Tuple{Vararg{Base.OneTo}}) |
| 56 | +## return sparse(similar)(interface, a, T, Base.to_shape(dims)) |
| 57 | +## end |
| 58 | + |
| 59 | +@interface sparse function Base.map(f, as...) |
| 60 | + # This is defined in this way so we can rely on the Broadcast logic |
| 61 | + # for determining the destination of the operation (element type, shape, etc.). |
| 62 | + return f.(as...) |
| 63 | +end |
| 64 | + |
| 65 | +@interface sparse function Base.map!(f, dest, as...) |
| 66 | + # Check `f` preserves zeros. |
| 67 | + # Define as `map_stored!`. |
| 68 | + # Define `eachstoredindex` promotion. |
| 69 | + for I in eachstoredindex(as...) |
| 70 | + dest[I] = f(map(a -> a[I], as)...) |
| 71 | + end |
| 72 | + return dest |
| 73 | +end |
| 74 | + |
| 75 | +# TODO: Define `AbstractSparseArrayStyle`, make this a subtype. |
| 76 | +struct SparseArrayStyle{N} <: Broadcast.AbstractArrayStyle{N} end |
| 77 | + |
| 78 | +SparseArrayStyle{M}(::Val{N}) where {M,N} = SparseArrayStyle{N}() |
| 79 | + |
| 80 | +@interface sparse function Broadcast.BroadcastStyle(type::Type) |
| 81 | + return SparseArrayStyle{ndims(type)}() |
| 82 | +end |
| 83 | + |
| 84 | +function Base.similar(bc::Broadcast.Broadcasted{<:SparseArrayStyle}, T::Type, axes::Tuple) |
| 85 | + # TODO: Allow `similar` to accept `axes` directly. |
| 86 | + return sparse(similar)(bc, T, Int.(length.(axes))) |
| 87 | +end |
| 88 | + |
| 89 | +using BroadcastMapConversion: map_function, map_args |
| 90 | +# TODO: Look into `SparseArrays.capturescalars`: |
| 91 | +# https://github.com/JuliaSparse/SparseArrays.jl/blob/1beb0e4a4618b0399907b0000c43d9f66d34accc/src/higherorderfns.jl#L1092-L1102 |
| 92 | +function Base.copyto!(dest::AbstractArray, bc::Broadcast.Broadcasted{<:SparseArrayStyle}) |
| 93 | + sparse(map!)(map_function(bc), dest, map_args(bc)...) |
| 94 | + return dest |
| 95 | +end |
| 96 | + |
| 97 | +using ArrayLayouts: ArrayLayouts, MatMulMatAdd |
| 98 | + |
| 99 | +abstract type AbstractSparseLayout <: ArrayLayouts.MemoryLayout end |
| 100 | + |
| 101 | +struct SparseLayout <: AbstractSparseLayout end |
| 102 | + |
| 103 | +@interface sparse function ArrayLayouts.MemoryLayout(type::Type) |
| 104 | + return SparseLayout() |
| 105 | +end |
| 106 | + |
| 107 | +using LinearAlgebra: LinearAlgebra |
| 108 | +@interface sparse function LinearAlgebra.mul!(a_dest, a1, a2, α, β) |
| 109 | + return ArrayLayouts.mul!(a_dest, a1, a2, α, β) |
| 110 | +end |
| 111 | + |
| 112 | +function mul_indices(I1::CartesianIndex{2}, I2::CartesianIndex{2}) |
| 113 | + if I1[2] ≠ I2[1] |
| 114 | + return nothing |
| 115 | + end |
| 116 | + return CartesianIndex(I1[1], I2[2]) |
| 117 | +end |
| 118 | + |
| 119 | +function default_mul!!( |
| 120 | + a_dest::AbstractMatrix, |
| 121 | + a1::AbstractMatrix, |
| 122 | + a2::AbstractMatrix, |
| 123 | + α::Number=true, |
| 124 | + β::Number=false, |
| 125 | +) |
| 126 | + mul!(a_dest, a1, a2, α, β) |
| 127 | + return a_dest |
| 128 | +end |
| 129 | + |
| 130 | +function default_mul!!( |
| 131 | + a_dest::Number, a1::Number, a2::Number, α::Number=true, β::Number=false |
| 132 | +) |
| 133 | + return a1 * a2 * α + a_dest * β |
| 134 | +end |
| 135 | + |
| 136 | +# a1 * a2 * α + a_dest * β |
| 137 | +function sparse_mul!( |
| 138 | + a_dest::AbstractArray, |
| 139 | + a1::AbstractArray, |
| 140 | + a2::AbstractArray, |
| 141 | + α::Number=true, |
| 142 | + β::Number=false; |
| 143 | + (mul!!)=(default_mul!!), |
| 144 | +) |
| 145 | + for I1 in eachstoredindex(a1) |
| 146 | + for I2 in eachstoredindex(a2) |
| 147 | + I_dest = mul_indices(I1, I2) |
| 148 | + if !isnothing(I_dest) |
| 149 | + a_dest[I_dest] = mul!!(a_dest[I_dest], a1[I1], a2[I2], α, β) |
| 150 | + end |
| 151 | + end |
| 152 | + end |
| 153 | + return a_dest |
| 154 | +end |
| 155 | + |
| 156 | +function ArrayLayouts.materialize!( |
| 157 | + m::MatMulMatAdd{<:AbstractSparseLayout,<:AbstractSparseLayout,<:AbstractSparseLayout} |
| 158 | +) |
| 159 | + sparse_mul!(m.C, m.A, m.B, m.α, m.β) |
| 160 | + return m.C |
| 161 | +end |
0 commit comments