简体   繁体   中英

MPI_Irecv doesn't receive message sent after loop

I've been doing a parallel implementation of the dining philosophers problem and have come to a problem which I cannot solve. Basically, I have a for loop in which a process checks for any new messages and then sleeps for half a second (it repeats this a random number of times). After that part it tries to gather all the needed forks in order to start the "eating" part of the programm.

The problem is that for some reason, if I send a MPI message from inside the for loop, the target process recieves it normally, but if I send a message after the loop, it doesn't. Let me show how that looks in code, hopefully it will make things a bit more clear:

for (int i = 0; i < think; i++) {
        Sleep(500);

        // Recieve messages
        MPI_Irecv(&rcv, 1, MPI_INT, MPI_ANY_SOURCE, NEED_FORK, MPI_COMM_WORLD, &req);
        MPI_Test(&req, &flag, &status);

        if(flag != 0) {
            // Do stuff
        }
        MPI_Send(&myid, 1, MPI_INT, target, NEED_FORK, MPI_COMM_WORLD); // He recieves this
    }
MPI_Send(&myid, 1, MPI_INT, target, NEED_FORK, MPI_COMM_WORLD); // He doesn't recieve this

The problem is that, for some reason, the targert process doesn't recieve any message sent after the for loop finished, while it recieves any message sent before or during the loop. I really have no idea what to do to make this work and would greatly appreciate any help...

Here is my answer on similar question. Even code looks similar. Infinite loop using MPI_Irecv and MPI_Test

Please look closely on calling MPI_Test and MPI_Irecv inside loop.

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