/* MPILab01.cpp 
 *
 * Send a double 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();
	 
	 for(i=1; i < size; i++)
	 {
	     cout << "Process 0 sending " << pi 
	          << " to process " << i << endl;;
   	     MPI::COMM_WORLD.Send(&pi, 1, MPI_DOUBLE, i, tag);
         }
   	 end_time = MPI_Wtime();
	 
	 for(i = 1; i < size; i++)
	 {
	     MPI::COMM_WORLD.Recv(message, 100, MPI_CHAR, i, tag, status);
	     cout << message << endl;
	 }
  	 cout << "Total time for sending/recieving data: "
	      << (end_time - start_time) << endl;

    } 
    else { //Process other than root
	 MPI::COMM_WORLD.Recv(&pi, 1, MPI_DOUBLE, 0, tag, status);
         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);

    }
 
    /* Shut down MPI */
    MPI::Finalize();
    
    return 0;
} /* main */

/*
lamboot -v lamhosts

mpiCC.lam -o Lab01StarterCC Lab01Starter.cpp

mpirun.lam -np 5 Lab01StarterCC

Process 0 sending 3.141590 to process 1
Process 0 sending 3.141590 to process 2
Process 0 sending 3.141590 to process 3
Process 0 sending 3.141590 to process 4
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.005054


*/



syntax highlighted by Code2HTML, v. 0.9.1