Skip to content

Instantly share code, notes, and snippets.

@antoine-levitt
Created January 27, 2018 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antoine-levitt/04487571690b4d69dfbcb0b671f648cd to your computer and use it in GitHub Desktop.
Save antoine-levitt/04487571690b4d69dfbcb0b671f648cd to your computer and use it in GitHub Desktop.
using BenchmarkTools
BLAS.set_num_threads(1)
function BLAS2(A,x)
A*x
end
function loop_mn(A,x)
m,n = size(A)
y = zeros(m)
@inbounds @simd for i=1:m
for j=1:n
y[i] += A[i,j]*x[j]
end
end
end
function loop_nm(A,x)
m,n = size(A)
y = zeros(m)
@inbounds @simd for j=1:n
for i=1:m
y[i] += A[i,j]*x[j]
end
end
end
function views_mn(A,x)
m,n = size(A)
y = zeros(m)
@inbounds for i=1:m
@views y[i] = dot(A[i,:],x)
end
end
function noviews_mn(A,x)
m,n = size(A)
y = zeros(m)
@inbounds for i=1:m
y[i] = dot(A[i,:],x)
end
end
function views_nm(A,x)
m,n = size(A)
y = zeros(m)
@inbounds for i=1:n
@views y .+= x[i]*A[:,i]
end
end
function noviews_nm(A,x)
m,n = size(A)
y = zeros(m)
@inbounds for i=1:n
y .+= x[i]*A[:,i]
end
end
m = 10
n = 10000
A = randn(m,n)
x = randn(n)
@btime BLAS2(A,x)
@btime loop_mn(A,x)
@btime loop_nm(A,x)
@btime views_mn(A,x)
@btime views_nm(A,x)
@btime noviews_mn(A,x)
@btime noviews_nm(A,x)
A = [randn(n) for i=1:m]
function array_of_arrays(A,x)
y = zeros(size(A,1))
for i = 1:size(A,1)
y[i] = dot(A[i],x)
end
end
@btime array_of_arrays(A,x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment