Simple multithreading in julia
See the codeSimple Multithreading in Julia
OhMyThreads.jl is meant to be a simple, unambitious package that provides user-friendly ways of doing task-based multithreaded calculations in Julia. Most importantly, with a
focus on data parallelism, it provides an API of higher-order functions (e.g. tmapreduce) as well as a macro API @tasks for ... end (conceptually similar to @threads).
using OhMyThreads: tmapreduce, @tasks
using BenchmarkTools: @btime
using Base.Threads: nthreads
# Variant 1: function API
function mc_parallel(N; ntasks=nthreads())
M = tmapreduce(+, 1:N; ntasks) do i
rand()^2 + rand()^2 < 1.0
end
pi = 4 * M / N
return pi
end
# Variant 2: macro API
function mc_parallel_macro(N; ntasks=nthreads())
M = @tasks for i in 1:N
@set begin
reducer=+
ntasks=ntasks
end
rand()^2 + rand()^2 < 1.0
end
pi = 4 * M / N
return pi
end
N = 100_000_000
mc_parallel(N) # gives, e.g., 3.14159924
@btime mc_parallel($N; ntasks=1) # use a single task (and hence a single thread)
@btime mc_parallel($N) # using all threads
@btime mc_parallel_macro($N) # using all threads
With 5 threads, timings might be something like this:
417.282 ms (14 allocations: 912 bytes)
83.578 ms (38 allocations: 3.08 KiB)
83.573 ms (38 allocations: 3.08 KiB)
(Check out the full Parallel Monte Carlo example if you like.)
For more information, please check out the documentation of the latest release (or the development version if you're curious).
361 followers · starred Feb 2024
80 followers · starred Jan 2024
268 followers · starred Jan 2024
240 followers · starred Jul 2025
Simple multithreading in julia
See the codeSimple Multithreading in Julia
OhMyThreads.jl is meant to be a simple, unambitious package that provides user-friendly ways of doing task-based multithreaded calculations in Julia. Most importantly, with a
focus on data parallelism, it provides an API of higher-order functions (e.g. tmapreduce) as well as a macro API @tasks for ... end (conceptually similar to @threads).
using OhMyThreads: tmapreduce, @tasks
using BenchmarkTools: @btime
using Base.Threads: nthreads
# Variant 1: function API
function mc_parallel(N; ntasks=nthreads())
M = tmapreduce(+, 1:N; ntasks) do i
rand()^2 + rand()^2 < 1.0
end
pi = 4 * M / N
return pi
end
# Variant 2: macro API
function mc_parallel_macro(N; ntasks=nthreads())
M = @tasks for i in 1:N
@set begin
reducer=+
ntasks=ntasks
end
rand()^2 + rand()^2 < 1.0
end
pi = 4 * M / N
return pi
end
N = 100_000_000
mc_parallel(N) # gives, e.g., 3.14159924
@btime mc_parallel($N; ntasks=1) # use a single task (and hence a single thread)
@btime mc_parallel($N) # using all threads
@btime mc_parallel_macro($N) # using all threads
With 5 threads, timings might be something like this:
417.282 ms (14 allocations: 912 bytes)
83.578 ms (38 allocations: 3.08 KiB)
83.573 ms (38 allocations: 3.08 KiB)
(Check out the full Parallel Monte Carlo example if you like.)
For more information, please check out the documentation of the latest release (or the development version if you're curious).
361 followers · starred Feb 2024
80 followers · starred Jan 2024
268 followers · starred Jan 2024
240 followers · starred Jul 2025