@@ -5,10 +5,9 @@ title: "Brusselator sparse AD benchmarks"
55``` julia
66using ADTypes
77using LinearAlgebra, SparseArrays
8- using BenchmarkTools, DataFrames
8+ using BenchmarkTools
99import DifferentiationInterface as DI
1010using Plots
11- import SparseDiffTools as SDT
1211using SparseConnectivityTracer: TracerSparsityDetector
1312using SparseMatrixColorings: GreedyColoringAlgorithm
1413using Symbolics: SymbolicsSparsityDetector
@@ -35,31 +34,33 @@ limit(a, N) =
3534function brusselator_2d! (du, u)
3635 t = 0.0
3736 N = size (u, 1 )
38- xyd = range (0 ; stop= 1 , length= N)
37+ xyd = range (0 ; stop = 1 , length = N)
3938 p = (3.4 , 1.0 , 10.0 , step (xyd))
4039 A, B, alpha, dx = p
4140 alpha = alpha / dx^ 2
4241
4342 @inbounds for I in CartesianIndices ((N, N))
4443 i, j = Tuple (I)
4544 x, y = xyd[I[1 ]], xyd[I[2 ]]
46- ip1, im1, jp1, jm1 = limit (i + 1 , N),
45+ ip1, im1, jp1,
46+ jm1 = limit (i + 1 , N),
4747 limit (i - 1 , N), limit (j + 1 , N),
4848 limit (j - 1 , N)
49- du[i, j, 1 ] =
50- alpha *
51- (u[im1, j, 1 ] + u[ip1, j, 1 ] + u[i, jp1, 1 ] + u[i, jm1, 1 ] - 4 u[i, j, 1 ]) +
52- B +
53- u[i, j, 1 ]^ 2 * u[i, j, 2 ] - (A + 1 ) * u[i, j, 1 ] + brusselator_f (x, y, t)
54- du[i, j, 2 ] =
55- alpha *
56- (u[im1, j, 2 ] + u[ip1, j, 2 ] + u[i, jp1, 2 ] + u[i, jm1, 2 ] - 4 u[i, j, 2 ]) +
57- A * u[i, j, 1 ] - u[i, j, 1 ]^ 2 * u[i, j, 2 ]
49+ du[i, j, 1 ] = alpha *
50+ (u[im1, j, 1 ] + u[ip1, j, 1 ] + u[i, jp1, 1 ] + u[i, jm1, 1 ] -
51+ 4 u[i, j, 1 ]) +
52+ B +
53+ u[i, j, 1 ]^ 2 * u[i, j, 2 ] - (A + 1 ) * u[i, j, 1 ] +
54+ brusselator_f (x, y, t)
55+ du[i, j, 2 ] = alpha *
56+ (u[im1, j, 2 ] + u[ip1, j, 2 ] + u[i, jp1, 2 ] + u[i, jm1, 2 ] -
57+ 4 u[i, j, 2 ]) +
58+ A * u[i, j, 1 ] - u[i, j, 1 ]^ 2 * u[i, j, 2 ]
5859 end
5960end ;
6061
6162function init_brusselator_2d (N:: Integer )
62- xyd = range (0 ; stop= 1 , length= N)
63+ xyd = range (0 ; stop = 1 , length = N)
6364 N = length (xyd)
6465 u = zeros (N, N, 2 )
6566 for I in CartesianIndices ((N, N))
@@ -108,8 +109,7 @@ Test Passed
108109
109110``` julia
110111c1 = ADTypes. column_coloring (S1, GreedyColoringAlgorithm ())
111- c2 = SDT. matrix_colors (S1)
112- @test c1 == c2
112+ @test length (unique (c1)) > 0
113113```
114114
115115```
@@ -125,31 +125,43 @@ Test Passed
125125``` julia
126126backend = AutoSparse (
127127 AutoForwardDiff ();
128- sparsity_detector= TracerSparsityDetector (),
129- coloring_algorithm= GreedyColoringAlgorithm (),
128+ sparsity_detector = TracerSparsityDetector (),
129+ coloring_algorithm = GreedyColoringAlgorithm ()
130130);
131131
132- extras = DI. prepare_jacobian (brusselator_2d!, similar (x0_32), backend, x0_32);
132+ prep = DI. prepare_jacobian (brusselator_2d!, similar (x0_32), backend, x0_32);
133133J1 = DI. jacobian! (
134- brusselator_2d!, similar (x0_32), similar (S1, eltype (x0_32)), extras , backend, x0_32
134+ brusselator_2d!, similar (x0_32), similar (S1, eltype (x0_32)), prep , backend, x0_32
135135)
136136
137- cache = SDT. sparse_jacobian_cache (
138- backend,
139- SDT. JacPrototypeSparsityDetection (; jac_prototype= S1),
140- brusselator_2d!,
141- similar (x0_32),
142- x0_32,
143- );
144- J2 = SDT. sparse_jacobian! (
145- similar (S1, eltype (x0_32)), backend, cache, brusselator_2d!, similar (x0_32), x0_32
146- )
147-
148- @test J1 == J2
137+ @test nnz (J1) > 0
149138```
150139
151140```
152- Test Passed
141+ Error: MethodError: no method matching _prepare_pushforward_aux(::Val{true}
142+ , ::DifferentiationInterface.PushforwardFast, ::typeof(Main.var"##WeaveSand
143+ Box#225".brusselator_2d!), ::Array{Float64, 3}, ::ADTypes.AutoForwardDiff{n
144+ othing, Nothing}, ::Array{Float64, 3}, ::Tuple{Array{Float64, 3}})
145+
146+ The autodiff backend you chose requires a package which may not be loaded.
147+ Please run the following command and try again:
148+
149+ import ForwardDiff
150+
151+ Closest candidates are:
152+ _prepare_pushforward_aux(::Val, !Matched::DifferentiationInterface.Pushfo
153+ rwardSlow, ::F, ::Any, ::ADTypes.AbstractADType, ::Any, ::Tuple{Vararg{T, N
154+ }} where {N, T}, DifferentiationInterface.Context...) where {F, C}
155+ @ DifferentiationInterface /cache/julia-buildkite-plugin/depots/5b300254
156+ -1738-4989-ae0a-f4d2d937f953/packages/DifferentiationInterface/afUhd/src/fi
157+ rst_order/pushforward.jl:303
158+ _prepare_pushforward_aux(::Val, !Matched::DifferentiationInterface.Pushfo
159+ rwardSlow, ::F, !Matched::ADTypes.AbstractADType, ::Any, !Matched::Tuple{Va
160+ rarg{T, N}} where {N, T}, !Matched::DifferentiationInterface.Context...) wh
161+ ere {F, C}
162+ @ DifferentiationInterface /cache/julia-buildkite-plugin/depots/5b300254
163+ -1738-4989-ae0a-f4d2d937f953/packages/DifferentiationInterface/afUhd/src/fi
164+ rst_order/pushforward.jl:283
153165```
154166
155167
@@ -194,21 +206,22 @@ end
194206
195207let
196208 pld = plot (;
197- title= " Sparsity detection on the Brusselator" ,
198- xlabel= " Input size N" ,
199- ylabel= " Runtime [s]" ,
209+ title = " Sparsity detection on the Brusselator" ,
210+ xlabel = " Input size N" ,
211+ ylabel = " Runtime [s]"
200212 )
201213 plot! (
202214 pld,
203215 N_values,
204216 td1;
205- lw= 2 ,
206- linestyle= :auto ,
207- markershape= :auto ,
208- label= " SparseConnectivityTracer" ,
217+ lw = 2 ,
218+ linestyle = :auto ,
219+ markershape = :auto ,
220+ label = " SparseConnectivityTracer"
209221 )
210- plot! (pld, N_values, td2; lw= 2 , linestyle= :auto , markershape= :auto , label= " Symbolics" )
211- plot! (pld; xscale= :log10 , yscale= :log10 , legend= :topleft , minorgrid= true )
222+ plot! (pld, N_values, td2; lw = 2 , linestyle = :auto ,
223+ markershape = :auto , label = " Symbolics" )
224+ plot! (pld; xscale = :log10 , yscale = :log10 , legend = :topleft , minorgrid = true )
212225 pld
213226end
214227```
@@ -220,32 +233,30 @@ end
220233### Coloring
221234
222235``` julia
223- tc1, tc2 = zeros ( length (N_values)), zeros (length (N_values))
236+ tc1 = zeros (length (N_values))
224237for (i, N) in enumerate (N_values)
225238 @info " Benchmarking coloring: N=$N "
226239 x0 = init_brusselator_2d (N)
227240 S = ADTypes. jacobian_sparsity (
228241 brusselator_2d!, similar (x0), x0, TracerSparsityDetector ()
229242 )
230243 tc1[i] = @belapsed ADTypes. column_coloring ($ S, GreedyColoringAlgorithm ())
231- tc2[i] = @belapsed SDT. matrix_colors ($ S)
232244end
233245
234246let
235247 plc = plot (;
236- title= " Coloring on the Brusselator" , xlabel= " Input size N" , ylabel= " Runtime [s]"
248+ title = " Coloring on the Brusselator" , xlabel = " Input size N" , ylabel = " Runtime [s]"
237249 )
238250 plot! (
239251 plc,
240252 N_values,
241253 tc1;
242- lw= 2 ,
243- linestyle= :auto ,
244- markershape= :auto ,
245- label= " SparseMatrixColorings" ,
254+ lw = 2 ,
255+ linestyle = :auto ,
256+ markershape = :auto ,
257+ label = " SparseMatrixColorings"
246258 )
247- plot! (plc, N_values, tc2; lw= 2 , linestyle= :auto , markershape= :auto , label= " SparseDiffTools" )
248- plot! (plc; xscale= :log10 , yscale= :log10 , legend= :topleft , minorgrid= true )
259+ plot! (plc; xscale = :log10 , yscale = :log10 , legend = :topleft , minorgrid = true )
249260 plc
250261end
251262```
257268### Differentiation
258269
259270``` julia
260- tj1, tj2 = zeros ( length (N_values)), zeros (length (N_values))
271+ tj1 = zeros (length (N_values))
261272for (i, N) in enumerate (N_values)
262273 @info " Benchmarking differentiation: N=$N "
263274 x0 = init_brusselator_2d (N)
@@ -266,45 +277,59 @@ for (i, N) in enumerate(N_values)
266277 )
267278 J = similar (S, eltype (x0))
268279
269- tj1[i] = @belapsed DI. jacobian! ($ brusselator_2d!, _y, _J, _prep, $ backend, $ x0) setup = (
280+ tj1[i] = @belapsed DI. jacobian! ($ brusselator_2d!, _y, _J, _prep, $ backend, $ x0) setup= (
270281 _y = similar ($ x0);
271282 _J = similar ($ J);
272283 _prep = DI. prepare_jacobian ($ brusselator_2d!, similar ($ x0), $ backend, $ x0)
273- ) evals = 1
274-
275- tj2[i] = @belapsed SDT. sparse_jacobian! (_J, $ backend, _cache, $ brusselator_2d!, _y, $ x0) setup = (
276- _y = similar ($ x0);
277- _J = similar ($ J);
278- _cache = SDT. sparse_jacobian_cache (
279- $ backend,
280- SDT. JacPrototypeSparsityDetection (; jac_prototype= $ S),
281- $ brusselator_2d!,
282- similar ($ x0),
283- $ x0,
284- )
285- ) evals = 1
284+ ) evals= 1
286285end
287286
288287let
289288 plj = plot (;
290- title= " Sparse Jacobian on the Brusselator" , xlabel= " Input size N" , ylabel= " Runtime [s]"
289+ title = " Sparse Jacobian on the Brusselator" , xlabel = " Input size N" , ylabel = " Runtime [s]"
291290 )
292291 plot! (
293292 plj,
294293 N_values,
295294 tj1;
296- lw= 2 ,
297- linestyle= :auto ,
298- markershape= :auto ,
299- label= " DifferentiationInterface" ,
295+ lw = 2 ,
296+ linestyle = :auto ,
297+ markershape = :auto ,
298+ label = " DifferentiationInterface"
300299 )
301- plot! (plj, N_values, tj2; lw= 2 , linestyle= :auto , markershape= :auto , label= " SparseDiffTools" )
302- plot! (plj; xscale= :log10 , yscale= :log10 , legend= :topleft , minorgrid= true )
300+ plot! (plj; xscale = :log10 , yscale = :log10 , legend = :topleft , minorgrid = true )
303301 plj
304302end
305303```
306304
307- ![ ] ( figures/BrusselatorSparseAD_10_1.png )
305+ ```
306+ Error: MethodError: no method matching _prepare_pushforward_aux(::Val{true}
307+ , ::DifferentiationInterface.PushforwardFast, ::typeof(Main.var"##WeaveSand
308+ Box#225".brusselator_2d!), ::Array{Float64, 3}, ::ADTypes.AutoForwardDiff{n
309+ othing, Nothing}, ::Array{Float64, 3}, ::Tuple{Array{Float64, 3}})
310+
311+ The autodiff backend you chose requires a package which may not be loaded.
312+ Please run the following command and try again:
313+
314+ import ForwardDiff
315+
316+ Closest candidates are:
317+ _prepare_pushforward_aux(::Val, !Matched::DifferentiationInterface.Pushfo
318+ rwardSlow, ::F, ::Any, ::ADTypes.AbstractADType, ::Any, ::Tuple{Vararg{T, N
319+ }} where {N, T}, DifferentiationInterface.Context...) where {F, C}
320+ @ DifferentiationInterface /cache/julia-buildkite-plugin/depots/5b300254
321+ -1738-4989-ae0a-f4d2d937f953/packages/DifferentiationInterface/afUhd/src/fi
322+ rst_order/pushforward.jl:303
323+ _prepare_pushforward_aux(::Val, !Matched::DifferentiationInterface.Pushfo
324+ rwardSlow, ::F, !Matched::ADTypes.AbstractADType, ::Any, !Matched::Tuple{Va
325+ rarg{T, N}} where {N, T}, !Matched::DifferentiationInterface.Context...) wh
326+ ere {F, C}
327+ @ DifferentiationInterface /cache/julia-buildkite-plugin/depots/5b300254
328+ -1738-4989-ae0a-f4d2d937f953/packages/DifferentiationInterface/afUhd/src/fi
329+ rst_order/pushforward.jl:283
330+ ```
331+
332+
308333
309334
310335
@@ -313,39 +338,22 @@ end
313338``` julia
314339let
315340 pl = plot (;
316- title= " Is the new pipeline worth it? \n Test case: Brusselator" ,
317- xlabel= " Input size N" ,
318- ylabel= " Runtime ratio DI / SparseDiffTools " ,
341+ title = " Sparsity detection: Symbolics vs SparseConnectivityTracer \n Test case: Brusselator" ,
342+ xlabel = " Input size N" ,
343+ ylabel = " Runtime ratio Symbolics / SCT "
319344 )
320345 plot! (
321346 pl,
322347 N_values,
323348 td2 ./ td1;
324- lw= 2 ,
325- linestyle= :dot ,
326- markershape= :utriangle ,
327- label= " sparsity detection speedup" ,
349+ lw = 2 ,
350+ linestyle = :dot ,
351+ markershape = :utriangle ,
352+ label = " sparsity detection speedup"
328353 )
329354 plot! (
330- pl,
331- N_values,
332- tc2 ./ tc1;
333- lw= 2 ,
334- linestyle= :dashdot ,
335- markershape= :diamond ,
336- label= " coloring speedup" ,
337- )
338- plot! (
339- pl,
340- N_values,
341- tj2 ./ tj1;
342- lw= 2 ,
343- linestyle= :dash ,
344- markershape= :pentagon ,
345- label= " differentiation speedup" ,
346- )
347- plot! (pl, N_values, ones (length (N_values)); lw= 3 , color= :black , label= " same speed" )
348- plot! (pl; xscale= :log10 , yscale= :log10 , minorgrid= true , legend= :right )
355+ pl, N_values, ones (length (N_values)); lw = 3 , color = :black , label = " same speed" )
356+ plot! (pl; xscale = :log10 , yscale = :log10 , minorgrid = true , legend = :right )
349357 pl
350358end
351359```
0 commit comments