0

I'm am following video lectures of Tim Mattson on OpenMP and there was one exercise to find errors in provided code that count area of the Mandelbrot. So here is the solution that was provided:

#define NPOINTS 1000
#define MAXITER 1000

void testpoint(struct d_complex);

struct d_complex{
      double r;
      double i;
};

struct d_complex c;
int numoutside = 0;

int main(){
int i,j;
double area, error, eps = 1.0e-5;

#pragma omp parallel for default(shared) private(c,j) firstprivate(eps)
    for(i = 0; i<NPOINTS; i++){
        for(j=0; j < NPOINTS; j++){
             c.r = -2.0+2.5*(double)(i)/(double)(NPOINTS)+eps;
             c.i = 1.125*(double)(j)/(double)(NPOINTS)+eps;
             testpoint(c);
        }
    }

area=2.0*2.5*1.125*(double)(NPOINTS*NPOINTS-numoutside)/(double)(NPOINTS*NPOINTS);
error=area/(double)NPOINTS;

printf("Area of Mandlebrot set = %12.8f +/- %12.8f\n",area,error);
printf("Correct answer should be around 1.510659\n");

}

void testpoint(struct d_complex c){

// Does the iteration z=z*z+c, until |z| > 2 when point is known to be outside set
// If loop count reaches MAXITER, point is considered to be inside the set

   struct d_complex z;
   int iter;
   double temp;

   z=c;
   for (iter=0; iter<MAXITER; iter++){
     temp = (z.r*z.r)-(z.i*z.i)+c.r;
     z.i = z.r*z.i*2+c.i;
     z.r = temp;
     if ((z.r*z.r+z.i*z.i)>4.0) {
       #pragma omp atomic
       numoutside++;
       break;
     }
   }
  }

The question I have is, could we use reduction in #pragma omp parallel of variable numoutside like:

#pragma omp parallel for default(shared) private(c,j) firstprivate(eps) reduction(+:numoutside)

without atomic construct in testpoint function?

I tested the function without atomic, and the result was different from the one I got in the first place. Why does that happen? And while I understand the concept of mutual exclusion and use of it because of race conditioning, isn't reduction just another form of solving that problem with private variables? Thank You in advance.

2

0

Your Answer

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