-
-
Notifications
You must be signed in to change notification settings - Fork 75
Implement Butterfly Factorization method using RecursiveFactorization.jl #785
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
Open
Shreyas-Ekanathan
wants to merge
12
commits into
SciML:main
Choose a base branch
from
Shreyas-Ekanathan:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
35641e0
add butterfly
Shreyas-Ekanathan 34dce92
add tests
Shreyas-Ekanathan fb28d18
fixes
Shreyas-Ekanathan 2cca03a
done
Shreyas-Ekanathan 93aa7ca
Update lu.jl
Shreyas-Ekanathan 0f344a2
Upstream merge
Shreyas-Ekanathan 5862ed7
Merge branch 'main' into main
Shreyas-Ekanathan 17ccc8c
fix various bugs
Shreyas-Ekanathan 56bbd71
prelim fixes
Shreyas-Ekanathan 0a61671
edits
Shreyas-Ekanathan 8de40b7
fix tests
Shreyas-Ekanathan 63c385d
Update Project.toml
ChrisRackauckas 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module LinearSolveRecursiveFactorizationExt | ||
|
||
using LinearSolve: LinearSolve, userecursivefactorization, LinearCache, @get_cacheval, | ||
RFLUFactorization, RF32MixedLUFactorization, default_alias_A, | ||
RFLUFactorization, ButterflyFactorization, RF32MixedLUFactorization, default_alias_A, | ||
default_alias_b | ||
using LinearSolve.LinearAlgebra, LinearSolve.ArrayInterface, RecursiveFactorization | ||
using SciMLBase: SciMLBase, ReturnCode | ||
|
@@ -19,7 +19,6 @@ function SciMLBase.solve!(cache::LinearSolve.LinearCache, alg::RFLUFactorization | |
end | ||
fact = RecursiveFactorization.lu!(A, ipiv, Val(P), Val(T), check = false) | ||
cache.cacheval = (fact, ipiv) | ||
|
||
if !LinearAlgebra.issuccess(fact) | ||
return SciMLBase.build_linear_solution( | ||
alg, cache.u, nothing, cache; retcode = ReturnCode.Failure) | ||
|
@@ -105,4 +104,32 @@ function SciMLBase.solve!( | |
alg, cache.u, nothing, cache; retcode = ReturnCode.Success) | ||
end | ||
|
||
function SciMLBase.solve!(cache::LinearSolve.LinearCache, alg::ButterflyFactorization; | ||
kwargs...) | ||
A = cache.A | ||
A = convert(AbstractMatrix, A) | ||
b = cache.b | ||
M, N = size(A) | ||
B, U, V = cache.cacheval[2], cache.cacheval[3], cache.cacheval[4] | ||
if cache.isfresh | ||
@assert M==N "A must be square" | ||
U, V, F, out = RecursiveFactorization.🦋workspace(A, b, B, U, V, alg.thread) | ||
cache.cacheval = (A, B, U, V, F) | ||
cache.isfresh = false | ||
if (M % 4 != 0) | ||
b = [b; rand(4 - M % 4)] | ||
Shreyas-Ekanathan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
end | ||
end | ||
A, B, U, V, F = cache.cacheval | ||
sol = V * (F \ (U * b)) | ||
|
||
out .= @view sol[1:M] | ||
SciMLBase.build_linear_solution(alg, out, nothing, cache) | ||
end | ||
|
||
function LinearSolve.init_cacheval(alg::ButterflyFactorization, A, b, u, Pl, Pr, maxiters::Int, | ||
abstol, reltol, verbose::Bool, assumptions::LinearSolve.OperatorAssumptions) | ||
A, A, A', A, RecursiveFactorization.lu!(rand(1, 1), alg.thread) | ||
Shreyas-Ekanathan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
end | ||
|
||
end | ||
|
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,35 @@ | ||
using LinearAlgebra, LinearSolve | ||
using Test | ||
using RecursiveFactorization | ||
|
||
@testset "Random Matricies" begin | ||
for i in 490 : 510 | ||
A = rand(i, i) | ||
b = rand(i) | ||
prob = LinearProblem(A, b) | ||
x = solve(prob, ButterflyFactorization()) | ||
@test norm(A * x .- b) <= 1e-4 | ||
end | ||
end | ||
|
||
function wilkinson(N) | ||
A = zeros(N, N) | ||
A[1:(N+1):N*N] .= 1 | ||
A[:, end] .= 1 | ||
for n in 1:(N - 1) | ||
for r in (n + 1):N | ||
@inbounds A[r, n] = -1 | ||
end | ||
end | ||
A | ||
end | ||
|
||
@testset "Wilkinson" begin | ||
for i in 790 : 810 | ||
A = wilkinson(i) | ||
b = rand(i) | ||
prob = LinearProblem(A, b) | ||
x = solve(prob, ButterflyFactorization()) | ||
@test norm(A * x .- b) <= 1e-10 | ||
end | ||
end |
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.
Why is this not just a struct and
!
operation? It would be much easier to read. I assume this is all just in-place and non-allocating.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.
as in make
U, V, F, out
a struct?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.
yes