-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathbruss.jmd
826 lines (698 loc) · 31.5 KB
/
bruss.jmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
---
title: Ill-Conditioned Nonlinear System Work-Precision Diagrams
author: Avik Pal
---
# Setup
Fetch required packages
```julia
using NonlinearSolve, SparseDiffTools, LinearAlgebra, SparseArrays, DiffEqDevTools,
CairoMakie, Symbolics, BenchmarkTools, PolyesterForwardDiff, LinearSolve, Sundials,
Enzyme, SparseConnectivityTracer, DifferentiationInterface
import NLsolve, MINPACK, PETSc, RecursiveFactorization
const RUS = RadiusUpdateSchemes;
BenchmarkTools.DEFAULT_PARAMETERS.seconds = 0.2;
```
Define a utility to timeout the benchmark after a certain time.
```julia
# Taken from ReTestItems.jl
function timeout(f, timeout)
cond = Threads.Condition()
timer = Timer(timeout) do tm
close(tm)
ex = ErrorException("timed out after $timeout seconds")
@lock cond notify(cond, ex; error=false)
end
Threads.@spawn begin
try
ret = $f()
isopen(timer) && @lock cond notify(cond, ret)
catch e
isopen(timer) && @lock cond notify(cond, CapturedException(e, catch_backtrace()); error=true)
finally
close(timer)
end
end
return @lock cond wait(cond) # will throw if we timeout
end
```
Define the Brussletor problem.
```julia
brusselator_f(x, y) = (((x - 3 // 10) ^ 2 + (y - 6 // 10) ^ 2) ≤ 0.01) * 5
limit(a, N) = ifelse(a == N + 1, 1, ifelse(a == 0, N, a))
function init_brusselator_2d(xyd, N)
N = length(xyd)
u = zeros(N, N, 2)
for I in CartesianIndices((N, N))
x = xyd[I[1]]
y = xyd[I[2]]
u[I, 1] = 22 * (y * (1 - y))^(3 / 2)
u[I, 2] = 27 * (x * (1 - x))^(3 / 2)
end
return u
end
function generate_brusselator_problem(N::Int; sparsity = nothing, kwargs...)
xyd_brusselator = range(0; stop = 1, length = N)
function brusselator_2d_loop(du_, u_, p)
A, B, α, δx = p
α = α / δx ^ 2
du = reshape(du_, N, N, 2)
u = reshape(u_, N, N, 2)
@inbounds @simd for I in CartesianIndices((N, N))
i, j = Tuple(I)
x, y = xyd_brusselator[I[1]], xyd_brusselator[I[2]]
ip1, im1 = limit(i + 1, N), limit(i - 1, N)
jp1, jm1 = limit(j + 1, N), limit(j - 1, N)
du[i, j, 1] = α * (u[im1, j, 1] + u[ip1, j, 1] + u[i, jp1, 1] + u[i, jm1, 1] -
4u[i, j, 1]) +
B + u[i, j, 1] ^ 2 * u[i, j, 2] - (A + 1) * u[i, j, 1] +
brusselator_f(x, y)
du[i, j, 2] = α * (u[im1, j, 2] + u[ip1, j, 2] + u[i, jp1, 2] + u[i, jm1, 2] -
4u[i, j, 2]) +
A * u[i, j, 1] - u[i, j, 1] ^ 2 * u[i, j, 2]
end
return nothing
end
return NonlinearProblem(
NonlinearFunction(brusselator_2d_loop; sparsity),
vec(init_brusselator_2d(xyd_brusselator, N)),
(3.4, 1.0, 10.0, step(xyd_brusselator));
kwargs...
)
end
```
```julia
function get_ordering(x::AbstractMatrix)
idxs = Vector{Int}(undef, size(x, 1))
placed = zeros(Bool, size(x, 1))
idx = 1
for j in size(x, 2):-1:1
row = view(x, :, j)
idxs_row = sortperm(row; by = x -> isnan(x) ? Inf : (x == -1 ? Inf : x))
for i in idxs_row
if !placed[i] && !isnan(row[i]) && row[i] ≠ -1
idxs[idx] = i
placed[i] = true
idx += 1
idx > length(idxs) && break
end
end
idx > length(idxs) && break
end
return idxs
end
```
# Scaling of Sparsity Detection Algorithm
We increase the problem size, and compute the jacobian 10 times similar to a real workload
where the jacobian is computed several times and amortizes the cost for computing the
sparsity pattern.
```julia
test_problem = generate_brusselator_problem(4)
bruss_f!, u0 = (du, u) -> test_problem.f(du, u, test_problem.p), test_problem.u0
y = similar(u0)
J = Float64.(ADTypes.jacobian_sparsity(bruss_f!, y, u0, TracerSparsityDetector()))
colors = matrix_colors(J)
begin
J_ = similar(J)
rows = rowvals(J)
vals = nonzeros(J)
for j in 1:size(J, 2)
for i in nzrange(J, j)
row = rows[i]
J_[j, row] = colors[j] # spy does a ordering I can't figure out. so transposing it here
end
end
end
function cache_and_compute_10_jacobians(adtype, f!::F, y, x, p) where {F}
prep = DifferentiationInterface.prepare_jacobian(f!, y, adtype, x, Constant(p))
J = DifferentiationInterface.jacobian(f!, y, prep, adtype, x, Constant(p))
for _ in 1:9
DifferentiationInterface.jacobian!(f!, y, J, prep, adtype, x, Constant(p))
end
return J
end
# XXX: run till 8 on CI
# Ns = [2^i for i in 1:8]
Ns = [2^i for i in 1:6]
adtypes = [
(
AutoSparse(AutoFiniteDiff(); sparsity_detector=TracerSparsityDetector()),
[:finitediff, :exact_sparse]
),
(
AutoSparse(
AutoPolyesterForwardDiff(; chunksize=8);
sparsity_detector=TracerSparsityDetector()
),
[:forwarddiff, :exact_sparse]
),
(
AutoSparse(
AutoEnzyme(; mode=Enzyme.Forward);
sparsity_detector=TracerSparsityDetector()
),
[:enzyme, :exact_sparse]
),
(
AutoSparse(
AutoFiniteDiff();
sparsity_detector=DenseSparsityDetector(AutoFiniteDiff(); atol=1e-5)
),
[:finitediff, :approx_sparse]
),
(
AutoSparse(
AutoPolyesterForwardDiff(; chunksize=8);
sparsity_detector=DenseSparsityDetector(
AutoPolyesterForwardDiff(; chunksize=8); atol=1e-5
)
),
[:polyester, :approx_sparse]
),
(
AutoSparse(
AutoEnzyme(; mode=Enzyme.Forward);
sparsity_detector=DenseSparsityDetector(
AutoEnzyme(; mode=Enzyme.Forward); atol=1e-5
)
),
[:enzyme, :approx_sparse]
),
(
AutoPolyesterForwardDiff(; chunksize=8),
[:polyester, :none]
),
]
times = Matrix{Float64}(undef, length(Ns), length(adtypes))
for (i, N) in enumerate(Ns)
@info N
test_problem = generate_brusselator_problem(N)
bruss_f! = test_problem.f
u0 = test_problem.u0
y = similar(u0)
for (j, (adtype, _)) in enumerate(adtypes)
times[i, j] = @belapsed begin
$(cache_and_compute_10_jacobians)(
$(adtype), $(bruss_f!), $(y), $(u0), $(test_problem.p)
)
end
@info times[i, j]
end
end
```
Plotting the results.
```julia
symbol_to_adname = Dict(
:finitediff => "Finite Diff",
:forwarddiff => "Forward Mode AD",
:polyester => "Threaded Forward Mode AD",
:enzyme => "Forward Mode AD (Enzyme)",
)
fig = begin
cycle = Cycle([:marker], covary=true)
plot_theme = Theme(Lines=(; cycle), Scatter=(; cycle))
with_theme(plot_theme) do
fig = Figure(; size=(1400, 1400 * 0.5))
ax = Axis(fig[1, 1]; title="Sparsity Pattern for 2D Brusselator Jacobian",
titlesize=22, titlegap=10,
xticksize=20, yticksize=20, xticklabelsize=20, yticklabelsize=20,
xtickwidth=2.5, ytickwidth=2.5, spinewidth=2.5, yreversed=true)
spy!(ax, J_; markersize=1, marker=:circle, framecolor=:lightgray,
colormap=:seaborn_bright)
ax = Axis(fig[1, 2]; title="Scaling of Sparse Jacobian Computation",
titlesize=22, titlegap=10, xscale=log10, yscale=log10,
xticksize=20, yticksize=20, xticklabelsize=20, yticklabelsize=20,
xtickwidth=2.5, ytickwidth=2.5, spinewidth=2.5,
xlabel=L"Input Dimension ($\mathbf{N}$)", ylabel=L"Time $\mathbf{(s)}$", xlabelsize=22,
ylabelsize=22, yaxisposition=:right)
colors = cgrad(:seaborn_bright, length(adtypes); categorical=true)
line_list = []
scatter_list = []
Ns_ = Ns .^ 2 .* 2
linestyles = [:solid, :solid, :solid, :dash, :dash, :dash, :dot, :dot]
for (i, times) in enumerate(eachcol(times))
l = lines!(Ns_, times; linewidth=5, color=colors[i], linestyle=linestyles[i])
push!(line_list, l)
sc = scatter!(Ns_, times; markersize=16, strokewidth=2, color=colors[i])
push!(scatter_list, sc)
end
tracer_idxs = [idx for idx in 1:length(adtypes) if :exact_sparse ∈ adtypes[idx][2]]
group_tracer = [
[
LineElement(;
color=line_list[idx].color,
linestyle=line_list[idx].linestyle,
linewidth=line_list[idx].linewidth,
),
MarkerElement(;
color=scatter_list[idx].color,
marker=scatter_list[idx].marker,
strokewidth=scatter_list[idx].strokewidth,
markersize=scatter_list[idx].markersize,
),
] for idx in tracer_idxs
]
local_sparse_idxs = [idx for idx in 1:length(adtypes) if :approx_sparse ∈ adtypes[idx][2]]
group_local_sparse = [
[
LineElement(;
color=line_list[idx].color,
linestyle=line_list[idx].linestyle,
linewidth=line_list[idx].linewidth,
),
MarkerElement(;
color=scatter_list[idx].color,
marker=scatter_list[idx].marker,
strokewidth=scatter_list[idx].strokewidth,
markersize=scatter_list[idx].markersize,
),
] for idx in local_sparse_idxs
]
non_sparse_idxs = [idx for idx in 1:length(adtypes) if :none ∈ adtypes[idx][2]]
group_nonsparse = [
[
LineElement(;
color=line_list[idx].color,
linestyle=line_list[idx].linestyle,
linewidth=line_list[idx].linewidth,
),
MarkerElement(;
color=scatter_list[idx].color,
marker=scatter_list[idx].marker,
strokewidth=scatter_list[idx].strokewidth,
markersize=scatter_list[idx].markersize,
),
] for idx in non_sparse_idxs
]
axislegend(
ax,
[group_tracer, group_local_sparse, group_nonsparse],
[
[symbol_to_adname[adtypes[idx][2][1]] for idx in tracer_idxs],
[symbol_to_adname[adtypes[idx][2][1]] for idx in local_sparse_idxs],
[symbol_to_adname[adtypes[idx][2][1]] for idx in non_sparse_idxs],
],
["Exact Sparsity", "Approx. Local Sparsity", "No Sparsity"];
position=:rb, framevisible=true, framewidth=2.5, titlesize=18,
labelsize=16, patchsize=(40.0f0, 20.0f0)
)
fig
end
end
```
```julia
save("brusselator_sparse_jacobian_scaling.svg", fig)
```
# Scaling with Problem Size
First, let us experiment the scaling of each algorithm with the problem size.
```julia
Ns = vcat(collect(2 .^ (2:7)), [150, 175, 200])
# XXX: PETSc just segfaults
solvers_scaling = [
(; pkg = :nonlinearsolve, sparsity = :none, name = "NR (No Sparsity)", alg = NewtonRaphson()),
# (; pkg = :nonlinearsolve, sparsity = :approx, name = "NR (Approx. Local Sparsity)", alg = NewtonRaphson()),
(; pkg = :nonlinearsolve, sparsity = :exact, name = "NR (Exact Sparsity)", alg = NewtonRaphson()),
(; pkg = :wrapper, sparsity = :none, name = "NR [NLsolve.jl]", alg = NLsolveJL(; method = :newton, autodiff = :forward)),
(; pkg = :wrapper, sparsity = :none, name = "Mod. NR [Sundials]", alg = KINSOL()),
# (; pkg = :wrapper, sparsity = :none, name = "NR [PETSc] (No Sparsity)", alg = PETScSNES(; snes_type = "newtonls")),
# (; pkg = :wrapper, sparsity = :none, name = "NR [PETSc] (Approx. Local Sparsity)", alg = PETScSNES(; snes_type = "newtonls")),
# (; pkg = :wrapper, sparsity = :none, name = "NR [PETSc] (Exact Sparsity)", alg = PETScSNES(; snes_type = "newtonls")),
(; pkg = :nonlinearsolve, sparsity = :none, name = "TR (No Sparsity)", alg = TrustRegion(; radius_update_scheme = RUS.NLsolve)),
# (; pkg = :nonlinearsolve, sparsity = :approx, name = "TR (Approx. Local Sparsity)", alg = TrustRegion(; radius_update_scheme = RUS.NLsolve)),
(; pkg = :nonlinearsolve, sparsity = :exact, name = "TR (Exact Sparsity)", alg = TrustRegion(; radius_update_scheme = RUS.NLsolve)),
(; pkg = :wrapper, sparsity = :none, name = "TR [NLsolve.jl]", alg = NLsolveJL(; autodiff = :forward)),
# (; pkg = :wrapper, sparsity = :none, name = "TR [PETSc] (No Sparsity)", alg = PETScSNES(; snes_type = "newtontr")),
# (; pkg = :wrapper, sparsity = :none, name = "TR [PETSc] (Approx. Local Sparsity)", alg = PETScSNES(; snes_type = "newtontr")),
# (; pkg = :wrapper, sparsity = :none, name = "TR [PETSc] (Exact Sparsity)", alg = PETScSNES(; snes_type = "newtontr")),
(; pkg = :wrapper, sparsity = :none, name = "Mod. Powell [MINPACK]", alg = CMINPACK()),
]
runtimes_scaling = fill(-1.0, length(solvers_scaling), length(Ns))
for (i, N) in enumerate(Ns)
prob_dense = generate_brusselator_problem(N)
prob_exact_sparse = generate_brusselator_problem(N;
sparsity = TracerSparsityDetector()
)
@info "Benchmarking N = $N"
for (j, solver) in enumerate(solvers_scaling)
ptype = solver.sparsity
alg = solver.alg
name = solver.name
prob = if ptype == :none
prob_dense
elseif ptype == :approx
# With Tracing based sparsity detection, we dont need this any more
error("Approximate Sparsity not implemented")
elseif ptype == :exact
prob_exact_sparse
end
if (j > 1 && runtimes_scaling[j - 1, i] == -1) ||
(alg isa CMINPACK && N > 32) ||
(alg isa KINSOL && N > 64) ||
(alg isa NLsolveJL && N > 64 && alg.method == :trust_region) ||
(alg isa GeneralizedFirstOrderAlgorithm && alg.name == :TrustRegion && N > 64) ||
(alg isa NLsolveJL && N > 128 && alg.method == :newton) ||
(alg isa GeneralizedFirstOrderAlgorithm && alg.name == :NewtonRaphson && N > 128 && ptype == :none) ||
(alg isa GeneralizedFirstOrderAlgorithm && alg.name == :NewtonRaphson && N > 150 && ptype == :approx)
# The last benchmark failed so skip this too
runtimes_scaling[j, i] = NaN
@warn "$(name): Would Have Timed out"
else
function benchmark_function()
termination_condition = (alg isa PETScSNES || alg isa KINSOL) ?
nothing :
NonlinearSolveBase.AbsNormTerminationMode(Base.Fix1(maximum, abs))
sol = solve(prob, alg; abstol=1e-6, reltol=1e-6, termination_condition)
runtimes_scaling[j, i] = @belapsed solve($prob, $alg; abstol=1e-6,
reltol=1e-6, termination_condition=$termination_condition)
@info "$(name): $(runtimes_scaling[j, i]) | $(norm(sol.resid, Inf)) | $(sol.retcode)"
end
timeout(benchmark_function, 600)
if runtimes_scaling[j, i] == -1
@warn "$(name): Timed out"
runtimes_scaling[j, i] = NaN
end
end
end
println()
end
```
Plot the results.
```julia
fig = begin
ASPECT_RATIO = 0.7
WIDTH = 1200
HEIGHT = round(Int, WIDTH * ASPECT_RATIO)
STROKEWIDTH = 2.5
cycle = Cycle([:marker], covary = true)
colors = cgrad(:seaborn_bright, length(solvers_scaling); categorical = true)
theme = Theme(Lines = (cycle = cycle,), Scatter = (cycle = cycle,))
LINESTYLES = Dict(
(:nonlinearsolve, :none) => :solid,
# (:nonlinearsolve, :approx) => :dash,
(:nonlinearsolve, :exact) => :dashdot,
# (:simplenonlinearsolve, :none) => :solid,
(:wrapper, :none) => :dot,
)
Ns_ = Ns .^ 2 .* 2
with_theme(theme) do
fig = Figure(; size = (WIDTH, HEIGHT))
ax = Axis(fig[1, 1:3], ylabel = L"Time ($s$)", xlabel = L"Problem Size ($N$)",
xscale = log10, yscale = log10, xlabelsize = 22, ylabelsize = 22,
xticklabelsize = 20, yticklabelsize = 20, xtickwidth = STROKEWIDTH,
ytickwidth = STROKEWIDTH, spinewidth = STROKEWIDTH)
idxs = get_ordering(runtimes_scaling)
ls, scs = [], []
for (i, solver) in zip(idxs, solvers_scaling[idxs])
linestyle = LINESTYLES[(solver.pkg, solver.sparsity)]
l = lines!(Ns_, runtimes_scaling[i, :]; linewidth = 5, color = colors[i],
linestyle)
sc = scatter!(Ns_, runtimes_scaling[i, :]; markersize = 16, strokewidth = 2,
color = colors[i])
push!(ls, l)
push!(scs, sc)
end
main_legend = [
[
LineElement(; color = ls[idx].color, linestyle = ls[idx].linestyle,
linewidth = ls[idx].linewidth),
MarkerElement(; color = scs[idx].color, marker = scs[idx].marker,
markersize = scs[idx].markersize, strokewidth = scs[idx].strokewidth)
]
for idx in 1:length(solvers_scaling)
]
sparsity_legend = [
LineElement(; linestyle = :solid, linewidth = 5),
# LineElement(; linestyle = :dash, linewidth = 5),
LineElement(; linestyle = :dashdot, linewidth = 5),
]
axislegend(ax, main_legend, [s.name for s in solvers_scaling[idxs]],
"Successful Solvers\n(Fastest to Slowest)";
framevisible=true, framewidth = STROKEWIDTH, orientation = :vertical,
titlesize = 20, nbanks = 1, labelsize = 16,
tellheight = true, tellwidth = false, patchsize = (60.0f0, 20.0f0),
position = :rb)
axislegend(ax, sparsity_legend,
[
"No Sparsity Detection",
# "Approx. Sparsity",
"Exact Sparsity"
],
"Sparsity Detection"; framevisible=true, framewidth = STROKEWIDTH,
orientation = :vertical, titlesize = 20, nbanks = 1, labelsize = 16,
tellheight = true, tellwidth = false, patchsize = (60.0f0, 20.0f0),
position = :lt)
fig[0, :] = Label(fig,
"Brusselator 2D: Scaling of First-Order Nonlinear Solvers with Problem Size",
fontsize = 24, tellwidth = false, font = :bold)
return fig
end
end
```
```julia
save("brusselator_scaling.svg", fig)
```
# Jacobian-Free Newton / TR Krylov Methods
In this section, we will benchmark jacobian-free nonlinear solvers with Krylov methods. We
will use preconditioning from `AlgebraicMultigrid.jl` and `IncompleteLU.jl`. Unfortunately,
our ability to use 3rd party software is limited here, since only `Sundials.jl` supports
jacobian-free methods via `:GMRES`.
```julia
using AlgebraicMultigrid, IncompleteLU
incompletelu(W, p = nothing) = ilu(W, τ = 50.0), LinearAlgebra.I
function algebraicmultigrid(W, p = nothing)
return aspreconditioner(ruge_stuben(convert(AbstractMatrix, W))), LinearAlgebra.I
end
function algebraicmultigrid_jacobi(W, p = nothing)
A = convert(AbstractMatrix, W)
Pl = AlgebraicMultigrid.aspreconditioner(AlgebraicMultigrid.ruge_stuben(
A, presmoother = AlgebraicMultigrid.Jacobi(rand(size(A, 1))),
postsmoother = AlgebraicMultigrid.Jacobi(rand(size(A, 1)))
))
return Pl, LinearAlgebra.I
end
Ns = 2 .^ (2:7)
solvers_scaling_jacobian_free = [
(; pkg = :nonlinearsolve, name = "Newton Krylov", alg = NewtonRaphson(; linsolve = KrylovJL_GMRES())),
(; pkg = :nonlinearsolve, name = "Newton Krylov (ILU)", alg = NewtonRaphson(; linsolve = KrylovJL_GMRES(; precs = incompletelu), concrete_jac = true)),
(; pkg = :nonlinearsolve, name = "Newton Krylov (AMG)", alg = NewtonRaphson(; linsolve = KrylovJL_GMRES(; precs = algebraicmultigrid), concrete_jac = true)),
(; pkg = :nonlinearsolve, name = "Newton Krylov (AMG Jacobi)", alg = NewtonRaphson(; linsolve = KrylovJL_GMRES(; precs = algebraicmultigrid_jacobi), concrete_jac = true)),
(; pkg = :nonlinearsolve, name = "TR Krylov", alg = TrustRegion(; linsolve = KrylovJL_GMRES())),
(; pkg = :nonlinearsolve, name = "TR Krylov (ILU)", alg = TrustRegion(; linsolve = KrylovJL_GMRES(; precs = incompletelu), concrete_jac = true)),
(; pkg = :nonlinearsolve, name = "TR Krylov (AMG)", alg = TrustRegion(; linsolve = KrylovJL_GMRES(; precs = algebraicmultigrid), concrete_jac = true)),
(; pkg = :nonlinearsolve, name = "TR Krylov (AMG Jacobi)", alg = TrustRegion(; linsolve = KrylovJL_GMRES(; precs = algebraicmultigrid_jacobi), concrete_jac = true)),
(; pkg = :wrapper, name = "Newton Krylov [Sundials]", alg = KINSOL(; linear_solver = :GMRES)),
]
runtimes_scaling = zeros(length(solvers_scaling_jacobian_free), length(Ns)) .- 1
for (i, N) in enumerate(Ns)
prob = generate_brusselator_problem(
N; sparsity = TracerSparsityDetector()
)
@info "Benchmarking N = $N"
for (j, solver) in enumerate(solvers_scaling_jacobian_free)
alg = solver.alg
name = solver.name
if (j > 1 && runtimes_scaling[j - 1, i] == -1)
# The last benchmark failed so skip this too
runtimes_scaling[j, i] = NaN
@warn "$(name): Would Have Timed out"
else
function benchmark_function()
termination_condition = (alg isa PETScSNES || alg isa KINSOL) ?
nothing :
NonlinearSolveBase.AbsNormTerminationMode(Base.Fix1(maximum, abs))
sol = solve(prob, alg; abstol=1e-6, reltol=1e-6,
linsolve_kwargs = (; abstol = 1e-9, reltol = 1e-9),
termination_condition)
if SciMLBase.successful_retcode(sol) || norm(sol.resid, Inf) ≤ 1e-5
runtimes_scaling[j, i] = @belapsed solve($prob, $alg; abstol=1e-6,
reltol=1e-6,
linsolve_kwargs = (; abstol = 1e-9, reltol = 1e-9),
termination_condition=$termination_condition)
else
runtimes_scaling[j, i] = NaN
end
@info "$(name): $(runtimes_scaling[j, i]) | $(norm(sol.resid, Inf)) | $(sol.retcode)"
end
timeout(benchmark_function, 600)
if runtimes_scaling[j, i] == -1
@warn "$(name): Timed out"
runtimes_scaling[j, i] = NaN
end
end
end
println()
end
```
Plot the results.
```julia
fig = begin
ASPECT_RATIO = 0.7
WIDTH = 1200
HEIGHT = round(Int, WIDTH * ASPECT_RATIO)
STROKEWIDTH = 2.5
cycle = Cycle([:marker], covary = true)
colors = cgrad(:seaborn_bright, length(solvers_scaling_jacobian_free); categorical = true)
theme = Theme(Lines = (cycle = cycle,), Scatter = (cycle = cycle,))
LINESTYLES = Dict(
(:nonlinearsolve, :none) => :solid,
(:nonlinearsolve, :amg) => :dot,
(:nonlinearsolve, :amg_jacobi) => :dash,
(:nonlinearsolve, :ilu) => :dashdot,
)
Ns_ = Ns .^ 2 .* 2
with_theme(theme) do
fig = Figure(; size = (WIDTH, HEIGHT))
ax = Axis(fig[1, 1:2], ylabel = L"Time ($s$)", xlabel = L"Problem Size ($N$)",
xscale = log10, yscale = log10, xlabelsize = 22, ylabelsize = 22,
xticklabelsize = 20, yticklabelsize = 20, xtickwidth = STROKEWIDTH,
ytickwidth = STROKEWIDTH, spinewidth = STROKEWIDTH)
idxs = get_ordering(runtimes_scaling)
ls, scs, labels = [], [], []
for (i, solver) in zip(idxs, solvers_scaling_jacobian_free[idxs])
all(isnan, runtimes_scaling[i, :]) && continue
precon = occursin("AMG Jacobi", solver.name) ? :amg_jacobi : occursin("AMG", solver.name) ? :amg : occursin("ILU", solver.name) ? :ilu : :none
linestyle = LINESTYLES[(solver.pkg, precon)]
l = lines!(Ns_, runtimes_scaling[i, :]; linewidth = 5, color = colors[i],
linestyle)
sc = scatter!(Ns_, runtimes_scaling[i, :]; markersize = 16, strokewidth = 2,
color = colors[i])
push!(ls, l)
push!(scs, sc)
push!(labels, solver.name)
end
axislegend(ax, [[l, sc] for (l, sc) in zip(ls, scs)], labels,
"Successful Solvers\n(Fastest to Slowest)";
framevisible=true, framewidth = STROKEWIDTH, orientation = :vertical,
titlesize = 20, labelsize = 16, position = :rb,
tellheight = true, tellwidth = false, patchsize = (40.0f0, 20.0f0))
axislegend(ax, [
LineElement(; linestyle = :solid, linewidth = 5),
LineElement(; linestyle = :dot, linewidth = 5),
LineElement(; linestyle = :dash, linewidth = 5),
LineElement(; linestyle = :dashdot, linewidth = 5),
], ["No Preconditioning", "AMG", "AMG Jacobi", "Incomplete LU"],
"Preconditioning"; framevisible=true, framewidth = STROKEWIDTH,
orientation = :vertical, titlesize = 20, labelsize = 16,
tellheight = true, tellwidth = true, patchsize = (40.0f0, 20.0f0),
position = :lt)
fig[0, :] = Label(fig,
"Brusselator 2D: Scaling of Jacobian-Free Nonlinear Solvers with Problem Size",
fontsize = 24, tellwidth = false, font = :bold)
return fig
end
end
```
```julia
save("brusselator_krylov_methods_scaling.svg", fig)
```
# Work-Precision Diagram
In this section, we will generate the work-precision of the solvers. All solvers that can
exploit sparsity will automatically do so.
```julia
solvers_all = [
(; pkg = :nonlinearsolve, name = "Default PolyAlg", solver = Dict(:alg => FastShortcutNonlinearPolyalg())),
(; pkg = :nonlinearsolve, name = "RobustMultiNewton (GMRES)", solver = Dict(:alg => RobustMultiNewton(; linsolve = KrylovJL_GMRES()))),
(; pkg = :nonlinearsolve, name = "Newton Raphson", solver = Dict(:alg => NewtonRaphson(; linsolve = nothing))),
(; pkg = :nonlinearsolve, name = "Newton Krylov", solver = Dict(:alg => NewtonRaphson(; linsolve = KrylovJL_GMRES()))),
(; pkg = :nonlinearsolve, name = "Trust Region", solver = Dict(:alg => TrustRegion())),
(; pkg = :nonlinearsolve, name = "TR Krylov", solver = Dict(:alg => TrustRegion(; linsolve = KrylovJL_GMRES()))),
(; pkg = :wrapper, name = "NR [NLsolve.jl]", solver = Dict(:alg => NLsolveJL(; method = :newton, autodiff = :forward))),
(; pkg = :wrapper, name = "TR [NLsolve.jl]", solver = Dict(:alg => NLsolveJL(; autodiff = :forward))),
(; pkg = :wrapper, name = "NR [Sundials]", solver = Dict(:alg => KINSOL())),
(; pkg = :wrapper, name = "Newton Krylov [Sundials]", solver = Dict(:alg => KINSOL(; linear_solver = :GMRES))),
(; pkg = :wrapper, name = "Mod. Powell [MINPACK]", solver = Dict(:alg => CMINPACK())),
];
```
```julia
prob_wpd = generate_brusselator_problem(32; sparsity = TracerSparsityDetector())
abstols = 1.0 ./ 10 .^ (2:10)
reltols = 1.0 ./ 10 .^ (2:10)
function check_solver(prob, solver)
try
sol = solve(prob, solver.solver[:alg]; abstol = 1e-4, reltol = 1e-4,
maxiters = 10000)
err = norm(sol.resid, Inf)
if !SciMLBase.successful_retcode(sol.retcode)
Base.printstyled("[Warn] Solver $(solver.name) returned retcode $(sol.retcode) with an residual norm = $(norm(sol.resid)).\n";
color = :red)
return false
elseif err > 1e3
Base.printstyled("[Warn] Solver $(solver.name) had a very large residual (norm = $(norm(sol.resid))).\n";
color = :red)
return false
elseif isinf(err) || isnan(err)
Base.printstyled("[Warn] Solver $(solver.name) had a residual of $(err).\n";
color = :red)
return false
end
Base.printstyled("[Info] Solver $(solver.name) successfully solved the problem (norm = $(norm(sol.resid))).\n";
color = :green)
catch e
Base.printstyled("[Warn] Solver $(solver.name) threw an error: $e.\n"; color = :red)
return false
end
return true
end
function generate_wpset(prob, solvers)
# Finds the solvers that can solve the problem
successful_solvers = filter(solver -> check_solver(prob, solver), solvers)
return WorkPrecisionSet(prob, abstols, reltols,
getfield.(successful_solvers, :solver);
names = getfield.(successful_solvers, :name), numruns = 10, error_estimate = :l∞,
maxiters = 1000, verbose = true), successful_solvers
end
```
```julia
wp_set, successful_solvers = generate_wpset(prob_wpd, solvers_all);
```
Plotting the Work-Precision Diagram.
```julia
fig = begin
LINESTYLES = Dict(:nonlinearsolve => :solid, :simplenonlinearsolve => :dash,
:wrapper => :dot)
ASPECT_RATIO = 0.7
WIDTH = 1200
HEIGHT = round(Int, WIDTH * ASPECT_RATIO)
STROKEWIDTH = 2.5
colors = cgrad(:seaborn_bright, length(successful_solvers); categorical = true)
cycle = Cycle([:marker], covary = true)
plot_theme = Theme(Lines = (; cycle), Scatter = (; cycle))
with_theme(plot_theme) do
fig = Figure(; size = (WIDTH, HEIGHT))
# `textbf` doesn't work
ax = Axis(fig[1, 1], ylabel = L"Time $\mathbf{(s)}$",
xlabelsize = 22, ylabelsize = 22,
xlabel = L"Error: $\mathbf{||f(u^\ast)||_\infty}$",
xscale = log10, yscale = log10, xtickwidth = STROKEWIDTH,
ytickwidth = STROKEWIDTH, spinewidth = STROKEWIDTH,
xticklabelsize = 20, yticklabelsize = 20)
idxs = sortperm(median.(getfield.(wp_set.wps, :times)))
ls, scs = [], []
for (i, (wp, solver)) in enumerate(zip(wp_set.wps[idxs], successful_solvers[idxs]))
(; name, times, errors) = wp
errors = [err.l∞ for err in errors]
l = lines!(ax, errors, times; linestyle = LINESTYLES[solver.pkg], label = name,
linewidth = 5, color = colors[i])
sc = scatter!(ax, errors, times; label = name, markersize = 16, strokewidth = 2,
color = colors[i])
push!(ls, l)
push!(scs, sc)
end
xlims!(ax; high=1)
ylims!(ax; low=5e-3)
axislegend(ax, [[l, sc] for (l, sc) in zip(ls, scs)],
[solver.name for solver in successful_solvers[idxs]], "Successful Solvers";
framevisible=true, framewidth = STROKEWIDTH, position = :rb,
titlesize = 20, labelsize = 16, patchsize = (40.0f0, 20.0f0))
fig[0, :] = Label(fig, "Brusselator Steady State PDE: Work Precision Diagram",
fontsize = 24, tellwidth = false, font = :bold)
fig
end
end
```
```julia
save("brusselator_wpd.svg", fig)
```
```julia, echo = false
using SciMLBenchmarks
SciMLBenchmarks.bench_footer(WEAVE_ARGS[:folder],WEAVE_ARGS[:file])
```