Skip to content

Commit 89b2781

Browse files
committed
Add precompile for NLLS
1 parent 40a9ded commit 89b2781

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

src/NonlinearSolve.jl

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ if isdefined(Base, :Experimental) && isdefined(Base.Experimental, Symbol("@max_m
55
end
66

77
import Reexport: @reexport
8-
import PrecompileTools
8+
import PrecompileTools: @recompile_invalidations, @compile_workload, @setup_workload
99

10-
PrecompileTools.@recompile_invalidations begin
10+
@recompile_invalidations begin
1111
using DiffEqBase, LinearAlgebra, LinearSolve, SparseArrays, SparseDiffTools
1212
using FastBroadcast: @..
1313
import ArrayInterface: restructure
@@ -95,17 +95,49 @@ include("jacobian.jl")
9595
include("ad.jl")
9696
include("default.jl")
9797

98-
PrecompileTools.@compile_workload begin
99-
for T in (Float32, Float64)
100-
probs = (NonlinearProblem{false}((u, p) -> u .* u .- p, T(0.1), T(2)),
101-
NonlinearProblem{false}((u, p) -> u .* u .- p, T[0.1], T[2]),
102-
NonlinearProblem{true}((du, u, p) -> du .= u .* u .- p, T[0.1], T[2]))
98+
@setup_workload begin
99+
nlfuncs = ((NonlinearFunction{false}((u, p) -> u .* u .- p), 0.1),
100+
(NonlinearFunction{false}((u, p) -> u .* u .- p), [0.1]),
101+
(NonlinearFunction{true}((du, u, p) -> du .= u .* u .- p), [0.1]))
102+
probs_nls = NonlinearProblem[]
103+
for T in (Float32, Float64), (fn, u0) in nlfuncs
104+
push!(probs_nls, NonlinearProblem(fn, T.(u0), T(2)))
105+
end
103106

104-
precompile_algs = (NewtonRaphson(), TrustRegion(), LevenbergMarquardt(),
105-
PseudoTransient(), GeneralBroyden(), GeneralKlement(), DFSane(), nothing)
107+
nls_algs = (NewtonRaphson(), TrustRegion(), LevenbergMarquardt(), PseudoTransient(),
108+
GeneralBroyden(), GeneralKlement(), DFSane(), nothing)
109+
110+
probs_nlls = NonlinearLeastSquaresProblem[]
111+
nlfuncs = ((NonlinearFunction{false}((u, p) -> (u .^ 2 .- p)[1:1]), [0.1, 0.0]),
112+
(NonlinearFunction{false}((u, p) -> vcat(u .* u .- p, u .* u .- p)), [0.1, 0.1]),
113+
(NonlinearFunction{true}((du, u, p) -> du[1] = u[1] * u[1] - p,
114+
resid_prototype = zeros(1)), [0.1, 0.0]),
115+
(NonlinearFunction{true}((du, u, p) -> du .= vcat(u .* u .- p, u .* u .- p),
116+
resid_prototype = zeros(4)), [0.1, 0.1]))
117+
for (fn, u0) in nlfuncs
118+
push!(probs_nlls, NonlinearLeastSquaresProblem(fn, u0, 2.0))
119+
end
120+
nlfuncs = ((NonlinearFunction{false}((u, p) -> (u .^ 2 .- p)[1:1]), Float32[0.1, 0.0]),
121+
(NonlinearFunction{false}((u, p) -> vcat(u .* u .- p, u .* u .- p)),
122+
Float32[0.1, 0.1]),
123+
(NonlinearFunction{true}((du, u, p) -> du[1] = u[1] * u[1] - p,
124+
resid_prototype = zeros(Float32, 1)), Float32[0.1, 0.0]),
125+
(NonlinearFunction{true}((du, u, p) -> du .= vcat(u .* u .- p, u .* u .- p),
126+
resid_prototype = zeros(Float32, 4)), Float32[0.1, 0.1]))
127+
for (fn, u0) in nlfuncs
128+
push!(probs_nlls, NonlinearLeastSquaresProblem(fn, u0, 2.0f0))
129+
end
106130

107-
for prob in probs, alg in precompile_algs
108-
solve(prob, alg, abstol = T(1e-2))
131+
nlls_algs = (LevenbergMarquardt(), GaussNewton(),
132+
LevenbergMarquardt(; linsolve = LUFactorization()),
133+
GaussNewton(; linsolve = LUFactorization()))
134+
135+
@compile_workload begin
136+
for prob in probs_nls, alg in nls_algs
137+
solve(prob, alg, abstol = 1e-2)
138+
end
139+
for prob in probs_nlls, alg in nlls_algs
140+
solve(prob, alg, abstol = 1e-2)
109141
end
110142
end
111143
end

test/23_test_problems.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ end
6868
broken_tests = Dict(alg => Int[] for alg in alg_ops)
6969
broken_tests[alg_ops[1]] = [3, 6, 17, 21]
7070
broken_tests[alg_ops[2]] = [3, 6, 17, 21]
71-
broken_tests[alg_ops[3]] = [6, 11, 21]
71+
broken_tests[alg_ops[3]] = [6, 11, 17, 21]
7272

7373
test_on_library(problems, dicts, alg_ops, broken_tests)
7474
end

test/basictests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ end
390390
@testset "Keyword Arguments" begin
391391
damping_initial = [0.5, 2.0, 5.0]
392392
damping_increase_factor = [1.5, 3.0, 10.0]
393-
damping_decrease_factor = Float64[2, 5, 12]
393+
damping_decrease_factor = Float64[2, 5, 10.0]
394394
finite_diff_step_geodesic = [0.02, 0.2, 0.3]
395395
α_geodesic = [0.6, 0.8, 0.9]
396396
b_uphill = Float64[0, 1, 2]
@@ -408,7 +408,7 @@ end
408408
b_uphill = options[6], min_damping_D = options[7])
409409

410410
probN = NonlinearProblem{false}(quadratic_f, [1.0, 1.0], 2.0)
411-
sol = solve(probN, alg, abstol = 1e-12)
411+
sol = solve(probN, alg; abstol = 1e-13, maxiters = 10000)
412412
@test all(abs.(quadratic_f(sol.u, 2.0)) .< 1e-10)
413413
end
414414
end

0 commit comments

Comments
 (0)