Search code examples
cmpidistributed-computingsendbarrier

MPI Isend and Ireceive doesn't work


I have a problem with using Isend and Ireceive. I am trying to send a message to all other processors and after that receiving the same kind of messages from other processors which permormed the same Isend method.

void msgs(int my_rank, int num_procs){
    int arrive_count = 1;
    int leave_count = 0;
    int i;
    bool b_req = true;
    MPI_Request req, req2;
    //Send to all other procs
    for(i=0; i<num_procs; i++){
        if(i != my_rank){
            MPI_Isend(&b_req,sizeof(bool),MPI_BYTE,i,1,MPI_COMM_WORLD, &req);
        }
    }
    bool c_req;
    //Receive msgs from all other procs
    while(arrive_count != num_procs){              
        for(i=0; i<num_procs; i++){
            if(i != my_rank){     
                MPI_Irecv(&c_req,sizeof(bool),MPI_BYTE,i,1,MPI_COMM_WORLD, &req2);              
                if(c_req){
                    c_req = false;
                    arrive_count ++;                
                }
                MPI_Request_free(&req2);
            }
        }
    }
    printf("Done from rank: %d \n", my_rank);    
}

Solution

  • There are at least three issues with your snippet :

    • you cannot use MPI_BYTE with a bool. The MPI standard allows you to use MPI_C_BOOL with a _Bool, an other option is to manually convert between bool and byte before transferring MPI_BYTE
    • you do not use MPI_Isend() nor MPI_Recv() correctly. You should have an array of requests, call MPI_Waitall() at the end.
    • you can either MPI_Irecv() all the remote peers in a first loop, and then MPI_Test() to see check whether the message has arrived in an other loop (note you will need an array of c_req), or have a single loop that MPI_Iprobe() and MPI_Recv() right after if a message has arrived (and that required a single c_req.

    As a side note, and unless you are doing any processing in the while loop, you might want to consider collective operations such as MPI_Alltoall().