|
| 1 | +using LinearSolve, LinearAlgebra, StableRNGs, Test |
| 2 | + |
| 3 | +# Warm dense-LU refactorizations (`cache.A = X; solve!(cache)`) with |
| 4 | +# `alias_A = true` must reuse the cacheval's pivot buffer instead of allocating |
| 5 | +# a fresh `ipiv`/`LU` wrapper through `lu!` on every call. The LAPACK |
| 6 | +# (`getrf!`) reuse path needs Julia >= 1.11; the generic-kernel path (NoPivot / |
| 7 | +# non-BLAS eltypes) is allocation-free on every supported version. |
| 8 | +const BLAS_IPIV_REUSE = VERSION >= v"1.11" |
| 9 | + |
| 10 | +rng = StableRNG(42) |
| 11 | + |
| 12 | +function refactor_solve!(cache, Awork, A) |
| 13 | + copyto!(Awork, A) |
| 14 | + cache.A = Awork |
| 15 | + return solve!(cache) |
| 16 | +end |
| 17 | + |
| 18 | +@testset "dense LU refactorization reuses pivot buffers" begin |
| 19 | + n = 51 |
| 20 | + A1 = rand(rng, n, n) + n * I |
| 21 | + A2 = rand(rng, n, n) + n * I |
| 22 | + b = rand(rng, n) |
| 23 | + |
| 24 | + # `maxalloc` is the per-refactorization allocation ceiling: 0 wherever the |
| 25 | + # reuse path is active. On Julia 1.10 the BLAS (`getrf!`-with-`ipiv`) |
| 26 | + # method does not exist, so those paths keep the allocating `lu!` (no |
| 27 | + # assertion); the generic NoPivot kernel still reuses the pivot there, but |
| 28 | + # 1.10's compiler does not always elide the small `LU`/solution wrapper |
| 29 | + # constructions that 1.11+ removes, hence the small nonzero ceiling. |
| 30 | + @testset "$(name)" for (name, alg, maxalloc) in ( |
| 31 | + ("LUFactorization RowMaximum", LUFactorization(), BLAS_IPIV_REUSE ? 0 : nothing), |
| 32 | + ( |
| 33 | + "LUFactorization NoPivot", LUFactorization(pivot = NoPivot()), |
| 34 | + VERSION >= v"1.11" ? 0 : 64, |
| 35 | + ), |
| 36 | + ("default algorithm", nothing, BLAS_IPIV_REUSE ? 0 : nothing), |
| 37 | + ) |
| 38 | + cache = init( |
| 39 | + LinearProblem(copy(A1), copy(b)), alg; |
| 40 | + alias = LinearAliasSpecifier(alias_A = true) |
| 41 | + ) |
| 42 | + Awork = cache.A |
| 43 | + @test solve!(cache).u ≈ A1 \ b |
| 44 | + |
| 45 | + # correctness across repeated refactorize+solve cycles |
| 46 | + for Ak in (A2, A1, A2) |
| 47 | + sol = refactor_solve!(cache, Awork, Ak) |
| 48 | + @test sol.retcode == ReturnCode.Success |
| 49 | + @test sol.u ≈ Ak \ b |
| 50 | + end |
| 51 | + |
| 52 | + # post-warmup refactorization must not allocate beyond the ceiling, |
| 53 | + # and the cached pivot vector must be reused, not replaced |
| 54 | + refactor_solve!(cache, Awork, A1) |
| 55 | + ipiv_before = alg isa LUFactorization ? cache.cacheval.ipiv : nothing |
| 56 | + alloc = @allocated refactor_solve!(cache, Awork, A2) |
| 57 | + if maxalloc !== nothing |
| 58 | + @test alloc <= maxalloc |
| 59 | + if alg isa LUFactorization |
| 60 | + @test cache.cacheval.ipiv === ipiv_before |
| 61 | + end |
| 62 | + end |
| 63 | + @test cache.u ≈ A2 \ b |
| 64 | + end |
| 65 | +end |
| 66 | + |
| 67 | +@testset "singular refactorization reports Failure without throwing" begin |
| 68 | + n = 8 |
| 69 | + Agood = rand(rng, n, n) + n * I |
| 70 | + Asing = copy(Agood) |
| 71 | + Asing[:, 1] .= 0 # exactly rank-deficient, singular under any pivoting |
| 72 | + b = rand(rng, n) |
| 73 | + |
| 74 | + @testset "$(name)" for (name, alg) in ( |
| 75 | + ("RowMaximum", LUFactorization()), |
| 76 | + ("NoPivot", LUFactorization(pivot = NoPivot())), |
| 77 | + ) |
| 78 | + cache = init( |
| 79 | + LinearProblem(copy(Agood), copy(b)), alg; |
| 80 | + alias = LinearAliasSpecifier(alias_A = true) |
| 81 | + ) |
| 82 | + Awork = cache.A |
| 83 | + @test solve!(cache).retcode == ReturnCode.Success |
| 84 | + |
| 85 | + # warm cycle so the singular refactorization exercises the reuse path |
| 86 | + refactor_solve!(cache, Awork, Agood) |
| 87 | + @test refactor_solve!(cache, Awork, Asing).retcode == ReturnCode.Failure |
| 88 | + |
| 89 | + # the cache recovers on the next nonsingular refactorization |
| 90 | + sol = refactor_solve!(cache, Awork, Agood) |
| 91 | + @test sol.retcode == ReturnCode.Success |
| 92 | + @test sol.u ≈ Agood \ b |
| 93 | + end |
| 94 | +end |
| 95 | + |
| 96 | +@testset "default algorithm QR safety fallback survives warm singular refactorization" begin |
| 97 | + n = 8 |
| 98 | + Agood = rand(rng, n, n) + n * I |
| 99 | + Asing = copy(Agood) |
| 100 | + Asing[:, 1] .= 0 |
| 101 | + b = rand(rng, n) |
| 102 | + |
| 103 | + cache = init( |
| 104 | + LinearProblem(copy(Agood), copy(b)), nothing; |
| 105 | + alias = LinearAliasSpecifier(alias_A = true) |
| 106 | + ) |
| 107 | + Awork = cache.A |
| 108 | + @test solve!(cache).retcode == ReturnCode.Success |
| 109 | + refactor_solve!(cache, Awork, Agood) |
| 110 | + |
| 111 | + # LU fails on the singular A, so the default algorithm's safetyfallback |
| 112 | + # rescues through column-pivoted QR (least-squares) instead of erroring |
| 113 | + sol = refactor_solve!(cache, Awork, Asing) |
| 114 | + @test sol.retcode == ReturnCode.Success |
| 115 | + @test Asing * sol.u ≈ Asing * (qr(Asing, ColumnNorm()) \ b) |
| 116 | + |
| 117 | + sol = refactor_solve!(cache, Awork, Agood) |
| 118 | + @test sol.retcode == ReturnCode.Success |
| 119 | + @test sol.u ≈ Agood \ b |
| 120 | +end |
| 121 | + |
| 122 | +@testset "generic (non-BLAS) eltype reuses the cached pivot" begin |
| 123 | + n = 6 |
| 124 | + A1 = big.(rand(rng, n, n)) + n * I |
| 125 | + A2 = big.(rand(rng, n, n)) + n * I |
| 126 | + b = big.(rand(rng, n)) |
| 127 | + |
| 128 | + cache = init( |
| 129 | + LinearProblem(copy(A1), copy(b)), LUFactorization(); |
| 130 | + alias = LinearAliasSpecifier(alias_A = true) |
| 131 | + ) |
| 132 | + Awork = cache.A |
| 133 | + @test solve!(cache).u ≈ A1 \ b |
| 134 | + for Ak in (A2, A1, A2) |
| 135 | + sol = refactor_solve!(cache, Awork, Ak) |
| 136 | + @test sol.retcode == ReturnCode.Success |
| 137 | + @test sol.u ≈ Ak \ b |
| 138 | + end |
| 139 | +end |
| 140 | + |
| 141 | +@testset "size change between refactorizations reallocates the pivot" begin |
| 142 | + n1, n2 = 5, 9 |
| 143 | + A1 = rand(rng, n1, n1) + n1 * I |
| 144 | + b1 = rand(rng, n1) |
| 145 | + |
| 146 | + cache = init( |
| 147 | + LinearProblem(copy(A1), copy(b1)), LUFactorization(); |
| 148 | + alias = LinearAliasSpecifier(alias_A = true) |
| 149 | + ) |
| 150 | + @test solve!(cache).u ≈ A1 \ b1 |
| 151 | + |
| 152 | + resize!(cache, n2) |
| 153 | + A2 = rand(rng, n2, n2) + n2 * I |
| 154 | + b2 = rand(rng, n2) |
| 155 | + cache.A = copy(A2) |
| 156 | + cache.b = copy(b2) |
| 157 | + cache.u = zeros(n2) |
| 158 | + @test solve!(cache).u ≈ A2 \ b2 |
| 159 | + |
| 160 | + # warm cycles at the new size are allocation-free again |
| 161 | + Awork = cache.A |
| 162 | + refactor_solve!(cache, Awork, A2) |
| 163 | + refactor_solve!(cache, Awork, A2) |
| 164 | + alloc = @allocated refactor_solve!(cache, Awork, A2) |
| 165 | + if BLAS_IPIV_REUSE |
| 166 | + @test alloc == 0 |
| 167 | + end |
| 168 | + @test cache.u ≈ A2 \ b2 |
| 169 | +end |
0 commit comments