Last active
March 12, 2021 09:35
-
-
Save genkuroki/a6dcca1fa79c297411807180f26b235b to your computer and use it in GitHub Desktop.
@my_distributed
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": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@which rand()", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 1, | |
"data": { | |
"text/plain": "rand() in Random at C:\\Julia-1.7.0-DEV\\share\\julia\\stdlib\\v1.7\\Random\\src\\Random.jl:256", | |
"text/html": "rand() in Random at <a href=\"file://C:/buildbot/worker/package_win64/build/usr/share/julia/stdlib/v1.7/Random/src/Random.jl\" target=\"_blank\">C:\\Julia-1.7.0-DEV\\share\\julia\\stdlib\\v1.7\\Random\\src\\Random.jl:256</a>" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "using Random\nusing Random: default_rng\n\n@which rand(default_rng())", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 2, | |
"data": { | |
"text/plain": "rand(rng::AbstractRNG) in Random at C:\\Julia-1.7.0-DEV\\share\\julia\\stdlib\\v1.7\\Random\\src\\Random.jl:256", | |
"text/html": "rand(rng::<b>AbstractRNG</b>) in Random at <a href=\"file://C:/buildbot/worker/package_win64/build/usr/share/julia/stdlib/v1.7/Random/src/Random.jl\" target=\"_blank\">C:\\Julia-1.7.0-DEV\\share\\julia\\stdlib\\v1.7\\Random\\src\\Random.jl:256</a>" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function pi_mc_localrng(N)\n rng = default_rng()\n c = 0\n for i in 1:N\n c += ifelse(rand(rng)^2 + rand(rng)^2 ≤ 1, 1, 0)\n end\n 4c/N\nend\n\n@time pi_mc_localrng(10^8)\n@time pi_mc_localrng(10^8)\n@time pi_mc_localrng(10^8)", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": " 0.320871 seconds\n 0.318611 seconds\n 0.331782 seconds\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 3, | |
"data": { | |
"text/plain": "3.1415676" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "myrng = default_rng()\n\nfunction pi_mc_globalrng(N)\n rng = myrng\n c = 0\n for i in 1:N\n c += ifelse(rand(rng)^2 + rand(rng)^2 ≤ 1, 1, 0)\n end\n 4c/N\nend\n\n@time pi_mc_globalrng(10^8)", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": " 18.565592 seconds (500.00 M allocations: 7.451 GiB, 8.88% gc time, 0.06% compilation time)\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 4, | |
"data": { | |
"text/plain": "3.14160424" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "myrng = default_rng()\n\nfunction pi_mc_globalrng_annotated_abstract(N)\n rng = myrng::AbstractRNG\n c = 0\n for i in 1:N\n c += ifelse(rand(rng)^2 + rand(rng)^2 ≤ 1, 1, 0)\n end\n 4c/N\nend\n\n@time pi_mc_globalrng_annotated_abstract(10^8)", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": " 6.704718 seconds (200.00 M allocations: 2.980 GiB, 10.01% gc time)\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 5, | |
"data": { | |
"text/plain": "3.14156632" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "myrng = default_rng()\n\nfunction pi_mc_globalrng_annotated(N)\n rng = myrng::MersenneTwister\n c = 0\n for i in 1:N\n c += ifelse(rand(rng)^2 + rand(rng)^2 ≤ 1, 1, 0)\n end\n 4c/N\nend\n\n@time pi_mc_globalrng_annotated(10^8)\n@time pi_mc_globalrng_annotated(10^8)\n@time pi_mc_globalrng_annotated(10^8)", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": " 0.362105 seconds\n 0.319010 seconds\n 0.320158 seconds\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 6, | |
"data": { | |
"text/plain": "3.14173388" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "using Distributed\nrmprocs(procs()[2:end])\naddprocs(8)\n@show workers();", | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "workers() = [2, 3, 4, 5, 6, 7, 8, 9]\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# See julia/stdlib/Distributed/src/macros.jl\n\nusing Distributed: preduce, pfor, make_preduce_body, make_pfor_body\n\nfunction my_make_preduce_body(var, prebody, body)\n quote\n function (reducer, R, lo::Int, hi::Int)\n $(esc(prebody))\n $(esc(var)) = R[lo]\n ac = $(esc(body))\n if lo != hi\n for $(esc(var)) in R[(lo+1):hi]\n ac = reducer(ac, $(esc(body)))\n end\n end\n ac\n end\n end\nend\n\nfunction my_make_pfor_body(var, prebody, body)\n quote\n function (R, lo::Int, hi::Int)\n $(esc(prebody))\n for $(esc(var)) in R[lo:hi]\n $(esc(body))\n end\n end\n end\nend\n\n\"\"\"\n @my_distributed\n\nA distributed memory, parallel for loop of the form:\n\n```julia\n@my_distributed begin\n prebody\nend [reducer] for var = range\n body\nend\n```\n\"\"\"\nmacro my_distributed(args...)\n na = length(args)\n if na==2\n prebody, loop = args\n elseif na==3\n prebody, reducer, loop = args\n else\n throw(ArgumentError(\"wrong number of arguments to @my_distributed\"))\n end\n if !isa(loop,Expr) || loop.head !== :for\n error(\"malformed @my_distributed loop\")\n end\n var = loop.args[1].args[1]\n r = loop.args[1].args[2]\n body = loop.args[2]\n if na==2\n syncvar = esc(Base.sync_varname)\n return quote\n local ref = pfor($(my_make_pfor_body(var, prebody, body)), $(esc(r)))\n if $(Expr(:islocal, syncvar))\n put!($syncvar, ref)\n end\n ref\n end\n else\n return :(preduce($(esc(reducer)), $(my_make_preduce_body(var, prebody, body)), $(esc(r))))\n end\nend\n\n@doc @my_distributed", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 8, | |
"data": { | |
"text/plain": "\u001b[36m @my_distributed\u001b[39m\n\n A distributed memory, parallel for loop of the form:\n\n\u001b[36m @my_distributed begin\u001b[39m\n\u001b[36m prebody\u001b[39m\n\u001b[36m end [reducer] for var = range\u001b[39m\n\u001b[36m body\u001b[39m\n\u001b[36m end\u001b[39m", | |
"text/markdown": "```\n@my_distributed\n```\n\nA distributed memory, parallel for loop of the form:\n\n```julia\n@my_distributed begin\n prebody\nend [reducer] for var = range\n body\nend\n```\n", | |
"text/latex": "\\begin{verbatim}\n@my_distributed\n\\end{verbatim}\nA distributed memory, parallel for loop of the form:\n\n\\begin{verbatim}\n@my_distributed begin\n prebody\nend [reducer] for var = range\n body\nend\n\\end{verbatim}\n" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function pi_mc_distributed(N)\n c = @distributed (+) for i in 1:N\n ifelse(rand()^2 + rand()^2 ≤ 1, 1, 0)\n end\n 4c/N\nend\n\n@time pi_mc_distributed(10^8)\n@time pi_mc_distributed(10^8)\n@time pi_mc_distributed(10^8)", | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": " 2.228225 seconds (1.00 M allocations: 59.814 MiB, 0.47% gc time, 14.64% compilation time)\n 0.268797 seconds (656 allocations: 28.922 KiB)\n 0.258660 seconds (657 allocations: 28.172 KiB)\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 9, | |
"data": { | |
"text/plain": "3.14168804" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@everywhere using Random: default_rng\n\nfunction pi_mc_my_distributed(N)\n c = @my_distributed begin\n rng = default_rng()\n end (+) for i in 1:N\n ifelse(rand(rng)^2 + rand(rng)^2 ≤ 1, 1, 0)\n end\n 4c/N\nend\n\n@time pi_mc_my_distributed(10^8)\n@time pi_mc_my_distributed(10^8)\n@time pi_mc_my_distributed(10^8)", | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": " 0.146287 seconds (51.06 k allocations: 2.936 MiB, 8.52% compilation time)\n 0.086861 seconds (659 allocations: 28.922 KiB)\n 0.087451 seconds (656 allocations: 28.141 KiB)\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 10, | |
"data": { | |
"text/plain": "3.1417494" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function f()\n @my_distributed begin\n id = myid()\n end for i in 1:8\n @info id\n end\nend\n\nf()\nsleep(1)\nprint()", | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39m2\n\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39m3\n\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39m5\n\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39m7\n\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39m9\n\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39m6\n\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39m8\n\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39m4\n", | |
"name": "stderr" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"@webio": { | |
"lastKernelId": null, | |
"lastCommId": null | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/a6dcca1fa79c297411807180f26b235b" | |
}, | |
"gist": { | |
"id": "a6dcca1fa79c297411807180f26b235b", | |
"data": { | |
"description": "@my_distributed", | |
"public": true | |
} | |
}, | |
"kernelspec": { | |
"name": "julia-1.7-depwarn-o3", | |
"display_name": "Julia 1.7.0-DEV depwarn -O3", | |
"language": "julia" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"name": "julia", | |
"mimetype": "application/julia", | |
"version": "1.7.0" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"base_numbering": 1, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment