Multi-Threading hangs in Windows

Hi Julia friends! I have a persistent problem with multi-threading hanging, sometimes for many minutes at a time. Notably, I only witness this when my code is allocating large amounts of memory. Of course, pre-allocating would generally solve the problem, but I can’t apply this solution in every case.

Here is a MWE, which is run on Julia 1.9.2 with 10 threads on a Windows machine (64-bit). The inner loop purposefully allocates a lot of memory in order to show the bug. When I run this code with @floop using only one or two threads (via the basesize trick), the problem goes away. It comes back as I use more threads.

using Plots

num_outer_loops    = 1000
num_parallel_loops = 15
time_vec           = zeros(num_outer_loops)
rvec1              = randn(10000)
rvec2              = randn(10000)
for ii in 1:num_outer_loops
    t1 = time()
    Threads.@threads for jj in 1:num_parallel_loops
        rvec_copy1 = rvec1.*rvec2
        rvec_copy2 = rvec1.*rvec2
        rvec_copy3 = rvec1.*rvec2
        rvec_copy4 = rvec1.*rvec2
        rvec_copy5 = rvec1.*rvec2
    end
    time_vec[ii] = time() - t1
    println(ii)
end

# plot hanging
Plots.plot(1:1000, time_vec, yaxis = :log, xlabel = "outer loop iteration", ylabel = "time (secs, log scale)")

Multi-threading hangs for ~3.3 seconds four times, which seems to be far longer than a simple gc routine:

Any suggestions @maleadt or @tkf or anyone else? Is this a bug?

1 Like