# Threads.@threads does not work properly

**URL:** <https://discourse.julialang.org/t/threads-threads-does-not-work-properly/116701>\
**Category:** General Usage\
**Created:** [July 6, 2024, 4:21pm UTC](https://discourse.julialang.org/t/threads-threads-does-not-work-properly/116701 "2024-07-06T16:21:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![CaG21](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@CaG21](https://discourse.julialang.org/u/CaG21)\
**Post date:** [July 6, 2024, 4:21pm UTC](https://discourse.julialang.org/t/threads-threads-does-not-work-properly/116701/1 "2024-07-06T16:21:26Z")

</div>

Hi everyone,

for the first time I am trying to exploit multi-threading in Julia. However, it seems I am doing some rookie mistake, since it does not work. I am running this very simple code (not a MWE)

```julia
t = 0.0
ℓ = 1000
ψ = randn(6, ℓ)

f = zeros(6, ℓ)

Threads.@threads for j in 1:ℓ
      f[:, j] = computeFreeDynamics(ψ[:, j], p, t)
end

```

where `p` is a structure of parameters and `computeFreeDynamics` is a “slow” function computing the free dynamics of a system.  
I benchmarked the code with and without `Threads.@threads` and there is basically no change. However, if I try to use the same `computeFreeDynamics` function as RHS of an `ODEProblem` to run a MonteCarlo, `Threads.@threads` works as expected.  
Do you have any idea what could be the problem?

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [July 6, 2024, 4:26pm UTC](https://discourse.julialang.org/t/threads-threads-does-not-work-properly/116701/2 "2024-07-06T16:26:00Z")

</div>

make sure you start julia itself with multiple threads, e.g. like `julia -t8` or `julia --threads=auto`

---

<div class="post-metadata">

**Author:** ![CaG21](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@CaG21](https://discourse.julialang.org/u/CaG21)\
**Post date:** [July 6, 2024, 4:28pm UTC](https://discourse.julialang.org/t/threads-threads-does-not-work-properly/116701/3 "2024-07-06T16:28:07Z")

</div>

Yes, Julia is started with `julia -t8` and, in fact, `Threads.nthreads()` gives 8 as output

---

<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:** [July 6, 2024, 4:32pm UTC](https://discourse.julialang.org/t/threads-threads-does-not-work-properly/116701/4 "2024-07-06T16:32:37Z")

</div>

Is `computeFreeDynamics` doing lots of matrix multiplications? if so, you will likely want to `BLAS.set_num_threads(1)` since otherwise all your cores will be used for the matrix multiplication, removing the ability to scale.

---

<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:** [July 6, 2024, 6:04pm UTC](https://discourse.julialang.org/t/threads-threads-does-not-work-properly/116701/5 "2024-07-06T18:04:04Z")

</div>

> [@CaG21](#):
>
> `f[:, j] = computeFreeDynamics(ψ[:, j], p, t)`

This allocates a new array for every `ψ[:, j]` and should be a `@view`. Moreover, you likely want to modify `f[:, j]` in place in `computeFreeDynamics`.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [July 7, 2024, 10:31am UTC](https://discourse.julialang.org/t/threads-threads-does-not-work-properly/116701/6 "2024-07-07T10:31:19Z")

</div>

In general, there is no guarantee that multithreading will speed up your code by the number of threads, or even speed it up at all. The best way to improve performance is by focusing on the sequential implementation, ensuring type-stability and in particular reducing allocations. Can you share more details about `computeFreeDynamics`?

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [July 7, 2024, 12:58pm UTC](https://discourse.julialang.org/t/threads-threads-does-not-work-properly/116701/7 "2024-07-07T12:58:06Z")

</div>

There’s little to go on in your example. As noted, if you’re doing some heavy lifting with linear algebra, it’s probably already parallelized.

There’s nothing wrong with the loop as such, except the allocations noted above. I.e. `ψ[:, j]` allocates a vector, and `computeFreeDynamics` also allocates a vector for return. Allocations in parallel programs can be a performance problem. But it’s possible to write fortran in any language, and make a pre allocated version `computeFreeDynamics!` so that the loop becomes

```julia
@threads for j in 1:ℓ
    computeFreeDynamics!(@view(f[:, j]), @view(ψ[:, j]), p, t)
end

```

You can of course keep an allocating version around as something like

```julia
computeFreeDynamics(args...) = computeFreeDynamics!(zeros(6), args...)

```
