Setting Number of Threads at startup for VS Code

Hello,

I am trying to set the number of threads (such as 4) in Julia by using the command:
JULIA_NUM_THREADS=4
in CMD in window before launching Julia, which works.

However, I need to set this issue inside “.jl” file by typing the command:
$ JULIA_NUM_THREADS=4 ./julia
at the beginning but it seems not working when I check the threads number by “Threads.threadid()” in the opened REPL.
By the way, I am using VS code to run the “.jl” file.

Thanks!
Amro

Thanks for the reply! I tried and it works.

However, can we define the number of threads from inside the “.jl” file rather set it manually?

no, because it’s defined when the Julia process starts, which is before anything inside a .jl file is read. Same reason you can’t change number of threads on the fly

Thanks, I got it except the term “on the fly”.

Can we create a file (not .jl) which sets the number of threads and also calls my .jl file?

you open up a Julia REPL, Threads.nthreads() shows a number, there’s no way to change this value within the same session (“on the fly”, as this Julia process is running).

yeah just use a bash or powershell steering script, or even a julia script :slight_smile:

yeah just use a bash or powershell steering script, or even a julia script :slight_smile:
[/quote]
Can you please give me an example using the julia script?

well this is super backwards because you could have just called Julia on your original script… but here we go:

akako@ThinkX /tmp> cat t.jl
run(`julia -t $(ARGS[1]) myscript.jl`)
akako@ThinkX /tmp> cat myscript.jl 
println(Threads.nthreads())
akako@ThinkX /tmp> julia t.jl 1
1
akako@ThinkX /tmp> julia t.jl 3
3

by any chance you are just looking for the -t? Multi-Threading · The Julia Language

I am not sure if I made it right.
1- I opened VS code
2- I created a new .jl file and I put in it
akako@ThinkX /tmp> cat t.jl

run(julia -t $(ARGS[5]) Test1.jl)

akako@ThinkX /tmp> cat Test1.jl

println(Threads.nthreads())
akako@ThinkX /tmp> julia t.jl 5

(Test1 is my .jl file and the number of the threads is 5).
3- Then click “Julia: Excute file in REPL”

what’s your end goal

Create a file that (1- defines the number of threads; 2- and executes some calculations in julia script).
I need to open this file by VScode and run it

what’s wrong with using VSCode’s terminal to run julia -t <num> yourscriipt.jl?

I just want to make it more automatic, rather than typing the above line.
However, while I typing the above line I got the following error"