Clarification on pmap master-worker model

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

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

or

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.

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.

1 Like

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

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

I will fix my packages accordingly.

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 :slight_smile:

1 Like