Skip to content

Commit eddbe86

Browse files
Fix type instability in LinearSolve QR factorization fallback
Requires SciML/LinearSolve.jl#617 This now uses the DefaultLinearSolver sum type in order to do the QR factorization fallback. This removes the `additional_lincache` which was `::Any`. This fixes a core type instability on the `LinearSolveResult` that was causing a pretty decently large slow down to the Newton methods. This also makes it so if the user specifically says they want LU, it don't do anything different.
1 parent b559071 commit eddbe86

File tree

2 files changed

+2
-45
lines changed

2 files changed

+2
-45
lines changed

lib/NonlinearSolveBase/ext/NonlinearSolveBaseLinearSolveExt.jl

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,53 +21,11 @@ function (cache::LinearSolveJLCache)(;
2121
linu !== nothing && NonlinearSolveBase.set_lincache_u!(cache, linu)
2222

2323
linres = solve!(cache.lincache)
24-
cache.lincache = linres.cache
25-
# Unfortunately LinearSolve.jl doesn't have the most uniform ReturnCode handling
2624
if linres.retcode === ReturnCode.Failure
27-
structured_mat = ArrayInterface.isstructured(cache.lincache.A)
28-
is_gpuarray = ArrayInterface.device(cache.lincache.A) isa ArrayInterface.GPU
29-
30-
if !(cache.linsolve isa QRFactorization{ColumnNorm}) && !is_gpuarray &&
31-
!structured_mat
32-
if verbose
33-
@warn "Potential Rank Deficient Matrix Detected. Attempting to solve using \
34-
Pivoted QR Factorization."
35-
end
36-
@assert (A !== nothing)&&(b !== nothing) "This case is not yet supported. \
37-
Please open an issue at \
38-
https://github.com/SciML/NonlinearSolve.jl"
39-
if cache.additional_lincache === nothing # First time
40-
linprob = LinearProblem(A, b; u0 = linres.u)
41-
cache.additional_lincache = init(
42-
linprob, QRFactorization(ColumnNorm()); alias_u0 = false,
43-
alias = LinearAliasSpecifier(alias_A = false, alias_b = false)
44-
)
45-
else
46-
cache.additional_lincache.A = A
47-
cache.additional_lincache.b = b
48-
cache.additional_lincache.Pl = cache.lincache.Pl
49-
cache.additional_lincache.Pr = cache.lincache.Pr
50-
end
51-
linres = solve!(cache.additional_lincache)
52-
cache.additional_lincache = linres.cache
53-
linres.retcode === ReturnCode.Failure &&
54-
return LinearSolveResult(; linres.u, success = false)
55-
return LinearSolveResult(; linres.u)
56-
elseif !(cache.linsolve isa QRFactorization{ColumnNorm})
57-
if verbose
58-
if structured_mat || is_gpuarray
59-
mat_desc = structured_mat ? "Structured" : "GPU"
60-
@warn "Potential Rank Deficient Matrix Detected. But Matrix is \
61-
$(mat_desc). Currently, we don't attempt to solve Rank Deficient \
62-
$(mat_desc) Matrices. Please open an issue at \
63-
https://github.com/SciML/NonlinearSolve.jl"
64-
end
65-
end
66-
end
6725
return LinearSolveResult(; linres.u, success = false)
26+
else
27+
return LinearSolveResult(; linres.u)
6828
end
69-
70-
return LinearSolveResult(; linres.u)
7129
end
7230

7331
function NonlinearSolveBase.needs_square_A(linsolve::SciMLLinearSolveAlgorithm, ::Any)

lib/NonlinearSolveBase/src/linear_solve.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ end
66
@concrete mutable struct LinearSolveJLCache <: AbstractLinearSolverCache
77
lincache
88
linsolve
9-
additional_lincache::Any
109
stats::NLStats
1110
end
1211

0 commit comments

Comments
 (0)