Skip to content

Faster QR #75

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 5 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/AbstractBlockBandedMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ blockbanded_blockrowstart(A, i::BlockRange) = blockbanded_blockrowstart(A, minim
blockbanded_blockcolstop(A, i::BlockRange) = blockbanded_blockcolstop(A, maximum(i))
blockbanded_blockrowstop(A, i::BlockRange) = blockbanded_blockrowstop(A, maximum(i))

const AllBlockBandedLayout{UPLO,UNIT} = Union{AbstractBlockBandedLayout,TriangularLayout{UPLO,UNIT,<:AbstractBlockBandedLayout}}

@inline blockcolsupport(::AbstractBlockBandedLayout, A, i) = blockbanded_blockcolstart(A,i):blockbanded_blockcolstop(A,i)
@inline blockrowsupport(::AbstractBlockBandedLayout, A, i) = blockbanded_blockrowstart(A,i):blockbanded_blockrowstop(A,i)
@inline blockcolsupport(::AllBlockBandedLayout, A, i) = blockbanded_blockcolstart(A,i):blockbanded_blockcolstop(A,i)
@inline blockrowsupport(::AllBlockBandedLayout, A, i) = blockbanded_blockrowstart(A,i):blockbanded_blockrowstop(A,i)


# length of i-the column/row
Expand Down
2 changes: 1 addition & 1 deletion src/BlockBandedMatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import BlockArrays: blocksize, blockcheckbounds, BlockedUnitRange, blockisequal,
Block, BlockSlice, getblock, unblock, setblock!, block, blockindex,
_blocklengths2blocklasts, BlockIndexRange, sizes_from_blocks, BlockSlice1,
blockcolsupport, blockrowsupport, blockcolstart, blockcolstop, blockrowstart, blockrowstop,
AbstractBlockLayout, BlockLayout, blocks
AbstractBlockLayout, BlockLayout, blocks, hasmatchingblocks

import BandedMatrices: isbanded, bandwidths, bandwidth, banded_getindex, colrange,
inbands_setindex!, inbands_getindex, banded_setindex!,
Expand Down
29 changes: 26 additions & 3 deletions src/blockskylineqr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,38 @@ function materialize!(Mul::MatLmulVec{<:AdjQLPackedQLayout{<:AbstractBlockBanded
Mul.B
end

function materialize!(Mul::MatLmulMat{<:AdjQRPackedQLayout{<:AbstractBlockBandedLayout}})
adjQ,Bin = Mul.A,Mul.B
Q = parent(adjQ)
A = Q.factors
l,u = blockbandwidths(A)
N,M = blocksize(A)
# impose block structure
ax1 = (axes(A,1),)
τ = PseudoBlockArray(Q.τ, ax1)
B = PseudoBlockArray(Bin, (axes(A,1),axes(Bin,2)))
for K = 1:min(N,M), J = 1:blocksize(Bin,2)
KR = Block.(K:min(K+l,N))
V = view(A,KR,Block(K))
t = view(τ,Block(K))
apply_qr!(V, t, view(B,KR,Block(J)))
end
Bin
end

# avoid LinearALgebra Strided obsession

for Typ in (:StridedVector, :StridedMatrix, :AbstractVector, :AbstractMatrix, :LayoutMatrix)
@eval function ldiv!(A::QR{<:Any,<:BlockSkylineMatrix}, B::$Typ)
lmul!(adjoint(A.Q), B)
M,N = blocksize(A.factors)
MN = min(M,N)
V = view(A.factors,Block.(1:MN), Block.(1:MN))
materialize!(Ldiv(UpperTriangular(V), view(B,1:size(V,1),:)))
if M == N
materialize!(Ldiv(UpperTriangular(A.factors), view(B,1:size(A.factors,1),:)))
else
MN = min(M,N)
V = view(A.factors,Block.(1:MN), Block.(1:MN))
materialize!(Ldiv(UpperTriangular(V), view(B,1:size(V,1),:)))
end
B
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/triblockbanded.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ isbandedblockbanded(A::AbstractTriangular) =
function blockbandwidths(A::Union{UpperTriangular,UnitUpperTriangular})
P = parent(A)
if hasmatchingblocks(P)
(min(0,blockbandwidths(P,1)), blockbandwidth(P,2))
(min(0,blockbandwidth(P,1)), blockbandwidth(P,2))
else
blockbandwidths(P)
end
Expand Down
33 changes: 31 additions & 2 deletions test/test_blockskylineqr.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BlockBandedMatrices, LinearAlgebra, MatrixFactorizations
using BlockBandedMatrices, BlockArrays, LinearAlgebra, MatrixFactorizations, Test
import BlockBandedMatrices: blockcolsupport

@testset "BlockBandedMatrix QR/QL" begin
@testset "Square QR" begin
Expand Down Expand Up @@ -118,7 +119,7 @@ using BlockBandedMatrices, LinearAlgebra, MatrixFactorizations
N = 5
A = BlockBandedMatrix{Float64}(undef, 1:N,1:N+1, (2,1))
A.data .= randn.()

@test_throws ArgumentError ql(A)
end

Expand Down Expand Up @@ -148,6 +149,34 @@ using BlockBandedMatrices, LinearAlgebra, MatrixFactorizations
end
end
end

@testset "Fast QR \\" begin
A = [1. 2; 3 4]; A = A + A'
B = [5. 6; 7 8]
N = 10_000;
T = BlockBandedMatrix(mortar(Tridiagonal(fill(Matrix(B'),N-1), fill(zeros(2,2),N), fill(B,N-1))))
z = 1+2im
@time F = qr(T - z*I);
@test blockbandwidths(UpperTriangular(F.factors)) == (0,2)
@test blockcolsupport(UpperTriangular(F.factors),Block(4)) == Block.(2:4)
V = view(F.factors,Block.(1:N), Block.(1:N))
@test blocksize(V) == (N,N)
@test @allocated(blocksize(V)) ≤ 40
@test blockbandwidths(V) == (1,2)
@test @allocated(blockbandwidths(V)) ≤ 40
R = UpperTriangular(V)
@test axes(R) == axes(V) == axes(F.factors)
@test blocksize(R) == (N,N)
@test @allocated(blocksize(R)) ≤ 40
@test blockbandwidths(R) == (0,2)
@test @allocated(blockbandwidths(R)) ≤ 40
@test blockcolsupport(R,Block(4)) == Block.(2:4)
@test @allocated(blockcolsupport(R,Block(4))) ≤ 40

b = [1; zeros(size(T,1)-1)]
B = [Matrix(I,2,2); zeros(size(T,1)-2,2)]
@test ((T - z*I)\b)[1] ≈ (F\b)[1] ≈ (F \ B)[1,1] ≈ ((T - z*I)\B)[1,1] ≈ -0.1309123477325813 + 0.28471699370329884im
end
end


Expand Down