Skip to content

Instantly share code, notes, and snippets.

@terasakisatoshi
Last active Aug 26, 2020
Embed
What would you like to do?
針をいっぱい投げる遊び throwing needle many times
function buffon(n_trial)
hit_counter = zero(Bool)
for i in 1:n_trial
position = rand()
φ = (rand() * π) - π / 2
x_right = position + cos(φ) / 2
x_left = position - cos(φ) / 2
is_hit = (x_right >= 1 || x_left <= 0) ? 1 : 0
hit_counter += is_hit
end
approx_π = n_trial/hit_counter * 2
end
using Distributed
function buffon_parallel(n_trial)
hit_counter = @distributed (+) for i in 1:n_trial
position = rand()
φ = (rand() * π) - π / 2
x_right = position + cos(φ) / 2
x_left = position - cos(φ) / 2
(x_right >= 1 || x_left <= 0) ? 1 : 0
end
approx_π = n_trial/hit_counter * 2
end
function main()
@time println(buffon(convert(Int,1e9)))
@time println(buffon_parallel(convert(Int,1e9)))
end
#main()
@terasakisatoshi
Copy link
Author

terasakisatoshi commented Apr 3, 2019

Usage

Prepare package

In julia REPL, install Distributed

julia> using Pkg
julia> Pkg.add("Distributed")

Run Script

$julia -p auto -L buffon.jl
julia> versioninfo()
Julia Version 1.1.0
Commit 80516ca202 (2019-01-21 21:24 UTC)
Platform Info:
  OS: Linux (arm-linux-gnueabihf)
  CPU: ARMv7 Processor rev 4 (v7l)
  WORD_SIZE: 32
  LIBM: libm
  LLVM: libLLVM-6.0.1 (ORCJIT, cortex-a53)
julia> main()
3.1414669167414653
211.599383 seconds (8 allocations: 96 bytes)
3.1416887981625017
 62.875292 seconds (107.00 k allocations: 3.941 MiB, 0.05% gc time)

@terasakisatoshi
Copy link
Author

terasakisatoshi commented May 3, 2019

Reference

Basic code is taken from

@terasakisatoshi
Copy link
Author

terasakisatoshi commented May 3, 2019

Appendix

Julia-1.2.0-pre0

julia> versioninfo()
Julia Version 1.2.0-pre.0
Commit 8a84ba5018 (2019-04-11 05:23 UTC)
Platform Info:
  OS: Linux (arm-linux-gnueabihf)
  CPU: ARMv7 Processor rev 4 (v7l)
  WORD_SIZE: 32
  LIBM: libm
  LLVM: libLLVM-6.0.1 (ORCJIT, cortex-a53)

julia> main()
 3.1414906316814646
211.563841 seconds (79.53 k allocations: 2.876 MiB, 0.01% gc time)
3.141546940061339
207.158560 seconds (566.01 k allocations: 22.122 MiB, 0.16% gc time)
# do again
julia> main()
3.1416427345748077
211.060580 seconds (13 allocations: 336 bytes)
3.1415514158030553
205.530051 seconds (52 allocations: 1.680 KiB)

@terasakisatoshi
Copy link
Author

terasakisatoshi commented Dec 10, 2019

Appendix

RaspberryPi4 (4GB)

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.3.0 (2019-11-26)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> versioninfo()
Julia Version 1.3.0
Commit 46ce4d7933 (2019-11-26 06:09 UTC)
Platform Info:
  OS: Linux (arm-linux-gnueabihf)
  CPU: ARMv7 Processor rev 3 (v7l)
  WORD_SIZE: 32
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, cortex-a72)
Environment:
  JULIA_NUM_THREADS = 4

julia> main()
3.1416308611070387
147.739304 seconds (75.45 k allocations: 2.511 MiB)
3.141568795816276
 42.032871 seconds (1.08 M allocations: 40.447 MiB, 0.16% gc time)

@terasakisatoshi
Copy link
Author

terasakisatoshi commented Mar 27, 2020

julia -p auto -L buffon.jl
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.3.1 (2019-12-30)
 _/ |\__'_|_|_|\__'_|  |
|__/                   |

julia> versioninfo()
Julia Version 1.3.1
Commit 2d5741174c (2019-12-30 21:36 UTC)
Platform Info:
  OS: Linux (arm-linux-gnueabihf)
  CPU: ARMv7 Processor rev 3 (v7l)
  WORD_SIZE: 32
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, cortex-a72)
Environment:
  JULIA_NUM_THREADS = 4

julia> main()
3.1415984784811313
141.060657 seconds (75.45 k allocations: 2.511 MiB)
3.141669018389191
 40.068105 seconds (1.08 M allocations: 40.449 MiB, 0.18% gc time)

@terasakisatoshi
Copy link
Author

terasakisatoshi commented Mar 27, 2020

pi@rpi4:~/jlbench $ jldev -p auto -L buffon.jl
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.4.0 (2020-03-21)
 _/ |\__'_|_|_|\__'_|  |
|__/                   |

julia> versioninfo()
Julia Version 1.4.0
Commit b8e9a9ecc6 (2020-03-21 16:36 UTC)
Platform Info:
  OS: Linux (arm-linux-gnueabihf)
  CPU: ARMv7 Processor rev 3 (v7l)
  WORD_SIZE: 32
  LIBM: libopenlibm
  LLVM: libLLVM-8.0.1 (ORCJIT, cortex-a72)
Environment:
  JULIA_NUM_THREADS = 4

julia> main()
3.1415344307516286
119.112676 seconds (474.79 k allocations: 14.237 MiB, 0.02% gc time)
3.141705696050348
 34.127721 seconds (752.97 k allocations: 27.785 MiB, 0.11% gc time)

@terasakisatoshi
Copy link
Author

terasakisatoshi commented Aug 26, 2020

pi@raspberrypi:~ $ julia -p auto -L buffon.jl
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.6.0-DEV.718 (2020-08-25)
 _/ |\__'_|_|_|\__'_|  |  Commit 3d04d15c31* (0 days old master)
|__/                   |

julia> versioninfo()
Julia Version 1.6.0-DEV.718
Commit 3d04d15c31* (2020-08-25 10:29 UTC)
Platform Info:
  OS: Linux (arm-linux-gnueabihf)
  CPU: ARMv7 Processor rev 3 (v7l)
  WORD_SIZE: 32
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, cortex-a72)

julia> main()
3.1416373061286977
124.725025 seconds (402.14 k allocations: 14.191 MiB, 0.02% gc time)
3.141535930876609
 35.777320 seconds (77.27 k allocations: 3.251 MiB, 0.06% gc time)

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