用fork创建MPI进程?
问题描述:
如果我使用MPI,我运行主程序时指定了多个进程。不过,我想从一个进程开始,并在运行时动态地决定是否以及何时需要更多进程,以便关闭更多进程。这是可能的吗?用fork创建MPI进程?
否则,我将不得不重塑我非常想避免的MPI。
答
不可能使用fork()
,因为子进程将无法使用MPI函数。 MPI中有一个简单的机制来动态创建新进程。您必须使用MPI_Comm_spawn
功能或MPI_Comm_spawn_mutliple
的openmpi DOC:http://www.open-mpi.org/doc/v1.4/man3/MPI_Comm_spawn.3.php
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#define NUM_SPAWNS 2
int main(int argc, char *argv[])
{
int np = NUM_SPAWNS;
int errcodes[NUM_SPAWNS];
MPI_Comm parentcomm, intercomm;
MPI_Init(&argc, &argv);
MPI_Comm_get_parent(&parentcomm);
if (parentcomm == MPI_COMM_NULL) {
MPI_Comm_spawn("spawn_example", MPI_ARGV_NULL, np, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &intercomm, errcodes);
printf("I'm the parent.\n");
} else {
printf("I'm the spawned.\n");
}
fflush(stdout);
MPI_Finalize();
return 0;
}
我是指你我的回答[这个问题] [1]。 [1]:http://stackoverflow.com/questions/9683331/changing-number-of-processors-during-execution-of-the-code-in-mpis-based-paralle/9683758# 9683758 – 2012-03-14 17:38:32
@HighPerformanceMark:但这只是开始一个新的过程。这不完全是从当前进程的精确位置(和配置)写入'fork'的副本。 – bitmask 2012-03-14 18:20:04
你问'是这样或者类似的可能吗?',我建议mph_comm_spawn是相似的。但是,如果它不适合您的要求,您可能会不得不重写MPI。或者使用另一个并行库/工具集/方法。 – 2012-03-14 18:54:49