-
Notifications
You must be signed in to change notification settings - Fork 42
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
Offset matrix multiplication via generic_matmatmul!
#270
Open
mcabbott
wants to merge
4
commits into
JuliaArrays:master
Choose a base branch
from
mcabbott:matmul
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using LinearAlgebra | ||
using LinearAlgebra: MulAddMul, mul! | ||
lapack_axes(t::AbstractChar, M::AbstractVecOrMat) = (axes(M, t=='N' ? 1 : 2), axes(M, t=='N' ? 2 : 1)) | ||
|
||
# The signatures of these differs from LinearAlgebra's *only* on C. | ||
LinearAlgebra.generic_matvecmul!(C::OffsetVector, tA, A::AbstractVecOrMat, B::AbstractVector, | ||
_add::MulAddMul) = unwrap_matvecmul!(C, tA, A, B, _add.alpha, _add.beta) | ||
LinearAlgebra.generic_matvecmul!(C::OffsetVector, tA, A::AbstractVecOrMat, B::AbstractVector, | ||
alpha, beta) = unwrap_matvecmul!(C, tA, A, B, alpha, beta) | ||
|
||
function unwrap_matvecmul!(C::OffsetVector, tA, A::AbstractVecOrMat, B::AbstractVector, | ||
alpha, beta) | ||
|
||
mB_axis = Base.axes1(B) | ||
mA_axis, nA_axis = lapack_axes(tA, A) | ||
|
||
if mB_axis != nA_axis | ||
throw(DimensionMismatch("mul! can't contract axis $(UnitRange(nA_axis)) from A with axes(B) == ($(UnitRange(mB_axis)),)")) | ||
end | ||
if mA_axis != Base.axes1(C) | ||
throw(DimensionMismatch("mul! got axes(C) == ($(UnitRange(Base.axes1(C))),), expected $(UnitRange(mA_axis))")) | ||
end | ||
|
||
C1 = no_offset_view(C) | ||
A1 = no_offset_view(A) | ||
B1 = no_offset_view(B) | ||
|
||
if tA == 'T' | ||
mul!(C1, transpose(A1), B1, alpha, beta) | ||
elseif tA == 'C' | ||
mul!(C1, adjoint(A1), B1, alpha, beta) | ||
elseif tA == 'N' | ||
mul!(C1, A1, B1, alpha, beta) | ||
else | ||
error("illegal char") | ||
end | ||
|
||
C | ||
end | ||
|
||
# The signatures of these differs from LinearAlgebra's *only* on C: | ||
# Old path | ||
LinearAlgebra.generic_matmatmul!(C::OffsetMatrix, tA, tB, A::AbstractMatrix, B::AbstractMatrix, | ||
_add::MulAddMul) = unwrap_matmatmul!(C, tA, tB, A, B, _add.alpha, _add.beta) | ||
LinearAlgebra.generic_matmatmul!(C::Union{OffsetMatrix, OffsetVector}, tA, tB, A::AbstractVecOrMat, B::AbstractVecOrMat, | ||
_add::MulAddMul) = unwrap_matmatmul!(C, tA, tB, A, B, _add.alpha, _add.beta) | ||
|
||
# New path | ||
LinearAlgebra.generic_matmatmul!(C::OffsetMatrix, tA, tB, A::AbstractMatrix, B::AbstractMatrix, | ||
alpha, beta) = unwrap_matmatmul!(C, tA, tB, A, B, alpha, beta) | ||
LinearAlgebra.generic_matmatmul!(C::Union{OffsetMatrix, OffsetVector}, tA, tB, A::AbstractVecOrMat, B::AbstractVecOrMat, | ||
alpha, beta) = unwrap_matmatmul!(C, tA, tB, A, B, alpha, beta) | ||
|
||
# Worker | ||
@inline function unwrap_matmatmul!(C::Union{OffsetMatrix, OffsetVector}, tA, tB, A::AbstractVecOrMat, B::AbstractVecOrMat, | ||
alpha, beta) | ||
|
||
mA_axis, nA_axis = lapack_axes(tA, A) | ||
mB_axis, nB_axis = lapack_axes(tB, B) | ||
|
||
if nA_axis != mB_axis | ||
throw(DimensionMismatch("mul! can't contract axis $(UnitRange(nA_axis)) from A with $(UnitRange(mB_axis)) from B")) | ||
elseif mA_axis != axes(C,1) | ||
throw(DimensionMismatch("mul! got axes(C,1) == $(UnitRange(axes(C,1))), expected $(UnitRange(mA_axis)) from A")) | ||
elseif nB_axis != axes(C,2) | ||
throw(DimensionMismatch("mul! got axes(C,2) == $(UnitRange(axes(C,2))), expected $(UnitRange(nB_axis)) from B")) | ||
end | ||
|
||
C1 = no_offset_view(C) | ||
A1 = no_offset_view(A) | ||
B1 = no_offset_view(B) | ||
|
||
if tA == 'N' | ||
if tB == 'N' | ||
mul!(C1, A1, B1, alpha, beta) | ||
elseif tB == 'T' | ||
mul!(C1, A1, transpose(B1), alpha, beta) | ||
elseif tB == 'C' | ||
mul!(C1, A1, adjoint(B1), alpha, beta) | ||
else | ||
error("illegal char") | ||
end | ||
elseif tA == 'T' | ||
if tB == 'N' | ||
mul!(C1, transpose(A1), B1, alpha, beta) | ||
elseif tB == 'T' | ||
mul!(C1, transpose(A1), transpose(B1), alpha, beta) | ||
elseif tB == 'C' | ||
mul!(C1, transpose(A1), adjoint(B1), alpha, beta) | ||
else | ||
error("illegal char") | ||
end | ||
elseif tA == 'C' | ||
if tB == 'N' | ||
mul!(C1, adjoint(A1), B1, alpha, beta) | ||
elseif tB == 'T' | ||
mul!(C1, adjoint(A1), transpose(B1), alpha, beta) | ||
elseif tB == 'C' | ||
mul!(C1, adjoint(A1), adjoint(B1), alpha, beta) | ||
else | ||
error("illegal char") | ||
end | ||
else | ||
error("illegal char") | ||
end | ||
|
||
C | ||
end | ||
|
||
no_offset_view(A::Adjoint) = Adjoint(no_offset_view(parent(A))) | ||
no_offset_view(A::Transpose) = Transpose(no_offset_view(parent(A))) | ||
no_offset_view(D::Diagonal) = Diagonal(no_offset_view(parent(D))) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bigger change to
LinearAlgebra.generic_matmatmul!
would be to make it keepadjoint
longer, before introducing'C'
, etc. Then this nest of conditions could be removed.It seems an odd design that MulAddMul pushes
α,β
into the type domain (partly) at the same time that it moves transpose/adjoint to values from types. Perhaps JuliaLang/julia#43552 could fix both at the same time.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
816b928 (and JuliaLang/julia@aad0522) changes this. It removes the extra allocation above.
The downside is that these methods will not be called for
mul!
with a version of Julia older than JuliaLang/julia#43552 .