-
-
Notifications
You must be signed in to change notification settings - Fork 42
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 docs of AutoSparseForwardDiff and matrix coloring #231
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,7 @@ are then applied at each point in space (they are broadcast). Use `dx=dy=1/32`. | |
The resulting `NonlinearProblem` definition is: | ||
|
||
```@example ill_conditioned_nlprob | ||
using NonlinearSolve, LinearAlgebra, SparseArrays, LinearSolve | ||
using NonlinearSolve, LinearAlgebra, SparseArrays, LinearSolve, Symbolics | ||
|
||
const N = 32 | ||
const xyd_brusselator = range(0, stop = 1, length = N) | ||
|
@@ -275,3 +275,28 @@ nothing # hide | |
|
||
For more information on the preconditioner interface, see the | ||
[linear solver documentation](https://docs.sciml.ai/LinearSolve/stable/basics/Preconditioners/). | ||
|
||
## Speed up Jacobian computation with sparsity exploitation and matrix coloring | ||
|
||
To cut down the of Jacobian building overhead, we can choose to exploit the sparsity pattern and deploy matrix coloring during Jacobian construction. With NonlinearSolve.jl, we can simply use ```autodiff=AutoSparseForwardDiff()``` to automatically exploit the sparsity pattern of Jacobian matrices: | ||
|
||
```@example ill_conditioned_nlprob | ||
@benchmark solve(prob_brusselator_2d, | ||
NewtonRaphson(linsolve=KrylovJL_GMRES(), precs=incompletelu, concrete_jac=true, | ||
autodiff=AutoSparseForwardDiff())); | ||
nothing # hide | ||
``` | ||
|
||
To setup matrix coloring for the jacobian sparsity pattern, we can simply get the coloring vector by using [ArrayInterface.jl](https://github.com/JuliaArrays/ArrayInterface.jl) for the sparsity pattern of `jac_prototype`: | ||
|
||
```@example ill_conditioned_nlprob | ||
using ArrayInterface | ||
colorvec = ArrayInterface.matrix_colors(jac_sparsity) | ||
ff = NonlinearFunction(brusselator_2d_loop; jac_prototype=float.(jac_sparsity), colorvec) | ||
Comment on lines
+294
to
+295
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't the color vectors done automatically so this step is unneccessary?
Comment on lines
+294
to
+295
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't the color vectors done automatically so this step is unneccessary? |
||
prob_brusselator_2d_sparse = NonlinearProblem(ff, u0, p) | ||
|
||
@benchmark solve(prob_brusselator_2d_sparse, | ||
NewtonRaphson(linsolve=KrylovJL_GMRES(), precs=incompletelu, concrete_jac=true, | ||
autodiff=AutoSparseForwardDiff())); | ||
nothing # hide | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems a bit oddly placed since the parts above already did sparsity and matrix coloring. This is just a less manual route for it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avik said the above part https://docs.sciml.ai/NonlinearSolve/dev/tutorials/advanced/#Declaring-a-Sparse-Jacobian-with-Automatic-Sparsity-Detection didn't actually use sparsity, we still need to use sparse AD type for matrix coloring. Did I understand this wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ErikQQY is right. Right now even if you have colorvecs and stuff but use a non-sparse AD type, we construct a dense jacobian based on how SparseDiffTools is setup. But this can be modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh. We should change that. If you pass a sparse matrix into jac_prototype it should always use sparse diff on it. The SparseDiffTools stuff should only override it to force sparse in the case of
jac_prototype=nothing
, otherwise it should respect the user's type and color on-demand (since coloring is super cheap).