# Error in using @threads

**URL:** <https://discourse.julialang.org/t/error-in-using-threads/77316>\
**Category:** New to Julia\
**Created:** [March 2, 2022, 4:31pm UTC](https://discourse.julialang.org/t/error-in-using-threads/77316 "2022-03-02T16:31:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Katasuke](https://avatars.discourse-cdn.com/v4/letter/k/65b543/32.png) [@Katasuke](https://discourse.julialang.org/u/Katasuke)\
**Post date:** [March 2, 2022, 4:31pm UTC](https://discourse.julialang.org/t/error-in-using-threads/77316/1 "2022-03-02T16:31:48Z")

</div>

I am trying to simulate a nested model that contains many for loops. I want to use the parallel calculation to speed up the simulation. But I failed to use @threads. Here is the structure of my code:

```julia
addprocs(10)
v_rep = 1:10
v_i = 1:10
v_j = 1:10
v_k = 1:10

@progress for rep = 1:10
    println("rep = $rep")
    val_rep = v_rep[rep]
    Threads.@threads for (i, j, k) in collect(Iterators.product(1:3, 1:3, 1:3))
        val_i = v_i[i]
        val_j = v_j[j]
        val_k = v_k[k]
        val = [val_i,val_j,val_k]
        # CSV.write("./val$rep$i$j$k.csv", val)
    end
end

```

Could you tell me where I got wrong? Thank you!

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [March 2, 2022, 6:18pm UTC](https://discourse.julialang.org/t/error-in-using-threads/77316/2 "2022-03-02T18:18:11Z")

</div>

Welcome to Julia Discourse!

Often when asking for help with why something doesn’t work, it is helpful to include the stacktrace of whatever errors you get.

It is also helpful if code you share is self-contained (i.e., can be run simply by copy-paste in a new Julia session). For example, `addprocs` requires the Distributed standard library package to be loaded (i.e., `using Distributed`), and `@progress` requires, e.g., ProgressMeter.jl. When I removed `addprocs(10)` and `@progress` from your code, it ran without errors for me, so I’m not sure what problem you are having. (Or maybe loading those packages is the answer you need?)

* * *

As a side note, `Threads.@threads` uses the `JULIA_NUM_THREADS` environment variable to determine the number of threads Julia can use. Alternatively, one can start Julia with the `-t` option to specify the number of threads to use (e.g., `julia -t 10`). In particular, `addprocs(10)` spawns 10 Julia processes (that can be used for parallel work using the Distributed standard library package), but does not enable multithreading.

~~As a style (and somewhat performance) note, the call to `collect` is unnecessary, i.e., `for (i, j, k) in Iterators.product(1:3, 1:3, 1:3)` suffices (and usually (always?) is preferred). `collect` makes an `Array`, so it is only needed if you actually need an `Array`.~~ Apparently `collect` is needed to work with `@threads`.

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [March 2, 2022, 8:26pm UTC](https://discourse.julialang.org/t/error-in-using-threads/77316/3 "2022-03-02T20:26:00Z")

</div>

Your problem is nothing to do with threads.

```julia
ERROR: TaskFailedException
Stacktrace:

```

Means one of your threads threw an exception

As you should with any threads problem. Comment out “Threads.@threads” and you’ll see the real cause:

CSV.write is expecting a Table not a Vector

```julia
julia> CSV.write("t.csv", [1,2,3])
ERROR: ArgumentError: 'Vector{Int64}' iterates 'Int64' values, which doesn't satisfy the Tables.jl `AbstractRow` interface

```

[https://csv.juliadata.org/stable/writing.html](https://csv.juliadata.org/stable/writing.html)

---

<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:** [March 2, 2022, 9:45pm UTC](https://discourse.julialang.org/t/error-in-using-threads/77316/4 "2022-03-02T21:45:33Z")

</div>

Interesting. I thought you’d have problems with the construction of the `@threads` `for`-loop but lo and behold the following is working for me on Julia 1.9.0 (and 1.7.2)

```julia
v_rep = 1:10
v_i = 1:10
v_j = 1:10
v_k = 1:10

for rep = 1:10
    println("rep = $rep")
    val_rep = v_rep[rep]
    Threads.@threads for (i, j, k) in collect(Iterators.product(1:3, 1:3, 1:3))
        val_i = v_i[i]
        val_j = v_j[j]
        val_k = v_k[k]
        val = [val_i,val_j,val_k]
        # CSV.write("./val$rep$i$j$k.csv", val)
    end
end

```

(removed `addprocs` because that is necessary for `Distributed`, but not for `Threads`).

Edit: Ah, I think @lawless-m understood your problem. The incantation to fix this is

```julia
        using Tables
        CSV.write("./val$rep$i$j$k.csv", Tables.table(val))

```

P.S.: please mention the error that struck you next time…

---

<div class="post-metadata">

**Author:** ![Katasuke](https://avatars.discourse-cdn.com/v4/letter/k/65b543/32.png) [@Katasuke](https://discourse.julialang.org/u/Katasuke)\
**Post date:** [March 3, 2022, 1:53am UTC](https://discourse.julialang.org/t/error-in-using-threads/77316/5 "2022-03-03T01:53:07Z")

</div>

Thanks for the detailed reply! This is my first time to ask question online. I will remember to include the errors.

I didn’t library the packages I needed. You are right. After I library the necessary packages, there is no error.

The code is simplified from a simulation model which contains many for loop. In front of each `for`, there always contains `@process`, which I actually don’t understand what it is doing. I direct copied and change the multiple and nested for loop to only one for loop, to catch the vector from a `collect`. I will remove `@process` in my original script and re-run it. Thank you!

---

<div class="post-metadata">

**Author:** ![Katasuke](https://avatars.discourse-cdn.com/v4/letter/k/65b543/32.png) [@Katasuke](https://discourse.julialang.org/u/Katasuke)\
**Post date:** [March 3, 2022, 1:55am UTC](https://discourse.julialang.org/t/error-in-using-threads/77316/6 "2022-03-03T01:55:05Z")

</div>

Thanks for your reply! I didn’t notice that `CSV.write` is only for Table. I will try it again.

---

<div class="post-metadata">

**Author:** ![Katasuke](https://avatars.discourse-cdn.com/v4/letter/k/65b543/32.png) [@Katasuke](https://discourse.julialang.org/u/Katasuke)\
**Post date:** [March 3, 2022, 3:05am UTC](https://discourse.julialang.org/t/error-in-using-threads/77316/7 "2022-03-03T03:05:02Z")

</div>

When I run `julia -t 10` in REPL. it shows

```julia
julia> julia -t 10
ERROR: syntax: extra token "10" after end of expression

```

which does not work. Did I misunderstand something? Thank you!

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [March 3, 2022, 6:26am UTC](https://discourse.julialang.org/t/error-in-using-threads/77316/8 "2022-03-03T06:26:26Z")

</div>

`julia -t auto ` should be from the command line, not in the REPL

You can also set the `JULIA_NUM_THREADS` environment variable, which I prefer because it is annoying to be part way through some work at the REPL and then want some threads available.

[https://docs.julialang.org/en/v1/manual/multi-threading/](https://docs.julialang.org/en/v1/manual/multi-threading/)

and -p / JULIA\_CPU\_THREADS for setting the number of “cores” ready to be Distributed to (

---

<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:** [March 3, 2022, 7:40am UTC](https://discourse.julialang.org/t/error-in-using-threads/77316/9 "2022-03-03T07:40:56Z")

</div>

> [@Katasuke](#):
>
> In front of each `for` , there always contains `@process` , which I actually don’t understand what it is doing.

That `@progress` I removed, because I got the error message `ERROR: LoadError: UndefVarError: @progress not defined`. But it probably is `Juno.@progress` which shows a progress bar in the Atom/Juno IDE.

---

<div class="post-metadata">

**Author:** ![FrancisKing](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/francisking/32/24966_2.png) [@FrancisKing](https://discourse.julialang.org/u/FrancisKing)\
**Post date:** [March 3, 2022, 9:03am UTC](https://discourse.julialang.org/t/error-in-using-threads/77316/10 "2022-03-03T09:03:37Z")

</div>

One thing that I noticed is that the threads are not being applied to the outer loop but instead to the inner loop. Is this a good thing, a bad thing, or does it not matter?

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [March 3, 2022, 10:13am UTC](https://discourse.julialang.org/t/error-in-using-threads/77316/11 "2022-03-03T10:13:52Z")

</div>

The threads are being applied to where they are told to.

The `Threads.@threads` macro applies to the block it encloses.

Currently they’re running one thread for every (i,j,k) tuple.

Whether that matters is a design choice for you to make.
