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

Improve performance of generic dot products #27678

Merged
merged 2 commits into from
Jul 11, 2018
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
62 changes: 18 additions & 44 deletions stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -621,21 +621,6 @@ opnorm(v::TransposeAbsVec) = norm(v.parent)

norm(v::Union{TransposeAbsVec,AdjointAbsVec}, p::Real) = norm(v.parent, p)

function dot(x::AbstractArray, y::AbstractArray)
lx = _length(x)
if lx != _length(y)
throw(DimensionMismatch("first array has length $(lx) which does not match the length of the second, $(_length(y))."))
end
if lx == 0
return dot(zero(eltype(x)), zero(eltype(y)))
end
s = zero(dot(first(x), first(y)))
for (Ix, Iy) in zip(eachindex(x), eachindex(y))
@inbounds s += dot(x[Ix], y[Iy])
end
s
end

"""
dot(x, y)
x ⋅ y
Expand Down Expand Up @@ -663,29 +648,23 @@ julia> dot(x, y)
function dot(x, y) # arbitrary iterables
ix = iterate(x)
iy = iterate(y)
if ix === nothing
if iy !== nothing
if ix == nothing
Copy link
Contributor

Choose a reason for hiding this comment

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

Why == everywhere?

if iy != nothing
throw(DimensionMismatch("x and y are of different lengths!"))
end
return dot(zero(eltype(x)), zero(eltype(y)))
end
if iy === nothing
if iy == nothing
throw(DimensionMismatch("x and y are of different lengths!"))
end
(vx, xs) = ix
(vy, ys) = iy
s = dot(vx, vy)
while true
ix = iterate(x, xs)
iy = iterate(y, ys)
if (ix == nothing) || (iy == nothing)
break
end
(vx, xs), (vy, ys) = ix, iy
s += dot(vx, vy)
s = dot(ix[1], iy[1])
ix, iy = iterate(x, ix[2]), iterate(y, iy[2])
while ix != nothing && iy != nothing
s += dot(ix[1], iy[1])
ix, iy = iterate(x, ix[2]), iterate(y, iy[2])
end
if !(iy == nothing && ix == nothing)
throw(DimensionMismatch("x and y are of different lengths!"))
throw(DimensionMismatch("x and y are of different lengths!"))
end
return s
end
Expand All @@ -709,24 +688,19 @@ julia> dot([im; im], [1; 1])
0 - 2im
```
"""
function dot(x::AbstractVector, y::AbstractVector)
if length(LinearIndices(x)) != length(LinearIndices(y))
throw(DimensionMismatch("dot product arguments have unequal lengths $(length(LinearIndices(x))) and $(length(LinearIndices(y)))"))
function dot(x::AbstractArray, y::AbstractArray)
lx = _length(x)
if lx != _length(y)
throw(DimensionMismatch("first array has length $(lx) which does not match the length of the second, $(_length(y))."))
end
ix = iterate(x)
if ix === nothing
# we only need to check the first vector, since equal lengths have been asserted
if lx == 0
return dot(zero(eltype(x)), zero(eltype(y)))
end
iy = iterate(y)
s = dot(ix[1], iy[1])
ix, iy = iterate(x, ix[2]), iterate(y, iy[2])
while ix != nothing
s += dot(ix[1], iy[1])
ix = iterate(x, ix[2])
iy = iterate(y, iy[2])
s = zero(dot(first(x), first(y)))
Copy link
Member

Choose a reason for hiding this comment

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

Seems the only potential downside to this implementation is that it retrieves the first element of each of x and y twice, rather than once with the explicit iterate implementations?

for (Ix, Iy) in zip(eachindex(x), eachindex(y))
@inbounds s += dot(x[Ix], y[Iy])
end
return s
s
end


Expand Down
12 changes: 7 additions & 5 deletions stdlib/LinearAlgebra/test/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,18 @@ end
@test dot(Z, Z) == convert(elty, 34.0)
end

dot_(x,y) = invoke(dot, Tuple{Any,Any}, x,y)
dot1(x,y) = invoke(dot, Tuple{Any,Any}, x,y)
dot2(x,y) = invoke(dot, Tuple{AbstractArray,AbstractArray}, x,y)
@testset "generic dot" begin
AA = [1+2im 3+4im; 5+6im 7+8im]
BB = [2+7im 4+1im; 3+8im 6+5im]
for A in (copy(AA), view(AA, 1:2, 1:2)), B in (copy(BB), view(BB, 1:2, 1:2))
@test dot(A,B) == dot(vec(A),vec(B)) == dot_(A,B) == dot(float.(A),float.(B))
@test dot(Int[], Int[]) == 0 == dot_(Int[], Int[])
@test dot(A,B) == dot(vec(A),vec(B)) == dot1(A,B) == dot2(A,B) == dot(float.(A),float.(B))
@test dot(Int[], Int[]) == 0 == dot1(Int[], Int[]) == dot2(Int[], Int[])
@test_throws MethodError dot(Any[], Any[])
@test_throws MethodError dot_(Any[], Any[])
for n1 = 0:2, n2 = 0:2, d in (dot, dot_)
@test_throws MethodError dot1(Any[], Any[])
@test_throws MethodError dot2(Any[], Any[])
for n1 = 0:2, n2 = 0:2, d in (dot, dot1, dot2)
if n1 != n2
@test_throws DimensionMismatch d(1:n1, 1:n2)
else
Expand Down