31

I am implementing in MPI a program in which the main process (with rank=0) should be able to receive requests from the other processes who ask for values of variables that are only known by the root. If I make MPI_Recv(...) by the rank 0, I have to specify the rank of the process which sends request to the root, but i cannot control that since the processes don't run in the order 1,2,3,.... How can I receive the request from any rank and use the number of the emitting process to send it the necessary information?

shkk
  • 313
  • 1
  • 3
  • 4

2 Answers2

57

This assumes you are using C. There are similar concepts in C++ and Fortran. You would just specify MPI_ANY_SOURCE as the source in the MPI_recv(). The status struct contains the actual source of the message.

int buf[32];
MPI_Status status;
// receive message from any source
MPI_recv(buf, 32, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
int replybuf[];
// send reply back to sender of the message received above
MPI_send(buf, 32, MPI_INT, status.MPI_SOURCE, tag, MPI_COMM_WORLD);
KeithB
  • 16,577
  • 3
  • 41
  • 45
  • It seems to me like the edit made the code unusable. The `status` struct is indeed used for the `MPI_Send` operation to identify the source of the original message: `status.MPI_SOURCE` – Marius Apr 10 '15 at 15:13
  • 1
    @Marius Thanks, I've rolled back to the original version, and added a couple of comments to make things clearer. – KeithB Apr 10 '15 at 17:23
3

MPI_ANY_SOURCE is the obvious answer.

However, if all the ranks will be sending a request to rank 0, then MPI_Irecv combined with MPI_Testall might also work as a pattern. This will allow the MPI_Send calls to be executed in any order, and the information can be received and processed in the order that the MPI_Irecv calls are matched.

Stan Graves
  • 6,795
  • 2
  • 18
  • 14