Skip to content

Create blockcholesky.jl #126

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 21 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 3 additions & 1 deletion src/BlockArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ using Base: ReshapedArray, dataids
import Base: (:), IteratorSize, iterate, axes1, strides, isempty
import Base.Broadcast: broadcasted, DefaultArrayStyle, AbstractArrayStyle, Broadcasted, broadcastable
import LinearAlgebra: lmul!, rmul!, AbstractTriangular, HermOrSym, AdjOrTrans,
StructuredMatrixStyle
StructuredMatrixStyle, cholesky, cholesky!, cholcopy
import ArrayLayouts: _fill_lmul!, MatMulVecAdd, MatMulMatAdd, MatLmulVec, MatLdivVec,
materialize!, MemoryLayout, sublayout, transposelayout, conjlayout,
triangularlayout, triangulardata, _inv, _copyto!, axes_print_matrix_row,
Expand All @@ -54,10 +54,12 @@ include("views.jl")
include("blocks.jl")
include("blockarrayinterface.jl")
include("blockbroadcast.jl")
include("blockcholesky.jl")
include("blocklinalg.jl")
include("blockproduct.jl")
include("show.jl")
include("blockreduce.jl")
include("blockdeque.jl")
include("blockbandedcholesky.jl")

end # module
91 changes: 91 additions & 0 deletions src/blockbandedcholesky.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@


sparsecholesky(A::Symmetric{<:Real,<:BlockArray},
::Val{false}=Val(false); check::Bool = true) = sparsecholesky!(cholcopy(A); check = check)



function _blockbandedcholesky!(A::BlockArray{T}, ::Type{UpperTriangular}) where T<:Real
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this is a separate function, not just block_chol! from beliw.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only difference is that I added a check of trivial blocks from backwards. It actually do the same thing on dense matrices. We can have another parameter for activating the 'check'.

n = blocksize(A)[1]
k_end = 0

@inbounds begin
for i = 1:n
Pii = getblock(A,i,i)
for k = 1:i-1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should loop over colsupport(A.blocks, i) ∩ 1:i-1 I think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

colsupport(_, A, j) = axes(A,1)

""""
colsupport(A, j)

gives an iterator containing the possible non-zero entries in the j-th column of A.
"""
colsupport(A, j) = colsupport(MemoryLayout(typeof(A)), A, j)

The code of colsupport(A, j) is just the axes(A,1), so this may not be useful.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we only care about the blockbanded cases, maybe we can move the file into the package BlockBandedMatrices?
Then it will be convenient where all the prerequisite functions are included.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it should be here. It will do the right thing for both dense and banded formats.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing is that why LAPACK.potrf!() do not work in-place on BlockBandedMatrices?
It gives the right output but the block will not be replaced by the decomposed matrix.

muladd!(-one(T), getblock(A,k,i)', getblock(A,k,i), one(T), Pii)
end
Aii, info = LinearAlgebra._chol!(Pii, UpperTriangular)
if !iszero(info)
@assert info > 0
if i == 1
return UpperTriangular(A), info
end
info += sum(size(A[Block(l,l)])[1] for l=1:i-1)
return UpperTriangular(A), info
end

k_start = n
while getblock(A,i,k_start) ≈ zeros(Float32, size(getblock(A,i,k_start))) && k_start > k_end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is strange

k_start -= 1
end
k_end = k_start

for j = k_start:-1:i+1
Pij = getblock(A,i,j)
for k = 1:i-1
muladd!(-one(T), getblock(A,k,i)', getblock(A,k,j), one(T), Pij)
end
ldiv!(UpperTriangular(getblock(A,i,i))', Pij)
end
end
end

return UpperTriangular(A), 0
end

function _blockbandedcholesky!(A::BlockArray{T}, ::Type{LowerTriangular}) where T<:Real
n = blocksize(A)[1]
k_end = 0

@inbounds begin
for i = 1:n
Pii = getblock(A,i,i)
for k = 1:i-1
muladd!(-one(T), getblock(A,i,k), getblock(A,i,k)', one(T), Pii)
end
Aii, info = LinearAlgebra._chol!(Pii, LowerTriangular)
if !iszero(info)
@assert info > 0
if i == 1
return UpperTriangular(A), info
end
info += sum(size(A[Block(l,l)])[1] for l=1:i-1)
return LowerTriangular(A), info
end

k_start = n
while getblock(A,k_start,i) ≈ zeros(Float32, size(getblock(A,k_start,i))) && k_start > k_end
k_start -= 1
end
k_end = k_start

for j = k_start:-1:i+1
Pij = getblock(A,j,i)
for k = 1:i-1
muladd!(-one(T), getblock(A,j,k), getblock(A,i,k)', one(T), Pij)
end
rdiv!(Pij, LowerTriangular(getblock(A,i,i))')
end
end
end

return LowerTriangular(A), 0
end


function sparsecholesky!(A::Symmetric{<:Real,<:BlockArray}, ::Val{false}=Val(false); check::Bool = true)
C, info = _blockbandedcholesky!(A.data, A.uplo == 'U' ? UpperTriangular : LowerTriangular)
#check && LinearAlgebra.checkpositivedefinite(info)
return Cholesky(C.data, A.uplo, info)
end
82 changes: 82 additions & 0 deletions src/blockcholesky.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@


##########################################
# Cholesky Factorization on BlockMatrices#
##########################################


cholesky(A::Symmetric{<:Real,<:BlockArray},
::Val{false}=Val(false); check::Bool = false) = cholesky!(cholcopy(A); check = check)


function _block_chol!(A::BlockArray{T}, ::Type{UpperTriangular}) where T<:Real
n = blocksize(A)[1]

@inbounds begin
for i = 1:n
Pii = getblock(A,i,i)
for k = 1:i-1
muladd!(-one(T), getblock(A,k,i)', getblock(A,k,i), one(T), Pii)
end
Aii, info = LinearAlgebra._chol!(Pii, UpperTriangular)
if !iszero(info)
@assert info > 0
if i == 1
return UpperTriangular(A), info
end
info += sum(size(A[Block(l,l)])[1] for l=1:i-1)
return UpperTriangular(A), info
end

for j = i+1:n
Pij = getblock(A,i,j)
for k = 1:i-1
muladd!(-one(T), getblock(A,k,i)', getblock(A,k,j), one(T), Pij)
end
ldiv!(UpperTriangular(getblock(A,i,i))', Pij)
end
end
end

return UpperTriangular(A), 0
end


function _block_chol!(A::BlockArray{T}, ::Type{LowerTriangular}) where T<:Real
n = blocksize(A)[1]

@inbounds begin
for i = 1:n
Pii = getblock(A,i,i)
for k = 1:i-1
muladd!(-one(T), getblock(A,i,k), getblock(A,i,k)', one(T), Pii)
end
Aii, info = LinearAlgebra._chol!(Pii, LowerTriangular)
if !iszero(info)
@assert info > 0
if i == 1
return UpperTriangular(A), info
end
info += sum(size(A[Block(l,l)])[1] for l=1:i-1)
return LowerTriangular(A), info
end

for j = i+1:n
Pij = getblock(A,j,i)
for k = 1:i-1
muladd!(-one(T), getblock(A,j,k), getblock(A,i,k)', one(T), Pij)
end
rdiv!(Pij, LowerTriangular(getblock(A,i,i))')
end
end
end

return LowerTriangular(A), 0
end

function cholesky!(A::Symmetric{<:Real,<:BlockArray}, ::Val{false}=Val(false); check::Bool = false)
C, info = _block_chol!(A.data, A.uplo == 'U' ? UpperTriangular : LowerTriangular)
#check && LinearAlgebra.checkpositivedefinite(info)
return Cholesky(C.data, A.uplo, info)
end

1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ using BlockArrays, LinearAlgebra, Test
include("test_blockproduct.jl")
include("test_blockreduce.jl")
include("test_blockdeque.jl")
include("test_cholesky.jl")
end
48 changes: 48 additions & 0 deletions test/test_cholesky.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using BlockArrays, Test, LinearAlgebra



@testset "Block cholesky" begin

# Generating random positive definite and symmetric matrices
A = BlockArray{Float32}(randn(9,9)+100I, fill(3,3), fill(3,3)); A = Symmetric(A)
B = BlockArray{Float32}(randn(55,55)+100I, 1:10, 1:10); B = Symmetric(B)
C = BlockArray{Float32}(randn(9,9)+100I, fill(3,3), fill(3,3)); C = Symmetric(C, :L)
D = BlockArray{Float32}(randn(55,55)+100I, 1:10, 1:10); D = Symmetric(D, :L)
E = BlockArray{Float32}(randn(9,9)+100I, fill(3,3), fill(3,3)); E = Symmetric(E)
E2 = copy(E); E2[2,2] = 0
E5 = copy(E); E5[5,5] = 0
E8 = copy(E); E8[8,8] = 0
nsym = BlockArray{Float32}(randn(6,8), fill(2,3), fill(2,4))

A_T = Matrix(A)
B_T = Matrix(B)
C_T = Matrix(C)
D_T = Matrix(D)

#Test on nonsymmetric matrix
@test_throws MethodError cholesky(nsym)

#Tests on A
@test cholesky(A).U ≈ cholesky(A_T).U
@test cholesky(A).U'cholesky(A).U ≈ A

#Tests on B
@test cholesky(B).U ≈ cholesky(B_T).U
@test cholesky(B).U'cholesky(B).U ≈ B

#Tests on C
@test cholesky(C).L ≈ cholesky(C_T).L
@test cholesky(C).L*cholesky(C).L' ≈ C

#Tests on D
@test cholesky(D).L ≈ cholesky(D_T).L
@test cholesky(D).L*cholesky(D).L' ≈ D

#Tests on non-PD matrices
@test cholesky(E2).info == 2
@test cholesky(E5).info == 5
@test cholesky(E8).info == 8

end