Question: I need to prallelize this code with MPI #include // for cos #include #include #include // for getopt #include // for isprint #ifdef USE_MPI #include

I need to prallelize this code with MPI

#include // for cos #include #include #include // for getopt #include // for isprint #ifdef USE_MPI #include #endif

double f(double x) { return cos(x); }

int main(int argc, char ** argv) { int i; int n; int c; double h; double a; double b; double approx; #ifdef USE_MPI double start_time; double stop_time; #endif

#ifdef USE_MPI MPI_Init(&argc,&argv); #endif // default inputs a = 0.0; b = M_PI/2.0; n = 100;

// parse command line arguments while ((c = getopt (argc, argv, "a:b:n:")) != -1) switch (c) { case 'a': //scale sscanf(optarg,"%lf",&a); break; case 'b': //center x sscanf(optarg,"%lf",&b); break; case 'n': //center y sscanf(optarg,"%d",&n); break; case '?': // error messages if (optopt == 'x' || optopt == 'X' || optopt == 'y' || optopt == 'Y' || optopt == 'd') fprintf (stderr, "Option -%c requires an argument. ", optopt); else if (isprint (optopt)) fprintf (stderr, "Unknown option `-%c'. ", optopt); else fprintf (stderr, "Unknown option character `\\x%x'. ", optopt); return 1; default: abort (); } printf("USAGE: trap [-a #] [-b #] [-n #] "); printf(" Integrates cos(x) from a to b in n steps "); printf(" -a lower limit (default 0) "); printf(" -a upper limit (default 2 pi) "); printf(" -n number trapezoids (default 100) ");

// echo input printf(" a = %lf b = %lf n = %d ",a,b,n);

// main trapezoidal rule loop #ifdef USE_MPI start_time = MPI_Wtime(); #endif

///// NOTE!!!!! THIS CODE NOT YET PARALLELIZED ///// All of the MPI boilerplate stuff is there for you //// but the actual parallelization is an exercise in class h = (b-a)/n; approx = (f(a)+f(b))/2.0; for(i=1;i

//output result printf("APPROXIMATION = %lf ",approx);

#ifdef USE_MPI MPI_Finalize(); #endif

}

REMEMBER TO USE MPI

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!