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

Add fast path for _colorediteration! with sparse J #124

Merged
merged 2 commits into from
Jul 19, 2021
Merged

Add fast path for _colorediteration! with sparse J #124

merged 2 commits into from
Jul 19, 2021

Conversation

sjdaines
Copy link

@sjdaines sjdaines commented Jul 18, 2021

Partial fix for JuliaDiff/SparseDiffTools.jl#138
JuliaDiff/SparseDiffTools.jl#100

Adds a fast path for the case where J and sparsity are are both
SparseMatrixCSC and have the same sparsity pattern

Partial fix for JuliaDiff/SparseDiffTools.jl#138
JuliaDiff/SparseDiffTools.jl#100

Adds a fast path for the case where J and sparsity are are both
SparseMatrixCSC and have the same number of columns and stored values.
Comment on lines 39 to 40
common_sparsity = (length(J.colptr) == length(sparsity.colptr) &&
length(J.nzval) == length(sparsity.nzval))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this. This just means that they have the same data size. Now, this might be fine because I can't think of a reason for someone hitting this case on purpose: the reason for sparsity vs J is so that you can say keep J dense but still use color differentiation (for smaller matrices), or do partial/approximate coloring. But if the sparsity has the same sparsity as the matrix you're trying to fill, then partial/approximate coloring doesn't make sense... so I think this is fine. I'll merge it under this understanding but it's good to have it mentioned.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can there be a test on this behavior?

Copy link
Author

@sjdaines sjdaines Jul 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated version simplifies this logic so that the fast path is only taken if J and sparsity have the same sparsity pattern (by comparing colptr and rowval).

There is an existing test (in coloring_tests.jl) for color differentiation writing to a dense J

This does reveal what I think might be unrelated pre-exisiting issues with accumulating components of a Jacobian into J, but I'm not sure what the use cases are here:

  • in general, J is initialised to zero, and elements are set, not added (so this isn't useful to accumulate)
  • if J is sparse, but with different stored elements to sparsity it's not clear to me what the correct behaviour should be:
    • error ? (instead of silently taking a slow path)
    • reset J to the pattern of sparsity ? (might make sense if the intent is to set J. Would allocate, but could be optimized)
    • keep J pattern, initialize to zero, and add new stored elements if needed ? (current behaviour)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the purpose I was thinking of was that, maybe you don't have just dense vs sparse at different parts of the code, but also do something like partial coloring so the sparsity may be in general Tridiagonal while the sparse matrix is not tridiagonal, i.e. using coloring on a subset so that you can use fewer f calculations and get an approximation to the full Jacobian. But if you do that, wouldn't you then want to just specialize the linear system solve on the approximate tridiagonal-ness? So I think this flexibility is somewhat left open as a research question.

But if it's two sparse matrices, I would assume it would only make sense if one has a subset of zeros of the other.

Simplifies fast path logic.
All other cases (J is not a SparseMatrixCSC, or J has a different sparsity
pattern) use previous slow path.
@ChrisRackauckas ChrisRackauckas merged commit d6df0e2 into JuliaDiff:master Jul 19, 2021
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.

2 participants