使用fork动态创建MPI进程?

bit*_*ask 5 fork mpi inter-process-communicat

如果我使用MPI,我在运行主程序时指定了许多进程.但是,我想从一个进程开始,并在运行时动态决定是否以及何时需要更多进程,以便关闭更多进程.是或类似的东西可能吗?

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

Val*_*ent 7

这是不可能使用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;
}
Run Code Online (Sandbox Code Playgroud)