-
Notifications
You must be signed in to change notification settings - Fork 39
Speedup matrix multiplications #129
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
2915481
2766105
e7dcb94
4f08ad6
5a999ba
9ef396f
15a15f1
8350786
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
const FillVector{F,A} = Fill{F,1,A} | ||
const FillMatrix{F,A} = Fill{F,2,A} | ||
const OnesVector{F,A} = Ones{F,1,A} | ||
const OnesMatrix{F,A} = Ones{F,2,A} | ||
const ZerosVector{F,A} = Zeros{F,1,A} | ||
const ZerosMatrix{F,A} = Zeros{F,2,A} | ||
|
||
## vec | ||
|
||
vec(a::Ones{T}) where T = Ones{T}(length(a)) | ||
|
@@ -87,11 +94,22 @@ end | |
*(a::Zeros{<:Any,2}, b::Diagonal) = mult_zeros(a, b) | ||
*(a::Diagonal, b::Zeros{<:Any,1}) = mult_zeros(a, b) | ||
*(a::Diagonal, b::Zeros{<:Any,2}) = mult_zeros(a, b) | ||
function *(a::Diagonal, b::AbstractFill{<:Any,2}) | ||
|
||
# Cannot unify following methods for Diagonal | ||
# due to ambiguity with general array mult. with fill | ||
function *(a::Diagonal, b::FillMatrix) | ||
size(a,2) == size(b,1) || throw(DimensionMismatch("A has dimensions $(size(a)) but B has dimensions $(size(b))")) | ||
a.diag .* b # use special broadcast | ||
end | ||
function *(a::FillMatrix, b::Diagonal) | ||
size(a,2) == size(b,1) || throw(DimensionMismatch("A has dimensions $(size(a)) but B has dimensions $(size(b))")) | ||
a .* permutedims(b.diag) # use special broadcast | ||
end | ||
function *(a::Diagonal, b::OnesMatrix) | ||
size(a,2) == size(b,1) || throw(DimensionMismatch("A has dimensions $(size(a)) but B has dimensions $(size(b))")) | ||
a.diag .* b # use special broadcast | ||
end | ||
function *(a::AbstractFill{<:Any,2}, b::Diagonal) | ||
function *(a::OnesMatrix, b::Diagonal) | ||
size(a,2) == size(b,1) || throw(DimensionMismatch("A has dimensions $(size(a)) but B has dimensions $(size(b))")) | ||
a .* permutedims(b.diag) # use special broadcast | ||
end | ||
|
@@ -100,23 +118,61 @@ end | |
*(a::Transpose{T, <:StridedMatrix{T}}, b::Fill{T, 1}) where T = reshape(sum(parent(a); dims=1) .* b.value, size(parent(a), 2)) | ||
*(a::StridedMatrix{T}, b::Fill{T, 1}) where T = reshape(sum(a; dims=2) .* b.value, size(a, 1)) | ||
|
||
function *(a::Adjoint{T, <:StridedMatrix{T}}, b::Fill{T, 2}) where T | ||
fB = similar(parent(a), size(b, 1), size(b, 2)) | ||
fill!(fB, b.value) | ||
return a*fB | ||
function *(x::AbstractMatrix, f::FillMatrix) | ||
axes(x, 2) ≠ axes(f, 1) && | ||
throw(DimensionMismatch("Incompatible matrix multiplication dimensions")) | ||
m = size(f, 2) | ||
repeat(sum(x, dims=2) * f.value, 1, m) | ||
end | ||
|
||
function *(f::FillMatrix, x::AbstractMatrix) | ||
axes(f, 2) ≠ axes(x, 1) && | ||
throw(DimensionMismatch("Incompatible matrix multiplication dimensions")) | ||
m = size(f, 1) | ||
repeat(sum(x, dims=1) * f.value, m, 1) | ||
end | ||
|
||
function *(a::Transpose{T, <:StridedMatrix{T}}, b::Fill{T, 2}) where T | ||
fB = similar(parent(a), size(b, 1), size(b, 2)) | ||
fill!(fB, b.value) | ||
return a*fB | ||
function *(x::AbstractMatrix, f::OnesMatrix) | ||
axes(x, 2) ≠ axes(f, 1) && | ||
throw(DimensionMismatch("Incompatible matrix multiplication dimensions")) | ||
m = size(f, 2) | ||
repeat(sum(x, dims=2) * one(eltype(f)), 1, m) | ||
end | ||
|
||
function *(a::StridedMatrix{T}, b::Fill{T, 2}) where T | ||
fB = similar(a, size(b, 1), size(b, 2)) | ||
fill!(fB, b.value) | ||
return a*fB | ||
function *(f::OnesMatrix, x::AbstractMatrix) | ||
axes(f, 2) ≠ axes(x, 1) && | ||
throw(DimensionMismatch("Incompatible matrix multiplication dimensions")) | ||
m = size(f, 1) | ||
repeat(sum(x, dims=1) * one(eltype(f)), m, 1) | ||
end | ||
|
||
*(x::FillMatrix, y::FillMatrix) = mult_fill(x, y) | ||
*(x::FillMatrix, y::OnesMatrix) = mult_fill(x, y) | ||
*(x::OnesMatrix, y::FillMatrix) = mult_fill(x, y) | ||
*(x::OnesMatrix, y::OnesMatrix) = mult_fill(x, y) | ||
*(x::ZerosMatrix, y::OnesMatrix) = mult_zeros(x, y) | ||
*(x::ZerosMatrix, y::FillMatrix) = mult_zeros(x, y) | ||
*(x::FillMatrix, y::ZerosMatrix) = mult_zeros(x, y) | ||
*(x::OnesMatrix, y::ZerosMatrix) = mult_zeros(x, y) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you are massively underestimating the number of ambiguities we need to take into account. For example we need to take into account It's also a bad idea to overload *(a::AbstractMatrix, b::AbstractFill{<:Any,2}) = fill_mul(a, b)
fill_mul(a, b::Ones) = ...
fill_mul(a, b::Zeros) = ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hopefully I addressed both concerns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a chance that overloading something other than Without special handling, it will end up after Or maybe that's too late, since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This issue is precisely what ArrayLayouts.jl is designed to avoid, and would also give us the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait how does that work? Is this only if you call something special like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, got it. Overloading for one supertype stops all its children from stepping on each others' toes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. although that's probably the right direction, It involves a lot of churn and the coordination of two packages, so I would merge this as it is for the time being There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I agree. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
FYI I wrote both packages so this isn't quite so bad. The only reason I haven't done this is FillArrays.jl is clean and simple, so needs a good reason to make it more complicated. I'm not sure we are there yet. |
||
|
||
# function *(a::Adjoint{T, <:StridedMatrix{T}}, b::Fill{T, 2}) where T | ||
# fB = similar(parent(a), size(b, 1), size(b, 2)) | ||
# fill!(fB, b.value) | ||
# return a*fB | ||
# end | ||
|
||
# function *(a::Transpose{T, <:StridedMatrix{T}}, b::Fill{T, 2}) where T | ||
# fB = similar(parent(a), size(b, 1), size(b, 2)) | ||
# fill!(fB, b.value) | ||
# return a*fB | ||
# end | ||
|
||
# function *(a::StridedMatrix{T}, b::Fill{T, 2}) where T | ||
# fB = similar(a, size(b, 1), size(b, 2)) | ||
# fill!(fB, b.value) | ||
# return a*fB | ||
# end | ||
|
||
function _adjvec_mul_zeros(a::Adjoint{T}, b::Zeros{S, 1}) where {T, S} | ||
la, lb = length(a), length(b) | ||
if la ≠ lb | ||
|
Uh oh!
There was an error while loading. Please reload this page.