Skip to content

Instantly share code, notes, and snippets.

@ChrisRackauckas
Last active August 8, 2016 06:25
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 ChrisRackauckas/6970aa6c3fa42c987b63dc9fe21c48fd to your computer and use it in GitHub Desktop.
Save ChrisRackauckas/6970aa6c3fa42c987b63dc9fe21c48fd to your computer and use it in GitHub Desktop.
function threadstest1{N}(A::Array{Float64},b::Vector{Float64},k::Vector{Array{Float64,N}})
@inbounds for i in eachindex(A)
A[i]+= sin(b[1]*k[1][i] + b[2]*k[2][i] + b[3]*k[3][i] + b[5]*k[5][i]) +
exp(b[7]*k[7][i] + b[8]*k[8][i] + b[10]*k[10][i] + b[11]*k[11][i]) +
erf(b[12]*k[12][i] + b[14]*k[14][i] + b[15]*k[15][i] + b[16]*k[16][i]) +
(b[18]*k[18][i] + b[19]*k[19][i] + b[20]*k[20][i] + b[21]*k[21][i]) +
(b[22]*k[22][i] + b[23]*k[23][i] + b[24]*k[24][i] + b[25]*k[25][i]) +
(b[26]*k[26][i] + b[27]*k[27][i] + b[28]*k[28][i] + b[29]*k[29][i]) +
(b[30]*k[30][i] + b[31]*k[31][i] + b[32]*k[32][i] + b[33]*k[33][i]) +
(b[34]*k[34][i] + b[35]*k[35][i])
end
A
end
function threadstest2{N}(A::Array{Float64},b::Vector{Float64},k::Vector{Array{Float64,N}})
@inbounds Threads.@threads for i in eachindex(A)
A[i]+= sin(b[1]*k[1][i] + b[2]*k[2][i] + b[3]*k[3][i] + b[5]*k[5][i]) +
exp(b[7]*k[7][i] + b[8]*k[8][i] + b[10]*k[10][i] + b[11]*k[11][i]) +
erf(b[12]*k[12][i] + b[14]*k[14][i] + b[15]*k[15][i] + b[16]*k[16][i]) +
(b[18]*k[18][i] + b[19]*k[19][i] + b[20]*k[20][i] + b[21]*k[21][i]) +
(b[22]*k[22][i] + b[23]*k[23][i] + b[24]*k[24][i] + b[25]*k[25][i]) +
(b[26]*k[26][i] + b[27]*k[27][i] + b[28]*k[28][i] + b[29]*k[29][i]) +
(b[30]*k[30][i] + b[31]*k[31][i] + b[32]*k[32][i] + b[33]*k[33][i]) +
(b[34]*k[34][i] + b[35]*k[35][i])
end
A
end
using BenchmarkTools
function benchmarkthreadtests()
const ds = [10 100 1000 2000 5000]
ts = Array{Any}(length(ds),2)
for i in eachindex(ds)
d = ds[i]
A = rand(d,d); b = rand(35)
k = [rand(d,d)]
for j = 1:34
push!(k,rand(d,d))
end
println("d = $d")
println("Serial")
ts[i,1] = @benchmark threadstest1($A,$b,$k)
println("Threads")
ts[i,2] = @benchmark threadstest2($A,$b,$k)
end
ts
end
ts = benchmarkthreadtests()
meantimes = [mean(x.times) for x in ts]
# Result:
#=
5×2 Array{Float64,2}:
1.24656e5 1.69682e5
1.63851e6 6.97997e6
2.13978e8 6.87377e8
7.20628e8 2.72495e9
4.91433e9 1.68647e10
=#
@ChrisRackauckas
Copy link
Author

This was with 4 threads.

julia> versioninfo()
Julia Version 0.5.0-rc0+99
Commit c10667f* (2016-08-01 07:45 UTC)
Platform Info:
  System: Linux (x86_64-redhat-linux)
  CPU: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
  WORD_SIZE: 64
  BLAS: libopenblas (DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblasp.so.0
  LIBM: libopenlibm
  LLVM: libLLVM-3.7.1 (ORCJIT, broadwell)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment