# How to use a variable value inside @tullio

**URL:** <https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172>\
**Category:** General Usage\
**Tags:** macros, metaprogramming, tullio\
**Created:** [September 7, 2020, 2:22am UTC](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172 "2020-09-07T02:22:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [September 7, 2020, 2:22am UTC](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172/1 "2020-09-07T02:22:44Z")

</div>

What’s the proper syntax to get this to run?

```julia
using Tullio

function ugh(tf::Bool)
    X = randn(10000,100)
    y = zeros(100)
    @tullio threads=tf y[j] = X[i,j]
end

ugh(true)

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 7, 2020, 2:45am UTC](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172/2 "2020-09-07T02:45:31Z")

</div>

What exactly is your question? Is it about passing a flag to `threads` or is it about the `@tullio` expression itself?

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [September 7, 2020, 2:46am UTC](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172/3 "2020-09-07T02:46:34Z")

</div>

about passing a flag to threads (I know I can just pass `true' or `false’)

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 7, 2020, 2:47am UTC](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172/4 "2020-09-07T02:47:51Z")

</div>

Alright. I’d probably do it like this:

```julia
if tf
   @tullio blah = blah
else
    @tullio blah = blah threads=false
end

```

I think that should probably work.

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [September 7, 2020, 2:48am UTC](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172/5 "2020-09-07T02:48:49Z")

</div>

Yeah, sure. But there’s no way to do it on the line itself? I have a lot of @tullio statements.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 7, 2020, 2:53am UTC](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172/6 "2020-09-07T02:53:09Z")

</div>

Not sure. I think because of how the function gets compiled it needs the value when it is created, not later. Perhaps someone with more macro knowledge can chime in. But if you do what I suggested then I think the compiler can just create two branches and smartly pick the right one.

Are you sure you need to be manually switching `threads` like this?

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [September 7, 2020, 2:54am UTC](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172/7 "2020-09-07T02:54:54Z")

</div>

I’ll settle for a switch (bool constant) at the top of the file.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 7, 2020, 2:56am UTC](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172/8 "2020-09-07T02:56:13Z")

</div>

It looks like there are also some global options available in Tullio. Perhaps you can turn off threading for all of them at once.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [September 7, 2020, 6:18am UTC](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172/9 "2020-09-07T06:18:41Z")

</div>

I never thought to try this, but the reason it doesn’t work is that values other than true/false are interpreted differently. The default behaviour does this:

```julia
@tullio y[j] = X[i,j] verbose=2
...
(Tullio.threader)(𝒜𝒸𝓉!, (Tullio.storage_type)(y, X), y, tuple(X), tuple(𝒶𝓍j), tuple(𝒶𝓍i), +, 262144, nothing)

```

where the argument `262144` is the macro’s estimate of how big X must be to justify multi-threading, based on the function not containing `log` etc. Which `threader` then uses once the actual size of `X` is known.

The option `threads=false` replaces that with `nothing`, which disables `threader` completely. But anything else is assumed to be a different threshold:

```julia
@tullio threads=tf y[j] = X[i,j] verbose=2
...
(Tullio.threader)(𝒜𝒸𝓉!, (Tullio.storage_type)(y, X), y, tuple(X), tuple(𝒶𝓍j), tuple(𝒶𝓍i), +, tf, nothing)

```

So a possible hack would be to write `threads=ts` where `ts == tf ? nothing : 100^2`. But it’s possible that this design ought to change.

> [@tbeason](#):
>
> there are also some global options available in Tullio

Indeed, if you write `@tullio threads=false` at the top of the file, then all subsequent macro calls will use that. But these global macro options affect the parse-time behaviour, not the running of already-parsed code.

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [September 7, 2020, 12:08pm UTC](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172/10 "2020-09-07T12:08:25Z")

</div>

Awesome! Thanks for the elaborate explanation. Perhaps, being able to pass it as a command line argument would be helpful.

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [September 7, 2020, 2:45pm UTC](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172/11 "2020-09-07T14:45:46Z")

</div>

The reason I wanted this is that sometimes I want to run a routine once, in which case I want to use the parallel version of Tullio, and sometimes I want to do a Monte Carlo simulation in which the routine gets called a few thousand times and the parallelization is more effectively done in the outer loop.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [September 7, 2020, 3:03pm UTC](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172/12 "2020-09-07T15:03:02Z")

</div>

I would guess that if the problem is big enough to benefit from multithreading when run once, then the overhead of unnecessary nthreads `@spawn` calls ought not to be large. I think Julia’s threading is fairly smart about this. But have not timed it extensively, and could be wrong about this.

Note that `threads=false` at present also disables memory tiling, which depending on the problem can make a big difference (especially things which reduce over the last index). `threads=typemax(Int)` is an ugly way to have tiles but declare no problem big enough to need two threads.

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [September 7, 2020, 8:12pm UTC](https://discourse.julialang.org/t/how-to-use-a-variable-value-inside-tullio/46172/13 "2020-09-07T20:12:54Z")

</div>

Re unnecessary @spawn calls, I’ll run it once it’s done later this week and report here.
