Skip to content

fastCholesky is slower than cholesky with StaticArrays #21

@btmit

Description

@btmit

Edit: I just found StaticArraysCoreExt which appears to try and cover this case. I'm not sure why this is so much slower. The StaticArraysCoreExt method also doesn't appear to check for near symmetry.

Edit: The reason fastcholesky(::Hermitian) is so much slower is because it always appears to dispatch to the PositiveFactorizations version of cholesky() even if this is unnecessary.

It seems like fastCholesky should fall through to the existing cholesky(::Hermitian{T,<:SMatrix}) implementation if the matrix is nearly symmetric.

There also appears to be some sort of dispatch bug with fastcholesky(::Hermitian{T,<:SMatrix)}).

using StaticArrays, BenchmarkTools, LinearAlgebra, FastCholesky

randPosDefs(n) = AbstractMatrix(Cholesky(LowerTriangular(rand(SMatrix{n,n}))))

function testCholesky(n)

    s = randPosDefs(n)
    m = Matrix(s)

    println("n = $n")
    print("cholesky(::Matrix)                     ") 
    @btime cholesky($m)
    print("fastcholesky(:Matrix)                  ") 
    @btime fastcholesky($m)
    print("cholesky(::SMatrix)                    ") 
    @btime cholesky($s)
    print("cholesky(::Hermitian{T,<:SMatrix)}     ") 
    @btime cholesky(Hermitian($s))
    print("fastcholesky(::SMatrix)                ") 
    @btime fastcholesky($s)
    print("fastcholesky(::Hermitian{T,<:SMatrix)})") 
    @btime fastcholesky(Hermitian($s))

    return nothing
end

n = 9
testCholesky(n)

# n = 9
# cholesky(::Matrix)                       659.377 ns (2 allocations: 720 bytes)
# fastcholesky(:Matrix)                    480.077 ns (2 allocations: 720 bytes)
# cholesky(::SMatrix)                      209.058 ns (0 allocations: 0 bytes)
# cholesky(::Hermitian{T,<:SMatrix)}       139.052 ns (0 allocations: 0 bytes)
# fastcholesky(::SMatrix)                  216.766 ns (0 allocations: 0 bytes)
# fastcholesky(::Hermitian{T,<:SMatrix)})  1.411 μs (6 allocations: 880 bytes)

n = 3
testCholesky(n)

# n = 3
# cholesky(::Matrix)                       237.731 ns (2 allocations: 144 bytes)
# fastcholesky(:Matrix)                    118.273 ns (2 allocations: 144 bytes)
# cholesky(::SMatrix)                      13.557 ns (0 allocations: 0 bytes)
# cholesky(::Hermitian{T,<:SMatrix)}       11.824 ns (0 allocations: 0 bytes)
# fastcholesky(::SMatrix)                  23.928 ns (0 allocations: 0 bytes)
# fastcholesky(::Hermitian{T,<:SMatrix)})  325.323 ns (6 allocations: 288 bytes)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions