# \# of threads in Optimal Transport Package

**URL:** <https://discourse.julialang.org/t/of-threads-in-optimal-transport-package/78560>\
**Category:** Performance\
**Tags:** package\
**Created:** [March 27, 2022, 5:27pm UTC](https://discourse.julialang.org/t/of-threads-in-optimal-transport-package/78560 "2022-03-27T17:27:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadir-gunel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kadir-gunel/32/35790_2.png) [@kadir-gunel](https://discourse.julialang.org/u/kadir-gunel)\
**Post date:** [March 27, 2022, 5:27pm UTC](https://discourse.julialang.org/t/of-threads-in-optimal-transport-package/78560/1 "2022-03-27T17:27:21Z")

</div>

Hello,

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 ?

B.R.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 27, 2022, 5:47pm UTC](https://discourse.julialang.org/t/of-threads-in-optimal-transport-package/78560/2 "2022-03-27T17:47:32Z")

</div>

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).

Best,  
Carsten

---

<div class="post-metadata">

**Author:** ![kadir-gunel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kadir-gunel/32/35790_2.png) [@kadir-gunel](https://discourse.julialang.org/u/kadir-gunel)\
**Post date:** [March 27, 2022, 6:02pm UTC](https://discourse.julialang.org/t/of-threads-in-optimal-transport-package/78560/3 "2022-03-27T18:02:35Z")

</div>

Hi,

> [@carstenbauer](#):
>
> 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.

I wrote it casually. Next time, I won’t 🙂

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. :

```julia
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 ?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 27, 2022, 6:22pm UTC](https://discourse.julialang.org/t/of-threads-in-optimal-transport-package/78560/4 "2022-03-27T18:22:41Z")

</div>

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](https://github.com/JuliaLinearAlgebra/MKL.jl). In fact, it’s just `] add MKL` and `using MKL`.

---

<div class="post-metadata">

**Author:** ![kadir-gunel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kadir-gunel/32/35790_2.png) [@kadir-gunel](https://discourse.julialang.org/u/kadir-gunel)\
**Post date:** [March 27, 2022, 6:38pm UTC](https://discourse.julialang.org/t/of-threads-in-optimal-transport-package/78560/5 "2022-03-27T18:38:25Z")

</div>

Thank you for reminding me MKL. I actually installed it, once it is installed I thought it is not necessary to call it again. 🙂 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 🙂
