Skip to content

Instantly share code, notes, and snippets.

@cako
Created March 14, 2018 22:25
Show Gist options
  • Save cako/60dc181ebd617fd1b5b3fb916ea8360b to your computer and use it in GitHub Desktop.
Save cako/60dc181ebd617fd1b5b3fb916ea8360b to your computer and use it in GitHub Desktop.
#!/usr/bin/env julia
function sdot(n, x, y)
a = 0
@inbounds @fastmath @simd for i=1:n
a += x[i]*y[i]
end
a
end
function pdot(n, x, y)
@fastmath @parallel (+) for i=1:n
x[i]*y[i]
end
end
n = 10000000
x = [ 1. for i=1:n]
y = [ i for i=1.:n]
println("Naive Julia")
println(@sprintf(" %.2f", sdot(n, x, y)))
@time for i=1:3
sdot(n, x, y)
end
println("\nNative Julia")
println(@sprintf(" %.2f", dot(x, y)))
@time for i=1:3
dot(x, y)
end
println("\nIdiomatic parallel Julia")
println(@sprintf(" %.2f", pdot(n, x, y)))
@time for i=1:3
pdot(n, x, y)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment