/* Jonathan Hauenstein - University of Notre Dame Center for Applied Mathematics Minicourse on Parallel Computing April 24, May 1, May 8, 2008 Sample programs are designed for educational use */ #include "trapezoid.h" #include int main(int argc, char **argv) { int i, my_id, num_proc, totalTrap = 0; MPI_Status status; double my_ans = 0, total = 0; trapInfo my_info; // initialize MPI MPI_Init(&argc, &argv); // find unique id MPI_Comm_rank(MPI_COMM_WORLD, &my_id); // find the number of processes running MPI_Comm_size(MPI_COMM_WORLD, &num_proc); if (my_id == 0) { // find the number of trapezoids the user wants to use printf("Please enter the number of trapezoids to use: "); scanf("%d", &totalTrap); // quick error checking if (totalTrap <= 0) { printf("The number of trapezoids was not read in properly. This program will terminate!\n\n"); MPI_Abort(MPI_COMM_WORLD, 1); } // determine my_info setup_my_info(&my_info, -1, 1, totalTrap, my_id, num_proc); } else { // recv my_info setup_my_info(&my_info, 0, 0, 0, my_id, num_proc); } // compute the trapezoid integral locally my_ans = composite_trapApprox(&my_info); // collect the sum on id 0 MPI_Reduce(&my_ans, &total, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); if (my_id == 0) { // find 2*total and print ans total *= 2; printf("Using %d trapezoids, the approximation to pi is %.15f.\n\n", totalTrap, total); } // finalize MPI MPI_Finalize(); return 0; }