Skip to content

Commit ba8ef7c

Browse files
committed
[documentation] Explain how to change detector and coloring_algorithm in sparse backends
1 parent 161c64e commit ba8ef7c

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
with:
2929
version: ${{ matrix.version }}
3030
arch: ${{ matrix.arch }}
31-
- uses: actions/cache@v1
31+
- uses: actions/cache@v4
3232
env:
3333
cache-name: cache-artifacts
3434
with:

docs/Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ OptimizationProblems = "5049e819-d29b-5fba-b941-0eee7e64c1c6"
1010
Percival = "01435c0c-c90d-11e9-3788-63660f8fbccc"
1111
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
1212
SolverBenchmark = "581a75fa-a23a-52d0-a590-d6201de2218a"
13+
SparseConnectivityTracer = "9f842d2f-2579-4b1d-911e-f412cf18a3f5"
14+
SparseMatrixColorings = "0a514795-09f3-496d-8182-132a7b665d35"
1315
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
1416

1517
[compat]
@@ -22,4 +24,6 @@ OptimizationProblems = "0.8"
2224
Percival = "0.7"
2325
Plots = "1"
2426
SolverBenchmark = "0.6"
27+
SparseConnectivityTracer = "0.6.14"
28+
SparseMatrixColorings = "0.4.14"
2529
Zygote = "0.6.62"

docs/src/sparse.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ using ADNLPModels, NLPModels
88
f(x) = (x[1] - 1)^2
99
T = Float64
1010
x0 = T[-1.2; 1.0]
11-
lvar, uvar = zeros(T, 2), ones(T, 2)
11+
nvar, ncon = 2, 1
12+
lvar, uvar = zeros(T, nvar), ones(T, nvar)
1213
lcon, ucon = -T[0.5], T[0.5]
1314
c!(cx, x) = begin
1415
cx[1] = x[2]
@@ -22,26 +23,48 @@ nlp = ADNLPModel!(f, x0, lvar, uvar, c!, lcon, ucon, backend = :optimized)
2223
```
2324

2425
```@example ex1
25-
x = rand(T, 2)
26+
x = rand(T, nvar)
2627
J = jac(nlp, x)
2728
```
2829

2930
```@example ex1
30-
x = rand(T, 2)
31+
x = rand(T, nvar)
3132
H = hess(nlp, x)
3233
```
3334

35+
## Options for sparsity pattern detection and coloring
36+
3437
The backends available for sparse derivatives (`SparseADJacobian`, `SparseEnzymeADJacobian`, `SparseADHessian`, `SparseReverseADHessian`, and `SparseEnzymeADHessian`) allow for customization through keyword arguments such as `detector` and `coloring_algorithm`.
3538
These arguments specify the sparsity pattern detector and the coloring algorithm, respectively.
3639

3740
- A **`detector`** must be of type `ADTypes.AbstractSparsityDetector`.
3841
The default detector is `TracerSparsityDetector()` from the package `SparseConnectivityTracer.jl`.
3942
Prior to version 0.8.0, the default was `SymbolicSparsityDetector()` from `Symbolics.jl`.
43+
A `TracerLocalSparsityDetector()` is also available and can be used if the sparsity pattern of Jacobians and Hessians depends on `x`.
44+
45+
```@example ex1
46+
import SparseConnectivityTracer.TracerLocalSparsityDetector
47+
48+
set_adbackend!(
49+
nlp,
50+
jacobian_backend = ADNLPModels.SparseADJacobian(nvar, f, ncon, c!, detector=TracerLocalSparsityDetector()),
51+
hessian_backend = ADNLPModels.SparseADHessian(nvar, f, ncon, c!, detector=TracerLocalSparsityDetector()),
52+
)
53+
```
4054

4155
- A **`coloring_algorithm`** must be of type `SparseMatrixColorings.GreedyColoringAlgorithm`.
4256
The default algorithm is `GreedyColoringAlgorithm{:direct}()` for `SparseADJacobian`, `SparseEnzymeADJacobian` and `SparseADHessian`, while it is `GreedyColoringAlgorithm{:substitution}()` for `SparseReverseADHessian` and `SparseEnzymeADHessian`.
4357
These algorithms are provided by the package `SparseMatrixColorings.jl`.
4458

59+
```@example ex1
60+
using SparseMatrixColorings
61+
62+
set_adbackend!(
63+
nlp,
64+
hessian_backend = ADNLPModels.SparseADHessian(nvar, f, ncon, c!, coloring_algorithm=GreedyColoringAlgorithm{:substitution}()),
65+
)
66+
```
67+
4568
The `GreedyColoringAlgorithm{:direct}()` performs column coloring for Jacobians and star coloring for Hessians.
4669
In contrast, `GreedyColoringAlgorithm{:substitution}()` applies acyclic coloring for Hessians. The `:substitution` mode generally requires fewer colors than `:direct`, thus fewer directional derivatives are needed to reconstruct the sparse Hessian.
4770
However, it necessitates storing the compressed sparse Hessian, while `:direct` coloring only requires storage for one column of the compressed Hessian.

0 commit comments

Comments
 (0)