How do you *get* the number of threads for BLAS operations in Julia?

If you have your Julia linked against OpenBLAS, which would be the case if you use the Windows, Mac or generic Linux binaries from julialang.org, or if you build from source and don't set USE_SYSTEM_BLAS=1, then you get the number threads used by OpenBLAS by calling ccall((:openblas_get_num_threads64_, Base.libblas_name), Cint, ()). Eventually, we'll probably provide this as a Julia function.


Module BLAS in Julia Base is a wrapper of blas library which can be openblas, mkl etc.

By default, OpenBLAS use one thread for small matrices and multi-thread you set in enviriment variables for large matrices.

Set the number of threads with environment variables.

Examples:

export OPENBLAS_NUM_THREADS=4 or

export GOTO_NUM_THREADS=4 or

export OMP_NUM_THREADS=4 The priorities are OPENBLAS_NUM_THREADS > GOTO_NUM_THREADS > OMP_NUM_THREADS.

If you compile this lib with USE_OPENMP=1, you should set OMP_NUM_THREADS >environment variable. OpenBLAS ignores OPENBLAS_NUM_THREADS and >GOTO_NUM_THREADS with USE_OPENMP=1.

MKL can automatically tune the number of threads for different size of matrices.


You can use global const dict variable ENV in Julia to get environment variables. But the number of threads you are using will be different when you set it on runtime or set it larger than the cores available in your machine.

If you use the Apple provided BLAS in vecLi, you can via ENV["VECLIB_MAXIMUM_THREADS"] to get the number of threads you are using whether you set the number of threads with environment variables or set it on runtime.


As of Julia 1.6, you can use LinearAlgebra.BLAS.get_num_threads(). So the following code will do the trick:

using LinearAlgebra
BLAS.get_num_threads()

Tags:

Julia