# How can I arrange to only use @threads if the number of iteration is higher than minimum?

**URL:** <https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177>\
**Category:** Performance\
**Tags:** multithreading\
**Created:** [September 14, 2021, 10:34pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177 "2021-09-14T22:34:59Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 14, 2021, 10:35pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/1 "2021-09-14T22:35:00Z")

</div>

I need to run @threads before “for loop” only if the number of iteration is bigger than a predetermined value (called `min_iter` here ), such as

```julia
@threads min_iter for i in ..., 

```

Any idea?

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 15, 2021, 6:33pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/2 "2021-09-15T18:33:29Z")

</div>

Any idea?

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [September 16, 2021, 3:04pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/3 "2021-09-16T15:04:04Z")

</div>

It’s a bit old, but perhaps still useful:

> [@Conditional Multithreading](https://discourse.julialang.org/t/conditional-multithreading/32421):
>
> Is there a way to enable/disable calls to Threads.@threads? I’m developing an optimization solver, and when the problem is sufficiently small, using multi-threading hurts performance, and incurs allocations (my code doesn’t make any allocations until I wrap it in @threads). However, when the problem size grows and the individual operations being threaded become more computationally intensive (booting the arithmetic intensity) I get some good performance increases. I’d like to be able to enable…

---

<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:** [September 16, 2021, 6:28pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/4 "2021-09-16T18:28:49Z")

</div>

Another: [Optional macro invocation - #21 by goerz](https://discourse.julialang.org/t/optional-macro-invocation/18588/21)

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 16, 2021, 6:30pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/5 "2021-09-16T18:30:23Z")

</div>

> [@Conditional Multithreading](https://discourse.julialang.org/t/conditional-multithreading/32421/1):
>
> incurs

Thank you for sharing, but the useful link was removed.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 16, 2021, 6:37pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/6 "2021-09-16T18:37:59Z")

</div>

> [@Jeff\_Emanuel](#):
>
> Another: [Optional macro invocation - #21 by goerz](https://discourse.julialang.org/t/optional-macro-invocation/18588/21)

Thank you very much for sharing the link. I have read the code there, but can you please give me an example how to implement it, such as by example?

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [September 16, 2021, 6:40pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/7 "2021-09-16T18:40:09Z")

</div>

You could try using [Polyester.jl](https://github.com/JuliaSIMD/Polyester.jl) with a batch size instead of `Threads.@threads`.

---

<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:** [September 16, 2021, 6:47pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/8 "2021-09-16T18:47:55Z")

</div>

I haven’t done it, but it looks like you should be able to do something like this:

```julia
@threadsif n>100 for i=1:n
   dowork()
end
```

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [September 16, 2021, 6:48pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/9 "2021-09-16T18:48:33Z")

</div>

Make a macro `@threadsif cond expr` expanding to `cond ? @threads(expr) : expr`?

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 16, 2021, 7:07pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/11 "2021-09-16T19:07:53Z")

</div>

Thanks for your reply. Can you help me in doing it, I dont know actually how to do ti.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 16, 2021, 7:31pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/12 "2021-09-16T19:31:47Z")

</div>

> [@Jeff\_Emanuel](#):
>
> ```julia
> @threadsif n>100 for i=1:n
> dowork()
> end
> 
> ```

I got it, thank you very much!  
Do you know, how to make it more fancy by choosing number of threads based on the size of `n`?

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 16, 2021, 7:32pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/13 "2021-09-16T19:32:56Z")

</div>

> [@fgerick](#):
>
> You could try using [Polyester.jl](https://github.com/JuliaSIMD/Polyester.jl) with a batch size instead of `Threads.@threads` .

Do you think the performance with ` @batch` would be the same as in `@threads`?

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [September 16, 2021, 7:42pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/14 "2021-09-16T19:42:28Z")

</div>

Well I’m no expert on this, but it seems like what you’re doing is at the limit of serial vs threads efficiency and Polyester.jl is exactly aimed at reducing the threads overhead. Probably @Elrod is more qualified to answer this.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 16, 2021, 8:04pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/15 "2021-09-16T20:04:02Z")

</div>

I see. Thank you very much for your support!

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [September 16, 2021, 8:22pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/16 "2021-09-16T20:22:45Z")

</div>

If it is dominated by threading or scheduling overhead, then Polyester should help.  
Also, as stated,

```julia
@batch minbatch=min_iter for i in ...

```

should work and do what you want. It won’t thread if less than `min_iter` iterations, and it will make sure each thread has at least `min_iter` iterations (so if the total number of iterations = `2.9min_iter`, it’ll use 2 threads). This slower ramp up should also help / let you avoid using more threads than is profitable.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 16, 2021, 9:33pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/18 "2021-09-16T21:33:33Z")

</div>

> [@Elrod](#):
>
> If it is dominated by threading or scheduling overhead, then Polyester should help.

@Elrod  
I have compared between the `@batch` and the method here:  
[Optional macro invocation](https://discourse.julialang.org/t/optional-macro-invocation/18588/21)

```julia
function ss()
dt = 0.001;     
tmin = 0;  
tmax = 5;
timeSim = tmin:dt:tmax;
A_Mat = spzeros(9,9); 
for i in 1:9
  A_Mat[i,i] = 1;
end
I_Mat = ones(9,3);
  iter = 0;
  for t in timeSim
      iter += 1;
      n=101
      #@threadsif n>100 for i in 1:n #by using it the time is 182 ms
      @batch minbatch=15 for i in 1:n #by using it the time is 879.872 ms
      V_Mat = A_Mat \ I_Mat;
    end
end
end
using SparseArrays;
using LinearAlgebra;
using .Threads
using Polyester
using BenchmarkTools
@btime ss()

```

Any reason why `@batch` results in a really big number comparing with conditional thread `@threadsif `?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [September 16, 2021, 10:55pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/19 "2021-09-16T22:55:26Z")

</div>

I assume your real problem is much larger? For a 9x9 array, it’d be much faster to use a dense matrix.

But, the problem is that Polyester is converting `Array`s into a custom array type that apparently doesn’t have these methods implemented. I’ll get around to implementing them some day.  
For now, you can work around it; this seems to be enough to hide the arrays and pass them through as is:

```julia
function ss()
       dt = 0.001;
       tmin = 0;
       tmax = 5;
       timeSim = tmin:dt:tmax;
       A_Mat = zeros(9,9);
       for i in 1:9
         A_Mat[i,i] = 1;
       end
       I_Mat = ones(9,3);
         iter = 0; A_Matr = (Am = A_Mat,); I_Matr = (Im = I_Mat,);
         for t in timeSim
             iter += 1;
             n=101
             #@threadsif n>100 for i in 1:n #by using it the time is 182 ms
             @batch minbatch=15 for i in 1:n #by using it the time is 879.872 ms
             V_Mat = A_Matr.Am \ I_Matr.Im;
           end
       end
       end
ss (generic function with 1 method)

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 16, 2021, 11:02pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/20 "2021-09-16T23:02:23Z")

</div>

> [@Elrod](#):
>
> I assume your real problem is much larger?

Thank you for your explanations. Yes, my real problem is much larger, thus, I am using a sparse Matrix
