# How can I control the threads in lower level in multithreading function?

**URL:** <https://discourse.julialang.org/t/how-can-i-control-the-threads-in-lower-level-in-multithreading-function/73227>\
**Category:** General Usage\
**Created:** [December 16, 2021, 8:01pm UTC](https://discourse.julialang.org/t/how-can-i-control-the-threads-in-lower-level-in-multithreading-function/73227 "2021-12-16T20:01:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [December 16, 2021, 8:01pm UTC](https://discourse.julialang.org/t/how-can-i-control-the-threads-in-lower-level-in-multithreading-function/73227/1 "2021-12-16T20:01:22Z")

</div>

I applied multithreading on a for loop that results in runtime speedup. I have 12 cores on my system and by adjusting the number of threads (e.g. 4, 8, and 12), I observe an improvement in runtime. However, the runtime improvement is not linear. For example, by using 4 threads, the runtime speedup is not 4 times better than using a single thread. Or, when I use 8 threads the runtime improvement is not twice as using 4 threads. Is there any way to find out how the threads are assigned to the tasks and how I can manage them to get a better runtime speedup?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [December 16, 2021, 8:06pm UTC](https://discourse.julialang.org/t/how-can-i-control-the-threads-in-lower-level-in-multithreading-function/73227/2 "2021-12-16T20:06:05Z")

</div>

Not an answer to your question, but there’s some discussion on (lack of ) speedup here: [Again on reaching optimal parallel scaling - #5 by carstenbauer](https://discourse.julialang.org/t/again-on-reaching-optimal-parallel-scaling/73203/5)

---

<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:** [December 16, 2021, 8:23pm UTC](https://discourse.julialang.org/t/how-can-i-control-the-threads-in-lower-level-in-multithreading-function/73227/3 "2021-12-16T20:23:21Z")

</div>

Unfortunately, without further information it is hardly possible to help you here. How does your loop kernel look like? Do you use `@threads` or `@spawn`? And, most importantly, do you have a good reason to expect linear scaling in the first place? Your kernel could easily be memory bound (quite common) in which case using more threads might not help you much. So, you shouldn’t expect linear scaling (even not theoretically) for an arbitrary loop-computation.

Regarding your question, note that for `@spawn` tasks might not be sticky, that is, they can, in principle, be moved around between different Julia threads. (`@threads` gives you sticky behavior) In any case, you can use `threadid()` to see on which thread a task is running on. (Perhaps [https://nbviewer.org/github/carstenbauer/JuliaNRWSS21/blob/main/backup/load\_balancing.ipynb](https://nbviewer.org/github/carstenbauer/JuliaNRWSS21/blob/main/backup/load_balancing.ipynb) might be an instructive example).
