#include #include "mpi.h" int main(int argc, char *argv[]) { /* Processnumber and Number of Procs */ int myid, numprocs; /* Just a state variable */ MPI_Status status; /* Initialize a processor */ MPI_Init(&argc,&argv); /* Retrieve it's id and the total amount of procs */ MPI_Comm_size(MPI_COMM_WORLD, &numprocs); MPI_Comm_rank(MPI_COMM_WORLD, &myid); /* All procs (but the first) wait for a token to come ... */ if (myid > 0) MPI_Recv( MPI_BOTTOM, 0, MPI_INT, myid - 1, 0, MPI_COMM_WORLD, &status); /* ... then write their id ... */ printf("Hello World from process %d\n", myid); /* ... flush the stdout buffer ... */ fflush(stdout); /* ... and send the token to the next proc (unless self=last) */ if (myid + 1 < numprocs) MPI_Send( MPI_BOTTOM, 0, MPI_INT, myid + 1, 0, MPI_COMM_WORLD); /* That's all !!! */ MPI_Finalize(); }