MPI_Gatherv小例子

访问本站观看效果更佳

MPI_Gatherv的作用:从一组进程中收集数据,汇总到根进程中。MPI_GATHERV扩展了MPI_GATHER的功能, 它可以从不同的进程接收不同数量的数 据,因为此时recvcounts是一个数组,除此之外,它还提供了更大的灵活性,比如它提供了一个新的参数displs,用户可以将接收的数据存放到根进程消息缓冲区的任意处.
MPI_GATHER 接收到的数据在接收缓冲区中是连续存放的, 而MPI_GATHERV 接收到的数据在接收缓冲区中是不连续存放的。

int MPI_Gatherv(const void *sendbuf,int sendcount,MPI_Datatype sendtype,void *recvbuf,const int *recvcounts,const int *displs,MPI_Datatype rectype,int root,MPI_Comm comm)

下面展示具体代码:

//Gathers into specified locations from all processes in a group
#include "mpi.h"

int main( int argc, char* argv[] ){
    int i;
    int rank, nproc;
    int isend[3],iscnt,irecv[6];
    int ircnt[3] = {1,2,3}, idisp[3] = {0,1,3};
 
    MPI_Init( &argc, &argv );
    MPI_Comm_size( MPI_COMM_WORLD, &nproc );
    MPI_Comm_rank( MPI_COMM_WORLD, &rank );

   if ( rank == 0 ) printf("This program must run with 3 processes: np = 3 \n");
   
    for(i=0; i<3; i++)
        isend[i] = rank + 1;
        iscnt = rank + 1;
//int MPI_Gatherv(const void *sendbuf,int sendcount,MPI_Datatype sendtype,void *recvbuf,const int *recvcounts,const int *displs,MPI_Datatype rectype,int root,MPI_Comm comm)
//displs
//integer array (of length group size). Entry i specifies the displacement relative to recvbuf at which to place the incoming data from process i (significant only at root)
       MPI_Gatherv( isend, iscnt, MPI_INT, irecv, ircnt, idisp, 
                               MPI_INT, 0, MPI_COMM_WORLD);
       if(rank == 0) {
             for(i=0; i<6; i++)  printf("irecv = %d\n", irecv[i]);
       }
    MPI_Finalize();
}

猜你喜欢

转载自blog.csdn.net/zcgyq/article/details/83088180
mpi