Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sparse matrix multiplication #2

Merged
merged 2 commits into from
Dec 2, 2024
Merged

Sparse matrix multiplication #2

merged 2 commits into from
Dec 2, 2024

Conversation

mtfishman
Copy link
Member

This adds:

  • Sparse matrix multiplication.
  • A module SparseArraysBaseNext.InterfaceImplementations for generic logic surrounding the @derive macro.
  • Support for specifying the interface manually rather than determining it from the array type with AbstractInterface(::Type).
  • Get rid of types like AbstractArrayGetIndex and just use getindex to specify which functions to derive.

So with this PR for a new type:

struct SparseArrayDOK{T,N} <: AbstractArray{T,N}
  storage::Dict{CartesianIndex{N},T}
  size::NTuple{N,Int}
end

you can do:

using SparseArraysBaseNext.InterfaceImplementations: InterfaceImplementations, @derive
using SparseArraysBaseNext: SparseArrayInterface

function InterfaceImplementations.AbstractInterface(::Type{<:SparseArrayDOK})
  return SparseArrayInterface()
end

@derive SparseArrayDOK (getindex, setindex!)
# Same as:
@derive SparseArrayDOK getindex
@derive SparseArrayDOK setindex!

or:

using SparseArraysBaseNext.InterfaceImplementations: @derive
using SparseArraysBaseNext: SparseArrayInterface

@derive SparseArrayDOK SparseArrayInterface() (getindex, setindex!)
# Same as:
@derive SparseArrayDOK SparseArrayInterface() getindex
@derive SparseArrayDOK SparseArrayInterface() setindex!

In that way you can have more fine-grained control over which implementation you want to use to derive a certain function.

Something that is pretty bad with the current code design is that each function has definitions like:

function derive(op::typeof(Base.getindex), interface::AbstractArrayInterface, type::Type)
  return quote
    function Base.getindex(a::$type, I::Int...)
      return $getindex($interface, a, I...)
    end
  end
end
function derive(op::typeof(Base.getindex), type::Type)
  return quote
    function Base.getindex(a::$type, I::Int...)
      return $getindex($AbstractInterface(a), a, I...)
    end
  end
end

to overload the Base definition and forward to the interface-based definition.

Ideally these would be generated automatically and there would be a way to specify the type signatures you want to overload. Maybe it would be better to have a syntax:

# Determine the interface to use (we will need a way to determine
# which inputs to use to determine the interface):
@derive getindex(::SparseArrayDOK, ::Int...)

# Specify the interface manually:
@derive getindex(::SparseArrayInterface, ::SparseArrayDOK, ::Int...)

# Multiple functions:
@derive begin
  T = SparseArrayDOK
  getindex(:T, ::Int...)
  setindex!(::T, ::Any, ::Int...)
  similar(::T, ::Type, ::Tuple)
end

# Multiple functions with interface specified:
@derive begin
  I = SparseArrayInterface
  T = SparseArrayDOK
  getindex(::I, :T, ::Int...)
  setindex!(::I, ::T, ::Any, ::Int...)
  similar(::I, ::T, ::Type, ::Tuple)
end

and then also we will need a way to generate lists of definitions programmatically, possibly with lists of expressions:

function AbstractArrayOps(I::Type, T::Type)
  return [
    :(getindex(::$I, ::$T, ::Int...)),
    :(setindex!(::$I, ::$T, ::Any, ::Int...)),
    :(similar(::$I, ::$T, ::Type, ::Tuple)),
  ]
end

@mtfishman
Copy link
Member Author

I'll merge and address the new @derive interface and implementation in a future PR.

@mtfishman mtfishman merged commit 4a73391 into main Dec 2, 2024
7 checks passed
@mtfishman mtfishman deleted the mul branch December 2, 2024 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant