Skip to content

Commit

Permalink
Define new dot method for abstract vectors (#22392)
Browse files Browse the repository at this point in the history
* Define dot product between Number and AbstractArray

* Define dot between abstract arrays

* Added docs for dot between arrays

* Revert "Define dot product between Number and AbstractArray"

This reverts commit d46cdc7.

* Define new dot method between AbstractVectors
  • Loading branch information
Evey Dee authored and andreasnoack committed Jul 18, 2017
1 parent 22ed8b5 commit 8a18928
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
27 changes: 25 additions & 2 deletions base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,8 @@ dot(x::Number, y::Number) = vecdot(x, y)
dot(x, y)
⋅(x,y)
Compute the dot product. For complex vectors, the first vector is conjugated.
Compute the dot product between two vectors. For complex vectors, the first vector is conjugated.
When the vectors have equal lengths, calling `dot` is semantically equivalent to `sum(vx'vy for (vx,vy) in zip(x, y))`.
# Example
Expand All @@ -690,7 +691,29 @@ julia> dot([im; im], [1; 1])
0 - 2im
```
"""
dot(x::AbstractVector, y::AbstractVector) = vecdot(x, y)
function dot(x::AbstractVector, y::AbstractVector)
if length(x) != length(y)
throw(DimensionMismatch("dot product arguments have lengths $(length(x)) and $(length(y))"))
end
ix = start(x)
if done(x, ix)
# we only need to check the first vector, since equal lengths have been asserted
return zero(eltype(x))'zero(eltype(y))
end
@inbounds (vx, ix) = next(x, ix)
@inbounds (vy, iy) = next(y, start(y))
s = vx'vy
while !done(x, ix)
@inbounds (vx, ix) = next(x, ix)
@inbounds (vy, iy) = next(y, iy)
s += vx'vy
end
return s
end

# Call optimized BLAS methods for vectors of numbers
dot(x::AbstractVector{<:Number}, y::AbstractVector{<:Number}) = vecdot(x, y)


###########################################################################################

Expand Down
3 changes: 3 additions & 0 deletions test/linalg/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ end
@test dot(x, 1:2,y, 1:2) == convert(elty, 12.5)
@test x.'*y == convert(elty, 29.0)
@test_throws MethodError dot(rand(elty, 2, 2), randn(elty, 2, 2))
X = convert(Vector{Matrix{elty}},[reshape(1:4, 2, 2), ones(2, 2)])
res = convert(Matrix{elty}, [7.0 13.0; 13.0 27.0])
@test dot(X, X) == res
end

vecdot_(x,y) = invoke(vecdot, Tuple{Any,Any}, x,y) # generic vecdot
Expand Down

9 comments on commit 8a18928

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

@tkelman
Copy link
Contributor

Choose a reason for hiding this comment

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

["tuple","reduction",("sum",(8,8))] got quite a bit slower, should probably be bisected

@KristofferC
Copy link
Member

Choose a reason for hiding this comment

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

It has been noisy though. Looking at generated code before doing a full bisection is likely recommended.

@tkelman
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't recall that being a common culprit of noisy results, especially not 3x slower

@KristofferC
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps the 8x8 one has been more stable than the smaller ones, yes.

@andreasnoack
Copy link
Member

@andreasnoack andreasnoack commented on 8a18928 Jul 18, 2017

Choose a reason for hiding this comment

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

I just tried to run this benchmark and got

0.6

julia> run(b["tuple"]["reduction"][("sum", (8,8))])
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     277.406 ns (0.00% GC)
  median time:      277.827 ns (0.00% GC)
  mean time:        279.050 ns (0.00% GC)
  maximum time:     1.297 μs (0.00% GC)
  --------------
  samples:          3550
  evals/sample:     999

master

julia> run(b["tuple"]["reduction"][("sum", (8,8))])
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     53.202 ns (0.00% GC)
  median time:      53.832 ns (0.00% GC)
  mean time:        54.636 ns (0.00% GC)
  maximum time:     253.917 ns (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     999

Update:
The worst run on master looked like an outlier so I'ver updated the numbers

@tkelman
Copy link
Contributor

Choose a reason for hiding this comment

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

And what about master from a few days ago?

@andreasnoack
Copy link
Member

Choose a reason for hiding this comment

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

On

Julia Version 0.7.0-DEV.983
Commit 2fd61b6 (2017-07-15 17:43 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU E7- 8850  @ 2.00GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Nehalem)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, westmere)
Environment:

which should be the day tested against according to Nanosoldier, it is basically the same:

BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     53.272 ns (0.00% GC)
  median time:      53.901 ns (0.00% GC)
  mean time:        54.075 ns (0.00% GC)
  maximum time:     246.786 ns (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     999

Please sign in to comment.