# Nested pmap() functions don not work as I expect

**URL:** https://discourse.julialang.org/t/nested-pmap-functions-don-not-work-as-i-expect/9789
**Category:** Performance
**Tags:** pmap
**Created:** [March 18, 2018, 3:17pm UTC](https://discourse.julialang.org/t/nested-pmap-functions-don-not-work-as-i-expect/9789 "2018-03-18T15:17:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ersoacc](https://avatars.discourse-cdn.com/v4/letter/e/9d8465/32.png) [@ersoacc](https://discourse.julialang.org/u/ersoacc)
#### Post date: [March 18, 2018, 3:17pm UTC](https://discourse.julialang.org/t/nested-pmap-functions-don-not-work-as-i-expect/9789/1 "2018-03-18T15:17:39Z")

</div>

Hello everyone.

I am using pmap function to calculate and sum a function with different arguments such as:

```julia
function gparal(nsubs)
                Gs = pmap(i -> ASPINS1Sub(i, ...), 1:nsubs)
                return sum(Gs)
            end
G = gparal(nsubs)

```

There is one more pmap code inside of a funciton within ASPINS1Sub .

Assume nsubs = 4 and 4 workers are computing seperatelly.  
For example: Worker 2 computes ASPINS1Sub and when it comes to inside pmap, it does not work in parallel. Just worker 2 does the job (it seems there is no internal pmap).

How can I fix that? I want that internal code runs in parallel.

---

<div class="post-metadata">

### Author: ![pasha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pasha/32/3319_2.png) [@pasha](https://discourse.julialang.org/u/pasha)
#### Post date: [March 18, 2018, 5:05pm UTC](https://discourse.julialang.org/t/nested-pmap-functions-don-not-work-as-i-expect/9789/2 "2018-03-18T17:05:04Z")

</div>

Are you saying that `ASPINS1Sub` also has a pmap in it, so you have a pmap running a function that also runs a pmap?

Assuming that’s what you mean, that’s not good way to do parallel work; likely the first pmap will allocate work to all workers and your nested process will have none. It’s better to farm bigger jobs (500 ms+ of work) to each worker - maybe there’s another way to organize your task?

If that’s not what you mean, my apologies, and please clarify?

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [March 18, 2018, 7:14pm UTC](https://discourse.julialang.org/t/nested-pmap-functions-don-not-work-as-i-expect/9789/3 "2018-03-18T19:14:34Z")

</div>

> [@ersoacc](#):
>
> For example: Worker 2 computes ASPINS1Sub and when it comes to inside pmap, it does not work in parallel. Just worker 2 does the job (it seems there is no internal pmap).

Nested `pmap` seems to work fine. See my [notebook](https://github.com/tk3369/julia-notebooks/blob/master/pmap%20inside%20pmap%20(juliabox).ipynb) for an example.

---

<div class="post-metadata">

### Author: ![ersoacc](https://avatars.discourse-cdn.com/v4/letter/e/9d8465/32.png) [@ersoacc](https://discourse.julialang.org/u/ersoacc)
#### Post date: [March 19, 2018, 12:36pm UTC](https://discourse.julialang.org/t/nested-pmap-functions-don-not-work-as-i-expect/9789/4 "2018-03-19T12:36:45Z")

</div>

> Are you saying that ASPINS1Sub also has a pmap in it, so you have a pmap running a function that also runs a pmap?
> 
> Assuming that’s what you mean, that’s not good way to do parallel work; likely the first pmap will allocate work to all workers and your nested process will have none. It’s better to farm bigger jobs (500 ms+ of work) to each worker - maybe there’s another way to organize your task?
> 
> If that’s not what you mean, my apologies, and please clarify?

Yes, that is what I meant.

> Nested pmap seems to work fine. See my notebook3 for an example.

I checked your notebook, it seems its working.

I added `println(myid())` on the function within insider pmap. It seems different procs handle the jobs seperately. But still it appears to me that it is not working as it should because when I run it serial, I get nearly same time output using `@time`.

Thank you for your answers.
