Skip to content

Commit e4a181b

Browse files
committed
Add docs of AutoSparseForwardDiff and matrix coloring
Signed-off-by: ErikQQY <[email protected]>
1 parent 143a966 commit e4a181b

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

docs/src/tutorials/advanced.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ are then applied at each point in space (they are broadcast). Use `dx=dy=1/32`.
5555
The resulting `NonlinearProblem` definition is:
5656

5757
```@example ill_conditioned_nlprob
58-
using NonlinearSolve, LinearAlgebra, SparseArrays, LinearSolve
58+
using NonlinearSolve, LinearAlgebra, SparseArrays, LinearSolve, Symbolics
5959
6060
const N = 32
6161
const xyd_brusselator = range(0, stop = 1, length = N)
@@ -275,3 +275,28 @@ nothing # hide
275275

276276
For more information on the preconditioner interface, see the
277277
[linear solver documentation](https://docs.sciml.ai/LinearSolve/stable/basics/Preconditioners/).
278+
279+
## Speed up Jacobian computation with sparsity exploitation and matrix coloring
280+
281+
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:
282+
283+
```@example ill_conditioned_nlprob
284+
@benchmark solve(prob_brusselator_2d,
285+
NewtonRaphson(linsolve=KrylovJL_GMRES(), precs=incompletelu, concrete_jac=true,
286+
autodiff=AutoSparseForwardDiff()));
287+
nothing # hide
288+
```
289+
290+
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`:
291+
292+
```@example ill_conditioned_nlprob
293+
using ArrayInterface
294+
colorvec = ArrayInterface.matrix_colors(jac_sparsity)
295+
ff = NonlinearFunction(brusselator_2d_loop; jac_prototype=float.(jac_sparsity), colorvec)
296+
prob_brusselator_2d_sparse = NonlinearProblem(ff, u0, p)
297+
298+
@benchmark solve(prob_brusselator_2d_sparse,
299+
NewtonRaphson(linsolve=KrylovJL_GMRES(), precs=incompletelu, concrete_jac=true,
300+
autodiff=AutoSparseForwardDiff()));
301+
nothing # hide
302+
```

0 commit comments

Comments
 (0)