# Error in Using "@threads"

**URL:** <https://discourse.julialang.org/t/error-in-using-threads/64290>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 8, 2021, 6:01pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290 "2021-07-08T18:01:30Z")\
**Posts on this page:** 19\
**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:** [July 8, 2021, 6:01pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/1 "2021-07-08T18:01:30Z")

</div>

Hello,

I have a julia file called “makeNetlistTest.jl” which calls function “DefineYnImproved()” and basically the execution works properly.

However, when I am trying to use “@threads” before the “for loop” in function “DefineYnImproved”, I am having error, as seen in the pictures:

 ![1](https://global.discourse-cdn.com/julialang/original/3X/0/8/0885f7dda3e7d7b8681e30163c49d0adf9538cd5.png)  
 ![2](https://global.discourse-cdn.com/julialang/original/3X/7/1/71612af51157a2105408d4457b10b8ca8c9fbe25.png)

Could you please guide me for the problem and solution?

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:** [July 8, 2021, 6:50pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/2 "2021-07-08T18:50:48Z")

</div>

You’re getting a `BoundsError`, which I don’t think has anything to do with the `@threads` macro, unless you’re doing something not thread-safe like reading from and writing to the same memory location on different threads. Unfortunately, the line that errored (31) is not included in your screenshot, so I can’t say much else.

It might be helpful to condense your code into a minimum working example (MWE) that reproduces the error. Or at the least, post the full `DefineYnImproved` method in a code block, i.e.,

````julia
```julia
Your code here
```

````

I think it’s also recommended to copy full error messages and display them in a code block as well.

---

<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:** [July 8, 2021, 7:02pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/4 "2021-07-08T19:02:24Z")

</div>

Line 31 is

```julia
                    Yn[i,netlist.cNeg[m]] += - (2 .* netlist.c[m]) ./ Dt;

```

---

<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:** [July 8, 2021, 7:07pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/5 "2021-07-08T19:07:39Z")

</div>

This is the whole error message:

```julia
  0.284914 seconds (549.45 k allocations: 33.806 MiB, 53.39% compilation time)

ERROR: LoadError: TaskFailedException
Stacktrace:
 [1] wait
   @ .\task.jl:322 [inlined]
 [2] threading_run(func::Function)
   @ Base.Threads .\threadingconstructs.jl:34
 [3] macro expansion
   @ .\threadingconstructs.jl:93 [inlined]
 [4] DefineYnImproved()
   @ Main c:\Work\From Javad\Whole Package\EMTJulia\DefineYnImproved.jl:6
 [5] top-level scope
   @ .\timing.jl:210

    nested task error: BoundsError: attempt to access 10-element Vector{Int64} at index [10]
    Stacktrace:
     [1] getindex
       @ .\array.jl:801 [inlined]
     [2] getindex(A::SparseMatrixCSC{Float64, Int64}, i0::Int64, i1::Int64)
       @ SparseArrays C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\SparseArrays\src\sparsematrix.jl:2134
     [3] macro expansion
       @ c:\Work\From Javad\Whole Package\EMTJulia\DefineYnImproved.jl:12 [inlined]
     [4] (::var"#41#threadsfor_fun#6"{UnitRange{Int64}})(onethread::Bool)
       @ Main .\threadingconstructs.jl:81
     [5] (::var"#41#threadsfor_fun#6"{UnitRange{Int64}})()
       @ Main .\threadingconstructs.jl:48
in expression starting at c:\Work\From Javad\Whole Package\EMTJulia\makeNetlistTest.jl:120

```

---

<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:** [July 8, 2021, 7:56pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/6 "2021-07-08T19:56:07Z")

</div>

I noticed that the error message you pasted above errored on a different line: line 12

```julia
Yn[i,i] += Dt ./ (2*netlist.l[m]);

```

instead of line 31.

Because each thread is working with a different `i`, the increment to `Yn` should be thread-safe.

Looking more closely at the offending lines and the stack trace, it looks like `Yn[i,i]` (line 12) or `Yn[i,netlist.cNeg[m]]` (line 31) are being indexed out of bounds:

> [@Amro](#):
>
> ```julia
> [2] getindex(A::SparseMatrixCSC{Float64, Int64}, i0::Int64, i1::Int64)
> @ SparseArrays C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\SparseArrays\src\sparsematrix.jl:2134
> 
> ```

(The other indexing operations in those lines are on `Vector`s, not `Matrix`es.) So it appears to me that multithreading is not to blame, but you are initializing `Yn` in some way that doesn’t mesh with `nbnodes` and/or `netlist.cNeg[m]`. It’s hard to say more than this without knowing anything about `Yn`, `nbnodes`, and `netlist`, which appear to be global variables.

---

<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:** [July 8, 2021, 8:23pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/9 "2021-07-08T20:23:25Z")

</div>

Please refer to above

---

<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:** [July 8, 2021, 8:40pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/10 "2021-07-08T20:40:39Z")

</div>

Line 31

> [@Amro](#):
>
> ```julia
> Yn[i,netlist.cNeg[m]] += - (2 .* netlist.c[m]) ./ Dt;
> 
> ```

seems to have errored because `netlist.cNeg[2]` is `0`. I’m not sure why line 12

> [@Amro](#):
>
> ```julia
> Yn[i,i] += Dt ./ (2*netlist.l[m]);
> 
> ```

errored. My only guess is that you accidentally updated `nbnodes` without recreating `Yn`.

---

<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:** [July 8, 2021, 8:44pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/11 "2021-07-08T20:44:07Z")

</div>

> [@Amro](#):
>
> `netlist.cNeg = [8 0]; `

As you can see, it is not 0.  
As a kindly reminder, the code worker properly when I dont use @threads.

---

<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:** [July 8, 2021, 9:07pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/12 "2021-07-08T21:07:57Z")

</div>

> [@Amro](#):
>
> > [@Amro](#):
> >
> > `netlist.cNeg = [8 0]; `
> 
> As you can see, it is not 0.

Right, `netlist.cNeg` is not `0`, but its second element is.

```julia
julia> a = [8 0]; a[2]
0

```

Yet it seems like I was wrong in my assessment, because I just copied and pasted all your code into the REPL and it ran to completion without any errors. (This is with `@threads`.) I’m not sure why it’s not working for you. My only guess is that you accidentally modified some global state during your testing. Either that or there really is some multithreading issue that isn’t easily reproduced.

---

<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:** [July 8, 2021, 9:17pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/14 "2021-07-08T21:17:18Z")

</div>

Just recently, I found that the code with @threads, runs a very few sometimes and doesnot run very often. It is quite confusing…  
each thread works on different row of Yn matix, so I think it is already thread-safe.

---

<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:** [July 9, 2021, 12:33am UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/15 "2021-07-09T00:33:23Z")

</div>

It’s not thread safe, here’s a smaller demonstration:

```julia
julia> using SparseArrays

julia> x = spzeros(1000)
1000-element SparseVector{Float64, Int64} with 0 stored entries

julia> x.nzind
Int64[]

julia> Threads.@threads for i in 1:2:1000
         x[i] = Threads.threadid()
       end
ERROR: TaskFailedException
Stacktrace:
 [1] wait
   @ ./task.jl:322 [inlined]
 [2] threading_run(func::Function)
   @ Base.Threads ./threadingconstructs.jl:38
 [3] top-level scope
   @ ./threadingconstructs.jl:97

    nested task error: BoundsError: attempt to access 545-element Vector{Float64} at index [85]
    Stacktrace:
     [1] _growat!
       @ ./array.jl:924 [inlined]
     [2] insert!
       @ ./array.jl:1370 [inlined]
     [3] setindex!(x::SparseVector{Float64, Int64}, v::Float64, i::Int64)
       @ SparseArrays ~/.julia/dev/julia/usr/share/julia/stdlib/v1.8/SparseArrays/src/sparsevector.jl:334
     [4] setindex!(x::SparseVector{Float64, Int64}, v::Int64, i::Int64)
       @ SparseArrays ~/.julia/dev/julia/usr/share/julia/stdlib/v1.8/SparseArrays/src/sparsevector.jl:340
     [5] macro expansion
       @ ./REPL[39]:2 [inlined]
     [6] (::var"#137#threadsfor_fun#10"{UnitRange{Int64}})(onethread::Bool)
       @ Main ./threadingconstructs.jl:85
     [7] (::var"#137#threadsfor_fun#10"{UnitRange{Int64}})()
       @ Main ./threadingconstructs.jl:52

julia> x.nzind
333-element Vector{Int64}:
   1
   3
   5
   7
...

```

Sparse arrays just list of indices and a vector of data, and when you write into them, they grow these lists.

---

<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:** [July 9, 2021, 1:22am UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/17 "2021-07-09T01:22:00Z")

</div>

> [@mcabbott](#):
>
> Sparse arrays just list of indices and a vector of data, and when you write into them, they grow these lists.

Do mean that threads will let the Sparse arrays grow the lists improperly, such as some threads will overwrite on the same indices? but the answer below seems to be correct, right?

> [@mcabbott](#):
>
> ```julia
> julia> x.nzind
> 333-element Vector{Int64}:
> 1
> 3
> 5
> 7
> ...
> 
> ```

---

<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:** [July 9, 2021, 3:27am UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/18 "2021-07-09T03:27:49Z")

</div>

> [@Amro](#):
>
> but the answer below seems to be correct, right?

It it finished, it would have values at all `1:2:1000`, hence length 500. On that run it crashed 2/3 of the way, but this will vary.

> [@Amro](#):
>
> some threads will overwrite on the same indices?

Yes. In a normal dense array there are pre-existing places to write. In a sparse array there are not, any write may need to move other data around, or grow an array (i.e. copy it to a new, larger, block of memory), and if different threads try this you may get wrong answers even if you don’t get an error.

This is what it stores

```julia
julia> x = sprand(5, 0.5)
5-element SparseVector{Float64, Int64} with 2 stored entries:
  [1] = 0.836658
  [4] = 0.0097166

julia> dump(x)
SparseVector{Float64, Int64}
  n: Int64 5
  nzind: Array{Int64}((2,)) [1, 4]
  nzval: Array{Float64}((2,)) [0.8366576714961177, 0.009716596005340361]

julia> x[2] = 222.2;

julia> dump(x)
SparseVector{Float64, Int64}
  n: Int64 5
  nzind: Array{Int64}((3,)) [1, 2, 4]
  nzval: Array{Float64}((3,)) [0.8366576714961177, 222.2, 0.009716596005340361]

```

---

<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:** [July 9, 2021, 1:54pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/19 "2021-07-09T13:54:34Z")

</div>

Understood and thank you very much!  
So, there is no way to use Sparse Array with @thread, right?

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [July 9, 2021, 1:57pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/20 "2021-07-09T13:57:52Z")

</div>

sure there is, you just need to get a mutex before modifying it.

---

<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:** [July 9, 2021, 2:00pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/21 "2021-07-09T14:00:48Z")

</div>

Could you give me an example please?

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [July 9, 2021, 2:16pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/22 "2021-07-09T14:16:29Z")

</div>

It’s not necessarily going to be useful in your example program, but if you’re doing a calculation that’s very long and then storing the result in a sparse array. then you could do something like

```julia
c = Base.Threads.Condition()

@threads for i in 1:1000
   val = do_some_calc(i)
   lock(c)
   mysparsearray[i] = val
   unlock(c)
end

```

This would work well if `do_some_calc` takes a while so that most of the time is spent there and not much time is spent waiting for the lock

---

<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:** [July 9, 2021, 2:46pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/23 "2021-07-09T14:46:23Z")

</div>

Thanks, but it seems that using val variable is not thread-safe, right?

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [July 9, 2021, 3:10pm UTC](https://discourse.julialang.org/t/error-in-using-threads/64290/24 "2021-07-09T15:10:51Z")

</div>

I would have to read up but I think each thread gets its own local variables
