How to check message receipt in MPI

I am trying to write MPI send / receive code, but I have a problem. I want to check the receipt of a message. If I have a message from any source, read this. Otherwise, continue the code.

MPI_Comm_size(MPI_COMM_WORLD,&p);
MPI_Comm_rank(MPI_COMM_WORLD,&id);
MPI_Status status;
int source,ask,request,answer,tag=10;
for(i=first; i<last; i++){
    if(there is a message for my id){
        MPI_Recv(&request,1,MPI_INT,MPI_ANY_SOURCE,tag,MPI_COMM_WORLD,&status);
        answer = request*request;
        source = status.MPI_SOURCE;
        MPI_Send(&answer,1,MPI_INT,source,tag+1,MPI_COMM_WORLD);
    }
    square=i*i;
    if(last < square){
    MPI_Send(&square,1,MPI_INT,id-1,tag,MPI_COMM_WORLD);
    MPI_Recv(&square,1,MPI_INT,id-1,tag+1,MPI_COMM_WORLD,&status);
    }
    local[i]=square;
}

If I cannot verify receipt of a message with an if statement, can I verify by timeout or wait? * I know this code is trivial, but I hope it can help us.

thank

+4
source share
1 answer

It's a little difficult to say exactly what you want, but it looks like you can look for either a non-blocking trick paired with a test, or MPI_IMPROBEpaired with MPI_MRECV.

, :

int flag = 0;
for (i=first; i<last; i++) {
    MPI_Irecv(&data, 1, MPI_INT, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &mpi_request);
    MPI_Test(&mpi_request, &flag, &status);
    while (!flag) {
         // Do other stuff
         MPI_Test(&mpi_request, &flag, &status);
    }
    // Do stuff with message
}

, , :

MPI_Message message;
int flag;
for (i=first; i<last; i++) {
    MPI_Improbe(MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &flag, &message, &status);
    while (!flag) {
         // Do other stuff
         MPI_Test(&mpi_request, &flag, &status);
    }
    MPI_Mrecv(&data, 1, MPI_INT, &message, &status);
    // Do stuff with message
}
+3

All Articles