MPI之自己实现Alltoall()函数
来源:互联网 发布:java split函数 编辑:程序博客网 时间:2024/05/22 16:44
MPI_ALLTOALL是组内进程之间完全的消息交换,其中每一个进程都向其它所有的进程发送消息,同时,每一个进程都从其它所有的进程接收消息。本质上,它实现的效果就是矩阵的转置。
如果不调用系统的MPI_ALLTOALL()函数,只用sendrecv()、send()或recv()该如何实现呢?
事实上,只要明白ALLTOALL()函数的工作原理,是很容易实现的。
如:
#include "stdio.h"#include "mpi.h"#include "stdlib.h"int my_alltoall(void *aa, int inputlen, MPI_Datatype inputtype, void *bb, int outputlen, MPI_Datatype outputtype, MPI_Comm comm, int size, int rank){int i,j;MPI_Status status;for(i=0 ; i<size; i++){MPI_Sendrecv(aa+i*sizeof(intputtype ), inputlen, inputtype, i, 123, bb+i*sizeof(outputtype ), outputlen, outputtype, i, 123, comm, &status);}MPI_Barrier(comm);return 0;}int main( int argc, char *argv[] ){ int rank;//iam int size;//npint *a, *b; MPI_Comm mycomm ; MPI_Init(&argc,&argv); MPI_Comm_dup(MPI_COMM_WORLD, &mycomm); MPI_Comm_rank(mycomm, &rank); MPI_Comm_size(mycomm, &size);a = (int *)malloc(size*sizeof(int));b = (int *)malloc(size*sizeof(int));printf("rank=%d, a=", rank);for(int i=0; i<size; i++){a[i]=i+rank*size;printf(" %d", a[i]);}printf("\n");for(int i=0; i<size; i++){b[i]=0;}MPI_Barrier(mycomm);my_alltoall(a, 1, MPI_INT, b, 1, MPI_INT, mycomm, size, rank);MPI_Barrier(mycomm);printf("rank=%d, b=", rank);for(int i=0; i<size; i++){printf(" %d", b[i]);}printf("\n"); MPI_Finalize(); return 0;}
编译运行,结果如下:
rank=0, a= 0 1 2rank=1, a= 3 4 5rank=2, a= 6 7 8rank=0, b= 0 3 6rank=1, b= 1 4 7rank=2, b= 2 5 8
可以看到结果确实实现了转置。
0 0
- MPI之自己实现Alltoall()函数
- MPI聚合通信之MPI_Barrier函数
- MPI聚合通信之MPI_Bcast函数
- MPI聚合通信之MPI_Gather函数
- mpi函数
- MPI函数
- mpi函数
- 实现函数自己调用自己
- MPI函数列表
- 常用的MPI函数
- MPI_Reduce mpi规约函数
- MPI函数学习
- MPI常用函数速查表
- 自己实现popen函数
- 自己实现strncpy函数
- 自己实现strcpy函数
- 自己实现popen函数
- 自己实现popen函数
- leetcode - Remove Duplicates from Sorted List
- 访问权限控制
- dhu 1180 诡异的楼梯
- Android应用中使用百度地图API并添加标注(一)
- 腾讯2014实习生笔试题(重庆站)
- MPI之自己实现Alltoall()函数
- PAT1003
- Linux Mint 16 petra升级到Mint 17 Qianna
- 我的ios开发笔记(1)tableview+nav
- HDU2220 Eddy's AC难题
- 基于ArcGIS Engine和C#的管线剖面图的制作
- 08.黑马程序员-id指针、构造方法
- iOS开发- 获取精确剩余电量
- PHP基础回顾(三)——数组的使用