/* L-19 MCS 572 Wed 5 Oct 2016 : use_allgather.c
 * Illustration of the command MPI_Allgather. */

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

int main ( int argc, char *argv[] )
{
   int i,j,p;
   MPI_Init(&argc,&argv);
   MPI_Comm_rank(MPI_COMM_WORLD,&i);
   MPI_Comm_size(MPI_COMM_WORLD,&p);
   {
      int data[p];
      for(j=0; j<p; j++) data[j] = 0;
      data[i] = i + 1;
      printf("data at node %d :",i);
      for(j=0; j<p; j++) printf(" %d",data[j]);
      printf("\n");
      MPI_Allgather(&data[i],1,MPI_INT,
                   data,1,MPI_INT,MPI_COMM_WORLD);
      printf("data at node %d :",i);
      for(j=0; j<p; j++) printf(" %d",data[j]);
      printf("\n");
   }
   MPI_Finalize();
   return 0;
}
