Skip to content

Commit 435aecb

Browse files
committed
docs: add a table to guarantee selections
1 parent eda2977 commit 435aecb

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

docs/src/basics/sparsity_detection.md

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,34 @@ to use this in an actual problem, see
66

77
Notation wise we are trying to solve for `x` such that `nlfunc(x) = 0`.
88

9+
## Big Table for Determining Sparsity Detection and Coloring Algorithms
10+
11+
| `f.sparsity` | `f.jac_prototype` | `f.colorvec` | Sparsity Detection | Coloring Algorithm |
12+
| :------------------------- | :---------------- | :----------- | :----------------------------------------------- | :---------------------------------------- |
13+
||| `Any` | `NoSparsityDetector()` | `NoColoringAlgorithm()` |
14+
|| Not Structured | `Any` | `NoSparsityDetector()` | `NoColoringAlgorithm()` |
15+
|| Structured || `KnownJacobianSparsityDetector(f.jac_prototype)` | `GreedyColoringAlgorithm(LargestFirst())` |
16+
|| Structured || `KnownJacobianSparsityDetector(f.jac_prototype)` | `GreedyColoringAlgorithm(LargestFirst())` |
17+
| - | - | - | - | - |
18+
| `AbstractMatrix` ||| `KnownJacobianSparsityDetector(f.sparsity)` | `ConstantColoringAlgorithm(f.colorvec)` |
19+
| `AbstractMatrix` ||| `KnownJacobianSparsityDetector(f.sparsity)` | `GreedyColoringAlgorithm(LargestFirst())` |
20+
| `AbstractMatrix` | Not Structured || `KnownJacobianSparsityDetector(f.sparsity)` | `ConstantColoringAlgorithm(f.colorvec)` |
21+
| `AbstractMatrix` | Not Structured || `KnownJacobianSparsityDetector(f.sparsity)` | `GreedyColoringAlgorithm(LargestFirst())` |
22+
| `AbstractMatrix` | Structured | `Any` | 🔴 | 🔴 |
23+
| - | - | - | - | - |
24+
| `AbstractSparsityDetector` || `Any` | `f.sparsity` | `GreedyColoringAlgorithm(LargestFirst())` |
25+
| `AbstractSparsityDetector` | Not Structured || `f.sparsity` | `ConstantColoringAlgorithm(f.colorvec)` |
26+
| `AbstractSparsityDetector` | Not Structured || `f.sparsity` | `GreedyColoringAlgorithm(LargestFirst())` |
27+
| `AbstractSparsityDetector` | Structured || `KnownJacobianSparsityDetector(f.jac_prototype)` | `ConstantColoringAlgorithm(f.colorvec)` |
28+
| `AbstractSparsityDetector` | Structured || `KnownJacobianSparsityDetector(f.jac_prototype)` | `GreedyColoringAlgorithm(LargestFirst())` |
29+
30+
1. `Structured` means either a `AbstractSparseMatrix` or `ArrayInterface.isstructured(x)` is true.
31+
2. ❌ means not provided (default)
32+
3. ✅ means provided
33+
4. 🔴 means an error will be thrown
34+
5. Providing a colorvec without specifying either sparsity or jac_prototype with a sparse or structured matrix will cause us to ignore the colorvec.
35+
6. The function calls demonstrated above are simply pseudo-code to show the general idea.
36+
937
## Case I: Sparse Jacobian Prototype is Provided
1038

1139
Let's say you have a Sparse Jacobian Prototype `jac_prototype`, in this case you can
@@ -27,19 +55,12 @@ prob = NonlinearProblem(
2755
If the `colorvec` is not provided, then it is computed on demand.
2856

2957
!!! note
30-
58+
3159
One thing to be careful about in this case is that `colorvec` is dependent on the
3260
autodiff backend used. `ADTypes.mode(ad) isa ADTypes.ForwardMode` will assume that the
3361
colorvec is the column colorvec, otherwise we will assume that the colorvec is the
3462
row colorvec.
3563

36-
!!! warning
37-
38-
Previously you could provide a `sparsity` argument to `NonlinearFunction` to specify
39-
the jacobian prototype. However, to avoid confusion, this is now deprecated. Instead,
40-
use the `jac_prototype` argument. `sparsity` must be used to exclusively specify the
41-
sparsity detection algorithm.
42-
4364
## Case II: Sparsity Detection algorithm is provided
4465

4566
If you don't have a Sparse Jacobian Prototype, but you know the which sparsity detection
@@ -59,7 +80,7 @@ for more information on sparsity detection algorithms.
5980
## Case III: Sparse AD Type is being Used
6081

6182
!!! warning
62-
83+
6384
This is now deprecated. Please use the previous two cases instead.
6485

6586
If you constructed a Nonlinear Solver with a sparse AD type, for example

src/internal/jacobian.jl

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ function construct_concrete_adtype(f::NonlinearFunction, ad::AbstractADType)
172172
end
173173
return ad # No sparse AD
174174
else
175+
if !sparse_or_structured_prototype(f.jac_prototype)
176+
if SciMLBase.has_colorvec(f)
177+
@warn "`colorvec` is provided but `jac_prototype` is not a sparse \
178+
or structured matrix. `colorvec` will be ignored."
179+
end
180+
return ad
181+
end
175182
return AutoSparse(
176183
ad;
177184
sparsity_detector = KnownJacobianSparsityDetector(f.jac_prototype),
@@ -181,13 +188,14 @@ function construct_concrete_adtype(f::NonlinearFunction, ad::AbstractADType)
181188
end
182189
else
183190
if f.sparsity isa AbstractMatrix
184-
if f.jac_prototype !== nothing && f.jac_prototype !== f.sparsity
185-
throw(ArgumentError("`sparsity::AbstractMatrix` and `jac_prototype` cannot \
186-
be both provided. Pass only `jac_prototype`."))
191+
if f.jac_prototype !== f.sparsity
192+
if f.jac_prototype !== nothing &&
193+
sparse_or_structured_prototype(f.jac_prototype)
194+
throw(ArgumentError("`sparsity::AbstractMatrix` and a sparse or \
195+
structured `jac_prototype` cannot be both \
196+
provided. Pass only `jac_prototype`."))
197+
end
187198
end
188-
Base.depwarn("`sparsity::typeof($(typeof(f.sparsity)))` is deprecated. \
189-
Pass it as `jac_prototype` instead.",
190-
:NonlinearSolve)
191199
return AutoSparse(
192200
ad;
193201
sparsity_detector = KnownJacobianSparsityDetector(f.sparsity),
@@ -209,8 +217,7 @@ function construct_concrete_adtype(f::NonlinearFunction, ad::AbstractADType)
209217
coloring_algorithm = GreedyColoringAlgorithm(LargestFirst())
210218
)
211219
else
212-
if f.jac_prototype isa AbstractSparseMatrix ||
213-
ArrayInterface.isstructured(f.jac_prototype)
220+
if sparse_or_structured_prototype(f.jac_prototype)
214221
if !(sparsity_detector isa NoSparsityDetector)
215222
@warn "`jac_prototype` is a sparse matrix but sparsity = $(f.sparsity) \
216223
has also been specified. Ignoring sparsity field and using \
@@ -254,3 +261,8 @@ end
254261

255262
get_dense_ad(ad) = ad
256263
get_dense_ad(ad::AutoSparse) = ADTypes.dense_ad(ad)
264+
265+
sparse_or_structured_prototype(::AbstractSparseMatrix) = true
266+
function sparse_or_structured_prototype(prototype::AbstractMatrix)
267+
return ArrayInterface.isstructured(prototype)
268+
end

0 commit comments

Comments
 (0)