Jiew Meng
Jiew Meng

Reputation: 88197

Other processes hang after MPI_Sendrecv

I think using MPI_Sendrecv

MPI_Sendrecv(&ballPos, 2, MPI_INT, FIELD, NEW_BALL_POS_TAG, &ballPos, 2, MPI_INT, winner, NEW_BALL_POS_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

But I noticed only the root (recieving party continues to run?). Having cout before and after Sendrecv produces:

0 b4 sendrecv
2 b4 sendrecv
4 b4 sendrecv
1 b4 sendrecv
3 b4 sendrecv
5 b4 sendrecv
0 after sendrecv

All processes OK before sendrecv, but only root unblocks afterwards.

Full source: see line 147

UPDATE

The result should be something similar to below

if (rank == winner) {
    ballPos[0] = rand() % 128;
    ballPos[1] = rand() % 64;
    cout << "new ball pos: " << ballPos[0] << " " << ballPos[1] << endl;
    MPI_Send(&ballPos, 2, MPI_INT, FIELD, NEW_BALL_POS_TAG, MPI_COMM_WORLD);
} else if (rank == FIELD) {
    MPI_Recv(&ballPos, 2, MPI_INT, winner, NEW_BALL_POS_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}

Upvotes: 0

Views: 803

Answers (1)

Hristo Iliev
Hristo Iliev

Reputation: 74385

The number of sends posted should be equal to the number of receives posted. In you case all ranks are sending to rank FIELD and receiving from rank winner, including FIELD and winner:

Rank       Sends to  Receives from
----------------------------------
0 (FIELD)  FIELD     winner
1          FIELD     winner
2          FIELD     winner
...        ...       ...
winner     FIELD     winner
...        ...       ...
numprocs-1 FIELD     winner

(such tables could be very useful sometimes)

Hence FIELD should receive numprocs messages but it only executes MPI_Sendrecv once and hence numprocs-1 calls to MPI_Sendrecv would not be able to complete their sends. The same goes for winner. It should send numprocs message but as it only executes MPI_Sendrecv once, only one message is sent and hence numprocs-1 calls to MPI_Sendrecv would not be able to complete their receives.

There is also another error. The MPI standard requires that the send and the receive buffers be disjoint (i.e. they should not overlap), which is not the case with your code. Your send and receive buffers not only overlap but they are one and the same buffer. If you want to perform the swap in the same buffer, MPI provides the MPI_Sendrecv_replace operation.

I am not sure what you are trying to achieve with this MPI_Sendrecv statement but I strongly suspect that you need to put it inside an if statement.

Upvotes: 1

Related Questions