首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用MPI_Comm_spawn创建子进程

使用MPI_Comm_spawn创建子进程
EN

Stack Overflow用户
提问于 2014-02-01 11:46:09
回答 2查看 6.7K关注 0票数 2

有人能解释为什么即使当我将进程数量设置为1以上时,在下面的代码中也只创建了两个进程子进程。每个MPI_Comm_spawn可以使用下面的代码创建两个子进程,在所使用的代码中,用mpirun创建的每个进程将调用MPI_Comm_spawn一次,并将创建2(#定义NUM_SPAWNS 2)子进程,因此如果我调用N进程,则必须创建childs 2*N进程子进程。但这种情况不会发生。

在下面的例子中,孩子的数量必须是4*2= 8。但是.

例如:

:~$ mpirun -np 4./sp份数_

产出:

我是家长。

我是家长。

我是家长。

我是家长。

我是孕育出来的。

我是孕育出来的。

产卵。

EN

回答 2

Stack Overflow用户

发布于 2017-07-21 21:48:51

只要MPI_Comm_spawn是集体调用,就可以使用MPI_COMM_SELF为特定的父级创建子级:

父级:

代码语言:javascript
运行
复制
// 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);

儿童:

代码语言:javascript
运行
复制
// 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;
}

其结果将是:

代码语言:javascript
运行
复制
> 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

这种方法的唯一问题是,不同父母的子女永远无法相互交流。

票数 5
EN

Stack Overflow用户

发布于 2019-12-13 17:59:30

这取决于通信参数。如果您使用MPI_COMM_SELF,那么每个主进程都会创建n个进程,但是如果在所有主程序中使用MPI_COMM_WORLD,则会创建n个进程。因此,如果在第一种情况下有两个主进程,则需要创建2*n进程。在第二种情况下,您将创建n个进程。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21497605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档