#include #include "mpi.h" /* If you'd like to use this external function to calculate each trapezoid... */ double Trap( double local_a, double local_b, int local_n, double h) { double integral; /* Store result in 'integral' */ double x; int i; double f(double x); /* function being integrated */ /* code goes here */ return integral; } int main(int argc, char **argv) { double a = 0.0; double b = 1.0; int n = 1024; double h; double local_a; /* Left endpoint of the process */ double local_b; /* Right endpoint of the process */ int local_n; /* Number of trapezoids for one process's calculation */ double integral; /* Integral over one process's interval */ double total; /* Total integral */ int source; /* one process sending an integral */ int dest = 0; /* all processes send to 0 */ int tag = 0; int rank, size; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); h = (b - a)/n; local_n = n/size; /* Length of each process's interval of integration = local_n * h. So each process's interval starts at: */ local_a = a + ???; local_b = local_a + ???; integral = Trap(local_a, local_b, local_n, h); if (rank == 0) { total = integral; for(source = 1; source < size; source++) { ??? } } else { ??? } if (rank == 0) { printf("With n = %d trapezoids, the estimate ",n); printf("of the integral from %lf to %lf = %lf\n", a, b, total); } MPI_Finalize(); return 0; } double f(double x) { return x * x; } /* CRAY SV1: cc integralMPI.c mpirun -np 4 a.out */