/* MPILab01.c 
 *
 * Send an integer process 0 to all other processes
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <mpi.h>
using namespace std;

int main(int argc, char* argv[]) {
    int         my_rank;       /* rank of process      */
    int         size;           /* number of processes  */
    int         source;        /* rank of sender       */
    int         dest;          /* rank of receiver     */
    int         tag = 0;       /* tag for messages     */
    double      pi = 3.14159;
    int         len;
    char        name[100];     // Processor name
    char        message[100];  
    int         i;
    double      start_time, 
                end_time;    
    MPI::Status  status;        /* return status for    */
                               /* receive              */

    /* Start up MPI */
    MPI::Init(argc, argv);

    /* Find out process rank  */
    my_rank = MPI::COMM_WORLD.Get_rank();

    /* Find out number of processes */
    size = MPI::COMM_WORLD.Get_size();
    
    MPI_Get_processor_name(name, &len);

    if (my_rank == 0) {
         start_time = MPI_Wtime();
	 
    } 
    // All processes 
    MPI::COMM_WORLD.Bcast(&pi, 1, MPI_DOUBLE, 0);
    if (my_rank > 0)
    {
      sprintf(message, 
	     "Process %d: received %f on machine %s", my_rank, pi, name);
      MPI::COMM_WORLD.Send(message, strlen(message)+1, MPI_CHAR, 0, tag);
    }
    else
    {
       	 for(i = 1; i < size; i++)
	 {
	     MPI::COMM_WORLD.Recv(message, 100, MPI_CHAR, i, tag, status);
	     cout << message << endl;
	 }
	 end_time = MPI_Wtime();
  	 cout << "Total time for sending/recieving data: "
	       << (end_time - start_time) << endl;
    }
    
 
    /* Shut down MPI */
    MPI::Finalize();
    
    return 0;
} /* main */

/*
lamboot -v lamhosts

mpicc.lam -o Lab01StarterB Lab01Starter.c

mpirun.lam -np 5 Lab01StarterB

mpirun.lam -np 5 Lab01StarterB
Process 1: received 3.141590 on machine slingshot
Process 2: received 3.141590 on machine slingshot
Process 3: received 3.141590 on machine slingshot
Process 4: received 3.141590 on machine slingshot
Total time for sending/recieving data: 0.011364

*/



syntax highlighted by Code2HTML, v. 0.9.1