MPI用MPI_Send, MPI_Recv实现MPI_Alltoall的块方法
2014-10-09 16:35
1626 查看
用了一个多小时,终于搞定用MPI_Send, MPI_Recv实现MPI_Alltoall,网上的类似的源码都是转置单个元素,想按块转置真不容易。首先科普一下什么是MPI_Alltoall, MPI的n个进程,一个进程有n个块,如果块只包含一个元素,则形成N*N的矩阵,MPI_Alltoall要做的是转置这个矩阵,即第i
个进程的第j块发送到第j个进程的第i块。废话不多,上源码。
个进程的第j块发送到第j个进程的第i块。废话不多,上源码。
#include "mpi.h" #include "stdio.h" int My_Alltoall(void *sendBuffer,int sendcnt,MPI_Datatype sendtype, void *receiveBuffer,int recvcnt,MPI_Datatype recvtype,MPI_Comm comm,int rank,int size) { int i,j; MPI_Status status; for(i=0;i<size;i++) { if(rank==i) { MPI_Sendrecv(sendBuffer+(sendcnt*i)*sizeof(sendtype),sendcnt,sendtype,i,99,receiveBuffer+(recvcnt*i)*sizeof(recvtype),recvcnt,recvtype,i,99,comm,&status); } } for(i=0;i<size;i++) { if(rank!=i) { MPI_Send(sendBuffer+(sendcnt*i)*sizeof(sendtype),sendcnt,sendtype,i,i,comm); } } for(i=0;i<size;i++) { if(rank!=i) { MPI_Recv(receiveBuffer+(recvcnt*i)*sizeof(recvtype),recvcnt,recvtype,i,rank,comm,&status); } } return 1; } int main (int argc, char *argv[]) { int i, myrank, size; int *send_buffer; int *recv_buffer; MPI_Init (&argc, &argv); MPI_Comm_rank (MPI_COMM_WORLD, &myrank); MPI_Comm_size (MPI_COMM_WORLD, &size); size = size *2; send_buffer = (int *) calloc (size, sizeof (int)); recv_buffer = (int *) calloc (size, sizeof (int)); for (i = 0; i < size; i++) send_buffer[i] = i+myrank*size; My_Alltoall (send_buffer, 2, MPI_INT, recv_buffer, 2, MPI_INT, MPI_COMM_WORLD,myrank,size/2); for(i=0;i<size;i++){ printf("myrank=%d,sendbuf[%d]=%d\n",myrank,i,send_buffer[i]); } for (i = 0; i < size; i++) printf ("myrank = %d, recv_buffer[%d] = %d\n", myrank, i, recv_buffer[i]); free (recv_buffer); free (send_buffer); MPI_Finalize (); return 0; }
相关文章推荐
- 采用MPI_Send 和MPI_Recv 编写代码来实现包括MPI_Bcast、MPI_Alltoall、MPI_Gather、MPI_Scatter 等MPI 群集通信函数的功能
- 【MPI】使用非阻塞Send/Recv实现并行折叠整数和
- 用MPI_Send和MPI_Recv实现简单集群通信函数
- 采用 MPI_Send 和 MPI_Recv 编写代码来实现 MPI_Allgather 的功能
- MPI之自己实现Alltoall()函数
- JS中实现replaceAll的方法
- 在for,foreach语句中小心使用控件的SendToBack,BringToFront方法
- LINQ TO SQL的ORM模型构架,实现DATA层的方法!
- 实现CopyToDataTable 的T不为DataRow的查询方法(和MSDN上的方法一样)
- Linq to entity 中 实现 Sql 中的like 的两个方法小解
- respondsToSelector判断是否实现了某方法
- 支付宝Payto接口的C#.net实现方法
- 怎样才能在同一个连接上同时实现Send&Recv啊..???
- 实现支付宝Payto接口的C#.net方法
- respondsToSelector判断是否实现了某方法
- Java:对集合中的对象进行排序需要实现Interface Comparable接口并实现int compareTo(T o)方法
- JAVA 1.3 中实现replaceAll 方法
- 安装jmail 出现The message was undeliverable. All servers failed to receive the message的解决方法
- jsonToxml 通过LL型方法实现(C#)[源]
- make: Nothing to be done for `all' 解决方法 ---实验2