-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
AbstractSparseArray Interface #25760
Comments
@timholy 's ArrayIteration.jl has some ideas here: https://github.com/timholy/ArrayIteration.jl But to really make that work well I think we need a standard interface in Base for everyone's array types to conform to in order to allow generic codes to work on it. The problem of course is to make generic sparse iteration efficient, if possible. |
In theory this is a good idea. In practice, I'm doubtful structured sparse matrices ( |
Why do you want to loop over sparse array instead of using a higher level function like |
|
I want to easily get information about sparsity patterns to be able to do sparse differentiation described here: I assume that this kind of stuff will be needed on a lot of different types, not only for our finite difference uses, but also in eventual AD implementations as well. I assume there must be some better way to handle this than to specialize to every matrix type, though I could be wrong there. |
Note that I am not saying anything about creating a generic sparse matrix multiplication or anything like that, that would be silly. Of course that requires utilizing the representation in order to be efficient at all. But I am not sure that the query for the sparsity structure has to be different for each sparse type, and as @dlfivefifty noted sometimes a simple loop over non-zero elements is required. If there was an easy way to get an efficient iterator for the right way to walk through the non-zero elements (along with gathering the indices) then that would be helpful. |
I think at the basic level we would want something like the following to work for general sparse matrices: convert(::Type{SparseMatrixCSC}, A::AbstractSparseMatrix) = sparse(nonzerorows(A), nonzerocolumns(A), nonzerovalues(A)) But it raises some questions on whether these should be separate iterators, or a single iterator whose items are |
We've done quite a bit of brainstorming about this in #15648 — which is precisely what led to Tim's ArrayIteration.jl package. |
Indeed, this falls very well in scope of #15648, so closing. |
FWIW, I posted an example iterator that gives |
I do think there could be half-way solutions here that could be quicker and easier to implement that might satisfy the root issue here. They won't be the whole ArrayIteration thing, but it seems clearer in my mind now that I imagine that doesn't solve your real problem, though, since you want "not-all-indices-are-stored" for dispatch purposes. That gets into the array storage trait that Sheehan's been working on — I've been imagining that as a 1.1 feature. |
Though indeed the part of this issue concerning iteration falls under #15648, the broader issue of clarification of the |
Yes, I agree. Key question here is what does it mean to be an
|
|
Is this the right place for this discussion? Developing a new clean interface is something that should happen in |
Saying the obvious here - J. H. Wilkinson's informal working definition of a sparse matrix was "any matrix with enough zeros that it pays to take advantage of them". This basically translates to having algorithms that can operate in time proportional to I am closing this - as there isn't much actionable here. If people want to keep it around for discussion, we can reopen. |
Here's the action — and why I re-opened this last time:
I'd love for this to go one step further and define a handful of core helper functions that we then can use with all arrays that are particularly efficient for sparse implementations. Of course, that's more than just a doc change, and somewhat more targeted by https://github.com/JuliaLang/julia/issues/26613 (which IMO is also a partial duplicate). |
Maybe we can continue that in #26613. I suspect that it isn't easy to come up with a data structure agnostic API that is fast. |
It would be nice to have a clearly defined
AbstractSparseArray <: AbstractArray
with an interface that defines where the non-zero values are, and that way generic looping over sparse arrays can be defined. Special matrix types likeTridiagonal
should fall underAbstractSparseArray
to allow for generic sparse iteration, and then it would make it easier for things like @dlfivefifty 's BandedMatrices.jl to plug into a more generic system of sparsity support.The text was updated successfully, but these errors were encountered: