MPI_Comm_spawn的使用方法

来源:互联网 发布:美团外卖数据分析 编辑:程序博客网 时间:2024/06/14 00:43

最近在使用MPI编程,当考虑到要用到父进程创建子进程的时候,在网上发现很少的例子程序,于是自己就根据官方文档自己尝试写了一点小程序来测试一下MPI_Comm_spawn的使用方法。由于自己也是第一次接触MPI,如有错误之处,敬请留言指出,谢谢。

首先来看看官方文档的说明:


NAME

       MPI_Comm_spawn -  Spawn a dynamic MPI process

SYNOPSIS

       #include <mpi.h>       int       MPI_Comm_spawn(char* command, char** argv, int maxprocs, MPI_Info info,                      int root, MPI_Comm comm, MPI_Comm *intercomm,                      int *errcodes)

INPUT PARAMETERS

       command              - Name of program to spawn (only significant at root)       argv   - arguments to command (only significant at root)       maxprocs              - max number of processes to start (only significant at root)       info   - startup hints       root   - rank of process to perform the spawn       comm   - parent intracommunicator

OUTPUT PARAMETERS

       intercomm              - child intercommunicator containing spawned processes       errcodes              - one code per process

DESCRIPTION

       A  group  of  processes  can  create  another  group  of processes with       MPI_Comm_spawn .  This function is  a  collective  operation  over  the       parent   communicator.    The  child  group  starts  up  like  any  MPI       application.  The processes must begin  by  calling  MPI_Init  ,  after       which the pre-defined communicator, MPI_COMM_WORLD , may be used.  This       world communicator contains only the child processes.  It  is  distinct       from the MPI_COMM_WORLD of the parent processes.       MPI_Comm_spawn_multiple   is  used  to  manually  specify  a  group  of       different executables and arguments to spawn.  MPI_Comm_spawn  is  used       to  specify  one  executable  and  set of arguments (although a LAM/MPI       appschema(5) can be provided to MPI_Comm_spawn via the "lam_file"  info       key).
(以上说明转自http://manpages.ubuntu.com/manpages/dapper/man3/MPI_Comm_spawn.3.html)
以上说明应该比较详细了,下面是我的程序:

父进程:

/* parent process*/#include "mpi.h"#include <stdlib.h>#include <iostream>#include <fstream>using namespace std;int main(int argc, char *argv[]){   ofstream out("parnet_data.txt");   if( !out )   {        cerr<<"Can not open the file!"<<endl;        exit(1);   }   int world_size, universe_size;   MPI_Comm everyone;           /* intercommunicator */   MPI_Comm intracomm;           // intracommunicator   char worker_program[100];   int parent_id;   MPI_Init(&argc, &argv);   MPI_Comm_rank( MPI_COMM_WORLD, &parent_id );//parnent process  id   MPI_Comm_size(MPI_COMM_WORLD, &world_size); // total process in current communication group   if (world_size != 1) // only set one parent process        cerr<<"Top heavy with management";    //child process size   universe_size = 5;    // the program for child process   char child[100] = "./Child";   // create child process   MPI_Comm_spawn(child, MPI_ARGV_NULL, universe_size,             MPI_INFO_NULL, 0, MPI_COMM_SELF, &everyone,             MPI_ERRCODES_IGNORE);    // create intra communicator    MPI_Intercomm_merge(everyone, 0, &intracomm);    //data to be spreaded    int data[10] = {0,1,2,3,4,5,6,7,8,9};    //broadcast the data to all child process    MPI_Bcast(&data, 10, MPI_INT, 0, intracomm );    //receive data from child process    int receive[10];    MPI_Status status;    for( int j = 0; j < universe_size; ++j )    {        MPI_Recv( &receive, 10, MPI_INT, j, j, everyone, &status );        out<<"receive data from "<<j <<endl;        for( int  i = 0; i<10; ++i ) // write all data to file            out<<receive[i]<<"  ";        out<<endl;    }   MPI_Finalize();   out.close();   return 0;}

子进程:

/* child process */#include <stdlib.h>#include "mpi.h"#include <iostream>using namespace std;int main(int argc, char *argv[]){   int size;   MPI_Status status;   MPI_Comm everyone;   int process_id;   MPI_Comm parent;   MPI_Comm intracomm;   MPI_Init(&argc, &argv);   MPI_Comm_get_parent(&parent);   if (parent == MPI_COMM_NULL)        cerr<<"No parent!";   MPI_Comm_remote_size(parent, &size);   if (size != 1)        cerr<<"Something's wrong with the parent";   MPI_Comm_rank( MPI_COMM_WORLD, &process_id );   MPI_Intercomm_merge(parent, 1, &intracomm);   int buffer[10];    //receive data from parent process    // note that Bcast use the intracommunicator   MPI_Bcast(&buffer, 10, MPI_INT, 0, intracomm );   for( int i=0; i< 10; ++i )        buffer[i] *= 10;    //send data to parent process    //note that Send use the intercommunicator    MPI_Send(buffer, 10, MPI_INT, 0, process_id, parent );   MPI_Finalize();   return 0;}



0 0
原创粉丝点击