What is the usage of reduction in openmp? -
i have piece of code parallelized.
int i,n; double area,pi,x; area=0.0; #pragma omp parallel private(x) reduction (+:area) for(i=0; i<n; i++){ x= (i+0.5)/n; area+= 4.0/(1.0+x*x); } pi = area/n;
it said reduction remove race condition happen if didn't use reduction. still i'm wondering need add lastprivate area since used outside parallel loop , not visible outside of it. else reduction cover well?
reduction takes care of making private copy of area
each thread. once parallel region ends area reduced in 1 atomic operation. in other words area
exposed aggregate of private area
s of each thread.
thread 1 - private area = compute(x) thread 2 - private area = compute(y) thread 3 - private area = compute(z) reduction step - public area = area<thread1> + area<thread2> + area<thread3> ...
Comments
Post a Comment