Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active February 20, 2021 05:37
Show Gist options
  • Save genkuroki/2e8f334f246775ff28d4ebb27322b767 to your computer and use it in GitHub Desktop.
Save genkuroki/2e8f334f246775ff28d4ebb27322b767 to your computer and use it in GitHub Desktop.
ThreadPools.tmap example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "https://github.com/tro3/ThreadPools.jl"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "VERSION",
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 1,
"data": {
"text/plain": "v\"1.6.0-rc1\""
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using ThreadPools\nusing Random",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function pimc(N, rng = Random.default_rng())\n c = 0\n for i in 1:N\n c += rand(rng)^2 + rand(rng)^2 ≤ 1\n end\n 4c/N\nend\n\n@time pimc(10^9)\n@time pimc(10^9)\n@time pimc(10^9)",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": " 3.236218 seconds (12 allocations: 19.656 KiB)\n 3.230368 seconds\n 3.237019 seconds\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "3.141570068"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "count_isindisk(N, rng = Random.default_rng()) =\n count(rand(rng)^2 + rand(rng)^2 ≤ 1 for _ in 1:N)\n\npimc_count(N, rng = Random.default_rng()) = 4count_isindisk(N, rng)/N\n\n@time pimc_count(10^9)\n@time pimc_count(10^9)\n@time pimc_count(10^9)",
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": " 2.969974 seconds\n 2.957641 seconds\n 2.960993 seconds\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "3.141619556"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "splitint(N, n) = ((q, r) = fldmod(N, n); [fill(q, n-1); q+r])\n\ncount_isindisk(N, rng = Random.default_rng()) =\n count(rand(rng)^2 + rand(rng)^2 ≤ 1 for _ in 1:N)\n\npimc_ThreadPools(N) =\n 4sum(tmap(count_isindisk, splitint(N, Base.Threads.nthreads())))/N\n\n@time pimc_ThreadPools(10^9)\n@time pimc_ThreadPools(10^9)\n@time pimc_ThreadPools(10^9)",
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": " 0.981527 seconds (161.94 k allocations: 9.436 MiB, 4.22% compilation time)\n 0.875493 seconds (262 allocations: 23.484 KiB)\n 0.885272 seconds (271 allocations: 23.703 KiB)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "3.14165234"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using BenchmarkTools\n\n@btime pimc(10^7)\n@btime pimc_count(10^7)\n@btime pimc_ThreadPools(10^7)",
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": " 31.696 ms (0 allocations: 0 bytes)\n 29.514 ms (0 allocations: 0 bytes)\n 7.849 ms (138 allocations: 13.70 KiB)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "3.1414952"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"_draft": {
"nbviewer_url": "https://gist.github.com/2e8f334f246775ff28d4ebb27322b767"
},
"gist": {
"id": "2e8f334f246775ff28d4ebb27322b767",
"data": {
"description": "ThreadPools.tmap example",
"public": true
}
},
"kernelspec": {
"name": "julia-1.6-depwarn-o3",
"display_name": "Julia 1.6.0-rc1 depwarn -O3",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.6.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