-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Julia implementation using
OhMyThreads.jl
- Loading branch information
Showing
4 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Manifest.toml |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[deps] | ||
OhMyThreads = "67456a42-1dca-4109-a031-0a68de7e3ad5" | ||
|
||
[compat] | ||
OhMyThreads = "0.7" | ||
julia = "1.10" |
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
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) |
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
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 "${@}" |