A Simple Example
/* ex0.c - Example using Broadcast, Wildcarding */
/* and Blocking Send and Receive */
#include "mpi.h"
#include <stdio.h>
#define HOST_ID 0
int main(argc,argv)
int argc;
char *argv[];
{
int myid, numprocs;
int i, n;
int tag = 23; /* arbitrary value */
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
if (myid == HOST_ID) {
/* HOST */
int reply;
MPI_Status status;
n = 4;
printf("Host Process about to broadcast\n");
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
printf("Broadcast %d\n", n);
for (i=1;i<numprocs; i++) {
printf("Host receiving reply %d\n", i);
MPI_Recv(&reply, 1, MPI_INT, MPI_ANY_SOURCE, tag,
MPI_COMM_WORLD, &status);
printf("Received %d from process %d\n", reply, status.MPI_SOURCE);
};
} else {
/* WORKER */
int reply;
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
reply = n + myid;
MPI_Send(&reply, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
};
MPI_Finalize();
}