Aakash Anuj
Aakash Anuj

Reputation: 3871

How to have different OpenMP threads execute different tasks

I am using open MP to speed up the flux calculation in my program. I basically want OpenMP to carry out both of these left and right flux calculations in parallel. But on the contrary, the following code takes even more time with the #pragma directives. What do i modify to get it right?

#pragma omp parallel num_threads(2)
{

#pragma omp single
{//first condition
//cerr<<"Executed thread 0"<<endl;
        if ( (fabs(lcellMach-1.0)<EPSILON) || ( (lcellMach-1.0) > 0.0 ) ){//purpose of Epsilon!!!!
                FluxP[0] = rhol * vnl;
                FluxP[1] = rhol * ul * vnl + Pl*nx;
                FluxP[2] = rhol * vl * vnl + Pl*ny;
                FluxP[3] = rhol * wl * vnl + Pl*nz;
                FluxP[4] = rhol * ((GAMMA * Pl / (rhol * (GAMMA-1.0))) + ((ul*ul + vl*vl + wl*wl)/2.0)) * vnl;
        }else if ( (fabs(lcellMach+1.0)<EPSILON) || ( (lcellMach+1.0) < 0.0 ) ){
                FluxP[0] = FluxP[1] = FluxP[2] = FluxP[3] = FluxP[4] = 0.0;// If flow direction is opposite the Flux + is zero
        }else {
                double ql = (ul*ul + vl*vl + wl*wl);// how did this come
                FluxP[0] = rhol * lcell_a * (lcellMach+1.0)*(lcellMach+1.0) / 4.0;
                FluxP[1] = FluxP[0] * ( ul + (nx*(0.0-vnl + 2.0*lcell_a)/GAMMA) );
                FluxP[2] = FluxP[0] * ( vl + (ny*(0.0-vnl + 2.0*lcell_a)/GAMMA) );
                FluxP[3] = FluxP[0] * ( wl + (nz*(0.0-vnl + 2.0*lcell_a)/GAMMA) );
                FluxP[4] = FluxP[0] * (  ((ql - vnl*vnl)/2.0) + (((GAMMA-1.0)*vnl + 2.0*lcell_a)*((GAMMA-1.0)*vnl + 2.0*lcell_a) / (2.0*(GAMMA*GAMMA-1.0)))  );
        }
}//end of 1st
#pragma omp single
{//second condition
//cerr<<"Executed thread 1"<<endl;
        if ((fabs(rcellMach+1.0)<EPSILON) || ((rcellMach+1.0) < 0.0)) {
                FluxM[0] = rhor * vnr;
                FluxM[1] = rhor * ur * vnr + Pr*nx;
                FluxM[2] = rhor * vr * vnr + Pr*ny;
                FluxM[3] = rhor * wr * vnr + Pr*nz;
                FluxM[4] = rhor * ((GAMMA * Pr / (rhor * (GAMMA-1.0))) + ((ur*ur + vr*vr + wr*wr)/2.0)) * vnr;
        }else if ((fabs(rcellMach-1.0)<EPSILON) || ((rcellMach-1.0) > 0.0)) {
                FluxM[0] = FluxM[1] = FluxM[2] = FluxM[3] = FluxM[4] = 0.0;
        }else {
                tempFlux[0] = rhor * vnr;
                tempFlux[1] = rhor * ur * vnr + Pr*nx;
                tempFlux[2] = rhor * vr * vnr + Pr*ny;
                tempFlux[3] = rhor * wr * vnr + Pr*nz;
                tempFlux[4] = rhor * ((GAMMA * Pr / (rhor * (GAMMA-1.0))) + ((ur*ur + vr*vr + wr*wr)/2.0)) * vnr;

                double qr = (ur*ur + vr*vr + wr*wr);
                tempFluxP[0] = rhor * rcell_a * (rcellMach+1.0)*(rcellMach+1.0) / 4.0;
                tempFluxP[1] = tempFluxP[0] * ( ur + (nx*(0.0-vnr + 2.0*rcell_a)/GAMMA) );
                tempFluxP[2] = tempFluxP[0] * ( vr + (ny*(0.0-vnr + 2.0*rcell_a)/GAMMA) );
                tempFluxP[3] = tempFluxP[0] * ( wr + (nz*(0.0-vnr + 2.0*rcell_a)/GAMMA) );
                tempFluxP[4] = tempFluxP[0] * (  ((qr - vnr*vnr)/2.0) + (((GAMMA-1.0)*vnr + 2.0*rcell_a)*((GAMMA-1.0)*vnr + 2.0*rcell_a) / (2.0*(GAMMA*GAMMA-1.0)))  );

                for (int j=0; j<O; j++) FluxM[j] = tempFlux[j] - tempFluxP[j];
        }
}
}//pragma

Urgent help required. Thanks.

Upvotes: 0

Views: 321

Answers (1)

Hristo Iliev
Hristo Iliev

Reputation: 74485

What you need is the sections construct:

#pragma omp parallel sections num_threads(2)
{
   #pragma omp section
   {
      ... code that updates FluxP ...
   }
   #pragma omp section
   {
      ... code that updates FluxM ...
   }
}

But your code doesn't seem like it would take much time to do the calculations (no big for loops inside for example) so the overhead that OpenMP will put onto it will most likely be more time consuming than the saving in computation time and hence the parallel version will most likely execute slower than the serial.

Upvotes: 1

Related Questions