什么是Read Read Write依赖?

时间:2017-05-03 13:39:38

标签: c++ parallel-processing vectorization

我有这个循环这个函数:

Mat HessianDetector::hessianResponse(const Mat &inputImage, float norm)
{
   //...
   const float *in = inputImage.ptr<float>(1);
   Mat outputImage(rows, cols, CV_32FC1);
   float      *out = outputImage.ptr<float>(1) + 1;
   //...
   for (int r = 1; r < rows - 1; ++r)
   {
      float v11, v12, v21, v22, v31, v32;      
      v11 = in[-stride]; v12 = in[1 - stride];
      v21 = in[      0]; v22 = in[1         ];
      v31 = in[+stride]; v32 = in[1 + stride];
      in += 2;
      for (int c = 1; c < cols - 1; ++c, in++, out++)
      {
         /* fetch remaining values (last column) */
         const float v13 = in[-stride];
         const float v23 = *in;
         const float v33 = in[+stride];

         // compute 3x3 Hessian values from symmetric differences.
         float Lxx = (v21 - 2*v22 + v23);
         float Lyy = (v12 - 2*v22 + v32);
         float Lxy = (v13 - v11 + v31 - v33)/4.0f;

         /* normalize and write out */
         *out = (Lxx * Lyy - Lxy * Lxy)*norm2;

         /* move window */
         v11=v12; v12=v13;
         v21=v22; v22=v23;
         v31=v32; v32=v33;

         /* move input/output pointers */
      }
      out += 2;
   }
   return outputImage;
}

使用以下方式调用:

#pragma omp for collapse(2) schedule(dynamic)
for(int i=0; i<levels; i++)
    for (int j = 1; j <= scaleCycles; j++)
    {
        int scaleCyclesLevel = scaleCycles * i;
        float curSigma = par.sigmas[j];
        hessResps[j+scaleCyclesLevel] = hessianResponse(blurs[j+scaleCyclesLevel], curSigma*curSigma);
    }

特别是,英特尔顾问表示内循环非常耗时,应该进行矢量化:

for (int c = 1; c < cols - 1; ++c, in++, out++)

但是,它还表示在这两行中存在写入后读取依赖性:

读:

float Lyy = (v12 - 2*v22 + v32);

写:

hessResps[j+scaleCyclesLevel] = hessianResponse(blurs[j+scaleCyclesLevel], curSigma*curSigma);

但我真的不明白为什么会这样(即使我知道RAW依赖的含义)。

这是优化报告:

   LOOP BEGIN at /home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Descriptors/hesaff/pyramid.cpp(92,7)
      remark #17104: loop was not parallelized: existence of parallel dependence
      remark #17106: parallel dependence: assumed ANTI dependence between *(in+cols*4) (95:28) and *out (105:11)
      remark #17106: parallel dependence: assumed FLOW dependence between *out (105:11) and *(in+cols*4) (95:28)
      remark #15344: loop was not vectorized: vector dependence prevents vectorization
      remark #15346: vector dependence: assumed ANTI dependence between *(in+cols*4) (95:28) and *out (105:11)
      remark #15346: vector dependence: assumed FLOW dependence between *out (105:11) and *(in+cols*4) (95:28)
   LOOP END

第95行是:

     const float v13 = in[-stride];

105行是:

     *out = (Lxx * Lyy - Lxy * Lxy)*norm2;

2 个答案:

答案 0 :(得分:1)

优化报告告诉您的是,循环的一次迭代中有一些值依赖于前一次迭代的值。特别是,“移动窗口”块会在本地之间复制值,以便下一次迭代中v11v12等的值取决于v12,{{1}的值在这个迭代中等。这可以防止编译器对循环进行矢量化。

解决方案是初始化v23循环体内的所有9个v变量。

我不知道修复此问题是否会解决原始的RAW问题。

另一个调整是将c移出scaleCyclesLevel循环(因此它是j循环),因为它的值不依赖于i

答案 1 :(得分:0)

我不知道如何将inputImageoutputImage传递给函数。如果你没有将它们作为restricted传递,编译器不知道数据是否重叠,因此写入*out是不安全的,因为它可能会覆盖*in的{​​{1}}下一次迭代。

看看如何告诉编译器图像数据不重叠。对于gcc,它是restrict