# MKLSparse: how to control thread affinity and the number of threads?

**URL:** <https://discourse.julialang.org/t/mklsparse-how-to-control-thread-affinity-and-the-number-of-threads/65601>\
**Category:** Numerics\
**Tags:** multithreading, mkl, sparse\
**Created:** [July 31, 2021, 2:14pm UTC](https://discourse.julialang.org/t/mklsparse-how-to-control-thread-affinity-and-the-number-of-threads/65601 "2021-07-31T14:14:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [July 31, 2021, 2:14pm UTC](https://discourse.julialang.org/t/mklsparse-how-to-control-thread-affinity-and-the-number-of-threads/65601/1 "2021-07-31T14:14:17Z")

</div>

As far as I understand, currently the go-to way if you need multithreaded Sparse Matrix operations is to interface with MKL using [MKLSparse.jl](https://github.com/JuliaSparse/MKLSparse.jl).

Now, imagine I have a machine with several NUMA nodes and want to run multithreaded sparse operations on each of the nodes in parallel. To do that, I have a set of julia workers with each worker initialized on the corresponding NUMA node. Each of the workers uses MKLSparse.jl to run the operations. **How do I control the affinity of the threads used by MKL so that MKL called by the worker on i-th node uses threads on the same node? Could you also remind me how to control the number of threads? (It is done by environment variable, right?)**
