0

我正在使用 open 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

需要紧急帮助。谢谢。

4

1 回答 1

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 将对其施加的开销很可能比节省计算时间更耗时,因此并行版本很可能会比串行执行得慢。

于 2012-06-15T07:32:31.193 回答