# Multithreading inconsistency issue

**URL:** <https://discourse.julialang.org/t/multithreading-inconsistency-issue/61879>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [May 26, 2021, 6:46pm UTC](https://discourse.julialang.org/t/multithreading-inconsistency-issue/61879 "2021-05-26T18:46:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [May 26, 2021, 6:46pm UTC](https://discourse.julialang.org/t/multithreading-inconsistency-issue/61879/1 "2021-05-26T18:46:44Z")

</div>

The following is a simplified version of a problem I’m working on. I get different answers depending on whether or not I use threads. This has got to be some sort of race condition, but I"m not sure how to straighten it out.

```julia
x0_vals = [[-1.],[1.],[2.],[5.]];
n_runs = 10;

function f!(x)
    for k in 1:10
        @. x += -0.5 * x
    end
    x
end

function class(x)
   return Int(x[1]>0) 
end

nx = length(x0_vals);
svals = zeros(Int,nx);
x = similar(x0_vals[1]);

```

without threads:

```julia
for l in 1:nx
    for k in 1:n_runs
        x .= deepcopy(x0_vals[l]);
        @. x +=k;
        f!(x);
        svals[l] += class(x);
    end
end

svals

4-element Vector{Int64}:
  9
 10
 10
 10

```

with threads:

```julia
@. svals = 0;
for l in 1:nx
    Threads.@threads for k in 1:n_runs
        x .= deepcopy(x0_vals[l]);
        @. x +=k;
        f!(x);
        svals[l] += class(x);
    end
end

svals 

4-element Vector{Int64}:
  6
  8
 10
  8​

```

As should be clear, this is an entirely deterministic problem.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 26, 2021, 6:54pm UTC](https://discourse.julialang.org/t/multithreading-inconsistency-issue/61879/2 "2021-05-26T18:54:19Z")

</div>

> [@gideonsimpson](#):
>
> `x .= deepcopy(x0_vals[l]);`

Aren’t all of your threads writing to the same `x` simultaneously?

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [May 26, 2021, 6:58pm UTC](https://discourse.julialang.org/t/multithreading-inconsistency-issue/61879/3 "2021-05-26T18:58:12Z")

</div>

Ok, I can see how that would be an issue. But if I get rid of the preallocationg of `x` and just have:

```julia
x = deepcopy(x0_vals[l]);

```

in each loop, I still have inconsistent results. I’m open to other suggestions on how to fix this, too.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [May 26, 2021, 7:06pm UTC](https://discourse.julialang.org/t/multithreading-inconsistency-issue/61879/4 "2021-05-26T19:06:16Z")

</div>

All your threads are reading and writing to the same address in svals:

```julia
svals[l] += class(x)

```

Maybe you could paralelize the outer loop instead of the inner one? (For the toy example you will not see any improvement possibly because of the cost of launching the threads).

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [May 26, 2021, 8:33pm UTC](https://discourse.julialang.org/t/multithreading-inconsistency-issue/61879/5 "2021-05-26T20:33:09Z")

</div>

Something like

```julia
@. svals = 0;
temp = zeros(n_runs)
for l in 1:nx
    Threads.@threads for k in 1:n_runs
        x .= deepcopy(x0_vals[l]);
        @. x +=k;
        f!(x);
        temp[k] = class(x);
    end
    svals[l] = reduce(+,temp)
end

```

Could work (taking the reduction outside of the threaded loop), although I am sure more optimizations are possible with the real code.  
Also, don’t forget to put this part also in a function.

Another option is puttin the inside of the k-loop inside of a function and then using mapreduce from the `ThreadsX` [package](https://tkf.github.io/ThreadsX.jl/dev/).
