Hi. I need to use parallel programming and I’ve been trying to play around with some basic parts. When I try to benchmark a simple threaded function, my laptop freezes for over five minutes and I resort to doing a hard shutdown.
using Distributed
using BenchmarkTools
using .Threads
function test_serial(m)
u = zeros(Int64, m)
for i in eachindex(u)
u[i] = threadid()
end
return u
end
function test_threads(m)
u = zeros(Int64, m)
Threads.@threads for i in eachindex(u)
u[i] = threadid()
end
return u
end
m = 100_000_000
@benchmark test_serial($m)
@benchmark test_threads($m)
When m
above is 100M, the code finishes no problem. When m
is 1B, test_serial
will benchmark two samples, and it is finished in about 6 seconds, but test_threads
freezes my laptop.
Doing Ctrl J K doesn’t stop it, nor does Ctrl Alt Del. I’m guessing this is because all of my processors are occupied. Is there any way I can perform these functions on arrays this long and benchmark them without risking the health of my laptop? Maybe a timeout? Tasks?
Thanks.
julia> @benchmark test_serial($m) # m = 100M
BenchmarkTools.Trial:
memory estimate: 762.94 MiB
allocs estimate: 2
--------------
minimum time: 362.364 ms (3.82% GC)
median time: 408.529 ms (15.66% GC)
mean time: 410.735 ms (16.18% GC)
maximum time: 445.156 ms (22.65% GC)
--------------
samples: 13
evals/sample: 1
julia> @benchmark test_threads($m) # m = 100M
BenchmarkTools.Trial:
memory estimate: 762.94 MiB
allocs estimate: 34
--------------
minimum time: 333.686 ms (0.13% GC)
median time: 396.594 ms (15.47% GC)
mean time: 396.439 ms (15.15% GC)
maximum time: 436.842 ms (23.00% GC)
--------------
samples: 13
evals/sample: 1
julia> @benchmark test_serial($m) # m = 1B
BenchmarkTools.Trial:
memory estimate: 7.45 GiB
allocs estimate: 2
--------------
minimum time: 3.714 s (0.41% GC)
median time: 6.760 s (0.54% GC)
mean time: 6.760 s (0.54% GC)
maximum time: 9.805 s (0.59% GC)
--------------
samples: 2
evals/sample: 1