Last active
January 8, 2018 05:54
-
-
Save goropikari/18dff0ee027952dabfdc198571b37e8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"toc": true | |
}, | |
"source": [ | |
"<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n", | |
"<div class=\"toc\" style=\"margin-top: 1em;\"><ul class=\"toc-item\"><li><span><a href=\"#simple-summation\" data-toc-modified-id=\"simple-summation-1\"><span class=\"toc-item-num\">1 </span>simple summation</a></span></li><li><span><a href=\"#inner-product\" data-toc-modified-id=\"inner-product-2\"><span class=\"toc-item-num\">2 </span>inner product</a></span></li><li><span><a href=\"#matrix-product\" data-toc-modified-id=\"matrix-product-3\"><span class=\"toc-item-num\">3 </span>matrix product</a></span></li><li><span><a href=\"#$\\pi$\" data-toc-modified-id=\"$\\pi$-4\"><span class=\"toc-item-num\">4 </span>$\\pi$</a></span></li><li><span><a href=\"#Thread-parallel-and-seeds-of-random-number\" data-toc-modified-id=\"Thread-parallel-and-seeds-of-random-number-5\"><span class=\"toc-item-num\">5 </span>Thread parallel and seeds of random number</a></span></li></ul></div>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Julia Version 0.6.2\n", | |
"Commit d386e40 (2017-12-13 18:08 UTC)\n", | |
"Platform Info:\n", | |
" OS: Linux (x86_64-pc-linux-gnu)\n", | |
" CPU: Intel(R) Core(TM) i5-4460T CPU @ 1.90GHz\n", | |
" WORD_SIZE: 64\n", | |
" BLAS: libopenblas (HASWELL)\n", | |
" LAPACK: libopenblas\n", | |
" LIBM: libm\n", | |
" LLVM: libLLVM-3.9.1 (ORCJIT, haswell)\n" | |
] | |
} | |
], | |
"source": [ | |
"versioninfo()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"using BenchmarkTools, ParallelAccelerator" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"Threads.nthreads()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4-element Array{Int64,1}:\n", | |
" 2\n", | |
" 3\n", | |
" 4\n", | |
" 5" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"addprocs()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# simple summation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"sumdirect (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function sumdirect(n::Int)\n", | |
" summation = 0\n", | |
" for i in 1:n\n", | |
" summation += i\n", | |
" end\n", | |
" return summation\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"sumproc (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function sumproc(n::Int)\n", | |
" summation = @parallel (+) for i in 1:n; i; end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"sumthread (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function sumthread(n::Int)\n", | |
" tmpsum = zeros(Int, Threads.nthreads())\n", | |
" Threads.@threads for i in 1:n\n", | |
" tmpsum[Threads.threadid()] += i\n", | |
" end\n", | |
" \n", | |
" return sum(tmpsum)\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"sumacc (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@acc sumacc(n::Int) = sum(1:n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"n = 100\n", | |
"sum(1:n) == sumdirect(n) == sumproc(n) == sumthread(n) == sumacc(n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 16 bytes\n", | |
" allocs estimate: 1\n", | |
" --------------\n", | |
" minimum time: 27.289 ns (0.00% GC)\n", | |
" median time: 30.028 ns (0.00% GC)\n", | |
" mean time: 34.928 ns (5.94% GC)\n", | |
" maximum time: 4.806 μs (97.02% GC)\n", | |
" --------------\n", | |
" samples: 10000\n", | |
" evals/sample: 995" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"n = 2_000_000_000\n", | |
"@benchmark sumdirect(n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 48 bytes\n", | |
" allocs estimate: 2\n", | |
" --------------\n", | |
" minimum time: 49.320 ns (0.00% GC)\n", | |
" median time: 54.919 ns (0.00% GC)\n", | |
" mean time: 71.437 ns (15.59% GC)\n", | |
" maximum time: 7.734 μs (98.11% GC)\n", | |
" --------------\n", | |
" samples: 10000\n", | |
" evals/sample: 988" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark sum(1:n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 39.00 KiB\n", | |
" allocs estimate: 479\n", | |
" --------------\n", | |
" minimum time: 216.440 μs (0.00% GC)\n", | |
" median time: 288.176 μs (0.00% GC)\n", | |
" mean time: 399.796 μs (2.55% GC)\n", | |
" maximum time: 95.261 ms (0.00% GC)\n", | |
" --------------\n", | |
" samples: 10000\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark sumproc(n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 160 bytes\n", | |
" allocs estimate: 3\n", | |
" --------------\n", | |
" minimum time: 1.798 s (0.00% GC)\n", | |
" median time: 1.810 s (0.00% GC)\n", | |
" mean time: 1.816 s (0.00% GC)\n", | |
" maximum time: 1.839 s (0.00% GC)\n", | |
" --------------\n", | |
" samples: 3\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark sumthread(n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 352 bytes\n", | |
" allocs estimate: 10\n", | |
" --------------\n", | |
" minimum time: 48.410 μs (0.00% GC)\n", | |
" median time: 52.126 μs (0.00% GC)\n", | |
" mean time: 60.791 μs (0.00% GC)\n", | |
" maximum time: 2.352 ms (0.00% GC)\n", | |
" --------------\n", | |
" samples: 10000\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark sumacc(n)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# inner product" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"n = 100_000\n", | |
"a, b = rand(n), rand(n);" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"innerdirect (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function innerdirect(a::Vector{Float64}, b::Vector{Float64}, n::Int)\n", | |
" inprod = 0.0\n", | |
" for i in 1:n\n", | |
" inprod += a[i] * b[i]\n", | |
" end\n", | |
" return inprod\n", | |
"end " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"innerproc (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function innerproc(a::Vector{Float64}, b::Vector{Float64}, n::Int)\n", | |
" inprod = @parallel (+) for i in 1:n\n", | |
" a[i] * b[i]\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"innerthread (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function innerthread(a::Vector{Float64}, b::Vector{Float64}, n::Int)\n", | |
" inprods = zeros(Threads.nthreads())\n", | |
" Threads.@threads for i in 1:n\n", | |
" inprods[Threads.threadid()] += a[i] * b[i]\n", | |
" end\n", | |
" \n", | |
" inprod = 0.0\n", | |
" for i in 1:Threads.nthreads()\n", | |
" inprod += inprods[i]\n", | |
" end\n", | |
" \n", | |
" return inprod\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"inneracc (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 19, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@acc inneracc(a::Vector{Float64}, b::Vector{Float64}) = a' * b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"false" | |
] | |
}, | |
"execution_count": 20, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a' * b == innerdirect(a,b,n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 21, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"isapprox(a' * b, innerdirect(a,b,n))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 22, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"isapprox(a' * b, innerproc(a,b,n))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"isapprox(a' * b, innerthread(a,b,n))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 24, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"isapprox(a' * b, inneracc(a, b))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 16 bytes\n", | |
" allocs estimate: 1\n", | |
" --------------\n", | |
" minimum time: 51.203 μs (0.00% GC)\n", | |
" median time: 51.671 μs (0.00% GC)\n", | |
" mean time: 53.231 μs (0.00% GC)\n", | |
" maximum time: 209.817 μs (0.00% GC)\n", | |
" --------------\n", | |
" samples: 10000\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 25, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark a' * b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 16 bytes\n", | |
" allocs estimate: 1\n", | |
" --------------\n", | |
" minimum time: 111.514 μs (0.00% GC)\n", | |
" median time: 116.100 μs (0.00% GC)\n", | |
" mean time: 126.662 μs (0.00% GC)\n", | |
" maximum time: 284.776 μs (0.00% GC)\n", | |
" --------------\n", | |
" samples: 10000\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 26, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark innerdirect(a,b,n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 46.52 KiB\n", | |
" allocs estimate: 540\n", | |
" --------------\n", | |
" minimum time: 1.902 ms (0.00% GC)\n", | |
" median time: 2.567 ms (0.00% GC)\n", | |
" mean time: 3.054 ms (0.46% GC)\n", | |
" maximum time: 12.361 ms (73.64% GC)\n", | |
" --------------\n", | |
" samples: 1627\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 27, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark innerproc(a,b,n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 176 bytes\n", | |
" allocs estimate: 3\n", | |
" --------------\n", | |
" minimum time: 110.826 μs (0.00% GC)\n", | |
" median time: 114.151 μs (0.00% GC)\n", | |
" mean time: 166.188 μs (0.00% GC)\n", | |
" maximum time: 9.297 ms (0.00% GC)\n", | |
" --------------\n", | |
" samples: 10000\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 28, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark innerthread(a,b,n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 352 bytes\n", | |
" allocs estimate: 8\n", | |
" --------------\n", | |
" minimum time: 115.428 μs (0.00% GC)\n", | |
" median time: 117.594 μs (0.00% GC)\n", | |
" mean time: 126.147 μs (0.00% GC)\n", | |
" maximum time: 2.099 ms (0.00% GC)\n", | |
" --------------\n", | |
" samples: 10000\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 29, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark inneracc(a, b)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# matrix product" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"n = 500\n", | |
"a, b = rand(n, n), rand(n, n);" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"matrixdirect (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 31, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function matrixdirect(a::Matrix{Float64}, b::Matrix{Float64}, n::Int)\n", | |
" c = zeros(n,n)\n", | |
" for j in 1:n\n", | |
" for i in 1:n\n", | |
" for k in 1:n\n", | |
" c[i,j] += a[i,k] * b[k,j]\n", | |
" end\n", | |
" end\n", | |
" end\n", | |
" return c\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"matrixproc (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 32, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function matrixproc(a::Matrix{Float64}, b::Matrix{Float64}, n::Int)\n", | |
" c = SharedMatrix{Float64}(n, n, init=0)\n", | |
" @sync @parallel for j in 1:n\n", | |
" for i in 1:n\n", | |
" for k in 1:n\n", | |
" c[i,j] += a[i,k] * b[k,j]\n", | |
" end\n", | |
" end\n", | |
" end\n", | |
" return c\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"matrixthread (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 33, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function matrixthread(a::Matrix{Float64}, b::Matrix{Float64}, n::Int)\n", | |
" c = zeros(n, n)\n", | |
" Threads.@threads for j in 1:n\n", | |
" for i in 1:n\n", | |
" for k in 1:n\n", | |
" c[i,j] += a[i,k] * b[k,j]\n", | |
" end\n", | |
" end\n", | |
" end\n", | |
" return c\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"matrixacc (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@acc matrixacc(a::Matrix{Float64}, b::Matrix{Float64}) = a * b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": { | |
"run_control": { | |
"marked": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 35, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"isapprox(a * b, matrixdirect(a,b,n))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 36, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"isapprox(a * b, matrixproc(a,b,n))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 37, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"isapprox(a * b, matrixthread(a,b,n))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"true" | |
] | |
}, | |
"execution_count": 38, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"isapprox(a * b, matrixacc(a,b))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 1.91 MiB\n", | |
" allocs estimate: 2\n", | |
" --------------\n", | |
" minimum time: 3.243 ms (0.00% GC)\n", | |
" median time: 3.432 ms (0.00% GC)\n", | |
" mean time: 3.788 ms (4.59% GC)\n", | |
" maximum time: 25.271 ms (0.00% GC)\n", | |
" --------------\n", | |
" samples: 1317\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 39, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark a * b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 1.91 MiB\n", | |
" allocs estimate: 2\n", | |
" --------------\n", | |
" minimum time: 422.659 ms (0.00% GC)\n", | |
" median time: 428.883 ms (0.00% GC)\n", | |
" mean time: 429.892 ms (0.04% GC)\n", | |
" maximum time: 439.370 ms (0.00% GC)\n", | |
" --------------\n", | |
" samples: 12\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark matrixdirect(a,b,n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 53.70 KiB\n", | |
" allocs estimate: 1392\n", | |
" --------------\n", | |
" minimum time: 503.624 ms (0.00% GC)\n", | |
" median time: 586.910 ms (0.00% GC)\n", | |
" mean time: 579.757 ms (0.00% GC)\n", | |
" maximum time: 655.664 ms (0.00% GC)\n", | |
" --------------\n", | |
" samples: 9\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 41, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark matrixproc(a,b,n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 1.91 MiB\n", | |
" allocs estimate: 3\n", | |
" --------------\n", | |
" minimum time: 127.710 ms (0.00% GC)\n", | |
" median time: 128.379 ms (0.00% GC)\n", | |
" mean time: 131.338 ms (0.12% GC)\n", | |
" maximum time: 148.098 ms (0.00% GC)\n", | |
" --------------\n", | |
" samples: 39\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 42, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark matrixthread(a,b,n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 1.91 MiB\n", | |
" allocs estimate: 7\n", | |
" --------------\n", | |
" minimum time: 3.383 ms (0.00% GC)\n", | |
" median time: 5.850 ms (0.00% GC)\n", | |
" mean time: 6.540 ms (3.26% GC)\n", | |
" maximum time: 26.686 ms (0.00% GC)\n", | |
" --------------\n", | |
" samples: 762\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 43, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark matrixacc(a,b)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"collapsed": true | |
}, | |
"source": [ | |
"# $\\pi$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"pidirect (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 44, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function pidirect(n)\n", | |
" hit = 0\n", | |
" for i in 1:n\n", | |
" x, y = rand(), rand()\n", | |
" if x^2 + y^2 < 1.\n", | |
" hit += 1\n", | |
" end\n", | |
" end\n", | |
" return 4. * hit / n\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"piproc (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 45, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function piproc(n) \n", | |
" estpi = @parallel (+) for i in 1:n\n", | |
" x, y = rand(), rand()\n", | |
" ifelse(x^2 + y^2 < 1., 4./n, 0.0)\n", | |
" end\n", | |
" return estpi\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"piacc (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 46, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@acc function piacc(n)\n", | |
" hit = sum( (rand(n).^2 + rand(n).^2) .< 1.)\n", | |
" return 4. * hit / n\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(3.192, 3.0040000000000022, 3.236)" | |
] | |
}, | |
"execution_count": 47, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"n = 1000\n", | |
"pidirect(n), piproc(n), piacc(n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"100000000" | |
] | |
}, | |
"execution_count": 48, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"n = 100_000_000" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 16 bytes\n", | |
" allocs estimate: 1\n", | |
" --------------\n", | |
" minimum time: 983.285 ms (0.00% GC)\n", | |
" median time: 986.297 ms (0.00% GC)\n", | |
" mean time: 992.197 ms (0.00% GC)\n", | |
" maximum time: 1.013 s (0.00% GC)\n", | |
" --------------\n", | |
" samples: 6\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 49, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark pidirect(n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 39.91 KiB\n", | |
" allocs estimate: 515\n", | |
" --------------\n", | |
" minimum time: 178.021 ms (0.00% GC)\n", | |
" median time: 197.273 ms (0.00% GC)\n", | |
" mean time: 202.848 ms (0.00% GC)\n", | |
" maximum time: 266.001 ms (0.00% GC)\n", | |
" --------------\n", | |
" samples: 25\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 50, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark piproc(n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 51, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"BenchmarkTools.Trial: \n", | |
" memory estimate: 5.11 KiB\n", | |
" allocs estimate: 70\n", | |
" --------------\n", | |
" minimum time: 2.103 s (0.00% GC)\n", | |
" median time: 2.131 s (0.00% GC)\n", | |
" mean time: 2.157 s (0.00% GC)\n", | |
" maximum time: 2.238 s (0.00% GC)\n", | |
" --------------\n", | |
" samples: 3\n", | |
" evals/sample: 1" | |
] | |
}, | |
"execution_count": 51, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@benchmark piacc(n)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Thread parallel and seeds of random number" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 52, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4-element Array{Float64,1}:\n", | |
" 0.204092\n", | |
" 0.680604\n", | |
" 0.046407\n", | |
" 0.587938" | |
] | |
}, | |
"execution_count": 52, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = zeros(Threads.nthreads())\n", | |
"Threads.@threads for j in 1:Threads.nthreads()\n", | |
" a[Threads.threadid()] = rand()\n", | |
"end\n", | |
"a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 53, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4-element Array{Float64,1}:\n", | |
" 0.577365\n", | |
" 0.577365\n", | |
" 0.577365\n", | |
" 0.577365" | |
] | |
}, | |
"execution_count": 53, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = zeros(Threads.nthreads())\n", | |
"Threads.@threads for i in 1:Threads.nthreads()\n", | |
" a[Threads.threadid()] = rand()\n", | |
"end\n", | |
"a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 54, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4-element Array{Float64,1}:\n", | |
" 0.758892\n", | |
" 0.876163\n", | |
" 0.758892\n", | |
" 0.758892" | |
] | |
}, | |
"execution_count": 54, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = zeros(Threads.nthreads())\n", | |
"Threads.@threads for i in 1:Threads.nthreads()\n", | |
" srand(Threads.threadid())\n", | |
"end\n", | |
"\n", | |
"\n", | |
"Threads.@threads for i in 1:Threads.nthreads()\n", | |
" a[Threads.threadid()] = rand()\n", | |
"end\n", | |
"a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.23603334566204692\n", | |
"0.34651701419196046\n", | |
"0.3127069683360675\n", | |
"0.00790928339056074\n" | |
] | |
} | |
], | |
"source": [ | |
"srand(1)\n", | |
"for i in 1:Threads.nthreads()\n", | |
" println(rand())\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.36679641243992434\n", | |
"0.5238785747844117\n", | |
"0.21025643065331256\n", | |
"0.8193381354589955\n" | |
] | |
} | |
], | |
"source": [ | |
"srand(2)\n", | |
"for i in 1:Threads.nthreads()\n", | |
" println(rand())\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.8116984049958615\n", | |
"0.9884323655013432\n", | |
"0.8076220876500786\n", | |
"0.9700908450487538\n" | |
] | |
} | |
], | |
"source": [ | |
"srand(3)\n", | |
"for i in 1:Threads.nthreads()\n", | |
" println(rand())\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.680079235935741\n", | |
"0.8744368931121158\n", | |
"0.9240700869281602\n", | |
"0.9293362188436958\n" | |
] | |
} | |
], | |
"source": [ | |
"srand(4)\n", | |
"for i in 1:Threads.nthreads()\n", | |
" println(rand())\n", | |
"end" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Julia 0.6.1", | |
"language": "julia", | |
"name": "julia-0.6" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"mimetype": "application/julia", | |
"name": "julia", | |
"version": "0.6.2" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"toc_cell": true, | |
"toc_position": {}, | |
"toc_section_display": "block", | |
"toc_window_display": true | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment