Skip to content
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

remove copy-allocation on accessing cholesky factors (.L, .U) #1186

Merged
merged 4 commits into from
Feb 5, 2025
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
8 changes: 4 additions & 4 deletions src/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ julia> C.U
⋅ ⋅ 3.0

julia> C.L
3×3 LowerTriangular{Float64, Matrix{Float64}}:
3×3 LowerTriangular{Float64, Adjoint{Matrix{Float64}}}:
2.0 ⋅ ⋅
6.0 1.0 ⋅
-8.0 5.0 3.0
Expand Down Expand Up @@ -530,7 +530,7 @@ julia> C.U
⋅ ⋅ 3.0

julia> C.L
3×3 LowerTriangular{Float64, Matrix{Float64}}:
3×3 LowerTriangular{Float64, Adjoint{Matrix{Float64}}}:
2.0 ⋅ ⋅
6.0 1.0 ⋅
-8.0 5.0 3.0
Expand Down Expand Up @@ -668,14 +668,14 @@ function _choleskyUfactor(Cfactors, Cuplo)
if Cuplo === 'U'
return UpperTriangular(Cfactors)
else
return copy(LowerTriangular(Cfactors)')
return LowerTriangular(Cfactors)'
Copy link

@devmotion devmotion Feb 5, 2025

Choose a reason for hiding this comment

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

I was slightly confused when reading this the first time, IMO from

Suggested change
return LowerTriangular(Cfactors)'
return UpperTriangular(Cfactors')

it would have been more obvious that in both branches an UpperTriangular is returned.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we can do that as well.

end
end
function _choleskyLfactor(Cfactors, Cuplo)
if Cuplo === 'L'
return LowerTriangular(Cfactors)
else
return copy(UpperTriangular(Cfactors)')
return UpperTriangular(Cfactors)'
end
end

Expand Down
30 changes: 24 additions & 6 deletions test/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -595,18 +595,18 @@ end
B = cholesky(A)
B32 = cholesky(Float32.(A))
@test B isa Cholesky{Float16, Matrix{Float16}}
@test B.U isa UpperTriangular{Float16, Matrix{Float16}}
@test B.L isa LowerTriangular{Float16, Matrix{Float16}}
@test B.UL isa UpperTriangular{Float16, Matrix{Float16}}
@test B.U isa UpperTriangular{Float16, <:AbstractMatrix{Float16}}
@test B.L isa LowerTriangular{Float16, <:AbstractMatrix{Float16}}
@test B.UL isa UpperTriangular{Float16, <:AbstractMatrix{Float16}}
@test B.U ≈ B32.U
@test B.L ≈ B32.L
@test B.UL ≈ B32.UL
@test Matrix(B) ≈ A
B = cholesky(A, RowMaximum())
B32 = cholesky(Float32.(A), RowMaximum())
@test B isa CholeskyPivoted{Float16,Matrix{Float16}}
@test B.U isa UpperTriangular{Float16, Matrix{Float16}}
@test B.L isa LowerTriangular{Float16, Matrix{Float16}}
@test B isa CholeskyPivoted{Float16, <:AbstractMatrix{Float16}}
@test B.U isa UpperTriangular{Float16, <:AbstractMatrix{Float16}}
@test B.L isa LowerTriangular{Float16, <:AbstractMatrix{Float16}}
@test B.U ≈ B32.U
@test B.L ≈ B32.L
@test Matrix(B) ≈ A
Expand Down Expand Up @@ -658,4 +658,22 @@ end
end
end

@testset "accessing both L and U factors should avoid allocations" begin
n = 30
A = rand(n, n)
Apd = A'A
allowed_cost_of_overhead = 32
@assert sizeof(Apd) > 4allowed_cost_of_overhead # ensure that we could positively identify extra copies

for uplo in (:L, :U)
C = Symmetric(Apd, uplo)
for val in (NoPivot(), RowMaximum())
B = cholesky(C, val)
B.L, B.U # access once to ensure the accessor is compiled already
@test (@allocated B.L) <= allowed_cost_of_overhead
@test (@allocated B.U) <= allowed_cost_of_overhead
end
end
end

end # module TestCholesky