Skip to content

Commit 8d33f02

Browse files
authored
Merge pull request #4 from j-fu/jf/precsisfresh
Add test for "reuse_precs=true" + fix logic
2 parents 536f8ec + 2d09816 commit 8d33f02

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/common.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ function SciMLBase.reinit!(cache::LinearCache;
223223

224224

225225
isfresh = !isnothing(A)
226-
precsisfresh = reuse_precs || isfresh || !isnothing(p)
226+
precsisfresh = !reuse_precs && (isfresh || !isnothing(p))
227227
isfresh |= cache.isfresh
228228
precsisfresh |= cache.precsisfresh
229229

@@ -246,7 +246,7 @@ function SciMLBase.reinit!(cache::LinearCache;
246246
cache.Pl = Pl
247247
cache.Pr = Pr
248248
cache.isfresh = true
249-
cache.isfresh = true
249+
cache.precsisfresh = precsisfresh
250250
end
251251
end
252252

test/basictests.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,30 @@ end
284284
end
285285
end
286286

287+
@testset "Reuse precs" begin
288+
num_precs_calls = 0
289+
290+
function countingprecs(A, p = nothing)
291+
num_precs_calls += 1
292+
(BlockJacobiPreconditioner(A, 2), I)
293+
end
294+
295+
n = 10
296+
A = spdiagm(-1 => -ones(n - 1), 0 => fill(10.0, n), 1 => -ones(n - 1))
297+
b = rand(n)
298+
p = LinearProblem(A, b)
299+
x0 = solve(p, KrylovJL_CG(precs = countingprecs, ldiv = false))
300+
cache = x0.cache
301+
x0 = copy(x0)
302+
for i in 4:(n - 3)
303+
A[i, i + 3] -= 1.0e-4
304+
A[i - 3, i] -= 1.0e-4
305+
end
306+
LinearSolve.reinit!(cache; A, reuse_precs = true)
307+
x1 = copy(solve!(cache))
308+
@test all(x0 .< x1) && num_precs_calls == 1
309+
end
310+
287311
if VERSION >= v"1.9-"
288312
@testset "IterativeSolversJL" begin
289313
kwargs = (; gmres_restart = 5)

0 commit comments

Comments
 (0)