# Clarification on pmap master-worker model

**URL:** <https://discourse.julialang.org/t/clarification-on-pmap-master-worker-model/7824>\
**Category:** Julia at Scale\
**Tags:** question, pmap\
**Created:** [December 18, 2017, 3:05am UTC](https://discourse.julialang.org/t/clarification-on-pmap-master-worker-model/7824 "2017-12-18T03:05:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 18, 2017, 3:05am UTC](https://discourse.julialang.org/t/clarification-on-pmap-master-worker-model/7824/1 "2017-12-18T03:05:38Z")

</div>

In the master-worker model with `pmap`, what is the correct way of checking for parallel vs. serial execution?

```julia
if nprocs() > 1
  # do task in parallel
else
  # fallback to serial execution
end

```

or

```julia
if nprocs() > 2
  # do task in parallel
else
  # fallback to serial execution
end

```

Can the master process perform work without overhead or I should add 1 for safety when querying `nprocs()`? Assuming this is to be run on a HPC cluster without inter-node SSH communication.

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [December 18, 2017, 3:10am UTC](https://discourse.julialang.org/t/clarification-on-pmap-master-worker-model/7824/2 "2017-12-18T03:10:12Z")

</div>

Without inter-node communication the master process shouldn’t do any work.

`nprocs() > 1` is checking for distributed execution. But you can also just query `nworkers()`,  
If `nprocs == 2` you won’t have parallel execution, but you still have a distributed setup.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 18, 2017, 3:14am UTC](https://discourse.julialang.org/t/clarification-on-pmap-master-worker-model/7824/3 "2017-12-18T03:14:25Z")

</div>

Thank you @vchuravy, very helpful information. From your answer, I understood that most use cases should be using `nworkers()` instead of `nprocs()`:

```julia
if nworkers() > 1
  # do task in parallel
end

```

I will fix my packages accordingly.

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [December 18, 2017, 3:17am UTC](https://discourse.julialang.org/t/clarification-on-pmap-master-worker-model/7824/4 "2017-12-18T03:17:14Z")

</div>

I normally just use `nprocs() > 1`, since the question for me most often is not, should I do this in parallel, but should I execute my program on the `workers()`. Even if only one worker is available 🙂
