7

如果我使用 MPI,我会在运行主程序时指定多个进程。但是,我想从一个进程开始,并在运行时动态决定是否以及何时需要更多进程,以分叉更多进程。那或类似的可能吗?

否则我将不得不重新发明我非常想避免的 MPI。

4

1 回答 1

8

这是不可能的,fork()因为子进程将无法使用 MPI 函数。MPI 中有一个简单的机制来动态创建新进程。您必须使用该MPI_Comm_spawn功能或MPI_Comm_spawn_mutliple

OpenMPI 文档: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;
}
于 2014-04-30T12:52:44.593 回答