Multi-threading bug?

I am trying to make my code to run on multiple threads and I was reading through this. I got the following very weird result:

julia> Threads.nthreads()
6

julia> Threads.@threads for i = 1:10
             println("i = $i on thread $(Threads.threadid())")
           end
i = 1 on thread 1
i = 2 on thread 1
i = 3 on thread 1
i = 4 on thread 1
i = 5 on thread 1
i = 6 on thread 1
i = 7 on thread 1
i = 8 on thread 1
i = 9 on thread 1
i = 10 on thread 1

That is what I got when I run thing in juno. So I tried to do this from the terminal. I run julia --proc 8 and it takes considerably more time to launch than when I just run julia but then I get:

julia> Threads.nthreads()
1

What is going on here?

I don’t know about what Juno does, but I assume it has something to do with why you are seeing everything on one thread here (though it does seem quite strange that you still get nthreads() == 6.

You don’t actually need multiple processes to run multiple threads. Using --proc creates separate Julia processes (separate in the sense that they each have their own memory and are managed by the OS), which is the main reason it takes longer to start. You can run a single process with multiple threads by setting the JULIA_NUM_THREADS variable.

I would start by verifying that the same code runs on multiple threads when you run Julia from the terminal having set JULIA_NUM_THREADS=6.

I tried also this, the number of threads is still 1.

I could not reproduce your results, either from Juno or a plain julia repl launched from the terminal. I did the following (julia 1.3.0 on Linux).

From a shell prompt I did:

~ export JULIA_NUM_THREADS=6

Then I launched atom from that same shell:

~ atom

Then from the julia repl inside juno I saw the following:

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.3.0 (2019-11-26)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> Threads.nthreads()
6

julia> Threads.@threads for i = 1:10
                   println("i = $i on thread $(Threads.threadid())")
                 end
i = 3 on thread 2
i = 4 on thread 2
i = 10 on thread 6
i = 5 on thread 3
i = 9 on thread 5
i = 6 on thread 3
i = 7 on thread 4
i = 8 on thread 4
i = 1 on thread 1
i = 2 on thread 1

I saw the same thing when launching a plain julia repl from that same shell session.

I restarted the juno shell and it works :pensive:

However I still cannot start julia from console with more than 1 threads.

I set JULIA_NUM_THREADS=6 and as expected echo $JULIA_NUM_THREADS prints 6 but when I start julia i get:

julia> Threads.nthreads()
1

Did you do export JULIA_NUM_THREADS=6 or just JULIA_NUM_THREADS=6 ?

You need the export, otherwise the value of the environment variable is not passed into any new processes (in this case the Julia process). This isn’t a Julia-specific thing–it’s just the way your shell works.

$ MY_VAR=my_value
$ echo $MY_VAR
my_value
$ julia -E 'ENV["MY_VAR"]'
ERROR: KeyError: key "MY_VAR" not found
Stacktrace:
 [1] (::getfield(Base, Symbol("##457#458")))(::String) at ./env.jl:79
 [2] access_env at ./env.jl:43 [inlined]
 [3] getindex(::Base.EnvDict, ::String) at ./env.jl:79
 [4] top-level scope at none:1
$ export MY_VAR=my_value
$ echo $MY_VAR
my_value
$ julia -E 'ENV["MY_VAR"]'
"my_value"

1 Like

Oh, I didn’t :frowning:
That solves it. Thanks!

Running julia -p 4 starts four processes, not four threads.

1 Like