# Multithreading for nested loops

**URL:** <https://discourse.julialang.org/t/multithreading-for-nested-loops/36002>\
**Category:** Performance\
**Tags:** multithreading\
**Created:** [March 15, 2020, 5:31am UTC](https://discourse.julialang.org/t/multithreading-for-nested-loops/36002 "2020-03-15T05:31:15Z")\
**Posts on this page:** 1\
**Showing post:** 25

<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:** [May 22, 2021, 12:10am UTC](https://discourse.julialang.org/t/multithreading-for-nested-loops/36002/25 "2021-05-22T00:10:21Z")

</div>

> [@Debasish](#):
>
> I think my matrix (RM) whose elements are being summed is being accessed by different threads inconsistently. Every time I run the program I get a different answer. This is the same problem that happens in Fortran when variables are not properly categorised into shared, private or reduction variables.

Indeed, lines like `RM[i1,i1] += -GEself[1,1]/8π` indicate that your code have data races. We can’t expect a deterministic behavior.

FYI, there are a couple of examples in FLoops documentation for writing reduction of mutable objects without data races. See: [In-place mutation](https://juliafolds.github.io/FLoops.jl/dev/howto/parallel/#In-place-mutation) and [Initialization with `@reduce(acc = op(init, x))` syntax](https://juliafolds.github.io/FLoops.jl/dev/tutorials/parallel/#Initialization-with-@reduce(acc-op(init,-x))-syntax). There’s also [private variables support](https://juliafolds.github.io/FLoops.jl/dev/howto/parallel/#private-variables).

---

_[View the full topic](https://discourse.julialang.org/t/multithreading-for-nested-loops/36002)._
