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.

有帮助吗?

解决方案

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.

其他提示

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.

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top