数据传输问题:MPI_Isend后跟MPI_Probe和MPI_Recv

时间:2014-07-19 15:47:08

标签: mpi

我有以下代码

int main(int argc, char* argv[])
{
    int rank, size;

    MPI_Init (&argc, &argv);    
    MPI_Comm_rank (MPI_COMM_WORLD, &rank);  
    MPI_Comm_size (MPI_COMM_WORLD, &size);  

    MPI_Request mpiRequest;
    MPI_Status myStatus;
    int tag = 11;


    int testData = 12;
    MPI_Isend(&testData,1,MPI_INT,(rank+1)%size,tag,MPI_COMM_WORLD,&mpiRequest); 
    MPI_Wait (&mpiRequest,&myStatus);

    int source = (rank+size-1)%size;
    int sizeProb = MPI_Probe(source,tag,MPI_COMM_WORLD,&myStatus);
    int * recvTransferArray = new int [sizeProb];

    MPI_Recv(recvTransferArray,sizeProb,MPI_INT,source,tag,MPI_COMM_WORLD,&myStatus);

    MPI_Finalize();

    return 0;
}

问题是在sizeProb中我得到0,我应该得到1。

我可以使用MPI_ISend发送数据并使用MPI_Probe / MPI_Recv组合接收数据吗?

1 个答案:

答案 0 :(得分:5)

sizeProb始终为0的事实是一件好事! MPI_Probe()会返回错误代码,表示您的情况为MPI_SUCCESS。要检索其他进程发送的数组大小,可以使用MPI_Get_count(&myStatus, MPI_INT, &sizeProb);

两个链接:

这是一个基本的例子:

#include "mpi.h"

int main(int argc, char* argv[]) { int rank, size;

  MPI_Init (&argc, &argv);    
  MPI_Comm_rank (MPI_COMM_WORLD, &rank);  
  MPI_Comm_size (MPI_COMM_WORLD, &size);  

  MPI_Request mpiRequest;
  MPI_Status myStatus;
  int tag = 11;


  int testData = rank;
  MPI_Isend(&testData,1,MPI_INT,(rank+1)%size,tag,MPI_COMM_WORLD,&mpiRequest); 
  MPI_Wait (&mpiRequest,&myStatus);

  int source = (rank+size-1)%size;
  MPI_Probe(source,tag,MPI_COMM_WORLD,&myStatus);
  int sizeProb;
  MPI_Get_count(&myStatus, MPI_INT, &sizeProb);
  int * recvTransferArray = new int [sizeProb];

  MPI_Recv(recvTransferArray,sizeProb,MPI_INT,source,tag,MPI_COMM_WORLD,&myStatus);
  std::cout<<"proc "<<rank<<" received "<<sizeProb<<" int, first being "<<recvTransferArray[0]<<std::endl;
  MPI_Finalize();

  return 0;

}