Julia Jupyter notebook with multiple threads - gher-uliege/Documentation GitHub Wiki

To use Julia Jupyter notebooks with more than one thread, install an additional Julia kernel with the environment variable JULIA_NUM_THREADS:

using IJulia
IJulia.installkernel("Julia 8 Threads", env=Dict(
           "JULIA_NUM_THREADS" => "8",
))

Of course, you can change the number of threads. Under Linux, you can use /proc/cpuinfo to see the number of processors (including possible hyper-threading):

cat /proc/cpuinfo | grep processor | wc -l

Alternatively, you can use the command top and pressing the number 1.

This creates a new directory (~/.local/share/jupyter/kernels/julia-8-threads-1.12) with the file kernel.json:

{
  "display_name":"Julia 8 Threads 1.12",
  "argv":["/mnt/data1/abarth/.julia/juliaup/julia-1.12.2+0.x64.linux.gnu/bin/julia","-i","--color=yes","-e","import IJulia; IJulia.run_kernel()","{connection_file}"],
  "language":"julia",
  "env":{"JULIA_NUM_THREADS":"8"},
  "interrupt_mode":"signal"
}

The shell command jupyter-kernelspec list shows all installed kernels.

In your Jupyter interface, you can now select the newly created kernel:

The following julia commands let you check the number of threads:

using Base.Threads
@show Threads.nthreads()

More information is available at: https://docs.julialang.org/en/v1/manual/multi-threading/#man-multithreading

In general, before parallelizing a code with multithreading it is advisable to check that the code is type stable and to avoid data allocation in "hot" loops (https://docs.julialang.org/en/v1/manual/performance-tips/).

A complete code example is available here: https://github.com/gher-uliege/Documentation/blob/master/example_multithreading.ipynb