Darqer
Darqer

Reputation: 2887

Issue with data transfer: MPI_Isend followed by MPI_Probe and MPI_Recv

I have following code

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;
}

Issue is that in sizeProb I get 0, and I should get 1.

Can I send data with MPI_ISend and receive it with MPI_Probe / MPI_Recv combination ?

Upvotes: 4

Views: 558

Answers (1)

francis
francis

Reputation: 9817

The fact that sizeProb is always 0 is a good thing ! MPI_Probe() returns an error code which means MPI_SUCCESS in your case. To retreive the size of the array that was sent by the other process, you can use MPI_Get_count(&myStatus, MPI_INT, &sizeProb);

Two links :

Here is a basic example :

#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;

}

Upvotes: 5

Related Questions