# Limit number of threads from Threads.@threads

**URL:** <https://discourse.julialang.org/t/limit-number-of-threads-from-threads-threads/79118>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [April 6, 2022, 6:28pm UTC](https://discourse.julialang.org/t/limit-number-of-threads-from-threads-threads/79118 "2022-04-06T18:28:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![biona001](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/biona001/32/16497_2.png) [@biona001](https://discourse.julialang.org/u/biona001)\
**Post date:** [April 6, 2022, 6:28pm UTC](https://discourse.julialang.org/t/limit-number-of-threads-from-threads-threads/79118/1 "2022-04-06T18:28:10Z")

</div>

Is there any way to limit the number of threads used in `Threads.@threads` loops (for instance, use at most 3 threads)?

I have a nested function where both inner and outer function does multithreading, but due to memory constraints, I can only run a few outer `i` loops at the same time. For example, say I enable 24 threads, can I run at most 3 `i` indices in the outer loop and have the remaining threads run the inner?

The pseudo-code looks like the following

```julia
function inner(v)
    # memory intensive code that is multithreaded
end

function outer()
    Threads.@threads for i in 1:100
        inner(v) # I only want a few `i` to run here
    end
end

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 6, 2022, 8:57pm UTC](https://discourse.julialang.org/t/limit-number-of-threads-from-threads-threads/79118/2 "2022-04-06T20:57:22Z")

</div>

Something like this?

```julia
function inner()
    # memory intensive code that is multithreaded
end

function outer()
    Threads.@threads for i in 1:3
        for j in i:3:100
            @show i, j
            inner() # I only want a few `i` to run here
        end
    end
end

```

---

<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:** [April 6, 2022, 9:03pm UTC](https://discourse.julialang.org/t/limit-number-of-threads-from-threads-threads/79118/3 "2022-04-06T21:03:25Z")

</div>

> [@biona001](#):
>
> I have a nested function where both inner and outer function does multithreading

Quite generally, `@threads` shouldn’t be nested. You might want to switch to using `Threads.@spawn` instead (which can be nested arbitrarily).

Otherwise, the answer is no, there is no easy way to limit the number of threads used by `@threads`. Of course, you can always use `@threads :static` and manually split up and distribute the work among a subset of the threads. But this can become tedious, in particular with nesting, and you should really think about whether this is the right approach.

---

<div class="post-metadata">

**Author:** ![biona001](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/biona001/32/16497_2.png) [@biona001](https://discourse.julialang.org/u/biona001)\
**Post date:** [April 6, 2022, 10:13pm UTC](https://discourse.julialang.org/t/limit-number-of-threads-from-threads-threads/79118/4 "2022-04-06T22:13:32Z")

</div>

> `@threads` shouldn’t be nested

I had always thought multithreading in Julia is [composable](https://julialang.org/blog/2019/07/multithreading/) (i.e. you can nest them as much as you want). Is composability restricted to just `@spawn`?

---

<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:** [April 6, 2022, 10:38pm UTC](https://discourse.julialang.org/t/limit-number-of-threads-from-threads-threads/79118/5 "2022-04-06T22:38:26Z")

</div>

> [@biona001](#):
>
> Is composability restricted to just `@spawn` ?

For now it is, yes. However, `@threads` will likely get more scheduler options in the future one of which will presumably have `@spawn` like composability properties (i.e. depth-first scheduling etc.). (Until recently, we only had `@threads :static`, now we also have `@threads :dynamic`, more to follow)
