Skip to content

Commit

Permalink
Safe guard against cases which might not support rank deficient matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Apr 26, 2024
1 parent b2a2316 commit 914f556
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/NonlinearSolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import PrecompileTools: @recompile_invalidations, @compile_workload, @setup_work
LinearAlgebra, LinearSolve, MaybeInplace, Preferences, Printf, SciMLBase,
SimpleNonlinearSolve, SparseArrays, SparseDiffTools

import ArrayInterface: undefmatrix, can_setindex, restructure, fast_scalar_indexing,
ismutable
import ArrayInterface: ArrayInterface, undefmatrix, can_setindex, restructure,
fast_scalar_indexing, ismutable
import DiffEqBase: AbstractNonlinearTerminationMode,
AbstractSafeNonlinearTerminationMode,
AbstractSafeBestNonlinearTerminationMode,
Expand Down
22 changes: 19 additions & 3 deletions src/internal/linear_solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,11 @@ function (cache::LinearSolverCache)(;
cache.lincache = linres.cache
# Unfortunately LinearSolve.jl doesn't have the most uniform ReturnCode handling
if linres.retcode === ReturnCode.Failure
# TODO: We need to guard this somehow because this will surely fail if A is on GPU
# TODO: or some fancy Matrix type
if !(cache.linsolve isa QRFactorization{ColumnNorm})
structured_mat = ArrayInterface.isstructured(cache.lincache.A)
is_gpuarray = ArrayInterface.device(cache.lincache.A) isa ArrayInterface.GPU
if !(cache.linsolve isa QRFactorization{ColumnNorm}) &&
!is_gpuarray &&
!structured_mat
if verbose
@warn "Potential Rank Deficient Matrix Detected. Attempting to solve using \
Pivoted QR Factorization."
Expand All @@ -189,6 +191,20 @@ function (cache::LinearSolverCache)(;
linres.retcode === ReturnCode.Failure &&
return LinearSolveResult(; u = linres.u, success = false)
return LinearSolveResult(; u = linres.u)
elseif !(cache.linsolve isa QRFactorization{ColumnNorm})
if verbose
if structured_mat
@warn "Potential Rank Deficient Matrix Detected. But Matrix is \

Check warning on line 197 in src/internal/linear_solve.jl

View check run for this annotation

Codecov / codecov/patch

src/internal/linear_solve.jl#L196-L197

Added lines #L196 - L197 were not covered by tests
Structured. Currently, we don't attempt to solve Rank Deficient \
Structured Matrices. Please open an issue at \
https://github.com/SciML/NonlinearSolve.jl"
elseif is_gpuarray
@warn "Potential Rank Deficient Matrix Detected. But Matrix is on GPU. \

Check warning on line 202 in src/internal/linear_solve.jl

View check run for this annotation

Codecov / codecov/patch

src/internal/linear_solve.jl#L201-L202

Added lines #L201 - L202 were not covered by tests
Currently, we don't attempt to solve Rank Deficient GPU \
Matrices. Please open an issue at \
https://github.com/SciML/NonlinearSolve.jl"
end
end
end
return LinearSolveResult(; u = linres.u, success = false)
end
Expand Down
11 changes: 3 additions & 8 deletions test/misc/polyalg_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,7 @@ end
u0 = @SVector [0.0, 0.0, 0.0]
prob = NonlinearProblem(f1_infeasible, u0)

try
sol = solve(prob)
@test all(!isnan, sol.u)
@test !SciMLBase.successful_retcode(sol.retcode)
@inferred solve(prob)
catch err
@test err isa LinearAlgebra.SingularException
end
sol = solve(prob)
@test all(!isnan, sol.u)
@test !SciMLBase.successful_retcode(sol.retcode)
end

0 comments on commit 914f556

Please sign in to comment.