# Correct way to set MKL threads at runtime

**URL:** <https://discourse.julialang.org/t/correct-way-to-set-mkl-threads-at-runtime/128470>\
**Category:** General Usage\
**Tags:** mkl, linearalgebra\
**Created:** [April 27, 2025, 9:19pm UTC](https://discourse.julialang.org/t/correct-way-to-set-mkl-threads-at-runtime/128470 "2025-04-27T21:19:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![VinceNeede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinceneede/32/215744_2.png) [@VinceNeede](https://discourse.julialang.org/u/VinceNeede)\
**Post date:** [April 27, 2025, 9:19pm UTC](https://discourse.julialang.org/t/correct-way-to-set-mkl-threads-at-runtime/128470/1 "2025-04-27T21:19:07Z")

</div>

What is the correct way to set `MKL` threads at runtime? [Documentation](https://github.com/JuliaLinearAlgebra/MKL.jl?tab=readme-ov-file#note-using-mkl-with-distributed) seems to be misleading, it says to use `BLAS.set_num_threads(1)` to be single threaded, but it does not work. The only way I have found to actually make it work is calling julia with `MKL_NUM_THREADS=1`, is there no way to set it at runtime then?

> **small code used to run**
>
> ```julia
> using MKL
> using LinearAlgebra
> BLAS.set_num_threads(1)
> A = rand(5000, 5000)
> 
> A .+= A'
> 
> eigen(A)
> 
> ```

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [April 28, 2025, 1:51am UTC](https://discourse.julialang.org/t/correct-way-to-set-mkl-threads-at-runtime/128470/2 "2025-04-28T01:51:04Z")

</div>

`BLAS.set_num_threads(1)` works for me.

---

<div class="post-metadata">

**Author:** ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)\
**Post date:** [April 28, 2025, 1:52am UTC](https://discourse.julialang.org/t/correct-way-to-set-mkl-threads-at-runtime/128470/3 "2025-04-28T01:52:34Z")

</div>

I thought this worked, but you’re right. As explained in the github issue you’ve also commented on, [Setting MKL thread number · Issue #174 · JuliaLinearAlgebra/MKL.jl · GitHub](https://github.com/JuliaLinearAlgebra/MKL.jl/issues/174#issuecomment-2546443942), when using MKL, `BLAS.set_num_threads(n)` only affects the number of threads used for BLAS functions like `A * B`, not LAPACK functions like `eigen`. I don’t think this is the intended semantics, so hopefully someone will follow up on the github issue.

---

<div class="post-metadata">

**Author:** ![VinceNeede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinceneede/32/215744_2.png) [@VinceNeede](https://discourse.julialang.org/u/VinceNeede)\
**Post date:** [April 28, 2025, 11:05am UTC](https://discourse.julialang.org/t/correct-way-to-set-mkl-threads-at-runtime/128470/4 "2025-04-28T11:05:08Z")

</div>

I have re-reported the issue on `libblastrampoline` [make MKL thread setter/getter work for both BLAS and LAPACK · Issue #151 · JuliaLinearAlgebra/libblastrampoline · GitHub](https://github.com/JuliaLinearAlgebra/libblastrampoline/issues/151) since this is where the link to the setter is being performed.

---

<div class="post-metadata">

**Author:** ![VinceNeede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinceneede/32/215744_2.png) [@VinceNeede](https://discourse.julialang.org/u/VinceNeede)\
**Post date:** [April 28, 2025, 6:50pm UTC](https://discourse.julialang.org/t/correct-way-to-set-mkl-threads-at-runtime/128470/5 "2025-04-28T18:50:01Z")

</div>

As of now, it just seems like it is not possible since it is not implemented by MKL itself [Solved: How to set number of LAPACK threads during runtime? - Intel Community](https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/How-to-set-number-of-LAPACK-threads-during-runtime/m-p/1638274#M36554)
