Repository navigation
Conversation
bf375dc to
49bf56c
Compare
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
e5396cd to
c9b96ed
Compare
Co-authored-by: Jutho <Jutho@users.noreply.github.com>
4750786 to
2f9a602
Compare
| d = Array(Q' * xv) | ||
| if β == 0 | ||
| y .= α .* d | ||
| else | ||
| y .= β .* y .+ α .* d | ||
| end |
There was a problem hiding this comment.
Any reason not to use
| d = Array(Q' * xv) | |
| if β == 0 | |
| y .= α .* d | |
| else | |
| y .= β .* y .+ α .* d | |
| end | |
| y = mul!(y, Q', xv, α, β) |
There was a problem hiding this comment.
Only that I ported what already existed, indeed I like this change better
| return β == 1 ? y : β == 0 ? zerovector!(y) : scale!(y, β) | ||
| end | ||
| Q = _basis_matrix(b, r, yv) | ||
| xd = copyto!(similar(yv, length(r)), collect(x)) |
There was a problem hiding this comment.
Why can we not use vec(x) directly?
There was a problem hiding this comment.
I guess I see. BLASVector includes StridedVector, and non-contiguous arrays would not be supported in level 2 BLAS methods. Can we maybe use another type alias for these methods, and restrict them so that we can actually leverage BLAS level 2, e.g.:
const BLAS1Vector{T <: BlasFloat} = Union{DenseArray{T}, StridedVector{T}}
const BLAS2Vector{T <: BlasFLoat} = DenseArray{T}There was a problem hiding this comment.
Though that comment pertains more to the type of y. The x in unproject should really be a vector, not only after reshape/vec, and we should be able to use x directly, no xd or vec(x) necessary. But the type of x is harder to fix. It is not necessarily DenseVector{T}, as it could also be a contiguous view. So StridedVector{T} after all, but with guaranteed unit stride. Not sure how to express that.
| mul!(yv, Q, xd, α, β) | ||
| return y | ||
| end | ||
| function unproject!!(y::AbstractArray, b, x::AbstractArray, α::Number = true, β::Number = false, r = Base.OneTo(length(b))) |
There was a problem hiding this comment.
| function unproject!!(y::AbstractArray, b, x::AbstractArray, α::Number = true, β::Number = false, r = Base.OneTo(length(b))) | |
| function unproject!!(y::AbstractArray, b, x::AbstractVector, α::Number = true, β::Number = false, r = Base.OneTo(length(b))) |
| function unproject!!(y::AbstractArray, b, x::AbstractArray, α::Number = true, β::Number = false, r = Base.OneTo(length(b))) | ||
| length(x) == length(r) || throw(DimensionMismatch()) | ||
| if get_num_threads() > 1 | ||
| return unproject_linear_multithreaded!(y, b, x, α, β, r) |
There was a problem hiding this comment.
Wasn't there some check for only calling this if the type of y exhibits efficient linear indexing in the original KrylovKit version?
There was a problem hiding this comment.
Actually, I think the check in KrylovKit now effectively boils down to y::Array{T}, which is even more restricted the case that would now go to the method above and immediately call BLAS level 2. The reason that this was not yet in KrylovKit, is that it was initially less restrictive, only checking whether y had efficient linear indexing using IndexStyle, but this then conflicted with GPUArrays.
There was a problem hiding this comment.
Conclusion, I think our own multithreaded linear-indexstyle implementations can go, which will simplify this greatly, and can probably remove the whole threading business (get_num_threads, enable_threading, ....) from VectorInterface and KrylovKit.
| end | ||
| end | ||
| function rank1update!( | ||
| b::OrthonormalBasis{<:BLASVector{T}}, y::BLASVector{T}, x::BLASVector{T}, |
There was a problem hiding this comment.
Similar story, y::BLAS2Vector{T}, x::StridedVector{T} with guaranteed/checked unit stride on x.
| #--------- | ||
| function project!(y, b, x, α::Number = true, β::Number = false, r = Base.OneTo(length(b))) | ||
| length(y) == length(r) || throw(DimensionMismatch()) | ||
| if get_num_threads() > 1 |
There was a problem hiding this comment.
So project! and basistransform! below are the only fallbacks where we could still use threading. The question is whether it's worth it. I don't know if this functionality was ever really used in KrylovKit.
| end | ||
|
|
||
| # use an underscored fallback so that the single-threaded unproject!! in src/abstractarray.jl | ||
| # can use this as well. |
There was a problem hiding this comment.
If we remove the linear_multithreaded stuff, this would no longer be necessary.
| if iszero(β) | ||
| y = scale!!(y, false) # should be hard zero | ||
| elseif !isone(β) | ||
| y = scale!!(y, β) | ||
| end | ||
| @inbounds for (i, ri) in enumerate(r) | ||
| y = add!!(y, b[ri], α * x[i]) | ||
| end |
There was a problem hiding this comment.
| if iszero(β) | |
| y = scale!!(y, false) # should be hard zero | |
| elseif !isone(β) | |
| y = scale!!(y, β) | |
| end | |
| @inbounds for (i, ri) in enumerate(r) | |
| y = add!!(y, b[ri], α * x[i]) | |
| end | |
| @inbounds for (i, ri) in enumerate(r) | |
| y = i == 1 ? add!!(y, b[ri], α * x[i], β) : add!!(y, b[ri], α * x[i]) | |
| end |
| y = add!!(y, b[i], U[i, j]) | ||
| end | ||
| return y | ||
| end |
There was a problem hiding this comment.
Should we rewrite this as unproject!!(y, b, view(U, :, j)). Maybe we should even simply write
function basistransform!(b, U) # U should be unitary or isometric
m, n = size(U)
m == length(b) || throw(DimensionMismatch())
b2 = [b * view(U, :, j) for j in 1:n)
for j in 1:n
b[j] = scale!!(b[j], b2[j], true)
end
return b
end| various kernels. Can be iterated over to generate sub-ranges to hand to each | ||
| thread to work on. See also [`splitrange`](@ref). | ||
| """ | ||
| struct SplitRange |
There was a problem hiding this comment.
All of this can also go if we don't do multithreading at this level. Let's ask Lander and Lukas.
This PR moves over some functionality related to
OrthnormalBasisfromKrylovKit.jl, including:project!(now with just one!)unproject!!rank1update!basistransform!As well as the definition and helper functions for
OrthonormalBasisitself. It also adds some fast-paths for dense arrays which should allow us to avoid using the kernel selector logic of Jutho/KrylovKit.jl#180. Finally, it adds some tests and makes sure to run the tests in multithreaded mode so that those paths do get exercised.Not everything is moved in, including
orthogonalizeandreorthogonalize, since those involve all the Gram-Schmidt algorithms and if we want them here, I think that could be a follow-up? Unless others feel strongly they should come in at the same time.I also didn't add a matrix-backed
OrthonormalBasiswhich could be done as a follow up PR. Basically, I want to move things without modifying how they work too much to make this easier to review, then we could do follow-on updates if needed.