Threads not working in julia 1.8.0

Just updated to 1.8.0 on my laptop and found out multithreading is not working as expected: loops never leave master thread

The basic example code behaves differently on 1.8.0 compared to my 1.6.2 installation

a = zeros(10)
Threads.@threads for i in 1:10
    a[i] = Threads.threadid()
end

On 1.6.2:

julia> a
10-element Vector{Float64}:
 1.0
 1.0
 2.0
 2.0
 3.0
 3.0
 4.0
 4.0
 5.0
 6.0

On 1.8.0:

julia> a
10-element Vector{Float64}:
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0

This is my setup on 1.8.0:

julia> versioninfo()
Julia Version 1.8.0
Commit 5544a0fab76 (2022-08-17 13:38 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × Intel(R) Core(TM) i5-8265U CPU @ 1.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, skylake)
  Threads: 6 on 8 virtual cores
Environment:
  JULIA_COPY_STACKS = 1
  JULIA_NUM_THREADS = 6
(@v1.8) pkg> status
Status `~/.julia/environments/v1.8/Project.toml`
  [6e4b80f9] BenchmarkTools v1.3.1
  [5fb14364] OhMyREPL v0.5.12
  [91a5bcdd] Plots v1.31.7
⌃ [d330b81b] PyPlot v2.10.0
  [295af30f] Revise v3.4.0

Any idea what’s happening?

EDIT: sorry I meant to post this in the general usage section; not sure how it ended up in the numerics

Are you sure you enabled multiple threads when starting Julia? Example: julia --threads=4.

See Multi-Threading · The Julia Language

I cannot reproduce your problem. I get, after starting julia with

julia -t auto
julia> a
10-element Vector{Float64}:
 1.0
 1.0
 2.0
 2.0
 3.0
 4.0
 7.0
 8.0
 6.0
 5.0

I set the number of threads by the env variable JULIA_NUM_THREADS and it is correctly read by the repl as you see in the versioninfo() report. Also calling Threads.nthreads() returns 6 in my case.
Starting with -t auto or other numbers changes the number of threads but does not change the results of the code.
What could cause this? I stress that if I make two exactly identical environments in 1.8.0 and 1.6.2, in 1.6.2 I get the correct behavior.
Should I just go for a full reinstallation of 1.8.0?

I can reproduce your result if and only if I set this environment variable. I’m not familiar with what it does but I would start investigating in that end.

Oh damn. I’m honestly not sure what it does exactly but it is (was?) required to make JavaCall work; or at least it was at some point.
Thanks for catching it, I had it sitting there for a few years and it never interfered with threading in the past versions.

Removing that variable solves the threading issue. Might need to open an issue for this

A PR was opened for this in March:

@threads does not work with JULIA_COPY_STACKS=1 #44589

2 Likes