#include "mpi.h" #include int main(int argc, char *argv[]) { int i, step, n, m, myrank, np; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &myrank); MPI_Comm_size(MPI_COMM_WORLD, &np); n = 1; step = 1; while (step < np) { if ((myrank % step) != 0) break /*continue*/; i = myrank / step; if ((i % 2) == 0) { if (myrank + step < np) { MPI_Recv(&m, 1, MPI_INT, myrank + step, step, MPI_COMM_WORLD, &status); n += m; } } else { MPI_Send(&n, 1, MPI_INT, myrank - step, step, MPI_COMM_WORLD); } step *= 2; } if (myrank == 0) printf("result = %d\n", n); MPI_Finalize(); return 0; }