Skip to content

Commit 924dda4

Browse files
authored
remove copy-allocation on accessing cholesky factors (.L, .U) (#1186)
1 parent 6f02532 commit 924dda4

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/cholesky.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ julia> C.U
6565
⋅ ⋅ 3.0
6666
6767
julia> C.L
68-
3×3 LowerTriangular{Float64, Matrix{Float64}}:
68+
3×3 LowerTriangular{Float64, Adjoint{Matrix{Float64}}}:
6969
2.0 ⋅ ⋅
7070
6.0 1.0 ⋅
7171
-8.0 5.0 3.0
@@ -530,7 +530,7 @@ julia> C.U
530530
⋅ ⋅ 3.0
531531
532532
julia> C.L
533-
3×3 LowerTriangular{Float64, Matrix{Float64}}:
533+
3×3 LowerTriangular{Float64, Adjoint{Matrix{Float64}}}:
534534
2.0 ⋅ ⋅
535535
6.0 1.0 ⋅
536536
-8.0 5.0 3.0
@@ -668,14 +668,14 @@ function _choleskyUfactor(Cfactors, Cuplo)
668668
if Cuplo === 'U'
669669
return UpperTriangular(Cfactors)
670670
else
671-
return copy(LowerTriangular(Cfactors)')
671+
return LowerTriangular(Cfactors)'
672672
end
673673
end
674674
function _choleskyLfactor(Cfactors, Cuplo)
675675
if Cuplo === 'L'
676676
return LowerTriangular(Cfactors)
677677
else
678-
return copy(UpperTriangular(Cfactors)')
678+
return UpperTriangular(Cfactors)'
679679
end
680680
end
681681

test/cholesky.jl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -595,18 +595,18 @@ end
595595
B = cholesky(A)
596596
B32 = cholesky(Float32.(A))
597597
@test B isa Cholesky{Float16, Matrix{Float16}}
598-
@test B.U isa UpperTriangular{Float16, Matrix{Float16}}
599-
@test B.L isa LowerTriangular{Float16, Matrix{Float16}}
600-
@test B.UL isa UpperTriangular{Float16, Matrix{Float16}}
598+
@test B.U isa UpperTriangular{Float16, <:AbstractMatrix{Float16}}
599+
@test B.L isa LowerTriangular{Float16, <:AbstractMatrix{Float16}}
600+
@test B.UL isa UpperTriangular{Float16, <:AbstractMatrix{Float16}}
601601
@test B.U B32.U
602602
@test B.L B32.L
603603
@test B.UL B32.UL
604604
@test Matrix(B) A
605605
B = cholesky(A, RowMaximum())
606606
B32 = cholesky(Float32.(A), RowMaximum())
607-
@test B isa CholeskyPivoted{Float16,Matrix{Float16}}
608-
@test B.U isa UpperTriangular{Float16, Matrix{Float16}}
609-
@test B.L isa LowerTriangular{Float16, Matrix{Float16}}
607+
@test B isa CholeskyPivoted{Float16, <:AbstractMatrix{Float16}}
608+
@test B.U isa UpperTriangular{Float16, <:AbstractMatrix{Float16}}
609+
@test B.L isa LowerTriangular{Float16, <:AbstractMatrix{Float16}}
610610
@test B.U B32.U
611611
@test B.L B32.L
612612
@test Matrix(B) A
@@ -658,4 +658,22 @@ end
658658
end
659659
end
660660

661+
@testset "accessing both L and U factors should avoid allocations" begin
662+
n = 30
663+
A = rand(n, n)
664+
Apd = A'A
665+
allowed_cost_of_overhead = 32
666+
@assert sizeof(Apd) > 4allowed_cost_of_overhead # ensure that we could positively identify extra copies
667+
668+
for uplo in (:L, :U)
669+
C = Symmetric(Apd, uplo)
670+
for val in (NoPivot(), RowMaximum())
671+
B = cholesky(C, val)
672+
B.L, B.U # access once to ensure the accessor is compiled already
673+
@test (@allocated B.L) <= allowed_cost_of_overhead
674+
@test (@allocated B.U) <= allowed_cost_of_overhead
675+
end
676+
end
677+
end
678+
661679
end # module TestCholesky

0 commit comments

Comments
 (0)