简体   繁体   中英

Sending to and receiving from different processes in MPI

So I'm writing a program to bounce a "virtual ball" between processes. The root process ie 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.

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 (ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM