#include #include #include #include "mpi.h" #include "io.h" int main(int argc, char** argv) { int me, nproc, io_clone, input; char hostname[PATH_MAX]; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &me); MPI_Comm_size(MPI_COMM_WORLD, &nproc); if((io_clone = get_io_clone()) < 0){ fprintf(stderr, "No one can do IO!\n"); goto exit; } if(gethostname(hostname, PATH_MAX) != 0){ fprintf(stderr, "gethostname failed, exiting\n"); goto exit; } if(me == io_clone){ fprintf(stderr, "I'm clone %3d on %s an I can do IO!\n", me, hostname); fprintf(stderr, "Give me a number\n"); scanf("%d", &input); fprintf(stderr, "You gave me %d\n", input); } //N.B. It is vital, for this next line to make sense, that //get_io_clone() returns the same value in all clones! //Hence the election process. MPI_Bcast(&input, 1, MPI_INT, io_clone, MPI_COMM_WORLD); fprintf(stderr, "Clone %3d input = %d!\n", me, input); exit: MPI_Finalize(); return 0; }