我有以下代码
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组合接收数据吗?
最佳答案
sizeProb
始终为0的事实是一件好事! MPI_Probe()
返回错误代码,在您的情况下表示MPI_SUCCESS
。要获取其他进程发送的数组的大小,可以使用MPI_Get_count(&myStatus, MPI_INT, &sizeProb);
两个链接:
示例http://mpitutorial.com/dynamic-receiving-with-mpi-probe-and-mpi-status/MPI_Get_count()
http://www.mpich.org/static/docs/v3.1/www3/MPI_Get_count.html
这是一个基本示例:
#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;
}