## OpenMP and poiters to member function

General OpenMP discussion

### OpenMP and poiters to member function

Hi, i have a paralleled class with a integrand function and multidimensional integration routine:

Code: Select all
`class NiceClass {    public:    double P;    double (NiceClass::*nrfunc)(double,double,double);void NiceClass(void){P=0;} //constructor// Integrator (sure it`s only blank construction)double qgaus5(double (NiceClass::*func) (double), double a, double b){   double x;   x=a*b*c;      return (this->*func)(x); }// Multidimensional integration routinedouble Multi_int_3d(double (NiceClass::*func)(double, double, double), double x1, double x2){nrfunc=func;return qgaus5(&NiceClass::f1,x1,x2);}double f1(double k){ksav=k;return qgaus5(&NiceClass::f2,-1,1);}double f2(double z){zsav=z;return qgaus5(&NiceClass::f3,-1,1);}double f3(double y){return (this->*nrfunc)(ksav,zsav,y);}// End of multidimensional integration routine//Intergranddouble Integrand(double x, double y, double z ){   return P*x*y*z;}void calc_int(double a){   P=a;   result=Multi_int_3d(&NiceClass::Integrand,x1,x2);}//here is paralleled sectionvoid get_result(){   double res[3];#pragma omp parallel num_threads(2){   #pragma omp for    for (int i = 0; i < 3; i++)   {      res[i]=calc_int( i );   }}//end pragma   }};main() {  NiceClass int;  int.get_result();}`

The problem is all threads become entangled and it all become messy and the result is wrong.
How to make it right?
stkubr

Posts: 1
Joined: Tue Jan 24, 2012 8:49 am

### Re: OpenMP and poiters to member function

The problem is that all threads are trying to use the same P.
One way to fix this would be to create a new instance of NiceClass for each iteration of the parallel loop.
MarkB

Posts: 557
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh