2

Here's how my code should work. Slave nodes will perform some computations, and each node will send a value, minE with a corresponding linear array, phi. Root node will then receive, 2 values. I'm trying to figure out on how I will store N-1 (number of slaves) phi in root node. I tried accepting phi in a 2D array but it doesn't work. Or maybe, I'm doing it wrong. So this code simply receives the values of phi from the last node to send.

if (world_rank == root) {
    for(i=1; i<world_size; i++) {
        MPI_Irecv(&local_minE[i], 1, MPI_FLOAT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, &rcv_request[i]);
        MPI_Irecv(phi, len, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, &rcv_request[i]); 
        MPI_Waitall(world_size-1, &rcv_request[1], &status[1]);
    }   
    MPI_Waitall(world_size-1, &rcv_request[1], &status[1]);  
}
else {
   //after computations, node will send a value, minE with a corresponding array phi
   MPI_Isend(&minE, 1, MPI_FLOAT, 0, 1, MPI_COMM_WORLD,&send_request[0]);
   MPI_Isend(phi, len, MPI_INT, root, 1, MPI_COMM_WORLD, &send_request[0]);
   MPI_Wait(&send_request[0], &status[0]);
}
Rowel
  • 115
  • 2
  • 11

1 Answers1

3

First of all, you should consider using MPI_Gather() or even MPI_Igather() if the non-blocking aspect is important for you.

Now, for what regards your code snippet, there is a fundamental problem: you try to use phi on the receiving part while it hasn't been received yet. You need to alter you code so that you first do the waiting part, and only after you do the copying / transposition part.

This would look something like this:

int *trphi = malloc(len*world_size*sizeof(int));
for(i=0; i<world_size; i++) {
    MPI_Irecv(trphi[i], len, MPI_INT, i, 1, MPI_COMM_WORLD, &rcv_request[i]);
}
MPI_Waitall(world_size-1, &rcv_request[1], &status[1]);
for(i=1; i<world_size; i++) {
    for(j=0; j<len; j++) {
        rphi[j][i] = trphi[i][j];
    }
}
for(j=0; j<len; j++) {
    rphi[j][O] = phi[j];
}
free(trphi);

But again, look at MPI_Gather() (and possibly also a good MPI_Datatype) to do it much more nicely.

Gilles
  • 9,269
  • 4
  • 34
  • 53