Hi Guys,
I've got a multi process program as below. I'm using proc 0 as a passive receiver which used non-blocking receiver to receive messages sent from the other procs. However what I'm currently seeing is it can't receive from the last proc
for example if I called the program like this:
mpirun -np 4 test
the output I get is:
receive some into position 0
receive some into position 1
receive some into position 2
before waitsome
how many done:1
Msg (sbuf = 1) from 1 with tag 1
receive some second time into position 1
cnt:2
before waitsome
how many done:1
Msg (sbuf = 2) from 2 with tag 2
receive some second time into position 2
cnt:1
before waitsome
<<--------- from here the program just stalls waiting for the message from proc 3 to come
Any help on this would be must appreciated thanks
Code:
#include "mpi.h"
#include <stdio.h>
typedef struct {
int answer;
int read_size;
}REPLY;
int main(int argc, char *argv[])
{
int rank, size, i, sbuf, cnt;
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &size );
sbuf = rank;
if (rank == 0) {
MPI_Request* requests = (MPI_Request*)malloc((size-1)*sizeof(MPI_Request));
MPI_Status* statuses = (MPI_Status*)malloc(sizeof(MPI_Status)*(size-1));
int* indices = (int *)malloc((size-1)*sizeof(int));
int* buf = (int *)malloc((size-1)*sizeof(int));
int j, ndone;
cnt = size-1;
for (i=0; i<size-1; i++) {
printf("receive some into position %d\n",i);
MPI_Irecv(buf+i, 1, MPI_INT, i, MPI_ANY_TAG, MPI_COMM_WORLD, &requests[i]);
}
while(cnt > 0) {
printf("before waitsome\n");
MPI_Waitsome(size-1, requests, &ndone, indices, statuses);
printf("how many done:%d\n",ndone);
for (i=0; i<ndone; i++) {
j = indices[i];
printf("Msg (sbuf = %d) from %d with tag %d\n",
buf[j],
statuses[i].MPI_SOURCE,
statuses[i].MPI_TAG);
printf("receive some second time into position %d\n",j);
MPI_Irecv(buf+j, 1, MPI_INT, j, MPI_ANY_TAG, MPI_COMM_WORLD, &requests[j]);
}
cnt -= ndone;
printf("cnt:%d\n",cnt);
}
}
else {
printf("proc %d sending...\n",rank);
MPI_Send( &sbuf, 1, MPI_INT, 0, rank, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}