c++cparallel-processingmpimpich

MPI Isend and Irecv problems


I'm having trouble with my MPI_Isend and MPI_Irecv blocks of code. I need to send a number Cin to the next process up the line, then the current process can go about it's business.

The receiving process needs to receive before it can go further in it's calculations, but when I don't have MPI_Wait it nevers gets the data, and when I do it just hangs forever. What am I doing wrong?

Note: I only set Cin to 3 in order to see when the message doesn't go through. Currently it just hangs.

void ComputeS5C()
{
    MPI_Request send_request, recv_request;
    MPI_Status status;
    int Cin[1] = {3};
    if(my_rank == 0){Cin[0] = 0;}
    else {
        MPI_Irecv(Cin, 1, MPI_INT, my_rank - 1, 0, MPI_COMM_WORLD, &recv_request);
        MPI_Wait(&recv_request, &status);
        fprintf(stderr, "RANK:%d   Message Received from rank%d: Cin=%d\n", my_rank, my_rank-1, Cin[0]);
    }

    int k;
    for(k = 0; k < Size_5; k++)
    {
        int s5clast;
        if(k==0)
        {
            s5clast = Cin[0];
        }
        else
        {
            s5clast = s5c[k-1];
        }

        s5c[k] = s5g[k] | (s5p[k]&s5clast);
    }

    //if not highest rank, pass the carryin upstream
    if(my_rank < world_size - 1){
        MPI_Isend(&s5c[k], 1, MPI_INT, my_rank+1, 1, MPI_COMM_WORLD, &send_request);
        fprintf(stderr, "RANK:%d   Message sent to rank%d: Cin=%d\n", my_rank, my_rank+1, s5c[k]);
    }
    MPI_Wait(&send_request, &status);
}

Solution

  • The error in your code has to do with the missmatch of tags. Messages are sent using a tag = 1 and received using tag = 0. Sends and receives are not matching explaining why all processes are stuck waiting that sent messages get consumed. Change the tags so that they match.

    A note, when using MPI_Irecv you always need an MPI_Wait to be sure to know when it is safe to consume received data. I think in your example use of MPI_Recv is more approriate.