Skip to content

Commit e1ef63b

Browse files
authored
Merge pull request #144 from st--/st/avoid_copy
Avoid copy in `chol_lower`
2 parents a0e7191 + 1ce765d commit e1ef63b

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/chol.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
chol_lower(a::Matrix) = cholesky(a).L
2-
3-
# always use the stored cholesky factor, not a copy
1+
# Accessing a.L directly might involve an extra copy();
2+
# instead, always use the stored Cholesky factor:
43
chol_lower(a::Cholesky) = a.uplo === 'L' ? a.L : a.U'
54
chol_upper(a::Cholesky) = a.uplo === 'U' ? a.U : a.L'
65

6+
# For a dense Matrix, the following allows us to avoid the Adjoint wrapper:
7+
chol_lower(a::Matrix) = cholesky(Hermitian(a, :L)).L
8+
79
if HAVE_CHOLMOD
810
CholTypeSparse{T} = SuiteSparse.CHOLMOD.Factor{T}
911

test/chol.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using LinearAlgebra, PDMats
2+
using PDMats: chol_lower, chol_upper
3+
4+
@testset "chol_lower" begin
5+
A = rand(100, 100)
6+
C = A'A
7+
size_of_one_copy = sizeof(C)
8+
@assert size_of_one_copy > 100 # ensure the matrix is large enough that few-byte allocations don't matter
9+
10+
chol_lower(C)
11+
@test (@allocated chol_lower(C)) < 1.05 * size_of_one_copy # allow 5% overhead
12+
13+
for uplo in (:L, :U)
14+
ch = cholesky(Symmetric(C, uplo))
15+
chol_lower(ch)
16+
@test (@allocated chol_lower(ch)) < 50 # allow small overhead for wrapper types
17+
chol_upper(ch)
18+
@test (@allocated chol_upper(ch)) < 50 # allow small overhead for wrapper types
19+
end
20+
end

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
include("testutils.jl")
2-
tests = ["pdmtypes", "addition", "generics", "kron"]
2+
tests = ["pdmtypes", "addition", "generics", "kron", "chol"]
33
println("Running tests ...")
44

55
for t in tests

0 commit comments

Comments
 (0)