title: Parallelizing function using OpenMP and C
body: <p>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. </p>
  
  <p>The function is called with the pragma's 
   <strong>#pragma omp parallel</strong> and <strong>#pragma omp single</strong> from main.</p>
  
  <p>Thank you</p>
  
  <p><strong>I have updated the code with my initial attempt to parallelize the function</strong></p>
  
  <pre><code>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) &lt;= 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) &lt;= 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;
    }
  }
  </code></pre>