0

So I'm writing a program to bounce a "virtual ball" between processes. The root process i.e. the task with rank 0 initializes the game and then sends it to a random process which is determined by rand() % size (with the random number generated seeded by the initial rank).

I've tried doing:

int rnk= rand() % size; MPI_Send(&ball,1, MPI_INT, rnk, MPI_COMM_WORLD);

This sends the ball to the next random process but upon running this the code is held up by the blocking MPI_Send. I've just begun parallel programming so I don't have enough grasp of this. How do I send to a random process and then they further send it to any random process?

Any pointers, tips, books and tutorials are welcome.

2 Answers2

1

There is an issue here if the root initially tries to send to itself (which can happen since rand()%size could be zero).

  • if you post the receive first on the root then it will block as it will never get to the send call (as pointed out by @Gregor above);
  • however, if you post the send first on the root then there is no guarantee that it will ever progress to the receive call as MPI_Send() is not guaranteed to be asynchronous (i.e. it could be implemented as a synchronous send which will wait forever for a matching receive).

You need to either ensure that the sender never sends to itself, or use non-blocking sends (or non-blocking receives) to avoid the potential deadlock.

David Henty
  • 1,694
  • 9
  • 11
0

You can use MPI_Recv with MPI_ANY_SOURCE as source (see comment by francis).

Make sure that you first call MPI_Recv in all processes except for the root, which first needs to send.

Gregor
  • 411
  • 1
  • 6
  • 12