Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Julia implementation using OhMyThreads.jl #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions julia_ohmythreads_pi_dir/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Manifest.toml
8 changes: 8 additions & 0 deletions julia_ohmythreads_pi_dir/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[deps]
OhMyThreads = "67456a42-1dca-4109-a031-0a68de7e3ad5"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[compat]
OhMyThreads = "0.7"
Pkg = "1"
julia = "1.10"
48 changes: 48 additions & 0 deletions julia_ohmythreads_pi_dir/pi.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env julia

using Pkg
Pkg.activate(@__DIR__)
Pkg.resolve()
Pkg.instantiate()

using Base.Threads: nthreads
using OhMyThreads: tmapreduce

function _picalc(numsteps)
slice = 1 / numsteps

return tmapreduce(+, 1:numsteps; ntasks=nthreads()) do i
4.0 / (1.0 + ((i - 0.5) * slice) ^ 2)
end * slice
end

function picalc(numsteps)

println("Calculating PI using:")
println(" ", numsteps, " slices")
println(" ", nthreads(), " thread(s)")

start = time()
mypi = _picalc(numsteps)
elapsed = time() - start

println("Obtained value of PI: ", mypi)
println("Time taken: ", round(elapsed; digits=3), " seconds")

end

numsteps = if length(ARGS) > 0
parse(Int, ARGS[1])
else
1_000_000_000
end

# Warm up kernel
print(" Warming up...")
warms = time()
_picalc(10)
warmt = time() - warms
println("done. [", round(warmt; digits=3), "s]\n")

# Run the full example
picalc(numsteps)
7 changes: 7 additions & 0 deletions julia_ohmythreads_pi_dir/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

if [[ -z "${JULIA_NUM_THREADS}" ]]; then
export JULIA_NUM_THREADS="${OMP_NUM_THREADS:-1}"
fi

julia pi.jl "${@}"