/*
** FIFO Order and Matching Rules, Part 0
** Copyright Rolf Riesen 2008
** 
** 
*/
#include <stdio.h>
#include <mpi.h>



int
main(int argc, char *argv[])
{

int my_rank, nproc;
int tag;
int count, dest;
int rbuf1, rbuf2;
int sbuf;
MPI_Status status;


    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &nproc);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    if (nproc != 2)   {
        if (my_rank == 0)   {
            printf("Please run this program on 2 nodes\n");
        }
        MPI_Finalize();
        return 0;
    }

    /* Initialize some variables */
    tag= 0;             /* Fixed tag */
    dest= 0;            /* Send to rank 0 */
    count= 1;           /* Send and receive 1 integer */


    if (my_rank == 0)   {
	MPI_Recv(&rbuf1, count, MPI_INT, MPI_ANY_SOURCE, tag,
            MPI_COMM_WORLD, &status);

	MPI_Recv(&rbuf2, count, MPI_INT, MPI_ANY_SOURCE, tag,
            MPI_COMM_WORLD, &status);

        printf("Rank %d: Received message %d first\n",
            my_rank, rbuf1);
        printf("Rank %d: Received message %d second\n",
            my_rank, rbuf2);

    } else if (my_rank == 1)   {
        sbuf= 0;      /* Initialize send buffer */
	MPI_Send(&sbuf, count, MPI_INT, dest, tag, MPI_COMM_WORLD);
        sbuf= 1;      /* Initialize send buffer */
	MPI_Send(&sbuf, count, MPI_INT, dest, tag, MPI_COMM_WORLD);
    }

    MPI_Finalize();
    return 0;

}  /* end of main() */
