Skip to content

Instantly share code, notes, and snippets.

@bicycle1885
Created June 5, 2019 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bicycle1885/e3be8a4e0d543c488f2f752eed2b08fa to your computer and use it in GitHub Desktop.
Save bicycle1885/e3be8a4e0d543c488f2f752eed2b08fa to your computer and use it in GitHub Desktop.
Parallel execution
macro par(expr)
thunk = esc(:(()->($expr)))
quote
local task = Task($thunk)
task.sticky = false
schedule(task)
task
end
end
function fib(n)
if n ≤ 1
return 1
else
return fib(n-1) + fib(n-2)
end
end
fetch(@par fib(3)) # compile
const N = 40
const N_TASKS = 128
println("Sequential:")
@time [fib(N) for _ in 1:N_TASKS]
println("Parallel:")
@time fetch.([@par fib(N) for _ in 1:N_TASKS])
@bicycle1885
Copy link
Author

bicycle1885 commented Jun 5, 2019

~/w/julia (master|…) [1] $ env JULIA_NUM_THREADS=4 ./julia par.jl
Sequential:
 61.244795 seconds (51.93 k allocations: 2.690 MiB)
Parallel:
 16.178837 seconds (471.02 k allocations: 24.798 MiB)

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