如果在类声明中未定义函数,则性能损失

时间:2015-01-12 15:30:24

标签: c++ templates optimization hpc

我有一个高性能代码,其中包含此帖子底部的类。我的问题是,只要我没有在类声明中定义advecu函数,而是将声明和实现(我喜欢)分开,我在英特尔C ++和Clang编译器中都会失去相当多的性能。但是,我不理解为什么。当我删除模板时,所有编译器的两种方式的性能都相同。

template<bool dim3>
struct Advec4Kernel
{
  static void advecu(double * restrict ut, double * restrict u, double * restrict v, double * restrict w, double * restrict dzi4, const Grid &grid)
  {
    int    ijk,kstart,kend;
    int    ii1,ii2,ii3,jj1,jj2,jj3,kk1,kk2,kk3;
    double dxi,dyi;

    ii1 = 1;
    ii2 = 2;
    ii3 = 3;
    jj1 = 1*grid.icells;
    jj2 = 2*grid.icells;
    jj3 = 3*grid.icells;
    kk1 = 1*grid.ijcells;
    kk2 = 2*grid.ijcells;
    kk3 = 3*grid.ijcells;

    kstart = grid.kstart;
    kend   = grid.kend;

    dxi = 1./grid.dx;
    dyi = 1./grid.dy;

    for(int k=grid.kstart; k<grid.kend; k++)
      for(int j=grid.jstart; j<grid.jend; j++)
        for(int i=grid.istart; i<grid.iend; i++)
        {
          ijk = i + j*jj1 + k*kk1;
          ut[ijk] -= ( cg0*((ci0*u[ijk-ii3] + ci1*u[ijk-ii2] + ci2*u[ijk-ii1] + ci3*u[ijk    ]) * (ci0*u[ijk-ii3] + ci1*u[ijk-ii2] + ci2*u[ijk-ii1] + ci3*u[ijk    ]))
                     + cg1*((ci0*u[ijk-ii2] + ci1*u[ijk-ii1] + ci2*u[ijk    ] + ci3*u[ijk+ii1]) * (ci0*u[ijk-ii2] + ci1*u[ijk-ii1] + ci2*u[ijk    ] + ci3*u[ijk+ii1]))
                     + cg2*((ci0*u[ijk-ii1] + ci1*u[ijk    ] + ci2*u[ijk+ii1] + ci3*u[ijk+ii2]) * (ci0*u[ijk-ii1] + ci1*u[ijk    ] + ci2*u[ijk+ii1] + ci3*u[ijk+ii2]))
                     + cg3*((ci0*u[ijk    ] + ci1*u[ijk+ii1] + ci2*u[ijk+ii2] + ci3*u[ijk+ii3]) * (ci0*u[ijk    ] + ci1*u[ijk+ii1] + ci2*u[ijk+ii2] + ci3*u[ijk+ii3])) ) * cgi*dxi;

          if(dim3)
          {
            ut[ijk] -= ( cg0*((ci0*v[ijk-ii2-jj1] + ci1*v[ijk-ii1-jj1] + ci2*v[ijk-jj1] + ci3*v[ijk+ii1-jj1]) * (ci0*u[ijk-jj3] + ci1*u[ijk-jj2] + ci2*u[ijk-jj1] + ci3*u[ijk    ]))
                       + cg1*((ci0*v[ijk-ii2    ] + ci1*v[ijk-ii1    ] + ci2*v[ijk    ] + ci3*v[ijk+ii1    ]) * (ci0*u[ijk-jj2] + ci1*u[ijk-jj1] + ci2*u[ijk    ] + ci3*u[ijk+jj1]))
                       + cg2*((ci0*v[ijk-ii2+jj1] + ci1*v[ijk-ii1+jj1] + ci2*v[ijk+jj1] + ci3*v[ijk+ii1+jj1]) * (ci0*u[ijk-jj1] + ci1*u[ijk    ] + ci2*u[ijk+jj1] + ci3*u[ijk+jj2]))
                       + cg3*((ci0*v[ijk-ii2+jj2] + ci1*v[ijk-ii1+jj2] + ci2*v[ijk+jj2] + ci3*v[ijk+ii1+jj2]) * (ci0*u[ijk    ] + ci1*u[ijk+jj1] + ci2*u[ijk+jj2] + ci3*u[ijk+jj3])) ) * cgi*dyi;
          }

          ut[ijk] -= ( cg0*((ci0*w[ijk-ii2-kk1] + ci1*w[ijk-ii1-kk1] + ci2*w[ijk-kk1] + ci3*w[ijk+ii1-kk1]) * (ci0*u[ijk-kk3] + ci1*u[ijk-kk2] + ci2*u[ijk-kk1] + ci3*u[ijk    ]))
                     + cg1*((ci0*w[ijk-ii2    ] + ci1*w[ijk-ii1    ] + ci2*w[ijk    ] + ci3*w[ijk+ii1    ]) * (ci0*u[ijk-kk2] + ci1*u[ijk-kk1] + ci2*u[ijk    ] + ci3*u[ijk+kk1]))
                     + cg2*((ci0*w[ijk-ii2+kk1] + ci1*w[ijk-ii1+kk1] + ci2*w[ijk+kk1] + ci3*w[ijk+ii1+kk1]) * (ci0*u[ijk-kk1] + ci1*u[ijk    ] + ci2*u[ijk+kk1] + ci3*u[ijk+kk2]))
                     + cg3*((ci0*w[ijk-ii2+kk2] + ci1*w[ijk-ii1+kk2] + ci2*w[ijk+kk2] + ci3*w[ijk+ii1+kk2]) * (ci0*u[ijk    ] + ci1*u[ijk+kk1] + ci2*u[ijk+kk2] + ci3*u[ijk+kk3])) )
                     * dzi4[k];
        }
  }
};

分离的版本显示为:

// header
template<bool dim3>
struct Advec4Kernel
{
  static void advecu(double *, double *, double *, double *, double *, const Grid &);
}

// source
template<bool dim3>
void Advec4Kernel<dim3>::advecu(double * restrict ut, double * restrict u, double * restrict v, double * restrict w, double * restrict dzi4, const Grid &grid)
{
  //...
}

2 个答案:

答案 0 :(得分:4)

显然,编译器使用restrict关键字执行一些优化。要从这些优化中受益,函数的声明必须包含restrict关键字。这是determined empirically;我不知道这是编译器缺陷还是法律。

代码:

// header
template<bool dim3>
struct Advec4Kernel
{
  static void advecu(double *restrict, double *restrict, double *restrict, double *restrict, double *restrict, const Grid &);
}

答案 1 :(得分:0)

在大多数情况下,类声明中定义的函数都会内联,这就是性能下降的原因