0

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;
  }
}
2

1 Answer 1

0

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...

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I have tried to find a solution to this as can be seen in the OP, but it runs really slow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.