Example-1-MPI_Scatterv and MPI_Gather
2015-08-19 02:43
225 查看
目的:利用例子说明collective communication函数功能
Example 1: This program shows how to use MPI_Scatterv and MPI_Gather. 假设有np个数据,要分配到nprocs个进程中,最后一个进程得到平均分配之后多余的数据。每个进程对接收到的数据进行一些改变,然后求和,最后master进程收集求和结果并打印输出。
Assuming np=105, nprocs=4. then the output will be like these:
Rank=0 Sum=325
Rank=1 Sum=1003.6
Rank=2 Sum=1682.6
Rank=3 Sum=2465.1
Example 1: This program shows how to use MPI_Scatterv and MPI_Gather. 假设有np个数据,要分配到nprocs个进程中,最后一个进程得到平均分配之后多余的数据。每个进程对接收到的数据进行一些改变,然后求和,最后master进程收集求和结果并打印输出。
#include "mpi.h" #include <cstdio> #include <math.h> #include <iostream> #define np 105 //number of elements need to be scattered at root const int MASTER = 0; // Rank of of the master process int main(int argc, char* argv[]) { double PosX[np]; //elements need to be scattered double *RecPosX; //receive buffer int nProcs , Rank ;//number of processes, and id of rank int i,j; int reminder; //reminder if it is not divisible double StartTime,EndTime; //timing MPI_Init ( & argc , & argv ); MPI_Comm_size ( MPI_COMM_WORLD , & nProcs ); MPI_Comm_rank ( MPI_COMM_WORLD , & Rank ); StartTime = MPI_Wtime(); //start timing //initialize array int* sendcounts = NULL ; int* displs = NULL ; if ( Rank == MASTER ) { for(i=0;i<np;i++) { PosX[i] = i*1.0; //initialize elements in master process } } sendcounts = new int [ nProcs ]; //allocate memory for array storing number of elements reminder = np%nProcs; //remineder //calculate number of elements need to be scattered in each process for(i=0;i<nProcs;i++) { sendcounts [i] = int(1.0*np/nProcs); } //number of elements in the last process sendcounts [nProcs-1] = sendcounts [nProcs-1] + reminder; //calculate corresponding displacement displs = new int [ nProcs ]; for(i=0;i<nProcs;i++) { displs[i] = 0; for(j=0;j<i;j++) { displs[i] = displs[i] + sendcounts[j]; } } //allocate the receive buffer RecPosX = (double*)malloc(sendcounts [Rank]*sizeof(double)); //now everything is ready and we can start MPI_Scatterv operation. MPI_Scatterv(PosX, sendcounts , displs, MPI_DOUBLE, RecPosX, sendcounts[Rank], MPI_DOUBLE, 0, MPI_COMM_WORLD); //output results after MPI_Scatterv operation //std::cout<<"My Rank = "<<Rank<<" And data I received are:"<<std::endl; double sum=0; double *rbuf; rbuf=(double*)malloc(nProcs*sizeof(double)); for(i=0;i<sendcounts[Rank];i++) { RecPosX[i] = RecPosX[i] + Rank/10.0; sum += RecPosX[i]; //std::cout<<RecPosX[i]<<" "; //if(i%10 == 0 && i != 0)std::cout<<std::endl; } std::cout<<std::endl; MPI_Gather(&sum,1,MPI_DOUBLE,rbuf,1,MPI_DOUBLE,MASTER,MPI_COMM_WORLD); EndTime = MPI_Wtime(); if(Rank == MASTER) { for(i=0;i<nProcs;i++) { std::cout<<"Rank = "<<i<<" Sum= "<<rbuf[i]<<std::endl; } std::cout<<"Total Time Spending For: "<<nProcs<<" Is "<<EndTime-StartTime<<" :Second"<<std::endl; } MPI_Finalize(); return 0; }
Assuming np=105, nprocs=4. then the output will be like these:
Rank=0 Sum=325
Rank=1 Sum=1003.6
Rank=2 Sum=1682.6
Rank=3 Sum=2465.1
相关文章推荐
- Linux C函数参考手册(PDF版)
- Lua教程(十七):C API简介
- C#实现打造气泡屏幕保护效果
- C/C++数据对齐详细解析
- 利用C语言来求最大连续子序列乘积的方法
- 字符串的组合算法问题的C语言实现攻略
- C 语言基础教程(我的C之旅开始了)[三]
- C++中的extern “C”用法详解
- C 语言基础教程(我的C之旅开始了)[七]
- 最大子矩阵问题实例解析
- C字符串操作函数实现方法小结
- 字典树的基本知识及使用C语言的相关实现
- C/C++ 宏详细解析
- 使用C语言解决字符串全排列问题
- C/C++实现对STORM运行信息查看及控制的方法
- 用C#生成不重复的随机数的代码
- 在c和c++中实现函数回调
- 浅析C/C++中被人误解的SIZEOF
- C 语言基础教程(我的C之旅开始了)[六]