title: How to reuse MPI_Scatter and MPI_Gather in a loop
body: <p>I am trying to learn how to use <code>MPI_Scatter</code> and <code>MPI_Gather</code> multiple times, and print out the result after waiting for these two MPI functions to complete. At process 0, top of the program, I want to use a while loop that call Scatter and Gather. Once they are done with all the calculation, I want to send this array back to these functions to do more calculations. I have explained in the code bellow what I am trying to do. Comments in <code>/*.....*/</code> are the tasks I am trying to achieve.<Br> 
  Following code is run using 4 processors. <br>
  <code>:$ mpicc test.c -o test</code><br>
  <code>:$ mpirun -np 4 test</code></p>
  
  <pre><code>#include &lt;mpi.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(int argc, char **argv) {
      int size, rank;
      MPI_Init(&amp;argc, &amp;argv);
      MPI_Comm_size(MPI_COMM_WORLD, &amp;size);
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);
  
      int globaldata[8];
      int localdata[2];
      int counter, i;
      if (rank == 0) 
      {
          for (i=0; i&lt;size*2; i++)//initializing array to all zeros, one time
              globaldata[i] = 0;
  
          /*counter=0;
          do
          {
              counter++;  */
              printf("Sending at Processor %d has data: ", rank);
              for (i=0; i&lt;size*2; i++)
                  printf("%d ", globaldata[i]);
              printf("\n");
  
              /*After MPI_Gather is done, I want to get the newly assined array here.
              Now the globaldata array should hold values: 0 0 1 1 2 2 3 3
              Therefore, in the next iteration of this while loop, these array values need 
              to be send for a new calculation with Scatter &amp; Gather
          }while(counter&lt;2);*/
  
          //Following need to be executed after all the scatter and gather has completed
          printf("Finally at Processor %d has data: ", rank);
          for (i=0; i&lt;size*2; i++)//Here the result should be: 0 0 2 2 3 3 4 4
              printf("%d ", globaldata[i]);
          printf("\n");
      }
  
  
      MPI_Scatter(globaldata, 2, MPI_INT, &amp;localdata, 2, MPI_INT, 0, MPI_COMM_WORLD); 
  
      localdata[0]= localdata[0]+rank;
      localdata[1]= localdata[1]+rank;
  
      MPI_Gather(&amp;localdata, 2, MPI_INT, globaldata, 2, MPI_INT, 0, MPI_COMM_WORLD);
  
      if (rank == 0) {//Currently I can only see the newly assinged array values if I print out the result at the bottom
          printf("At the bottom, Processor %d has data: ", rank);
          for (i=0; i&lt;size*2; i++)
              printf("%d ", globaldata[i]);
          printf("\n");
      }
  
  
      MPI_Finalize();
      return 0;
  }
  </code></pre>
  
  <p><strong>More explanation on what I am trying to do above:</strong>
  I am wanting to send my <code>globaldata</code> array to all processors. Then get an updated <code>globaldata</code> array. once I get the updated array, I want to resend this array back to all other process again to do more calculations. I have written the following code that does a similar job using MPI_Send and MPI_Recv. Here I am using MPI_Send to send my array to all processors. Then this array will change it and send back to the root/process 0. Once I get the modified array, <code>do while</code> loop will run again and perform more calculation. What I am trying to do is: use MPI_Scatter and MPI_Gather in a similar way. Where I get a updated <code>globaldata</code> array and send it back to MPI_Scatter and MPI_Gather to change that array again</p>
  
  <pre><code>#include &lt;mpi.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(int argc, char **argv) {
      int size, rank;
      MPI_Init(&amp;argc, &amp;argv);
      MPI_Comm_size(MPI_COMM_WORLD, &amp;size);
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);
  
      int globaldata[8];
      int counter, i;
      if (rank == 0) 
      {
          for (i=0; i&lt;size*2; i++)
              globaldata[i] = 0;
  
          counter=0;
          do
          {   /*becase of this do while loop "globaldata" array will always be updated and resend for more caculations*/
              counter++;  
              printf("Sending at Processor %d has data: ", rank);
              for (i=0; i&lt;size*2; i++)
                  printf("%d ", globaldata[i]);
              printf("\n");
  
              for(i = 0; i &lt; 4; i++)
              {
                  MPI_Send(&amp;globaldata, 8, MPI_INT, i, 0, MPI_COMM_WORLD);
              }
              for(i = 1; i &lt; 4; i++)
              {         
                  MPI_Recv(&amp;globaldata, 8, MPI_INT, i, 99, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
              }
  
          }while(counter&lt;2);
  
          /*Following executes after all the above calculations has completed*/
          printf("Finally at Processor %d has data: ", rank);
          for (i=0; i&lt;size*2; i++)
              printf("%d ", globaldata[i]);
          printf("\n");
      }
  
      counter=0;
      do
      {
          counter++; 
          MPI_Recv(&amp;globaldata, 8, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); 
          globaldata[rank]=globaldata[rank]+rank;
          globaldata[rank+1]=globaldata[rank+1]+rank;
          MPI_Send(&amp;globaldata, 8, MPI_INT, 0, 99, MPI_COMM_WORLD);
      }while(counter&lt;2);
  
      MPI_Finalize();
      return 0;
  }
  </code></pre>