1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include "mpi.h"
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char idstr[128];
char buff[128];
int numprocs;
int myid;
int i;
MPI_Status stat;
/*********************************************
Begin program
*********************************************/
MPI_Init(&argc,&argv);// Initialize
MPI_Comm_size(MPI_COMM_WORLD, &numprocs); // Get # processors
MPI_Comm_rank(MPI_COMM_WORLD, &myid);// Get my rank (id)
if( myid == 0 )
{ // Master
printf("WE have %d processors\n", numprocs);
for( i = 1; i < numprocs; i++){
sprintf(buff, "Hello %d", i);
MPI_Send(buff, 128, MPI_CHAR, i, 0, MPI_COMM_WORLD);
//MPI_Send(void *buff, int count, MPI_Datatype type, int dest, int tag, int comm)
}
for( i = 1; i < numprocs; i++){
MPI_Recv(buff, 128, MPI_CHAR, i, 0, MPI_COMM_WORLD, &stat);
//MPI_Recv(void *buff, int count, MPI_Datatype type, int source, int tag, int comm,MPI_Status *status)
printf("In master %s\n", buff);
}
}
else
{ // Slave
MPI_Recv(buff, 128, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &stat);
sprintf(idstr, "in Slave Hello 0, Processor %d is present and accounted for !", myid);
MPI_Send(buff, 128, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}
| |