c++ - 这是什么写后读依赖?

标签 c++ parallel-processing vectorization intel-advisor

我有这个循环这个函数:

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);
    }

Intel Advisor 特别指出,内部循环非常耗时,应该进行矢量化:

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;

最佳答案

优化报告告诉您的是,您在循环的一次迭代中有一些值取决于前一次迭代的值。特别是,“移动窗口” block 在局部变量之间复制值,以便下一次迭代中 v11v12 等的值取决于 v12 的值v23 等在此迭代中。这会阻止编译器对循环进行矢量化。

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

我不知道解决这个问题是否会解决原始 RAW 问题。

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

关于c++ - 这是什么写后读依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43761770/

相关文章:

c++ - Qtableview搜索栏

r - 对具有后向依赖性的 R 循环进行向量化

c++ - 在另一个类中使用成员方法 "generically"

c++ - 飞盘轨迹

c - 如何不使用 waitpid 阻止父级

r - foreach 什么时候调用 .combine?

multithreading - 使用 Delphi XE7 并行库

matlab - 在 MATLAB 中对复杂向量进行高效分类

python - 不同等级的 Matmul

c++ - 指针转换,无法将 Two* 转换为 One*