/* -*- Mode: C; -*- */

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

int
main (int argc, char **argv)
{
  int nprocs = -1;
  int rank = -1;
  int tag1 = 0;
  MPI_Comm comm = MPI_COMM_WORLD;
  char processor_name[128];
  int namelen = 128;
  int buf0[128];
  int buf1[128];
  int i;
  MPI_Request aReq[2];
  MPI_Status aStatus[2];

  /* init */
  MPI_Init (&argc, &argv);
  MPI_Comm_size (comm, &nprocs);
  MPI_Comm_rank (comm, &rank);
  MPI_Get_processor_name (processor_name, &namelen);
  printf ("(%d) is alive on %s\n", rank, processor_name);
  fflush (stdout);

  for (i = 0; i < 128; i++)
    {
      buf0[i] = i;
      buf1[i] = 127 - i;
    }

  MPI_Barrier(MPI_COMM_WORLD);

  switch (rank)
    {
    case 0:
      MPI_Isend (buf0, 128, MPI_INT, 1, tag1, comm, &aReq[0]);
      MPI_Wait (&aReq[0], &aStatus[0]);
//      MPI_Test (&aReq[0], &flag, &aStatus[0]);
      MPI_Isend (buf1, 128, MPI_INT, 2, tag1, comm, &aReq[1]);
      MPI_Wait ( &aReq[1], &aStatus[1]);
  //    MPI_Test (&aReq[1], &flag, &aStatus[1]);

      break;

    case 1:
      MPI_Isend (buf0, 128, MPI_INT, 2, tag1, comm, &aReq[0]);
      MPI_Wait (&aReq[0], &aStatus[0]);
     // MPI_Test (&aReq[0], &flag, &aStatus[0]);
      MPI_Irecv (buf0, 128, MPI_INT, 0, tag1, comm, &aReq[1]);
      MPI_Wait (&aReq[1], &aStatus[1]);
      //MPI_Test (&aReq[1], &flag, &aStatus[1]);
      break;

	case 2:
      MPI_Irecv (buf0, 128, MPI_INT, MPI_ANY_SOURCE, tag1, comm, &aReq[0]);
      MPI_Wait (&aReq[0], &aStatus[0]);
      //MPI_Test (&aReq[0], &flag,&aStatus[0]);
      MPI_Irecv (buf0, 128, MPI_INT, MPI_ANY_SOURCE, tag1, comm, &aReq[0]);
      MPI_Wait (&aReq[1], &aStatus[1]);
      //MPI_Test (&aReq[1],&flag ,&aStatus[1]);
      break;
    }

  MPI_Finalize ();
  printf ("(%d) Finished normally\n", rank);
  return 0;
}

/* EOF */

