Skip to content

Commit 914f556

Browse files
committed
Safe guard against cases which might not support rank deficient matrices
1 parent b2a2316 commit 914f556

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

src/NonlinearSolve.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import PrecompileTools: @recompile_invalidations, @compile_workload, @setup_work
1212
LinearAlgebra, LinearSolve, MaybeInplace, Preferences, Printf, SciMLBase,
1313
SimpleNonlinearSolve, SparseArrays, SparseDiffTools
1414

15-
import ArrayInterface: undefmatrix, can_setindex, restructure, fast_scalar_indexing,
16-
ismutable
15+
import ArrayInterface: ArrayInterface, undefmatrix, can_setindex, restructure,
16+
fast_scalar_indexing, ismutable
1717
import DiffEqBase: AbstractNonlinearTerminationMode,
1818
AbstractSafeNonlinearTerminationMode,
1919
AbstractSafeBestNonlinearTerminationMode,

src/internal/linear_solve.jl

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,11 @@ function (cache::LinearSolverCache)(;
163163
cache.lincache = linres.cache
164164
# Unfortunately LinearSolve.jl doesn't have the most uniform ReturnCode handling
165165
if linres.retcode === ReturnCode.Failure
166-
# TODO: We need to guard this somehow because this will surely fail if A is on GPU
167-
# TODO: or some fancy Matrix type
168-
if !(cache.linsolve isa QRFactorization{ColumnNorm})
166+
structured_mat = ArrayInterface.isstructured(cache.lincache.A)
167+
is_gpuarray = ArrayInterface.device(cache.lincache.A) isa ArrayInterface.GPU
168+
if !(cache.linsolve isa QRFactorization{ColumnNorm}) &&
169+
!is_gpuarray &&
170+
!structured_mat
169171
if verbose
170172
@warn "Potential Rank Deficient Matrix Detected. Attempting to solve using \
171173
Pivoted QR Factorization."
@@ -189,6 +191,20 @@ function (cache::LinearSolverCache)(;
189191
linres.retcode === ReturnCode.Failure &&
190192
return LinearSolveResult(; u = linres.u, success = false)
191193
return LinearSolveResult(; u = linres.u)
194+
elseif !(cache.linsolve isa QRFactorization{ColumnNorm})
195+
if verbose
196+
if structured_mat
197+
@warn "Potential Rank Deficient Matrix Detected. But Matrix is \
198+
Structured. Currently, we don't attempt to solve Rank Deficient \
199+
Structured Matrices. Please open an issue at \
200+
https://github.com/SciML/NonlinearSolve.jl"
201+
elseif is_gpuarray
202+
@warn "Potential Rank Deficient Matrix Detected. But Matrix is on GPU. \
203+
Currently, we don't attempt to solve Rank Deficient GPU \
204+
Matrices. Please open an issue at \
205+
https://github.com/SciML/NonlinearSolve.jl"
206+
end
207+
end
192208
end
193209
return LinearSolveResult(; u = linres.u, success = false)
194210
end

test/misc/polyalg_tests.jl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,7 @@ end
250250
u0 = @SVector [0.0, 0.0, 0.0]
251251
prob = NonlinearProblem(f1_infeasible, u0)
252252

253-
try
254-
sol = solve(prob)
255-
@test all(!isnan, sol.u)
256-
@test !SciMLBase.successful_retcode(sol.retcode)
257-
@inferred solve(prob)
258-
catch err
259-
@test err isa LinearAlgebra.SingularException
260-
end
253+
sol = solve(prob)
254+
@test all(!isnan, sol.u)
255+
@test !SciMLBase.successful_retcode(sol.retcode)
261256
end

0 commit comments

Comments
 (0)