-1

What is the final value printed if the the program below is executed with 5 MPI processes?

#include <stdio.h> 
#include <stdlib.h> 
#include <mpi.h> 
int main (int argc, char** argv) 
{ 
  int value=0; 
  int size, rank; 
  
  MPI_Status s; 
  MPI_Init (&argc, &argv); 
  
  MPI_Comm_size (MPI_COMM_WORLD, &size); 
  MPI_Comm_rank (MPI_COMM_WORLD, &rank); 
  
  if (rank==2) 
  { 
    for (int i=0; i<size; i++) 
    { 
        if (i!=2) 
        { 
            int v; 
            MPI_Recv(&v, 1, MPI_INT, i, 42, MPI_COMM_WORLD, &s); 
            value+=v; 
        } 
    } 
    printf("Final value is %i\n",value);




    MPI_Send(&value, 1, MPI_INT, 2, 42, MPI_COMM_WORLD); 
  } 
  
  MPI_Finalize(); 
}

I can't seem to figure it out and I can't run it, it just get blocked ?

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 28 '22 at 03:11

1 Answers1

2

Maybe you should move the MPI_Send into an else block after the if (rank ==2) block?

As it is, unless rank == 2 it does nothing, and rank 2 blocks on MPI_Recv, hence you get a deadlock.

janneb
  • 36,249
  • 2
  • 81
  • 97