How to change num_threads in Startup.jl on Windows (Julia)

Viewed 629

I am trying to run my code in parallel (CPU has 8 cores) and wanted to automatically have julia startup with 8 threads. I went to C:\Users\chris\.julia\config and created a startup.jl file that only contains one line:

JULIA_NUM_THREADS = 8

When starting Julia and then checking with Threads.nthreads(), it still only shows 1 thread. I know the file is read upon startup, because it throws an error if I write any nonsense into it. I also tried ENV["JULIA_NUM_THREADS"] = 8, but that did not work. What am I doing wrong?

2 Answers

There is a note about this in the documentation:

Note:

JULIA_NUM_THREADS must be defined before starting julia; defining it in startup.jl is too late in the startup process.


So you have to set things up from outside Julia. Depending how you start Julia:

  • if you are using VS code, you can change the number of threads in the settings of the Julia extension
  • if you are running julia from the command-line, you can set the JULIA_NUM_THREADS environment variable globally (using the Windows configuration panel, or setting it in your .bashrc/.profile file if you're using Linux)

I've just add the variable to Environment Variables like below. It works fine.

enter image description here

Related