/* bcast.c * * simple MPI_Bcast example * read in an integer from a file (para.dat) and broadcast it to all processes * */ #include #include "mpi.h" #define ROOT 0 int main(int argc, char *argv[]) { int size, rank, x=0; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (rank == ROOT) { FILE *fp = fopen("para.dat","r"); fscanf(fp,"%d",&x); fclose(fp); } printf("(before) my rank = %d, x = %d\n", rank, x); MPI_Bcast(&x, 1, MPI_INT, ROOT, MPI_COMM_WORLD); printf("(after) my rank = %d, x = %d\n", rank, x); MPI_Finalize(); return 0; }