# Nested parallel loop

**URL:** <https://discourse.julialang.org/t/nested-parallel-loop/26623>\
**Category:** General Usage\
**Created:** [July 22, 2019, 9:39am UTC](https://discourse.julialang.org/t/nested-parallel-loop/26623 "2019-07-22T09:39:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [July 22, 2019, 9:39am UTC](https://discourse.julialang.org/t/nested-parallel-loop/26623/1 "2019-07-22T09:39:48Z")

</div>

Is there an easy way, to make the example below work?

```julia
using Distributed

addprocs(3)

@sync @distributed (+) for x in collect(1:10), y in collect(1:10)
    sum(rand(x,y))
end

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 22, 2019, 10:22am UTC](https://discourse.julialang.org/t/nested-parallel-loop/26623/2 "2019-07-22T10:22:00Z")

</div>

What do you expect from the reducer (+) ?

Doing sum(rand(…)) for all combinations of x and y could be:

```julia
@sync @distributed for t in [(x,y) for x in collect(1:10),y in collect(1:10)]
    sum(rand(t[1],t[2]))
end

```

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [July 22, 2019, 10:24am UTC](https://discourse.julialang.org/t/nested-parallel-loop/26623/3 "2019-07-22T10:24:32Z")

</div>

> [@oheil](#):
>
> @sync @distributed for t in [(x,y) for x in collect(1:10),y in collect(1:10)] sum(rand(t[1],t[2])) end

Thank you. this works.  
Well, this was only a toy example. But I often have a reducer (maybe vcat or similar)

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 22, 2019, 10:28am UTC](https://discourse.julialang.org/t/nested-parallel-loop/26623/4 "2019-07-22T10:28:07Z")

</div>

But beware, the comprehension `[(x,y) for x in collect(1:10),y in collect(1:10)]` is not distributed, or in other words the code is like:

```julia
arrayOfTuples = [(x,y) for x in collect(1:10),y in collect(1:10)];
@sync @distributed for t in arrayOfTuples
    sum(rand(t[1],t[2]))
end

```

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [July 22, 2019, 10:34am UTC](https://discourse.julialang.org/t/nested-parallel-loop/26623/5 "2019-07-22T10:34:34Z")

</div>

that is perfectly fine. in my case ‘the workload’ in in place of sum(rand(…))
