/* simple send and receive */
#include <stdio.h>
#include <mpi.h>

main (int argc, char **argv) {

  int myrank, size, count, i;
  MPI_Status status;
  double a[100],b[100];

  MPI_Init(&argc, &argv);  /* Initialize MPI */
  MPI_Comm_rank(MPI_COMM_WORLD, &myrank); /* Get rank */
  MPI_Comm_size(MPI_COMM_WORLD, &size);   /* Get the total 
					     number of processors */
  printf("Processor %d of %d: Hello World!\n", myrank, size);

  if( myrank == 0 ) {        /* Send a message */
    for (i=0;i<100;++i)
      a[i]= (double)i;
	// Send to all slave nodes
    for (i=1; i<size; i++)
      MPI_Send( a, 100, MPI_DOUBLE, i, 17, MPI_COMM_WORLD );
  } else if( myrank != 0 ){  /* Receive a message */
#ifdef SYNC
    MPI_Recv( b, 100, MPI_DOUBLE, 0, 17, MPI_COMM_WORLD, &status );
#else
    MPI_Recv( b, 300, MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status );   
    MPI_Get_count(&status, MPI_DOUBLE, &count);
    printf("P:%d message came from rank %d\n", myrank, status.MPI_SOURCE);
    printf("P:%d message had tag %d\n", myrank, status.MPI_TAG);
    printf("P:%d message size was %d\n", myrank, count);
#endif
    for (i=0;i<100;++i)
      printf("%.2f ", b[i]);
    printf("\n");
  }
  MPI_Finalize();          /* Terminate MPI */
}
