#include #include "mpi.h" #include "trap.h" void Get_data2(int me, float* a_ptr, float* b_ptr, int* n_ptr){ int root = 0; if (me == 0){ printf("Enter a, b, and n\n"); scanf("%f %f %d", a_ptr, b_ptr, n_ptr); } MPI_Bcast(a_ptr, 1, MPI_FLOAT, root, MPI_COMM_WORLD); MPI_Bcast(b_ptr, 1, MPI_FLOAT, root, MPI_COMM_WORLD); MPI_Bcast(n_ptr, 1, MPI_INT, root, MPI_COMM_WORLD); } int main(int argc, char** argv) { int me, nproc, n, local_n; float h, a, b, local_a, local_b, integral, total; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &me); MPI_Comm_size(MPI_COMM_WORLD, &nproc); Get_data2(me, &a, &b, &n); h = (b-a)/n; local_n = n/nproc; local_a = a + me*local_n*h; local_b = local_a + local_n*h; integral = Trap(local_a, local_b, local_n, h); MPI_Reduce(&integral, &total, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD); if (me == 0) { printf("With n = %d trapezoids, our estimate\n", n); printf("of the integral from %f to %f = %f\n", a, b, total); } MPI_Finalize(); return 0; }