# Problem with \`@reduce\` of \`FLoops\`; data race?

**URL:** <https://discourse.julialang.org/t/problem-with-reduce-of-floops-data-race/65410>\
**Category:** New to Julia\
**Tags:** package\
**Created:** [July 28, 2021, 3:49am UTC](https://discourse.julialang.org/t/problem-with-reduce-of-floops-data-race/65410 "2021-07-28T03:49:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HJW019](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hjw019/32/22102_2.png) [@HJW019](https://discourse.julialang.org/u/HJW019)\
**Post date:** [July 28, 2021, 3:49am UTC](https://discourse.julialang.org/t/problem-with-reduce-of-floops-data-race/65410/1 "2021-07-28T03:49:41Z")

</div>

I try to use multithreading in Julia and am aware of the data race issue. I then found that `FLoops` provides macros that seems to be able to guard against the problem. I nevertheless encountered a strange behavior with `@reduce`. Results from functions using `@reduce` are not consistent when the number of loops incresases.

The following is a MWE, which is designed to have 1.1 as the correct answer regardless of the number of loops (`cc` in the example). The problem begins to emerge when `cc >= 8`. If I switch off the line `@reduce(aa += bb)` and use `aa += bb` instead (that is, single thread), the function returns correct results.

For my real work, I have to do some nonlinear transformation before adding up the elements and taking the average. The MWE is only to highlight the problem with `@reduce`.

```julia
using Statistics, FLoops

function demo1(cc)
    e = ones(10)*0.1
    M = ones(5, 10)
    vec1 = zeros(cc)

    @floop for i in 1:cc
            aa = 0.0
            bb = 0.0
            for j in 1:size(M,1)  
                bb = e[i] + M[j,i]
                @reduce(aa += bb) # multi-threading, wrong answer
                # aa += bb # usual single thread, correct answer
            end
           vec1[i] = aa/size(M,1)
    end
   return mean(vec1)
end

```

Let’s iterate the function with different numbers of loops in the function.

```julia
julia> for k in 1:10
            display(demo1(k))
       end
1.1
1.1
1.1
1.1
1.1
1.0999999999999999
1.0999999999999999
1.6499999999999997
1.5888888888888886
1.5399999999999996

```

Any advice will be appreciated.

HJ

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [July 28, 2021, 4:01am UTC](https://discourse.julialang.org/t/problem-with-reduce-of-floops-data-race/65410/2 "2021-07-28T04:01:41Z")

</div>

You’d need to use `@floop ThreadedEx() for` and then use `aa += bb` (no `@reduce`). For this code, you can also just use `Threads.@threads for`.

This is not the intended pattern for `@reduce` but it’d be nicer to give less cryptic results. I need to debug this.

FYI: `@floop for` without `@reduce` defaults to single-thread because of some historical API. I probably should stop doing this.

---

<div class="post-metadata">

**Author:** ![HJW019](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hjw019/32/22102_2.png) [@HJW019](https://discourse.julialang.org/u/HJW019)\
**Post date:** [July 28, 2021, 4:14am UTC](https://discourse.julialang.org/t/problem-with-reduce-of-floops-data-race/65410/3 "2021-07-28T04:14:58Z")

</div>

Thanks a lot! That solves my question. I had tried `Threads.@threads` in the past in other problems and I thought I was bit by the data race problem. Guess it won’t occur in the current scenario.
