/* MCS 572 Mon 3 Oct 2016 : use_sendrecv.c
 * This program is a simple illustration of the sendrecv command in MPI
 * for two processors.  Run this program as mpirun -np 2 use_sendrecv. */

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

#define sendtag 100

int main ( int argc, char *argv[] )
{
   int i,j;
   MPI_Status status;

   MPI_Init(&argc,&argv);
   MPI_Comm_rank(MPI_COMM_WORLD,&i);
   j = (i+1) % 2;  /* the other node */

   {
      char c = 'a' + (char)i;
      printf("Node %d will send %c to %d\n",i,c,j);
      char d;
      MPI_Sendrecv(&c,1,MPI_CHAR,j,sendtag,
                   &d,1,MPI_CHAR,MPI_ANY_SOURCE,
                   MPI_ANY_TAG,MPI_COMM_WORLD,&status);
      printf("Node %d received %c from %d\n",i,d,j);
   }

   MPI_Finalize();
   return 0;
}
