简体   繁体   中英

MPI_Irecv doesn't receive message in this easy code

I have this code for testing MPI_Irecv and MPI_Isend

    if(rank==1){
        int cc;
        MPI_Request request;
        MPI_Status status;
        int flag;
        do{
            MPI_Irecv(&cc, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, allcomm, &request);
            //MPI_Wait(&request, &status);
            MPI_Test(&request, &flag, &status);
            if (flag != 0) { 
                printf("recv : %d, slave : %d\n", cc, status.MPI_SOURCE);
            }
        } while(flag != 0);

    }
    if(rank==0){
        int cc=0;
        MPI_Request request;
        for(int i=1;i<10;i++){
            cc+=1;
            MPI_Isend(&cc, 1, MPI_INT, 1, 0, allcomm, &request);
        }
    }

If I execute this code, sometimes it prints cc from 1 to 9 and sometimes it prints nothing.

It makes sense, since the process 0 may be executed after process 1 finish.

However, if I let process 1 be a infinity loop like this:

    if(rank==1){
        int cc;
        MPI_Request request;
        MPI_Status status;
        int flag;
        do{
            MPI_Irecv(&cc, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, allcomm, &request);
            //MPI_Wait(&request, &status);
            MPI_Test(&request, &flag, &status);
            if (flag != 0) { 
                printf("recv : %d, slave : %d\n", cc, status.MPI_SOURCE);
            }
        } while(true);

    }

I think process 1 will print cc from 1 to 9 every time, but it won't.

If I insert MPI_Wait() in process 1, it seems process 1 will print cc from 1 to 9 every time. But it will also wait forever if there is nothing sent by other process and I want non-blocking communication actually.

Reply to comments:

1.About version: mpiexec --version mpiexec (OpenRTE) 2.1.1

2.I don't get the point what 'MPI progress thread' means.

Actually I want each process can communicate with each other without blocking and get the information in the buffer each time when receive.

3.executable code following

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <unistd.h>

int main(void)
{
    MPI_Comm allcomm;
    int i;
    int size, rank, irank;
    int tablesize, localtablesize;
    int *table,*table2;

    allcomm = MPI_COMM_WORLD;
    MPI_Init(NULL, NULL);
    MPI_Comm_size(allcomm, &size);
    MPI_Comm_rank(allcomm, &rank);

    if(rank==1){
        int cc;
        MPI_Request request;
        MPI_Status status;
        int flag;
        do{
            MPI_Irecv(&cc, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, allcomm, &request);
            //MPI_Wait(&request, &status);
            MPI_Test(&request, &flag, &status);
            if (flag != 0) { 
                printf("recv : %d, slave : %d\n", cc, status.MPI_SOURCE);
            }
        } //while(flag!=0);
        while(true);

    }
    if(rank==0){
        int cc=0;
        MPI_Request request;
        for(int i=1;i<10;i++){
            cc+=1;
            MPI_Isend(&cc, 1, MPI_INT, 1, 0, allcomm, &request);
        }
    }
    MPI_Finalize();
}

Your program is incorrect.

For example, if rank 1 is faster than rank 0 , the first MPI_Test() will not match any message and then go straight to MPI_Finalize()

Bottom line, this program might print between 0 and 10 lines. This program will only print 10 lines if rank 1 is somehow lagging behind rank 0 , but since both ranks are not synchronized, this is quite unlikely to happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM