# of threads in Optimal Transport Package


I tried to increase the # of threads in OT.jl by initializing julia with flags such as -p 16 or -t 32 but no success. I can only use 8 threads by default.

Is there any way that we can increase the # of threads ?


Hey there,

first of all, only -t is the command line option to set the number of Julia threads. The option -p is for multiprocessing not multithreading. When you say “but no success”, how do you measure success here? Threads.nthreads() should definitely give you 32 as output when you started Julia with julia -t 32. Are you sure that OT.jl has multithreading support built-in? Judging by a quick search in the package docs, it does not (I don’t see any mention of multithreading at least).



I wrote it casually. Next time, I won’t :slight_smile:

You are right, there is no call of Base.Threads in their repository but when I call sinkhorn function, it starts to do multithreading operations. For the m.w.e. :

using OptimalTransport

n = 5000
C = rand(n, n)
collect(sinkhorn(ones(n), ones(n), C, .05) for i in 1:30)

The above code does some multithreading. May be the matrix operations are done in multihtreading which is dependent to directly to julia, right ?

In any case, isn’t it sufficient to start julia by using -t or -p flag for using more threads/cores ?

As of now, the multithreading of OpenBLAS is pretty much entirely separate, that is -t won’t have any effect on the matrix operations (technically, that’s only true if you don’t call OpenBLAS from multiple Julia threads, but that’s a different story and irrelevant here). You can query the number of OpenBLAS threads via using LinearAlgebra; BLAS.get_num_threads() and set them via BLAS.set_num_threads(N) (or via the environment variable OPENBLAS_NUM_THREADS=N). A natural choice for N would be the number of available physical CPU cores, for example.

Another thing you could try to speed up things is switching out OpenBLAS in favor of MKL. This is super easy with Julia >= 1.7 using MKL.jl. In fact, it’s just ] add MKL and using MKL.


Thank you for reminding me MKL. I actually installed it, once it is installed I thought it is not necessary to call it again. :slight_smile: My bad.

Thinks are now working as expected. But … we should also not forget that increasing the number of threads does not necessarily increase the performance :slight_smile: