Skip to content

Sparse matrix format interfaces #538

Open
@termi-official

Description

@termi-official

Following a discussion on slack about sparse matrix format interfaces (https://julialang.slack.com/archives/C6A044SQH/p1715862033197389) I would like to bring up the discussion again here on GitHub, also for future reference on the topic.

Problem

In SparseArrays.jl we currently have the types AbstractSparseMatrix and AbstractSparseMatrixCSC. However, we do not have abstract types for other matrix formats, e.g. probably the most common would be AbstratSparseMatrixCSR. Just to make sure I am not misunderstood, I am not arguing that SparseArrays.jl should have a concrete version of SparseMatrixCSR and im am totally fine that there is none in SparseArrays.jl. I am really just concerned about interfaces. IMO having some abstract types and a set of function as an interface for the most common matrix formats can be beneficial. My main concern here is interfacing with non-julia libraries providing CSR matrices (e.g. Ginkgo, CUSPARSE, rocSPARSE, ...).

The for having an interface is that framework developers can write their algorithms generically per sparse matrix format directly.

Solution Proposal

I think a starting point could be to have a generic interface to CSR matrices in SparseArrays.jl directly.

I am thinking about the follwing, symmetric to AbstractSparseMatrixCSC

  • AbstractSparseMatrixCSR <: AbstractSparseMatrix
  • colvals(::AbstractSparseMatrixCSR)
  • nzrange(::AbstractSparseMatrixCSR, row::Int)
  • dropzeros!
  • permute[!](::AbstractSparseMatrixCSR, p::AbstractVector, q::AbstractVector)

Alternatively I had the idea that we might be able to move the abstract portion of SparseArrays.jl into a separate package SparseArrayInterfaces.jl, so downstream package developers can decide whether they need the full SparseArrays.jl infrastructure or just the interface.

Alternative Solutions

The obvious alternative is that framework developers could just define internal trait systems and control their dispatches with the traits. However, I am not experienced enough to see what the potential impact of this on the package ecosystem could be (positive or negative).

In the slack discussion @rayegun brought up another very interesting alternative to having different interface to different matrix formats. IIUC the idea is to provide a generic iterator interface on sparse matrices which should be used in the algorithms (xref #167 ) or generating the code for sparse matrices in one way or another (e.g. via Finch.jl). I really like these ideas from a software development point of view, because it allows properly decoupling the algorithms on sparse matrices from the specific data structure. For the iterator interface however, I am not sure about its limitations.

Feel free to convert the issue to a discussion if it is more appropriate. I just opened it as an issue, because from my experience they have better visibility than discussion threads.

X-Refs

Some more discussion on SparseMatrixCSR vs SparseMatrixCSC.

https://discourse.julialang.org/t/csc-kills-the-prospect-of-multithreading-shouldnt-julia-use-csr/102491/
https://discourse.julialang.org/t/what-to-do-since-csr-matrixes-are-not-in-sparse-arrays/53991
#41
#520

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions