MPI_Gatherv函数的使用
来源:互联网 发布:js高德地图轨迹清除 编辑:程序博客网 时间:2024/05/01 00:13
#include "mpi.h"#include<stdio.h>int main(int argc,char **argv){//每个进程发送100*150矩阵的第rank列的前(100-rank)个数//stride要>=rcounts[i] int gsize;int rank; int *rbuf,*displs,stride; int sendarray[100][150],*sptr;int *rcounts; int root=0;MPI_Datatype stype; stride=102; for(int i=0;i<100;i++)for(int j=0;j<150;j++)sendarray[i][j]=i*150+j;MPI_Init(&argc,&argv);MPI_Comm_size(MPI_COMM_WORLD,&gsize);MPI_Comm_rank(MPI_COMM_WORLD,&rank);rbuf=(int*)malloc(gsize*stride*sizeof(int));displs=(int*)malloc(gsize*sizeof(int)); rcounts=(int*)malloc(gsize*sizeof(int));for(int i=0;i<gsize;i++){displs[i]=i*stride; rcounts[i]=100-i;} //create the datatype for the column to be sent MPI_Type_vector(100-rank,1,150,MPI_INT,&stype);MPI_Type_commit(&stype); /*sptr is the address of the start of rank column*/ sptr=&sendarray[0][rank];MPI_Gatherv(sptr,1,stype,rbuf,rcounts,displs,MPI_INT,root,MPI_COMM_WORLD); if(rank==0){for(int i=0;i<gsize;i++){ int k=0; for(int j=displs[i];k<rcounts[i];k++) printf("%d\n",rbuf[j+k]); } }MPI_Finalize(); }
用MPI_Type_struct的等效实现
MPI_UB type allows the user to easily set the end of the structure
if you define a structure datatype and wish to send or receive multiple items,
you should explicitly include an MPI_UB entry as the last member of the structure
当定义某个结构体,并且需要发送或接收该结构体的多个项时,为了对齐与填充的需要,
需要显示指定MPI_UB的项作为该结构体的最后一个成员
#include "mpi.h"#include<stdio.h>int main(int argc,char **argv){//每个进程发送100*150矩阵的第rank列的前(100-rank)个数//stride要>=rcounts[i] int gsize;int rank; int *rbuf,*displs,stride; int sendarray[100][150],*sptr,disp[2],blocklen[2];int *rcounts; int root=0;MPI_Datatype stype,type[2]; stride=102; for(int i=0;i<100;i++)for(int j=0;j<150;j++)sendarray[i][j]=i*150+j;MPI_Init(&argc,&argv);MPI_Comm_size(MPI_COMM_WORLD,&gsize);MPI_Comm_rank(MPI_COMM_WORLD,&rank);rbuf=(int*)malloc(gsize*stride*sizeof(int));displs=(int*)malloc(gsize*sizeof(int)); rcounts=(int*)malloc(gsize*sizeof(int));for(int i=0;i<gsize;i++){displs[i]=i*stride; rcounts[i]=100-i;} //create the datatype for one int ,with extent of entire row blocklen[0]=1,blocklen[1]=1; disp[0]=0,disp[1]=150*sizeof(int); type[0]=MPI_INT,type[1]=MPI_UB; MPI_Type_struct(2,blocklen,disp,type,&stype);MPI_Type_commit(&stype); /*sptr is the address of the start of rank column*/ sptr=&sendarray[0][rank];MPI_Gatherv (sptr,100-rank,stype,rbuf,rcounts,displs,MPI_INT,root,MPI_COMM_WORLD); if(rank==0){for(int i=0;i<gsize;i++){ int k=0; for(int j=displs[i];k<rcounts[i];k++) printf("%d\n",rbuf[j+k]); } }MPI_Finalize(); }
- MPI_Gatherv函数的使用
- MPI_Gatherv函数的应用实例
- MPI_Gatherv 因该注意的问题
- MPI_gather/MPI_gatherv测试
- 使用excel的函数
- format函数的使用
- format函数的使用
- SetTimer函数的使用
- showModalDialog函数的使用
- 函数对象的使用
- DECODE函数的使用
- IIF函数的使用
- AnimateWindow()函数的使用
- count(*)函数的使用
- ceiling函数的使用
- strcat函数的使用
- 常用函数的使用
- translate函数的使用
- 最佳Linux服务器发行版
- dll调用
- 一个好用的编码类
- 精通UIPasteboard 粘贴板
- NYOJ 10 Skiing 解题报告
- MPI_Gatherv函数的使用
- error while loading shared libraries 错误解决办法总结,
- c++出错问题解析
- 10幅图告诉你Pinterest的魅力所在
- Oracle数据库的十种重新启动步骤
- 用vim比较文件(Using vim to compare files)
- socket 互相通信
- string2int int2string
- openstack swift