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
- MPI_Comm_spawn的使用方法
- MPI_Comm_spawn问题(求解答)
- 的使用方法
- ctags使用方法 ctags的使用方法
- UITabBarController的使用方法、 UINavigationController的使用方法、 presentModalViewController的使用方法
- UITabBarController的使用方法 UINavigationController的使用方法
- ctags使用方法 ctags的使用方法1
- DateTime的一些使用方法
- CSDNBlog的使用方法
- RetroGuard的使用方法(转)
- 一些Blog的使用方法
- RAR的命令行使用方法
- VI的使用方法
- Make的使用方法
- COM组件的使用方法
- AgileFlow的使用方法
- COM组件的使用方法
- Log4j包的使用方法
- vc++ 查询数据很长如何分页
- 兄弟郊游
- 关于setuid的分析
- ubuntu安装和查看已安装
- 详细图解 Pig介绍、安装配置
- MPI_Comm_spawn的使用方法
- android获取屏幕宽高的两种方法
- 关于三次握手和四次断开的段子
- C++中extern “C”含义深层探索
- 数组,指针数组,数组指针,函数指针数组,函数指针数组的指针
- 第08章 二叉树
- ubuntu安装R语言编译器
- shell脚本test命令没用好,写了好几天的代码全被删了
- 写给准备参加秋招的学弟学妹们~一定要来看哦~