-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathbruss_krylov.jmd
311 lines (256 loc) · 12.9 KB
/
bruss_krylov.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
---
title: Ill-Conditioned Nonlinear System Work-Precision Diagrams (Krylov Methods)
author: Avik Pal
---
# Setup
Fetch required packages
```julia
using NonlinearSolve, LinearAlgebra, SparseArrays, DiffEqDevTools,
CairoMakie, Symbolics, BenchmarkTools, PolyesterForwardDiff, LinearSolve, Sundials,
Enzyme, SparseConnectivityTracer, DifferentiationInterface, SparseMatrixColorings
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
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
```
# Brusselator
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
```
# 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)
Dinv = 1.0 ./ diag(A) # PETSc-style Jacobi: inverse of diagonal
smoother = AlgebraicMultigrid.Jacobi(Dinv)
Pl = aspreconditioner(AlgebraicMultigrid.ruge_stuben(
A,
presmoother = smoother,
postsmoother = smoother
))
return Pl, LinearAlgebra.I
end
Ns = 2 .^ (2:7)
krylov_dim = 1000
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, maxsetupcalls=1, krylov_dim)),
(; pkg = :wrapper, name = "Newton Krylov [PETSc]", alg = PETScSNES(; snes_type = "newtonls", snes_linesearch_type = "basic", ksp_type = "gmres", snes_mf = true, ksp_gmres_restart = krylov_dim)),
(; pkg = :wrapper, name = "Newton Krylov (ILU) [PETSc]", alg = PETScSNES(; snes_type = "newtonls", snes_linesearch_type = "basic", ksp_type = "gmres", pc_type = "ilu", ksp_gmres_restart = krylov_dim, pc_factor_levels = 0, pc_factor_drop_tolerance = 50.0)),
(; pkg = :wrapper, name = "Newton Krylov (AMG) [PETSc]", alg = PETScSNES(; snes_type = "newtonls", snes_linesearch_type = "basic", ksp_type = "gmres", pc_type = "gamg", ksp_gmres_restart = krylov_dim)),
(; pkg = :wrapper, name = "Newton Krylov (AMG Jacobi) [PETSc]", alg = PETScSNES(; snes_type = "newtonls", snes_linesearch_type = "basic", ksp_type = "gmres", pc_type = "gamg", mg_levels_ksp_type = "richardson", mg_levels_pc_type = "jacobi", ksp_gmres_restart = krylov_dim)),
(; pkg = :wrapper, name = "TR Krylov (Not Matrix Free) [PETSc]", alg = PETScSNES(; snes_type = "newtontr", ksp_type = "gmres", ksp_gmres_restart = krylov_dim)),
(; pkg = :wrapper, name = "TR Krylov (ILU) [PETSc]", alg = PETScSNES(; snes_type = "newtontr", ksp_type = "gmres", pc_type = "ilu", ksp_gmres_restart = krylov_dim, pc_factor_levels = 0, pc_factor_drop_tolerance = 50.0)),
(; pkg = :wrapper, name = "TR Krylov (AMG) [PETSc]", alg = PETScSNES(; snes_type = "newtontr", ksp_type = "gmres", pc_type = "gamg", ksp_gmres_restart = krylov_dim)),
(; pkg = :wrapper, name = "TR Krylov (AMG Jacobi) [PETSc]", alg = PETScSNES(; snes_type = "newtontr", ksp_type = "gmres", pc_type = "gamg", mg_levels_ksp_type = "richardson", mg_levels_pc_type = "jacobi", ksp_gmres_restart = krylov_dim)),
]
gc_disabled = false
runtimes_scaling = fill(-1.0, length(solvers_scaling_jacobian_free), length(Ns))
for (j, solver) in enumerate(solvers_scaling_jacobian_free)
alg = solver.alg
name = solver.name
if !gc_disabled && alg isa PETScSNES
GC.enable(false)
global gc_disabled = true
@info "Disabling GC for $(name)"
end
for (i, N) in enumerate(Ns)
prob = generate_brusselator_problem(N; sparsity = TracerSparsityDetector())
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))
# PETSc doesn't converge properly
tol = alg isa PETScSNES ? 1e-6 : 1e-4
sol = solve(prob, alg; abstol=tol, reltol=tol,
linsolve_kwargs = (; abstol = 1e-8, reltol = 1e-8),
termination_condition)
if SciMLBase.successful_retcode(sol) || norm(sol.resid, Inf) ≤ 1e-4
runtimes_scaling[j, i] = @belapsed solve($prob, $alg;
abstol=$tol, reltol=$tol,
linsolve_kwargs = (; abstol = 1e-8, reltol = 1e-8),
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
successful_solvers = map(x -> any(isfinite, x), eachrow(runtimes_scaling))
solvers_scaling_jacobian_free = solvers_scaling_jacobian_free[successful_solvers]
runtimes_scaling = runtimes_scaling[successful_solvers, :]
cycle = Cycle([:marker], covary = true)
colors = cgrad(:tableau_20, length(solvers_scaling_jacobian_free); categorical = true)
theme = Theme(Lines = (cycle = cycle,), Scatter = (cycle = cycle,))
LINESTYLES = Dict(
:none => :solid,
:amg => :dot,
:amg_jacobi => :dash,
: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 = log2, yscale = log2, 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[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";
framevisible=true, framewidth = STROKEWIDTH, orientation = :vertical,
titlesize = 20, labelsize = 16, position = :lt, nbanks = 2,
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 = :rb)
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)
```