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

Simplify triangular multiplication code slightly #52393

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make copyto! use adjoint!/transpose! when appropriate
  • Loading branch information
dkarrasch committed Dec 4, 2023
commit c18a9ff11cfb11ce89644ceacd13c1e5517afba9
8 changes: 4 additions & 4 deletions stdlib/LinearAlgebra/src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ matrix), then use `copy_similar`.

See also: `Base.copymutable`, `copy_similar`.
"""
copymutable_oftype(A::AbstractArray, ::Type{S}) where {S} = copyto!(similar(A, S), A)
copymutable_oftype(A::AbstractArray, ::Type{S}) where {S} = _at_copyto!(similar(A, S), A)

"""
copy_similar(A, T)
Expand All @@ -417,7 +417,7 @@ of the output corresponds to that of the three-argument method `similar(A, T, si

See also: `copymutable_oftype`.
"""
copy_similar(A::AbstractArray, ::Type{T}) where {T} = copyto!(similar(A, T, size(A)), A)
copy_similar(A::AbstractArray, ::Type{T}) where {T} = _at_copyto!(similar(A, T, size(A)), A)


include("adjtrans.jl")
Expand Down Expand Up @@ -568,9 +568,9 @@ function ldiv(F::Factorization, B::AbstractVecOrMat)

if n > size(B, 1)
# Underdetermined
copyto!(view(BB, 1:m, :), B)
_at_copyto!(view(BB, 1:m, :), B)
else
copyto!(BB, B)
_at_copyto!(BB, B)
end

ldiv!(FF, BB)
Expand Down
8 changes: 4 additions & 4 deletions stdlib/LinearAlgebra/src/abstractq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ function mul!(C::AbstractVecOrMat{T}, Q::AbstractQ{T}, B::Union{AbstractVecOrMat
nB != nC && throw(DimensionMismatch())
if mB < mC
inds = CartesianIndices(axes(B))
copyto!(view(C, inds), B)
_at_copyto!(view(C, inds), B)
C[CartesianIndices((mB+1:mC, axes(C, 2)))] .= zero(T)
return lmul!(Q, C)
else
return lmul!(Q, copyto!(C, B))
return lmul!(Q, _at_copyto!(C, B))
end
end
function mul!(C::AbstractVecOrMat{T}, A::AbstractVecOrMat, Q::AbstractQ{T}) where {T}
Expand All @@ -217,11 +217,11 @@ function mul!(C::AbstractVecOrMat{T}, A::AbstractVecOrMat, Q::AbstractQ{T}) wher
qsize_check(A, Q)
if nA < nC
inds = CartesianIndices(axes(A))
copyto!(view(C, inds), A)
_at_copyto!(view(C, inds), A)
C[CartesianIndices((axes(C, 1), nA+1:nC))] .= zero(T)
return rmul!(C, Q)
else
return rmul!(copyto!(C, A), Q)
return rmul!(_at_copyto!(C, A), Q)
end
end

Expand Down
4 changes: 4 additions & 0 deletions stdlib/LinearAlgebra/src/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ _unwrap_at(A) = A
_unwrap_at(A::Adjoint) = parent(A)
_unwrap_at(A::Transpose) = parent(A)

_at_copyto!(C, A) = copyto!(C, A)
_at_copyto!(C, A::Adjoint) = adjoint!(C, parent(A))
_at_copyto!(C, A::Transpose) = transpose!(C, parent(A))

Base.dataids(A::Union{Adjoint, Transpose}) = Base.dataids(A.parent)
Base.unaliascopy(A::Union{Adjoint,Transpose}) = typeof(A)(Base.unaliascopy(A.parent))

Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ end
ldiv!(Y::AbstractArray, s::Number, X::AbstractArray) = Y .= s .\ X

# Generic fallback. This assumes that B and Y have the same sizes.
ldiv!(Y::AbstractArray, A::AbstractMatrix, B::AbstractArray) = ldiv!(A, copyto!(Y, B))
ldiv!(Y::AbstractArray, A::AbstractMatrix, B::AbstractArray) = ldiv!(A, _at_copyto!(Y, B))


"""
Expand Down
3 changes: 1 addition & 2 deletions stdlib/LinearAlgebra/src/transpose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ end

function Base.copyto_unaliased!(deststyle::IndexStyle, dest::AbstractMatrix, srcstyle::IndexCartesian, src::AdjOrTransAbsMat)
if axes(dest) == axes(src)
f! = inplace_adj_or_trans(src)
f!(dest, parent(src))
_at_copyto!(dest, src)
else
@invoke Base.copyto_unaliased!(deststyle::IndexStyle, dest::AbstractArray, srcstyle::IndexStyle, src::AbstractArray)
end
Expand Down
18 changes: 9 additions & 9 deletions stdlib/LinearAlgebra/src/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -703,9 +703,9 @@ mul!(C::AbstractMatrix, A::AbstractTriangular, B::AbstractTriangular) = _trimul!
_trimul!(C::AbstractVecOrMat, A::AbstractTriangular, B::AbstractVector) =
lmul!(A, copyto!(C, B))
_trimul!(C::AbstractMatrix, A::AbstractTriangular, B::AbstractMatrix) =
lmul!(A, inplace_adj_or_trans(B)(C, _unwrap_at(B)))
lmul!(A, _at_copyto!(C, B))
_trimul!(C::AbstractMatrix, A::AbstractMatrix, B::AbstractTriangular) =
rmul!(inplace_adj_or_trans(A)(C, _unwrap_at(A)), B)
rmul!(_at_copyto!(C, A), B)
_trimul!(C::AbstractMatrix, A::AbstractTriangular, B::AbstractTriangular) =
lmul!(A, copyto!(C, B))
# redirect for UpperOrLowerTriangular
Expand Down Expand Up @@ -752,9 +752,9 @@ end
ldiv!(C::AbstractVecOrMat, A::AbstractTriangular, B::AbstractVecOrMat) = _ldiv!(C, A, B)
# generic fallback for AbstractTriangular, directs to 2-arg [l/r]div!
_ldiv!(C::AbstractVecOrMat, A::AbstractTriangular, B::AbstractVecOrMat) =
ldiv!(A, inplace_adj_or_trans(B)(C, _unwrap_at(B)))
ldiv!(A, _at_copyto!(C, B))
_rdiv!(C::AbstractMatrix, A::AbstractMatrix, B::AbstractTriangular) =
rdiv!(inplace_adj_or_trans(A)(C, _unwrap_at(A)), B)
rdiv!(_at_copyto!(C, A), B)
# redirect for UpperOrLowerTriangular to generic_*div!
_ldiv!(C::AbstractVecOrMat, A::UpperOrLowerTriangular, B::AbstractVecOrMat) =
generic_trimatdiv!(C, uplo_char(A), isunit_char(A), wrapperop(parent(A)), _unwrap_at(parent(A)), B)
Expand Down Expand Up @@ -828,16 +828,16 @@ end

# multiplication
generic_trimatmul!(c::StridedVector{T}, uploc, isunitc, tfun::Function, A::StridedMatrix{T}, b::AbstractVector{T}) where {T<:BlasFloat} =
BLAS.trmv!(uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, A, c === b ? c : copyto!(c, b))
BLAS.trmv!(uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, A, c === b ? c : _at_copyto!(c, b))
generic_trimatmul!(C::StridedMatrix{T}, uploc, isunitc, tfun::Function, A::StridedMatrix{T}, B::AbstractMatrix{T}) where {T<:BlasFloat} =
BLAS.trmm!('L', uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, one(T), A, C === B ? C : copyto!(C, B))
BLAS.trmm!('L', uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, one(T), A, C === B ? C : _at_copyto!(C, B))
generic_mattrimul!(C::StridedMatrix{T}, uploc, isunitc, tfun::Function, A::AbstractMatrix{T}, B::StridedMatrix{T}) where {T<:BlasFloat} =
BLAS.trmm!('R', uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, one(T), B, C === A ? C : copyto!(C, A))
BLAS.trmm!('R', uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, one(T), B, C === A ? C : _at_copyto!(C, A))
# division
generic_trimatdiv!(C::StridedVecOrMat{T}, uploc, isunitc, tfun::Function, A::StridedMatrix{T}, B::AbstractVecOrMat{T}) where {T<:BlasFloat} =
LAPACK.trtrs!(uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, A, C === B ? C : copyto!(C, B))
LAPACK.trtrs!(uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, A, C === B ? C : _at_copyto!(C, B))
generic_mattridiv!(C::StridedMatrix{T}, uploc, isunitc, tfun::Function, A::AbstractMatrix{T}, B::StridedMatrix{T}) where {T<:BlasFloat} =
BLAS.trsm!('R', uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, one(T), B, C === A ? C : copyto!(C, A))
BLAS.trsm!('R', uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, one(T), B, C === A ? C : _at_copyto!(C, A))

errorbounds(A::AbstractTriangular{T}, X::AbstractVecOrMat{T}, B::AbstractVecOrMat{T}) where {T<:Union{BigFloat,Complex{BigFloat}}} =
error("not implemented yet! Please submit a pull request.")
Expand Down