What to do since CSR matrixes are not in Sparse Arrays?

Viral B. Shah discussed (in 2015!) why CSR matrixes were not included in Sparse Arrays: link. The justification makes sense, but I now find myself wishing there was an easy way to access CSR sparse array. I just want to get stuff done, and I don’t care much about the elegance of the abstraction design at the current time.

Does anyone have suggestions for an easy work-around so that I can use a CSR matrix? A few “solutions” I’ve considered:

  1. Bite the bullet and implement a CSR matrix from the ground up. This feels like a waste of time because Viral mentions that someone has already done this – it just wasn’t included in Sparse Arrays. (Does anyone know where I can find this implementation?)
  2. Hack together a CSR matrix that is simply an interface to a CSC matrix. This might take less time to implement than option 1, and it might require less debugging. Again, this feels like a waste of time and coding effort.
  3. Use a 3rd party project that hasn’t been touched since 201(whatever). A quick search on github or google will reveal a few potential candidates. This seems risky since most of the projects I’ve seen do not have testing, documentation, or a broad user base.

Why not just use a CSC matrix and transpose it?

Can you perhaps just use a transposed view of a CSC matrix? This could be as easy as:

julia> A_csr = transpose(sprand(10, 5, 0.1))
5×10 LinearAlgebra.Transpose{Float64,SparseMatrixCSC{Float64,Int64}}
...

transpose preserves the underlying sparse matrix and presents a transposed view of it, as if it were actually stored in CSR format.

3 Likes

just for reference and probably not of too much use.

1 Like

I ran some benchmarks using the suggestions of @rdeits and @Oscar_Smith . The example matrix is the 2D finite-difference laplacian on a grid of 10,000 x 10,000 points. The “CSR” matrix is the transpose of the CSC matrix. Below are the benchmark results and a conclusion. All times are the minimum benchmark time:

Thoughts? I can provide the pluto notebook upon request.

2 Likes

Matrix-vector mult is much faster with the CSR format. I use https://github.com/gridap/SparseMatricesCSR.jl. And https://github.com/BacAmorim/ThreadedSparseCSR.jl.

4 Likes

I am glad ThreadedSparseCSR.jl is being useful for you!

1 Like

What is the best way to convert a CSC matrix to CSR? I am doing:

using LinearAlgebra, SparseArrays, SparseMatricesCSR
A = sprand(10,20,0.5); # CSC
Acsr = SparseMatricesCSR.SparseMatrixCSR(transpose(sparse(transpose(A)))); # CSR
@assert(norm(Acsr-A) == 0)

but feel like I’m missing something…