I'm doing my homework whith following assignment:
Every process takes a double as an input. Using function
MPI_Sendrecv_replace()swap all doubles with processes of opposite rank (first & last, second & last but one, ...). In every process output recieved number.
So here is the code that I wrote.
#include "mpi.h"
#include <stdio.h>
#include "pt4.h"
int main(int argc, char *argv[])
{
    MPI_Init(&argc,&argv);
    int flag;
    MPI_Initialized(&flag);
    if (flag == 0)
        return;
    int rank, size;
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    double n;
    pt >> n; // pt is a stream provided by side library (works perfectly fine)
    int oppositeRank = (size - 1) - rank;
    if (rank != oppositeRank)
    {
        MPI_Status status;
        MPI_Sendrecv_replace(&n, 1, MPI_DOUBLE, oppositeRank, 0, 
                             rank, 0, MPI_COMM_WORLD, &status);
    }
    pt << n;
    MPI_Finalize();
    return 0;
}
Although this code compiles without any problems, it never stops. So the question is why? What am I doing wrong?
Replace this:
        MPI_Sendrecv_replace(&n, 1, MPI_DOUBLE, oppositeRank, 0, 
                         rank, 0, MPI_COMM_WORLD, &status);
with this:
        MPI_Sendrecv_replace(&n, 1, MPI_DOUBLE, oppositeRank, 0, 
                         oppositeRank, 0, MPI_COMM_WORLD, &status);
You may find this documentation page useful.
This function sends the buffer to a processor (dest, or the 4th argument) and receives from another (source, the 6th argument). To do a swap you send to another rank and receive from that same rank. In your case you were sending to the opposite rank and receiving from yourself, which would never come, hence the deadlock.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With