如何让不同的OpenMP线程执行不同的任务

时间:2012-06-15 06:45:53

标签: openmp

我正在使用开放式MP来加速我程序中的通量计算。我基本上希望OpenMP能够并行执行这些左右通量计算。但恰恰相反,以下代码使用#pragma指令需要更多时间。为了做到这一点,我该修改什么?

#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

需要紧急帮助。感谢。

1 个答案:

答案 0 :(得分:1)

您需要的是sections构造:

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

但是您的代码似乎不需要花费太多时间来进行计算(例如,内部没有大的for循环)因此OpenMP将在其上投入的开销很可能比节省计算时间,因此并行版本很可能比串行执行速度慢。