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

Unrolled cholesky factorization #243

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bshall
Copy link
Contributor

@bshall bshall commented Jul 8, 2017

This PR extends the chol to higher dimensions, adds a cholfact method and a \ based on the triangular solvers in #222. Also, chol will now return an UpperTriangular fixing #194.

The Cholesky struct in Base changed in v0.7 so I've targeted this later version with this PR. I'm not sure how to get this working on v0.6 and v0.7 simultaneously.

I've kept the old tests for the time being (just to show that everything is consistent) but these should be removed when everything is ready to be merged.

@bshall
Copy link
Contributor Author

bshall commented Jul 8, 2017

I'm also not sure if the cholfact and \ should have an @inline.

@andyferris
Copy link
Member

I'm not sure how to get this working on v0.6 and v0.7 simultaneously.

Would using our own struct be suitable, in your opinion? (You can use @static if VERSION < v"0.7" to make this v0.6 only, if that is better).

I'm also not sure if the cholfact and \ should have an @inline.

I think that might be helpful. Benchmarking over a range of matrix sizes is the way to answer this question. (There's some pretty unclear code in the perf directory which can be extended if you want to play with this).

@andyferris
Copy link
Member

Awesome PR, btw. I haven't looked at the code in detail yet, but thanks :)

_chol(Size(A), A.data)
end
@inline _chol(A::StaticMatrix{<:Any,<:Any,T}, ::Type{UpperTriangular}) where {T} = _chol(Size(A), A, UpperTriangular)
@inline _chol(A::StaticMatrix{<:Any,<:Any,T}, ::Type{LowerTriangular}) where {T} = _chol(Size(A), A, LowerTriangular)
Copy link
Member

Choose a reason for hiding this comment

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

Seems like these two methods could simply be:

@inline _chol(A::StaticMatrix, ::Type{UL}) where {UL} = _chol(Size(A), A, UL)

newtype = similar_type(A,T)
@generated function _chol(::Size{s}, A::StaticMatrix{<:Any,<:Any,T}, ::Type{UpperTriangular}) where {s, T}
if s[1] != s[2]
error("matrix must be square")
Copy link
Member

Choose a reason for hiding this comment

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

This check is not necessary; the ishermitian check does this before we enter this function.

error("matrix must be square")
end
n = s[1]
TX = promote_type(typeof(chol(one(T), UpperTriangular)), Float32)
Copy link
Member

Choose a reason for hiding this comment

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

TX = typeof(chol(one(T)))

ref JuliaLang/julia#22245 (comment)

newtype = similar_type(A,T)
@generated function _chol(::Size{s}, A::StaticMatrix{<:Any, <:Any, T}, ::Type{LowerTriangular}) where {s, T}
if s[1] != s[2]
error("matrix must be square")
Copy link
Member

Choose a reason for hiding this comment

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

Same comment as above.

end

n = s[1]
TX = promote_type(typeof(chol(one(T), LowerTriangular)), Float32)
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.

T = promote_type(typeof(sqrt(one(eltype(A)))), Float32)
newtype = similar_type(A,T)
## Numbers
_chol(x::Number, uplo) = Base.LinAlg._chol!(x, uplo)
Copy link
Member

Choose a reason for hiding this comment

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

Accidentally left this in from Base?

@bshall
Copy link
Contributor Author

bshall commented Jul 12, 2017

Would using our own struct be suitable, in your opinion?

Yeah, I think this is probably the best way to go. I'll get the done and make the changes suggested by @fredrikekre. Thanks for the feedback by the way.

@andyferris
Copy link
Member

Where is this at?

@bshall
Copy link
Contributor Author

bshall commented Jul 24, 2017

Sorry about the delay, I've been away for a bit. I'll hopefully finish this up by the end of the week.

@c42f
Copy link
Member

c42f commented Aug 1, 2019

There's a few conflicts but it appears there's still a lot of potential in this PR if it was modernized. We should have some benchmarks to decide when to stop unrolling.

@c42f c42f added the feature features and feature requests label Aug 1, 2019
@c42f c42f changed the title Improve cholesky factorization Unrolled cholesky factorization Aug 1, 2019
oschulz pushed a commit to oschulz/StaticArrays.jl that referenced this pull request Apr 4, 2023
Unify getindex and view to use the same approach. This helps keep component types better than the Base slicing fallback.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature features and feature requests linear-algebra
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants