I've few problems with an 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've an infinite loop and i always receive a message from the slave -1 (according to status.MPI_Source).
So i don't understand why i receive all these messages from an unknown process (-1)...
My code :
#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;
}
Thanks.
ps : sorry for my english