Enable multiple Cores for Jupyter Lab

There are a few ways:

(1) A simple way to do this is to put export JULIA_NUM_THREADS=4 in your ~/.bashrc or whatever the shell configuration you need to use.

(2) Another way to do this is to put ENV["JULIA_NUM_THREADS"] = 4 in ~/.julia/config/startup.jl. If ~/.julia/config does not exist, just try create one. However, note that:

Some variables, such as JULIA_NUM_THREADS and JULIA_PROJECT, need to be set before Julia starts, therefore adding these to ~/.julia/config/startup.jl is too late in the startup process.
Environment Variables · The Julia Language

For this reason, I don’t recommend this method. However, if you invoke IJulia.notebook() from standard REPL then ENV["JULIA_NUM_THREADS"] can still take an effect for the Julia processes inside Jupyter notebook, but not the Julia process invoking IJulia.notebook().

(3) If you want to set JULIA_NUM_THREADS only for Julia process inside Jupyter notebook, you can edit the kernel spec ~/.local/share/jupyter/kernels/julia-1.0/kernel.json to something like

{
  "display_name": "Julia 1.0.0",
  "argv": [
    "env",  # add this
    "JULIA_NUM_THREADS=4",  # add this
    "/PATH/TO/bin/julia",
    "-i",
    "--startup-file=yes",
    "--color=yes",
    "/home/USERNAME/.julia/packages/IJulia/0cLgR/src/kernel.jl",
    "{connection_file}"
  ],
  "language": "julia"
}

It’s handy (say) if you want to add Julia kernel with different number of threads. For example, create ~/.local/share/jupyter/kernels/julia-1.0-one-thread/kernel.json with

{
  "display_name": "Julia 1.0.0 (one thread)",
  "argv": [
    "env",
    "JULIA_NUM_THREADS=1",
    "/PATH/TO/bin/julia",
    ...

Actual path to kernel.json depends on OS. See also Making kernels for Jupyter — jupyter_client 8.6.0 documentation

5 Likes