Does multithreading require a new process?

I’m looking at the new multithreading features (vs. multiprocess). Based on the documentation, it looks like currently you cannot generate new threads in a running Julia process? That is, they need to be specified as a startup argument or environment variable?

https://docs.julialang.org/en/v1/manual/multi-threading/

If some functions allow you to create a thread and then destroy it on completion, that isn’t clear from the documentation.

If it requires a new process, is there a plan to allow creation and destruction of threads within a process?

In Julia I don’t believe there is ever a reason for more threads than CPU cores. This is because Julia basically uses cooperative multitasking, which means that if a Task doesn’t need to use the CPU it lets another Task use it.

If you have 4 CPU cores with 4 threads and 4 Tasks using 100% of the CPU you are doing calculations as fast as you can. If you bump that up to 8 threads and 8 Tasks using 100% you will actually do calculations slower. This is because the OS will swap the threads onto the CPU to ensure that all threads get equal time on the CPU (50% of the CPU). This swap is not free, thread state has to be saved and restored, and the CPU cache will be flushed since the new thread will probably not be accessing the same memory that the old thread was.

The down side to this is when creating a “desktop app” you can’t have a “UI thread” that just handles responses to user actions.

2 Likes

Julia multithreading works by having Julia create N operating system threads at initialization, and then when you want to run something in parallel you @spawn a task… this task will get bound to a specific thread, and execute in parallel with other tasks on different threads.

it’s not clear to me how tasks get scheduled on a given thread and tasks can’t migrate from thread to thread yet.

I actually think you should set the JULIA_NUM_THREADS to be the same as your number of actual cores plus a small constant, like 2 or 3. Then you can have a few threads to do UI and file IO type stuff and the OS will take care of scheduling them, without tying up your computation threads.

once Julia can migrate tasks across threads this would be maybe less important.

2 Likes

See https://github.com/JuliaLang/julia/issues/16134.

1 Like

Thanks all. I’m thinking of how to provide a package that can provide multithreaded performance without the user having to know about that as part of their setup. If a user has to set an environment variable, that’s one more barrier to use.

In Julia 1.5 you can start julia with --threads N also.

2 Likes

I suspect most naive users will probably interact with the Julia REPL without ever typing the command line. Atom, VS Code, or Start Menu shortcut launch it directly.

Note that Atom states with a number of threads equal to physical cores I think by default, vscode has a setting for this also but it seems blank by default

1 Like

Ah, I’ve been using VSCode lately, seems to default to 1.

Maybe @pfitzseb who has now worked on both editors can comment on why the default differs between them

Well, we just haven’t gotten around to adding that logic to VSCode (in the hopes that Julia might support a JULIA_NUM_THREADS=auto setting or something like that) :slight_smile:

2 Likes

Cool thanks - I just wanted to see whether there’s anything with the way the VSCode extension runs that makes putting this setting to 4 or whatever less innocuous than it was in Juno.

1 Like