Reputation: 3
I'm trying to convert the function "integrate_openMP", which implements the trapezoid rule, so that it can be run in parallel. I'm in doubt as to which parts of the function should be governed by the "Critical" pragma and how to deal with the calculation itself regarding OpenMP.
The function is called with the pragma's #pragma omp parallel and #pragma omp single from main.
Thank you
I have updated the code with my initial attempt to parallelize the function
double integrate_openMP(double a, double b, double (*f)(double), double e)
{
calls++;
double int_result;
double m = (a + b) / 2;
double one_trap_area = (b - a) * (f(a) + f(b)) / 2;
double two_trap_area = (b - a) * (f(a) + f(b) + 2 * f(m)) / 4;
if (fabs(one_trap_area - two_trap_area) <= e)
{
return two_trap_area;
}
else
{
double left_area, right_area;
#pragma omp task shared(left_area)
{
left_area = integrate_openMP(a, m, f, e / 2);
}
#pragma omp task shared(right_area)
{
right_area = integrate_openMP(m, b, f, e / 2);
}
#pragma omp taskwait
int_result = left_area + right_area;
return int_result;
}
}
double integrate_single(double a, double b, double (*f) (double), double e) {
calls ++;
double m = (a + b) / 2;
double one_trap_area = (b - a) * (f(a) + f(b)) / 2;
double two_trap_area = (b - a) * (f(a) + f(b) + 2 * f(m))/ 4;
if (fabs(one_trap_area - two_trap_area) <= e) {
return two_trap_area;
} else {
double left_area, right_area;
left_area = integrate_single(a, m, f, e/2);
right_area = integrate_single(m, b, f, e/2);
return left_area + right_area;
}
}
Upvotes: 0
Views: 138
Reputation: 2859
Ask yourself a few questions... "Is this loop parallleism?" in which case omp for is useful. "Is this recursive parallelism?" in which case go read up on openmp tasks...
Upvotes: 0