2

Can someone explain why even when I set the number of process to more than 1 only two process child are created in the code below. Each MPI_Comm_spawn can create two child process using the code below, in the code used each process created with mpirun will call MPI_Comm_spawn once and will create 2 (#define NUM_SPAWNS 2) child process, so if I call N process then childs 2*N process child must be created. But this does not happen.

In the example below the number of the children must be 4 * 2 = 8. But...

for example:

:~$ mpirun -np 4 ./spawn_example

output:

I'm the parent.

I'm the parent.

I'm the parent.

I'm the parent.

I'm the spawned.

I'm the spawned.

The following sample code illustrates MPI_Comm_spawn.

3 Answers 3

9

You seem to misunderstand what MPI_Comm_spawn does. It is a collective call and it does not spawn n additional processes per rank but rather it spawns a child MPI job with n processes, therefore adding n to the total number of processes. When called with n = 2, it spawn a child job with 2 processes and that's exactly what you observe in the output.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you so much! How could I create n processes child per rank (parent)? It is possible?
No, MPI doesn't have a concept of per-rank child processes. Even more, processes spawned with MPI_Comm_spawn could end up on a totally different set of CPUs (or cluster nodes) than the processes that were initially part of the MPI job. You could use fork() to start child processes per rank but that is non-standard and not supported by all MPI implementations (e.g. on Blue Gene/Q).
5

As long as MPI_Comm_spawn is collective call you can use MPI_COMM_SELF to create children for that particular parent:

Parent:

// Child communicator
MPI_Comm child;
// spawn errors
int spawnError[N];
// Spawn 2 child process for each process
MPI_Comm_spawn("./child", MPI_ARGV_NULL, 2, MPI_INFO_NULL, 0, MPI_COMM_SELF, &child, spawnError);
// Broadcast world id for current parent process to children
int myid;
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Bcast(&myid,1,MPI_INT,MPI_ROOT,child);

Child:

// Obtain an intercommunicator to the parent MPI job
MPI_Comm parent;
MPI_Comm_get_parent(&parent);
// Get child rank
int myid;
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
// Check if this process is a spawned one and if so get parent CPU rank
if (parent != MPI_COMM_NULL) {
  int parent_id;
  MPI_Bcast(&parent_id, 1, MPI_INT,0, parent);
  std::cout<<"Child "<<myid<<" of Parent "<<parent_id<<std::endl;
}

The result will be:

> mpirun -np 4 parent
Child 0 of Parent 2
Child 0 of Parent 1
Child 0 of Parent 0
Child 0 of Parent 3
Child 1 of Parent 0
Child 1 of Parent 2
Child 1 of Parent 1
Child 1 of Parent 3

The only problem for this approach is that children of a different parent will never be able to communicate with each other.

Comments

0

It depends on comm parameter. If you use MPI_COMM_SELF then every master will create n processes, but if you use MPI_COMM_WORLD among all masters will create n processes. So if you have 2 masters in the first case you are gointg to create 2 * n processes. In the second case you are going to create n processes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.