Infinite loop using MPI_Irecv and MPI_Test

I have few problems with the MPI program in C. I want to send two messages with MPI_Send from slaves to master (using MPI_Send, MPI_Irecv and MPI_Test), but only the first message works. After that I have an infinite loop, and I always get a message from slave -1 (according to status.MPI_Source).

So, I don’t understand why I get all these messages from an unknown process (-1) ...

My code is:

#include <stdio.h>
#include <mpi.h>
#include <sys/time.h>

int main(int argc, char *argv[])
{

int rank, size;
MPI_Status status;

/* Init */
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

if (rank != 0) { // Slaves
    int buf;

    if (rank == 1) {
        buf = 1;
        MPI_Send(&buf, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); 
    }
    if (rank == 2) {
        buf = 2;
        MPI_Send(&buf, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); 
    }

}
else { // Master
    int sum = 0;
    int flag, res;
    MPI_Request request;
    MPI_Status status;

    MPI_Irecv(&res, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &request);

    while (1) { 
        flag = 0;

        MPI_Test(&request, &flag, &status);

        if (flag != 0) { 
            printf("recv : %d, slave : %d\n", res, status.MPI_SOURCE);
            if (status.MPI_SOURCE != -1) 
                sum += res;
        }
        else
            printf("fail!\n");

        if (sum == 3)
            break;
    }

    printf("sum : %d\n", sum);
}

MPI_Finalize();
return 0;

}

Thank.

ps: sorry for my english

+5
source share
2 answers

, - . MPI_Irecv , , MPI_Test ( if (status.MPI_SOURCE != -1)), .

+4

, MPI_Irecv , . 2 . , .

, MPI_Irecv. . .

else { // Master
int sum = 0;
int flag, res;
MPI_Request request;
MPI_Status status;

while (1) { 
    flag = 0;
    MPI_Irecv(&res, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &request);
    MPI_Test(&request, &flag, &status);
    if (flag != 0) { 
        printf("recv : %d, slave : %d\n", res, status.MPI_SOURCE);
        if (status.MPI_SOURCE != -1) 
            sum += res;
    }
    else
        printf("fail!\n");

    if (sum == 3)
        break;
}

, ( , ), , : |

0              | called first MPI_Irecv, allocated memory for MPI_Request object
1              | called second MPI_Irecv, allocated memory for MPI_Request (lets say) object2
2              | called third MPI_Irecv, allocated memory for MPI_Request object3
3              | called MPI_Send in slave no. 1
4              | called MPI_Send in slave no. 2
5              | received message by master from slave no. 1, filled object, flag variable still 0 because its related to object3
6              | received message by master from slave no. 2, filled object2, flag variable still 0 because its related to object3
7,8,9...       | infinite loop, flag still has value 0
 n            | error: MPI_Irecv(147): MPI_Irecv(buf=0x7fffecfa60c4, count=1, MPI_INT, src=MPI_ANY_SOURCE, tag=MPI_ANY_TAG, MPI_COMM_WORLD, request=0x7fffecfa60c8)
MPID_Irecv(53): failure occurred while allocating memory for a request object

. -, sleep (3) while, , MPI_Send.

-, MPI_Irecv , . MPI_Irecv 0 . , -1 MPI_Irecv , -1. ​​

:

#include <stdio.h>
#include <mpi.h>
#include <sys/time.h>

int main(int argc, char *argv[])
{

int rank, size;
MPI_Status status;

/* Init */
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

if (rank != 0) { // Slaves
    int buf;

    if (rank == 1) {
        buf = 1;
        MPI_Send(&buf, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); 
    }
    if (rank == 2) {
        buf = 2;
        MPI_Send(&buf, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); 
    }

}
else { // Master
    int sum = 0;
    int flag = -1, res;
    MPI_Request request;
    MPI_Status status;
    while (1) { 
    if(flag != 0)
    {
        MPI_Irecv(&res, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &request);
        flag = 0;
    }
        MPI_Test(&request, &flag, &status);

        if (flag != 0) { 
            printf("recv : %d, slave : %d\n", res, status.MPI_SOURCE);
            if (status.MPI_SOURCE != -1) 
                sum += res;
        flag = -1;
        }


        if (sum == 3)
            break;
    }

    printf("sum : %d\n", sum);
}

MPI_Finalize();
return 0;

}
+10

All Articles