/* MPILab01.c
*
* Send an integer 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();
for(i=1; i < size; i++)
{
printf("Process 0 sending %f to process %d\n", pi, i);
MPI_Send(&pi, 1, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
}
end_time = MPI_Wtime();
for(i = 1; i < size; i++)
{
MPI_Recv(message, 100, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
printf("%s\n", message);
}
printf("Total time for sending/recieving data: %f\n",
end_time - start_time);
}
else { //Process other than root
MPI_Recv(&pi, 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
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);
}
/* Shut down MPI */
MPI_Finalize();
return 0;
} /* main */
/*
lamboot -v lamhosts
mpicc.lam -o Lab01Starter Lab01Starter.c
mpirun.lam -np 5 Lab01Starter
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