有人能解释为什么即使当我将进程数量设置为1以上时,在下面的代码中也只创建了两个进程子进程。每个MPI_Comm_spawn可以使用下面的代码创建两个子进程,在所使用的代码中,用mpirun创建的每个进程将调用MPI_Comm_spawn一次,并将创建2(#定义NUM_SPAWNS 2)子进程,因此如果我调用N进程,则必须创建childs 2*N进程子进程。但这种情况不会发生。
在下面的例子中,孩子的数量必须是4*2= 8。但是.
例如:
:~$ mpirun -np 4./sp份数_
产出:
我是家长。
我是家长。
我是家长。
我是家长。
我是孕育出来的。
我是孕育出来的。
产卵。
发布于 2017-07-21 21:48:51
只要MPI_Comm_spawn是集体调用,就可以使用MPI_COMM_SELF为特定的父级创建子级:
父级:
// 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);
儿童:
// 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;
}
其结果将是:
> 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
这种方法的唯一问题是,不同父母的子女永远无法相互交流。
发布于 2019-12-13 17:59:30
这取决于通信参数。如果您使用MPI_COMM_SELF,那么每个主进程都会创建n个进程,但是如果在所有主程序中使用MPI_COMM_WORLD,则会创建n个进程。因此,如果在第一种情况下有两个主进程,则需要创建2*n进程。在第二种情况下,您将创建n个进程。
https://stackoverflow.com/questions/21497605
复制相似问题