iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
👨‍👨‍👦‍👦

Using Multi-threading for Julia in Jupyter

に公開
2025/01/06

As described here, you can use thread parallelism by starting Julia with julia -t 4 or julia -t auto, but how do you use thread parallelism in Jupyter Notebook/Lab? The IJulia.jl kernel is single-threaded by default, so if you want to use multi-threading, you need to add a kernel using the following method.

Installing IJulia.jl

If you haven't installed Jupyter Notebook/Lab yet, add IJulia.jl with import Pkg; Pkg.add("IJulia"). Detailed steps are explained in the article below.

https://zenn.dev/ohno/articles/5ad94fb39a3948#jupyter-notebook

Adding a Multi-threaded Kernel

I forget where I first saw this idea, but searching online leads to the same code on this page. You can automatically add a Julia kernel with the maximum number of threads specified using the command below. If you want to specify a specific number of threads, please refer to this page.

How to add
using IJulia
installkernel("Julia (Multi-threads)", "--threads=auto")

After adding it, start Jupyter Lab as usual.

How to start
using IJulia; jupyterlab(detached=true)

You can confirm that the parallelized kernel has been added.

You can check the number of threads as follows.

Checking the number of threads
Threads.nthreads()

Application Example

I have prepared an example where thread parallelism actually improves speed. Many researchers have developed fast solvers for matrix eigenvalue problems, but since we need to handle the matrix construction ourselves, examples like the one below are helpful.

https://gist.github.com/ohno/9a9e9358f8f86fc303f5e4acbd75fb47

Discussion