#include #include "mpi.h" #include "trap.h" #define MainProcess 0 typedef struct { float a; float b; int n; } INDATA_TYPE; int main(int argc, char** argv) { int me, nproc, local_n; float h, local_a, local_b, integral, total; INDATA_TYPE indata; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &me); MPI_Comm_size(MPI_COMM_WORLD, &nproc); if (me == MainProcess){ printf("Enter a, b, and n\n"); scanf("%f %f %d", &(indata.a), &(indata.b), &(indata.n)); } MPI_Bcast(&indata, sizeof(INDATA_TYPE), MPI_BYTE, MainProcess, MPI_COMM_WORLD); h = (indata.b-indata.a)/indata.n; local_n = indata.n/nproc; local_a = indata.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, MainProcess, MPI_COMM_WORLD); if(me == MainProcess) { printf("With n = %d trapezoids, our estimate\n", indata.n); printf("of the integral from %f to %f = %f\n", indata.a, indata.b, total); } MPI_Finalize(); return 0; }