Enable Parallel Processing of Garbage Collections

Here is my code:

using IJulia
installkernel("Julia (4 threads)", env=Dict("JULIA_NUM_THREADS"=>"4"))
Threads.nthreads()

This code creates a new kernel with multiple threads and works

How can I modify this kernel so that there is parallel processing of the garbage collection?

I need to add the --gcthreads=M somewhere.

https://julialang.github.io/IJulia.jl/stable/library/public/#IJulia.installkernel

has an example of passing command-line flags, “-O3” specifically. Perhaps you can replace that with "--gcthreads=$(M)".

2 Likes

Awesome.

So Threads.nthreads() lets me know the number of threads present.

Is there a command that lets me know the number of gcthreads?

Threads.ngcthreads()

2 Likes

You can also set the number of garbage collection threads as an environment variable: Environment Variables · The Julia Language
Note that by default, Julia sets this to nthreads() ÷ 2, so you shouldn’t need to set this explicitly.

3 Likes

More of a theoretical question, but why set this to half the number of threads? Why not the full amount of threads, if that is available?

Yessir! It shows 4, which is what I wanted.

To avoid oversubscription of CPU-cores. But it’s only a reasonable heuristic. You may need to tweak this.

1 Like

Thank you @Jeff_Emanuel, @stillyslalom, and @carstenbauer for the speedy response! I appreciate you all.

1 Like

See this issue for some detail about slowdowns observed when setting the number of GC threads to greater than the number of CPU cores: High (default) gcthreads count causing slowdown on Julia 1.10.0-beta2 · Issue #51044 · JuliaLang/julia · GitHub

1 Like

So if I have 4 CPU Cores, the sum of my threads and gcthreads can’t exceed 4?

Also, if I have 4 CPU cores, can I have 0 threads and 4 gcthreads?

you can set both to 4. regular threads and gc threads don’t run at the same time.

1 Like