# Threads question

**URL:** <https://discourse.julialang.org/t/threads-question/96326>\
**Category:** Performance\
**Tags:** threads\
**Created:** [March 20, 2023, 1:38am UTC](https://discourse.julialang.org/t/threads-question/96326 "2023-03-20T01:38:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [March 20, 2023, 1:38am UTC](https://discourse.julialang.org/t/threads-question/96326/1 "2023-03-20T01:38:20Z")

</div>

Running Julia on 1 thread on a 4 Core Windows 10 PC, the line

```julia
rand(10_000,10_000)^2

```

Uses 100% of the CPUs.

From this, I assume that unless you are very advanced, there’s no benefit to writing multi-threaded code as the compiler will do a better job of optimizing code across all the cores.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [March 20, 2023, 2:06am UTC](https://discourse.julialang.org/t/threads-question/96326/2 "2023-03-20T02:06:54Z")

</div>

Julia automatically only automatically multithreads matmul and factorization (in the future we might multi-thread broadcasting also, but currently do not). If you rely a lot on linear algebra, you may want to install `MKL` which will make all of your linear algebra faster (it can’t be included by default for license reasons).

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [March 20, 2023, 3:33am UTC](https://discourse.julialang.org/t/threads-question/96326/3 "2023-03-20T03:33:19Z")

</div>

Thanks that’s really interesting.

---

<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 20, 2023, 7:39am UTC](https://discourse.julialang.org/t/threads-question/96326/4 "2023-03-20T07:39:04Z")

</div>

> [@Lincoln\_Hannah](#):
>
> From this, I assume that unless you are very advanced, there’s no benefit to writing multi-threaded code as the compiler will do a better job of optimizing code across all the cores.

> [@Oscar\_Smith](#):
>
> Julia automatically only automatically multithreads matmul and factorization

To clarify and to avoid a potential misunderstanding here.

1. Your matrix multiplication **is** running multithreaded.
2. It’s not that the compiler is super smart here (in the sense of auto-parallelizing the matmul) but just that someone else wrote the multithreaded code for you already.
3. Specifically, the matmul code that runs isn’t in Julia but provided through OpenBLAS (or MKL, if you load it), an external dependency that Julia uses for most linear algebra functionality.
4. As a consequence of 3), one must distinguish between Julia threads and OpenBLAS threads. Even if you run Julia with a single thread, i.e. `julia -t 1`, OpenBLAS, and thus your matmul, will run on multiple OpenBLAS threads. (`OPENBLAS_NUM_THREADS` is your friend to control the latter).

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [March 20, 2023, 9:19am UTC](https://discourse.julialang.org/t/threads-question/96326/5 "2023-03-20T09:19:23Z")

</div>

Thanks.  
Is there any update on Multi-Threaded Broadcasting ?  
This thread stopped in 2021.

> [@Multithreaded broadcast?](https://discourse.julialang.org/t/multithreaded-broadcast/26786):
>
> So, with the new improvements to threading, I’m wondering how that can be exposed easily to users. One of the most common parallelizable pattern is broadcasting (discussed in [https://github.com/JuliaLang/julia/issues/19777](https://github.com/JuliaLang/julia/issues/19777)). The conservative approach is to make @threads work for these, so we can do @threads a.= b.+ c. This is fine, but adding annotations gets annoying really fast (“so, what combination of @threads @simd @inbounds @. do I need this time?”), especially in matlab/numpy vector code …

I use Broadcasting all the time because it’s so concise.
