#include #include "mpi.h" #include "trap.h" void Get_data4(int me, float* a_ptr, float* b_ptr, int* n_ptr){ int root = 0, position = 0; char buffer[100]; if(me == 0){ printf("Enter a, b, and n\n"); scanf("%f %f %d", a_ptr, b_ptr, n_ptr); MPI_Pack(a_ptr, 1, MPI_FLOAT, buffer, 100, &position, MPI_COMM_WORLD); MPI_Pack(b_ptr, 1, MPI_FLOAT, buffer, 100, &position, MPI_COMM_WORLD); MPI_Pack(n_ptr, 1, MPI_INT, buffer, 100, &position, MPI_COMM_WORLD); MPI_Bcast(buffer, 100, MPI_PACKED, root, MPI_COMM_WORLD); } else { MPI_Bcast(buffer, 100, MPI_PACKED, root, MPI_COMM_WORLD); MPI_Unpack(buffer, 100, &position, a_ptr, 1, MPI_FLOAT, MPI_COMM_WORLD); MPI_Unpack(buffer, 100, &position, b_ptr, 1, MPI_FLOAT, MPI_COMM_WORLD); MPI_Unpack(buffer, 100, &position, n_ptr, 1, MPI_INT, 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_data4(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; }