Search code examples
cmpinonblockingopenmpi

How can i use MPI_Testany and MPI_Irecv to get only the first irecv that will arrive?


In c in "mpi.h", i think it will be something like

MPI_Request mpireq[2];
MPI_Status mpistat;
int temp, index, flag;
MPI_Irecv(temp, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &mpireq[0]);
MPI_Irecv(temp, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, &mpireq[1]);
MPI_Testany(2, mpireq, &index, &flag, &mpistat);

but i think Testany is a non-blocking so i don't know how to use flag and index i propose something like but i don't if this is going to work.

if(flag){
  printf("someone came\n");
  if (index == 0){do something}
  else{do something else}
} else
  printf("No one is here yet");

Solution

  • I tried something like this and it seems to work

    MPI_Irecv
    MPI_Irecv
    MPI_Testany
    while(!flag)
      MPI_Testany();
    if(flag)
      capture Irecv that comes first;