Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the trapezoidal rule program that uses a parallel for directive (omp_trap_3.c ) so that the parallel for is modified by a schedule(runtime) clause. Run

Modify the trapezoidal rule program that uses a parallel for directive (omp_trap_3.c ) so that the parallel for is modified by a schedule(runtime) clause. Run the program with various assignments to the environment variable OMP_SCHEDULE and determine which iterations are assigned to which thread. This can be done by allocating an array iterations of n ints and in the Trap function assigning omp_get_thread_num() to iterations[i] in the ith iteration of the for loop. What is the default assignment of iterations on your system?

/* File: omp_trap3.c * Purpose: Estimate definite integral (or area under curve) using the * trapezoidal rule. This version uses a parallel for directive * * Input: a, b, n * Output: estimate of integral from a to b of f(x) * using n trapezoids. * * Compile: gcc -g -Wall -fopenmp -o omp_trap3 omp_trap3.c * Usage: ./omp_trap3 * * Notes: * 1. The function f(x) is hardwired. * 2. In this version, it's not necessary for n to be * evenly divisible by thread_count. * * IPP: Section 5.5 (pp. 224 and ff.) */ #include #include #include #include void Usage(char* prog_name); double f(double x); /* Function we're integrating */ double Trap(double a, double b, int n, int thread_count); int main(int argc, char* argv[]) { double global_result = 0.0; /* Store result in global_result */ double a, b; /* Left and right endpoints */ int n; /* Total number of trapezoids */ int thread_count; if (argc != 2) Usage(argv[0]); thread_count = strtol(argv[1], NULL, 10); printf("Enter a, b, and n "); scanf("%lf %lf %d", &a, &b, &n); global_result = Trap(a, b, n, thread_count); printf("With n = %d trapezoids, our estimate ", n); printf("of the integral from %f to %f = %.14e ", a, b, global_result); return 0; } /* main */ /*-------------------------------------------------------------------- * Function: Usage * Purpose: Print command line for function and terminate * In arg: prog_name */ void Usage(char* prog_name) { fprintf(stderr, "usage: %s ", prog_name); exit(0); } /* Usage */ /*------------------------------------------------------------------ * Function: f * Purpose: Compute value of function to be integrated * Input arg: x * Return val: f(x) */ double f(double x) { double return_val; return_val = x*x; return return_val; } /* f */ /*------------------------------------------------------------------ * Function: Trap * Purpose: Use trapezoidal rule to estimate definite integral * Input args: * a: left endpoint * b: right endpoint * n: number of trapezoids * Return val: * approx: estimate of integral from a to b of f(x) */ double Trap(double a, double b, int n, int thread_count) { double h, approx; int i; h = (b-a)/n; approx = (f(a) + f(b))/2.0; # pragma omp parallel for num_threads(thread_count) \ reduction(+: approx) for (i = 1; i <= n-1; i++) approx += f(a + i*h); approx = h*approx; return approx; } /* Trap */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions

Question

2. Why?

Answered: 1 week ago

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago