Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit 3572380

Browse files
committed
Dont use benchmarktools
1 parent f7fd112 commit 3572380

3 files changed

Lines changed: 70 additions & 11 deletions

File tree

Project.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ julia = "1.6"
6060
ArrayInterfaceBandedMatrices = "2e50d22c-5be1-4042-81b1-c572ed69783d"
6161
ArrayInterfaceBlockBandedMatrices = "5331f1e9-51c7-46b0-a9b0-df4434785e0a"
6262
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
63-
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
6463
BlockBandedMatrices = "ffab5731-97b5-5995-9138-79e8c1846df0"
6564
IterativeSolvers = "42fd0dbc-a981-5370-80f2-aaf504508153"
6665
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
@@ -85,5 +84,4 @@ test = [
8584
"Symbolics",
8685
"Zygote",
8786
"StaticArrays",
88-
"BenchmarkTools",
8987
]

docs/src/index.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,67 @@ function g(x) # out-of-place
3737
end
3838
```
3939

40+
## High Level API
41+
42+
We need to perform the following steps to utilize SparseDiffTools:
43+
44+
1. Specify a Sparsity Detection Algorithm. There are 3 possible choices currently:
45+
1. `NoSparsityDetection`: This will ignore any AD choice and compute the dense Jacobian
46+
2. `JacPrototypeSparsityDetection`: If you already know the sparsity pattern, you can
47+
specify it as `JacPrototypeSparsityDetection(; jac_prototype=<sparsity pattern>)`.
48+
3. `SymbolicsSparsityDetection`: This will use `Symbolics.jl` to automatically detect
49+
the sparsity pattern. (Note that `Symbolics.jl` must be explicitly loaded before
50+
using this functionality.)
51+
2. Now choose an AD backend from `ADTypes.jl`:
52+
1. If using a Non `*Sparse*` type, then we will not use sparsity detection.
53+
2. All other sparse AD types will internally compute the proper sparsity pattern, and
54+
try to exploit that.
55+
3. Now there are 2 options:
56+
1. Precompute the cache using `sparse_jacobian_cache` and use the `sparse_jacobian` or
57+
`sparse_jacobian!` functions to compute the Jacobian. This option is recommended if
58+
you are repeatedly computing the Jacobian for the same function.
59+
2. Directly use `sparse_jacobian` or `sparse_jacobian!` to compute the Jacobian. This
60+
option should be used if you are only computing the Jacobian once.
61+
62+
```julia
63+
using Symbolics
64+
65+
sd = SymbolicsSparsityDetection()
66+
adtype = AutoSparseFiniteDiff()
67+
x = rand(30)
68+
y = similar(x)
69+
70+
# Option 1
71+
## OOP Function
72+
cache = sparse_jacobian_cache(adtype, sd, g, x; fx=y) # Passing `fx` is needed if size(y) != size(x)
73+
J = sparse_jacobian(adtype, cache, g, x)
74+
### Or
75+
J_preallocated = similar(J)
76+
sparse_jacobian!(J_preallocated, adtype, cache, g, x)
77+
78+
## IIP Function
79+
cache = sparse_jacobian_cache(adtype, sd, f, y, x)
80+
J = sparse_jacobian(adtype, cache, f, y, x)
81+
### Or
82+
J_preallocated = similar(J)
83+
sparse_jacobian!(J_preallocated, adtype, cache, f, y, x)
84+
85+
# Option 2
86+
## OOP Function
87+
J = sparse_jacobian(adtype, sd, g, x)
88+
### Or
89+
J_preallocated = similar(J)
90+
sparse_jacobian!(J_preallocated, adtype, sd, g, x)
91+
92+
## IIP Function
93+
J = sparse_jacobian(adtype, sd, f, y, x)
94+
### Or
95+
J_preallocated = similar(J)
96+
sparse_jacobian!(J_preallocated, adtype, sd, f, y, x)
97+
```
98+
99+
## Lower Level API
100+
40101
For this function, we know that the sparsity pattern of the Jacobian is a
41102
`Tridiagonal` matrix. However, if we didn't know the sparsity pattern for
42103
the Jacobian, we could use the `Symbolics.jacobian_sparsity` function to automatically

test/test_sparse_jacobian.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ x = randn(Float32, 100);
2121

2222
J_true = ForwardDiff.jacobian(fdiff, x);
2323

24-
@info "`ForwardDiff.jacobian` time: $(@belapsed(ForwardDiff.jacobian($fdiff, $x)))s"
24+
@info "`ForwardDiff.jacobian` time: $(@elapsed(ForwardDiff.jacobian(diff, x)))s"
2525

2626
# SparseDiffTools High-Level API
2727
J_sparsity = Symbolics.jacobian_sparsity(fdiff, similar(x), x);
@@ -44,15 +44,15 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(jac_prototype = J_spars
4444
@test J J_true
4545
@inferred sparse_jacobian!(J, difftype, cache, fdiff, x)
4646

47-
t₁ = @belapsed sparse_jacobian!($J, $difftype, $cache, $fdiff, $x)
47+
t₁ = @elapsed sparse_jacobian!(J, difftype, cache, fdiff, x)
4848
@info "$(nameof(typeof(difftype)))() `sparse_jacobian!` (only differentiation) time: $(t₁)s"
4949

5050
J = sparse_jacobian(difftype, cache, fdiff, x)
5151

5252
@test J J_true
5353
# @inferred sparse_jacobian(difftype, cache, fdiff, x)
5454

55-
t₂ = @belapsed sparse_jacobian($difftype, $cache, $fdiff, $x)
55+
t₂ = @elapsed sparse_jacobian(difftype, cache, fdiff, x)
5656
@info "$(nameof(typeof(difftype)))() `sparse_jacobian` (with matrix allocation) time: $(t₂)s"
5757
end
5858

@@ -62,7 +62,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(jac_prototype = J_spars
6262
@test J J_true
6363
# @inferred sparse_jacobian(difftype, sd, fdiff, x)
6464

65-
t₁ = @belapsed sparse_jacobian($difftype, $sd, $fdiff, $x)
65+
t₁ = @elapsed sparse_jacobian(difftype, sd, fdiff, x)
6666
@info "$(nameof(typeof(difftype)))() `sparse_jacobian` (complete) time: $(t₁)s"
6767

6868
cache = sparse_jacobian_cache(difftype, sd, fdiff, x)
@@ -73,7 +73,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(jac_prototype = J_spars
7373
@test J J_true
7474
@inferred sparse_jacobian!(J, difftype, sd, fdiff, x)
7575

76-
t₂ = @belapsed sparse_jacobian!($J, $difftype, $sd, $fdiff, $x)
76+
t₂ = @elapsed sparse_jacobian!(J, difftype, sd, fdiff, x)
7777
@info "$(nameof(typeof(difftype)))() `sparse_jacobian!` (with matrix coloring) time: $(t₂)s"
7878
end
7979
end
@@ -92,15 +92,15 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(jac_prototype = J_spars
9292
@test J J_true
9393
@inferred sparse_jacobian!(J, difftype, cache, fdiff, y, x)
9494

95-
t₁ = @belapsed sparse_jacobian!($J, $difftype, $cache, $fdiff, $y, $x)
95+
t₁ = @elapsed sparse_jacobian!(J, difftype, cache, fdiff, y, x)
9696
@info "$(nameof(typeof(difftype)))() `sparse_jacobian!` (only differentiation) time: $(t₁)s"
9797

9898
J = sparse_jacobian(difftype, cache, fdiff, y, x)
9999

100100
@test J J_true
101101
# @inferred sparse_jacobian(difftype, cache, fdiff, y, x)
102102

103-
t₂ = @belapsed sparse_jacobian($difftype, $cache, $fdiff, $y, $x)
103+
t₂ = @elapsed sparse_jacobian(difftype, cache, fdiff, y, x)
104104
@info "$(nameof(typeof(difftype)))() `sparse_jacobian` (with jacobian allocation) time: $(t₂)s"
105105
end
106106

@@ -110,7 +110,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(jac_prototype = J_spars
110110
@test J J_true
111111
# @inferred sparse_jacobian(difftype, sd, fdiff, y, x)
112112

113-
t₁ = @belapsed sparse_jacobian($difftype, $sd, $fdiff, $y, $x)
113+
t₁ = @elapsed sparse_jacobian(difftype, sd, fdiff, y, x)
114114
@info "$(nameof(typeof(difftype)))() `sparse_jacobian` (complete) time: $(t₁)s"
115115

116116
J = SparseDiffTools.__init_𝒥(cache)
@@ -120,7 +120,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(jac_prototype = J_spars
120120
@test J J_true
121121
@inferred sparse_jacobian!(J, difftype, sd, fdiff, y, x)
122122

123-
t₂ = @belapsed sparse_jacobian!($J, $difftype, $sd, $fdiff, $y, $x)
123+
t₂ = @elapsed sparse_jacobian!(J, difftype, sd, fdiff, y, x)
124124
@info "$(nameof(typeof(difftype)))() `sparse_jacobian!` (with matrix coloring) time: $(t₂)s"
125125
end
126126
end

0 commit comments

Comments
 (0)