title: In MPI, how to make the following program Wait till all calculations are completed
body: <p>I am new to MPI and this program has been written using C language. In the following program, I am asking other processors to print messages. But I want to print the "END" message at process/rank 0 after all these other processes have been completed. To run this program I am using 4 processors and following commands <code>mpicc file.c -o objfile</code> and <code>mpirun -np 4 objfile</code> 
  <br>Please show me with an example if possible.</p>
  
  <pre><code>#include &lt;mpi.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  int main(int argc, char** argv) 
  {
      MPI_Init(&amp;argc, &amp;argv);
      int world_rank;
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;world_rank);
      int world_size;
      MPI_Comm_size(MPI_COMM_WORLD, &amp;world_size);
  
      int i;
      double centroid[3];/*ignore this array*/
  
      if (world_rank == 0) 
      {
          int destination;
          for (i=0; i&lt;3; i++)
          {
              /*Ignore centroid buffer been sent for now*/
  
              destination = i+1;/*destination rank or process*/
              MPI_Send(&amp;centroid, 3, MPI_DOUBLE, destination, 0, MPI_COMM_WORLD);
          }
  
          printf("\nEND: This need to print after all MPI_Send/MPI_Recv has been completed\n\n");
      } 
      else
      {   
          MPI_Recv(&amp;centroid, 3, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);        
          sleep(1); /*This represent many calculations that will happen here later, instead of sleep*/
          printf("Printing at Rank/Process number: %d\n", world_rank);
      }
  
  
      MPI_Finalize();
      return 0;
  }
  </code></pre>
  
  <p>Result:</p>
  
  <p><code>END: This need to print after all MPI_Send/MPI_Recv has been completed</code>
  <code>
  Printing at Rank/Process number: 2
  Printing at Rank/Process number: 3
  Printing at Rank/Process number: 1</code></p>
  
  <p>Please modify this code or show me with an example of how to wait till all these other processors are done
  <br><br></p>