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 d125c7e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 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 \

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

View check run for this annotation

Codecov / codecov/patch

src/internal/linear_solve.jl#L171-L172

Added lines #L171 - L172 were not covered by tests
Pivoted QR Factorization."
Expand All @@ -189,6 +191,20 @@ function (cache::LinearSolverCache)(;
linres.retcode === ReturnCode.Failure &&

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

View check run for this annotation

Codecov / codecov/patch

src/internal/linear_solve.jl#L189-L191

Added lines #L189 - L191 were not covered by tests
return LinearSolveResult(; u = linres.u, success = false)
return LinearSolveResult(; u = linres.u)

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

View check run for this annotation

Codecov / codecov/patch

src/internal/linear_solve.jl#L193

Added line #L193 was not covered by tests
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

0 comments on commit d125c7e

Please sign in to comment.