#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
loop_source; /*just an iterator*/
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;
int
got_message[20],received; //used as a boolean
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
(loop_source=1;loop_source < num_procs; loop_source++) {
got_message[loop_source]=0;
}
loop_source = 1;
while
(1) {
for
(source = 1; source < num_procs;source++) {
if
(!(got_message[source]==1)) {
MPI_Iprobe(source,tag,MPI_COMM_WORLD,&received,&status);
if
(received) {
got_message[source]=1;
MPI_Recv(message, maximum_message_length + 1, MPI_CHAR, source, tag, MPI_COMM_WORLD, &status);
printf( "%s \n", message);
loop_source++;
}//received
}
}//for source
if
(loop_source==num_procs)break
;
}//while
}//if-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
/*----------------------------------------------------------------*/