/* MPILab01.c 
 *
 * Send an integer from process 0 to all other processes
*/
#include <stdio.h>
#include <string.h>
#include <mpi.h>

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  */
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    /* Find out number of processes */
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    
    MPI_Get_processor_name(name, &len);

    if (my_rank == 0) {
         start_time = MPI_Wtime();
	 
    } 
    // All processes 
    MPI_Bcast(&pi, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
    if (my_rank > 0)
    {
      sprintf(message, 
	     "Process %d: received %f on machine %s", my_rank, pi, name);
      MPI_Send(message, strlen(message)+1, MPI_CHAR, 0, tag,
                	    MPI_COMM_WORLD);
    }
    else
    {
       	 for(i = 1; i < size; i++)
	 {
	     MPI_Recv(message, 100, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
	     printf("%s\n", message);
	 }
	 end_time = MPI_Wtime();
  	 printf("Total time for sending/recieving data: %f\n",
	                end_time - start_time);
    }
    
 
    /* 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