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

Add @inbounds to generic_matvecmul! #532

Closed
dlfivefifty opened this issue Jun 10, 2018 · 3 comments · Fixed by JuliaLang/julia#28163
Closed

Add @inbounds to generic_matvecmul! #532

dlfivefifty opened this issue Jun 10, 2018 · 3 comments · Fixed by JuliaLang/julia#28163
Labels
performance Must go faster

Comments

@dlfivefifty
Copy link
Contributor

dlfivefifty commented Jun 10, 2018

I'm a bit confused why generic_matvecmul! doesn't use @inbounds for the forlloops, as it makes it 5x faster:

julia> import LinearAlgebra: lapack_size

julia> function generic_matvecmul!(C::AbstractVector{R}, tA, A::AbstractVecOrMat, B::AbstractVector) where R
           mB = length(B)
           mA, nA = lapack_size(tA, A)
           if mB != nA
               throw(DimensionMismatch("matrix A has dimensions ($mA,$nA), vector B has length $mB"))
           end
           if mA != length(C)
               throw(DimensionMismatch("result C has length $(length(C)), needs length $mA"))
           end

           Astride = size(A, 1)

           if tA == 'T'  # fastest case
               for k = 1:mA
                   aoffs = (k-1)*Astride
                   if mB == 0
                       s = zero(R)
                   else
                       s = zero(A[aoffs + 1]*B[1] + A[aoffs + 1]*B[1])
                   end
                   for i = 1:nA
                       s += transpose(A[aoffs+i]) * B[i]
                   end
                   C[k] = s
               end
           elseif tA == 'C'
               for k = 1:mA
                   aoffs = (k-1)*Astride
                   if mB == 0
                       s = zero(R)
                   else
                       s = zero(A[aoffs + 1]*B[1] + A[aoffs + 1]*B[1])
                   end
                   for i = 1:nA
                       s += A[aoffs + i]'B[i]
                   end
                   C[k] = s
               end
           else # tA == 'N'
               @inbounds for i = 1:mA
                   if mB == 0
                       C[i] = zero(R)
                   else
                       C[i] = zero(A[i]*B[1] + A[i]*B[1])
                   end
               end
               @inbounds for k = 1:mB
                   aoffs = (k-1)*Astride
                   b = B[k]
                   for i = 1:mA
                       C[i] += A[aoffs + i] * b
                   end
               end
           end
           C
       end
generic_matvecmul! (generic function with 1 method)

julia> n = 1000; A = randn(n,n); x = randn(n); y = randn(n);

julia> @time generic_matvecmul!(y, 'N', A, x);
  0.000530 seconds (4 allocations: 160 bytes)

julia> @time LinearAlgebra.generic_matvecmul!(y, 'N', A, x);
  0.001840 seconds (4 allocations: 160 bytes)
@Sacha0
Copy link
Member

Sacha0 commented Jun 10, 2018

Regarding the benchmarks, I imagine you performed warmup runs but omitted them from the above (and are aware of BenchmarkTools)? :)

@Sacha0
Copy link
Member

Sacha0 commented Jun 10, 2018

The conclusion to JuliaLang/julia#20469 suggests that judicious use of @inbounds in such generic methods should be alright.

@dlfivefifty
Copy link
Contributor Author

OK, fine, 3x speedup:

julia> @btime generic_matvecmul!(y, 'N', A, x);
  284.819 μs (0 allocations: 0 bytes)

julia> @btime LinearAlgebra.generic_matvecmul!(y, 'N', A, x);
  840.900 μs (0 allocations: 0 bytes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants