#include <stdio.h>
#include <time.h>
#include <string.h>
#include "mpi.h"
int main (int argc, char* argv[])
{ /* main */
const int maximum_message_length = 100;
const int master_rank = 0;
char message[maximum_message_length+1];
MPI_Status status; /* Info about receive status */
int my_rank; /* This process ID */
int num_procs; /* Number of processes in run */
int source; /* Process ID to receive from */
int destination; /* Process ID to send to */
int tag = 0; /* Message ID */
int mpi_error; /* Error code for MPI calls */
int icount ;
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_length;
mpi_error =MPI_Init(&argc, &argv);//will not deal with mpi_error except last one.
mpi_error =MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
mpi_error =MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
if (my_rank != master_rank) {
mpi_error=MPI_Get_processor_name(processor_name,&name_length);
sprintf(message, "Greetings from process #%d running on %s\n", my_rank, processor_name);
destination = master_rank;
mpi_error =MPI_Send(message, strlen(message) + 1, MPI_CHAR,destination, tag, MPI_COMM_WORLD);
} /* if (my_rank != master_rank) */
else {
for (source = 0; source < num_procs; source++) {
if (source != master_rank) {
mpi_error =
MPI_Recv(message, maximum_message_length + 1, MPI_CHAR, source, tag, MPI_COMM_WORLD, &status);
printf( "%s \n", message);
}//if
}//for
} /* if (my_rank != master_rank)else */
mpi_error = MPI_Finalize();
if (MPI_SUCCESS!=mpi_error)
return mpi_error;
else
return 0;
} /* main */
/*----------------------------------------------------------------*/
Brought To You By CToHTML
http://www.cs.washington.edu/homes/zahorjan/homepage/Tools/index.htm
/*----------------------------------------------------------------*/