|
| 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 | +setstoredindex!(a, value, I::Int...) = error() |
| 9 | +setunstoredindex!(a, value, I::Int...) = error() |
| 10 | + |
| 11 | +# Interface defaults. |
| 12 | +# TODO: Have a fallback that handles element types |
| 13 | +# that don't define `zero(::Type)`. |
| 14 | +getunstoredindex(a, I::Int...) = zero(eltype(a)) |
| 15 | + |
| 16 | +# Derived interface. |
| 17 | +storedlength(a) = length(storedvalues(a)) |
| 18 | +storedpairs(a) = map(I -> I => getstoredindex(a, I), eachstoredindex(a)) |
| 19 | + |
| 20 | +function eachstoredindex(a1, a2, a_rest...) |
| 21 | + # TODO: Make this more customizable, say with a function |
| 22 | + # `combine/promote_storedindices(a1, a2)`. |
| 23 | + return union(eachstoredindex.((a1, a2, a_rest...))...) |
| 24 | +end |
| 25 | + |
| 26 | +using Derive: Derive, @interface, AbstractArrayInterface |
| 27 | + |
| 28 | +# TODO: Add `ndims` type parameter. |
| 29 | +# TODO: This isn't used to define interface functions right now. |
| 30 | +# Currently, `@interface` expects an instance, probably it should take a |
| 31 | +# type instead so fallback functions can use abstract types. |
| 32 | +abstract type AbstractSparseArrayInterface <: AbstractArrayInterface end |
| 33 | + |
| 34 | +# TODO: Use `ArrayLayouts.layout_getindex`, `ArrayLayouts.sub_materialize` |
| 35 | +# to handle slicing (implemented by copying SubArray). |
| 36 | +@interface AbstractSparseArrayInterface function Base.getindex(a, I::Int...) |
| 37 | + !isstored(a, I...) && return getunstoredindex(a, I...) |
| 38 | + return getstoredindex(a, I...) |
| 39 | +end |
| 40 | + |
| 41 | +@interface AbstractSparseArrayInterface function Base.setindex!(a, value, I::Int...) |
| 42 | + iszero(value) && return a |
| 43 | + if !isstored(a, I...) |
| 44 | + setunstoredindex!(a, value, I...) |
| 45 | + return a |
| 46 | + end |
| 47 | + setstoredindex!(a, value, I...) |
| 48 | + return a |
| 49 | +end |
| 50 | + |
| 51 | +# TODO: This may need to be defined in `sparsearraydok.jl`, after `SparseArrayDOK` |
| 52 | +# is defined. And/or define `default_type(::SparseArrayStyle, T::Type) = SparseArrayDOK{T}`. |
| 53 | +@interface AbstractSparseArrayInterface function Base.similar( |
| 54 | + a, T::Type, size::Tuple{Vararg{Int}} |
| 55 | +) |
| 56 | + # TODO: Define `default_similartype` or something like that? |
| 57 | + return SparseArrayDOK{T}(size...) |
| 58 | +end |
| 59 | + |
| 60 | +@interface AbstractSparseArrayInterface function Base.map!(f, dest, as...) |
| 61 | + # Check `f` preserves zeros. |
| 62 | + # Define as `map_stored!`. |
| 63 | + # Define `eachstoredindex` promotion. |
| 64 | + for I in eachstoredindex(as...) |
| 65 | + dest[I] = f(map(a -> a[I], as)...) |
| 66 | + end |
| 67 | + return dest |
| 68 | +end |
| 69 | + |
| 70 | +# TODO: Make this a subtype of `Derive.AbstractArrayStyle{N}` instead. |
| 71 | +using Derive: Derive |
| 72 | +abstract type AbstractSparseArrayStyle{N} <: Derive.AbstractArrayStyle{N} end |
| 73 | + |
| 74 | +struct SparseArrayStyle{N} <: AbstractSparseArrayStyle{N} end |
| 75 | + |
| 76 | +SparseArrayStyle{M}(::Val{N}) where {M,N} = SparseArrayStyle{N}() |
| 77 | + |
| 78 | +@interface AbstractSparseArrayInterface function Broadcast.BroadcastStyle(type::Type) |
| 79 | + return SparseArrayStyle{ndims(type)}() |
| 80 | +end |
| 81 | + |
| 82 | +using ArrayLayouts: ArrayLayouts, MatMulMatAdd |
| 83 | + |
| 84 | +abstract type AbstractSparseLayout <: ArrayLayouts.MemoryLayout end |
| 85 | + |
| 86 | +struct SparseLayout <: AbstractSparseLayout end |
| 87 | + |
| 88 | +@interface AbstractSparseArrayInterface function ArrayLayouts.MemoryLayout(type::Type) |
| 89 | + return SparseLayout() |
| 90 | +end |
| 91 | + |
| 92 | +function mul_indices(I1::CartesianIndex{2}, I2::CartesianIndex{2}) |
| 93 | + if I1[2] ≠ I2[1] |
| 94 | + return nothing |
| 95 | + end |
| 96 | + return CartesianIndex(I1[1], I2[2]) |
| 97 | +end |
| 98 | + |
| 99 | +function default_mul!!( |
| 100 | + a_dest::AbstractMatrix, |
| 101 | + a1::AbstractMatrix, |
| 102 | + a2::AbstractMatrix, |
| 103 | + α::Number=true, |
| 104 | + β::Number=false, |
| 105 | +) |
| 106 | + mul!(a_dest, a1, a2, α, β) |
| 107 | + return a_dest |
| 108 | +end |
| 109 | + |
| 110 | +function default_mul!!( |
| 111 | + a_dest::Number, a1::Number, a2::Number, α::Number=true, β::Number=false |
| 112 | +) |
| 113 | + return a1 * a2 * α + a_dest * β |
| 114 | +end |
| 115 | + |
| 116 | +# a1 * a2 * α + a_dest * β |
| 117 | +function sparse_mul!( |
| 118 | + a_dest::AbstractArray, |
| 119 | + a1::AbstractArray, |
| 120 | + a2::AbstractArray, |
| 121 | + α::Number=true, |
| 122 | + β::Number=false; |
| 123 | + (mul!!)=(default_mul!!), |
| 124 | +) |
| 125 | + for I1 in eachstoredindex(a1) |
| 126 | + for I2 in eachstoredindex(a2) |
| 127 | + I_dest = mul_indices(I1, I2) |
| 128 | + if !isnothing(I_dest) |
| 129 | + a_dest[I_dest] = mul!!(a_dest[I_dest], a1[I1], a2[I2], α, β) |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | + return a_dest |
| 134 | +end |
| 135 | + |
| 136 | +function ArrayLayouts.materialize!( |
| 137 | + m::MatMulMatAdd{<:AbstractSparseLayout,<:AbstractSparseLayout,<:AbstractSparseLayout} |
| 138 | +) |
| 139 | + sparse_mul!(m.C, m.A, m.B, m.α, m.β) |
| 140 | + return m.C |
| 141 | +end |
0 commit comments