-
Notifications
You must be signed in to change notification settings - Fork 27
Change default solver to QR #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
|
|
||
| abstract type CoarseSolver end | ||
|
|
||
| """ | ||
| Pinv{T} <: CoarseSolver | ||
|
|
||
| Moore-Penrose pseudo inverse coarse solver. Calls `pinv` | ||
| """ | ||
| struct Pinv{T} <: CoarseSolver | ||
| pinvA::Matrix{T} | ||
| Pinv{T}(A) where T = new{T}(pinv(Matrix(A))) | ||
| end | ||
| Pinv(A) = Pinv{eltype(A)}(A) | ||
| Base.show(io::IO, p::Pinv) = print(io, "Pinv") | ||
|
|
||
| (p::Pinv)(x, b) = mul!(x, p.pinvA, b) | ||
|
|
||
| # This one is used internally. | ||
| """ | ||
| LinearSolveWrapperInternal <: CoarseSolver | ||
|
|
||
| Helper to allow the usage of LinearSolve.jl solvers for the coarse-level solve. Constructed via `LinearSolveWrapper`. | ||
| """ | ||
| struct LinearSolveWrapperInternal{LC <: LinearSolve.LinearCache} <: CoarseSolver | ||
| linsolve::LC | ||
| function LinearSolveWrapperInternal(A, alg::LinearSolve.SciMLLinearSolveAlgorithm) | ||
| rhs_tmp = zeros(eltype(A), size(A,1)) | ||
| u_tmp = zeros(eltype(A), size(A,2)) | ||
| linprob = LinearProblem(A, rhs_tmp; u0 = u_tmp, alias_A = false, alias_b = false) | ||
| linsolve = init(linprob, alg) | ||
| new{typeof(linsolve)}(linsolve) | ||
| end | ||
| end | ||
|
|
||
| function (p::LinearSolveWrapperInternal{LC})(x, b) where {LC <: LinearSolve.LinearCache} | ||
| for i ∈ 1:size(b, 2) | ||
| # Update right hand side | ||
| p.linsolve.b = b[:, i] | ||
| # Solve for x and update | ||
| x[:, i] = solve!(p.linsolve).u | ||
| end | ||
| end | ||
|
|
||
| function Base.show(io::IO, ml::LinearSolveWrapperInternal) | ||
| print(io, ml.linsolve.alg) | ||
| end | ||
|
|
||
|
|
||
| # This one simplifies passing of LinearSolve.jl algorithms into AlgebraicMultigrid.jl as coarse solvers. | ||
| """ | ||
| LinearSolveWrapper <: CoarseSolver | ||
|
|
||
| Helper to allow the usage of LinearSolve.jl solvers for the coarse-level solve. | ||
| """ | ||
| struct LinearSolveWrapper{A <: LinearSolve.SciMLLinearSolveAlgorithm} <: CoarseSolver | ||
| alg::A | ||
| end | ||
| (p::LinearSolveWrapper)(A::AbstractMatrix) = LinearSolveWrapperInternal(A, p.alg) | ||
|
|
||
|
|
||
| """ | ||
| QRSolver{F} <: CoarseSolver | ||
|
|
||
| Coarse solver using Julia's built-in factorizations via `qr()`. | ||
| """ | ||
| struct QRSolver{F} <: CoarseSolver | ||
| factorization::F | ||
| function QRSolver(A) | ||
| fact = qr(A) | ||
| new{typeof(fact)}(fact) | ||
| end | ||
| end | ||
| Base.show(io::IO, p::QRSolver) = print(io, "QRSolver") | ||
|
|
||
| function (solver::QRSolver)(x, b) | ||
| # Handle multiple RHS efficiently | ||
| for i ∈ 1:size(b, 2) | ||
| # Use backslash - Julia's factorizations are optimized for this | ||
| x[:, i] = solver.factorization \ b[:, i] | ||
| end | ||
| end | ||
|
|
||
| # Guess the best coarse solver based on the matrix type | ||
| _default_coarse_solver(A) = QRSolver | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make this a column-pivoted QR, so it handles singularities?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am pretty sure SPQR already handles these faithfully:
Or do you have something else in mind? Note that if we start converting the coarse matrix from sparse to dense (as done in the pinv solver), then we again run into memory issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dug into this topic quite a bit, and yes SPQR does work. There's now also https://github.com/SciML/SparseColumnPivotedQR.jl now, but SPQR is likely better for the matrices that people are using an AMG for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking time here! I tried to benchmark everything to look out for regressions, but ran into some trouble with the new OrdinaryDiffEq release (SciML/SciMLBenchmarks.jl#1601).
FYI if the coarse solver is accessible via LinearSolve, then it can be used via
coarse_solver = AMG.LinearSolveWrapper(myalg)already since AMG v1.0 or so.