Skip to content

Commit

Permalink
Merge pull request #4 from j-fu/jf/precsisfresh
Browse files Browse the repository at this point in the history
Add test for "reuse_precs=true" + fix logic
  • Loading branch information
oscardssmith authored Oct 18, 2024
2 parents 536f8ec + 2d09816 commit 8d33f02
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function SciMLBase.reinit!(cache::LinearCache;


isfresh = !isnothing(A)
precsisfresh = reuse_precs || isfresh || !isnothing(p)
precsisfresh = !reuse_precs && (isfresh || !isnothing(p))
isfresh |= cache.isfresh
precsisfresh |= cache.precsisfresh

Expand All @@ -246,7 +246,7 @@ function SciMLBase.reinit!(cache::LinearCache;
cache.Pl = Pl
cache.Pr = Pr
cache.isfresh = true
cache.isfresh = true
cache.precsisfresh = precsisfresh
end
end

Expand Down
24 changes: 24 additions & 0 deletions test/basictests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,30 @@ end
end
end

@testset "Reuse precs" begin
num_precs_calls = 0

function countingprecs(A, p = nothing)
num_precs_calls += 1
(BlockJacobiPreconditioner(A, 2), I)
end

n = 10
A = spdiagm(-1 => -ones(n - 1), 0 => fill(10.0, n), 1 => -ones(n - 1))
b = rand(n)
p = LinearProblem(A, b)
x0 = solve(p, KrylovJL_CG(precs = countingprecs, ldiv = false))
cache = x0.cache
x0 = copy(x0)
for i in 4:(n - 3)
A[i, i + 3] -= 1.0e-4
A[i - 3, i] -= 1.0e-4
end
LinearSolve.reinit!(cache; A, reuse_precs = true)
x1 = copy(solve!(cache))
@test all(x0 .< x1) && num_precs_calls == 1
end

if VERSION >= v"1.9-"
@testset "IterativeSolversJL" begin
kwargs = (; gmres_restart = 5)
Expand Down

0 comments on commit 8d33f02

Please sign in to comment.