I just set up a new machine with a CUDA GPU and started playing with the getting started examples from CuArrays.jl when I ran into a strange problem, not linked to CuArrays, but with BenchmarkTools and Threads.
The following code is a slightly modified version of the example from CuArrays:
using BenchmarkTools N = 2^20 x = fill(1.0f0, N) # a vector filled with 1.o (Float32) y = fill(2.0f0, N) # a vector filled with 2.0 function sequential_add!(y, x) for i in eachindex(y, x) @inbounds y[i] += x[i] end return nothing end @btime sequential_add!(y, x) function parallel_add!(y, x) Threads.@spawn for i in eachindex(y, x) #The original Threads.@threads for i in eachindex(y, x) works @inbounds y[i] += x[i] end return nothing end fill!(y, 2.0f0) @btime parallel_add!(y, x)
On my machine (Windows 10, Julia 1.3, official binaries), this quite repeatably hangs Julia on the last line. Well, at least the CPU jumps to ~60% for 30-40sec, then drops to ~30% and stays there for a minute or two until my patience runs out and I kill the process.
@time instead of
@benchmark works fine, as does returning the modified line in
parallel_add! to its original form of using
Threads.@threads instead of
Threads.@spawn. It seems to only be the combination of
@benchmark that causes the “hang”.
Could anyone please either explain what is happening, or point me to somewhere I can read up on this?