c++ - 我的代码中的 “Peel/Remainder”循环无效

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

我有这个功能:

bool interpolate(const Mat &im, float ofsx, float ofsy, float a11, float a12, float a21, float a22, Mat &res)
{         
   bool ret = false;
   // input size (-1 for the safe bilinear interpolation)
   const int width = im.cols-1;
   const int height = im.rows-1;
   // output size
   const int halfWidth  = res.cols >> 1;
   const int halfHeight = res.rows >> 1;
   float *out = res.ptr<float>(0);
   const float *imptr  = im.ptr<float>(0);
   for (int j=-halfHeight; j<=halfHeight; ++j)
   {
      const float rx = ofsx + j * a12;
      const float ry = ofsy + j * a22;
      #pragma omp simd
      for(int i=-halfWidth; i<=halfWidth; ++i, out++)
      {
         float wx = rx + i * a11;
         float wy = ry + i * a21;
         const int x = (int) floor(wx);
         const int y = (int) floor(wy);
         if (x >= 0 && y >= 0 && x < width && y < height)
         {
            // compute weights
            wx -= x; wy -= y;
            int rowOffset = y*im.cols;
            int rowOffset1 = (y+1)*im.cols;
            // bilinear interpolation
            *out =
                (1.0f - wy) * ((1.0f - wx) * imptr[rowOffset+x]   + wx * imptr[rowOffset+x+1]) +
                (       wy) * ((1.0f - wx) * imptr[rowOffset1+x] + wx * imptr[rowOffset1+x+1]);
         } else {
            *out = 0;
            ret =  true; // touching boundary of the input            
         }
      }
   }
   return ret;
}
halfWidth是非常随机的:可以是9、84、20、95、111 ...我只是在尝试优化此代码,我不了解它的详细信息。

如您所见,内部for已经被矢量化了,但是Intel Advisor建议这样做:

enter image description here

这是行程计数分析结果:

enter image description here

据我了解,这意味着:
  • vector 长度为​​8,因此,每个循环可以同时处理8个浮点数。这将意味着(如果我没记错的话)数据是32字节对齐的(即使正如我解释here一样,编译器似乎认为数据未对齐)。
  • 平均来说,总共2个周期是矢量化的,而其余3个周期是循环。最小和最大也是如此。否则我不明白;是什么意思。

  • 现在我的问题是:如何遵循英特尔顾问的第一个建议?它说“增加对象的大小并添加迭代,以便行程数是 vector 长度的倍数” ...好吧,所以这只是说“嘿,所以这样做,所以halfWidth*2 +1(因为它从-halfWidth变成+halfWidth是8)的倍数。”。但是我该怎么做呢?如果添加随机周期,显然会破坏算法!

    我想到的唯一解决方案是添加“伪”迭代,如下所示:
    const int vectorLength = 8;
    const int iterations = halfWidth*2+1;
    const int remainder = iterations%vectorLength;
    
    for(int i=0; i<loop+length-remainder; i++){
      //this iteration was not supposed to exist, skip it!
      if(i>halfWidth) 
         continue;
    }
    

    当然,此代码不起作用,因为它从-halfWidth变为halfWidth,但这是为了让您了解我的“伪”迭代策略。

    关于第二个选项(“增加静态和自动对象的大小,并使用编译器选项添加数据填充”),我不知道如何实现此目的。

    最佳答案

    首先,您必须检查Vector Advisor效率指标以及与Loop Body相比在Loop Remainder中花费的相对时间(请参阅Advisor中的热点列表)。如果效率接近100%(或在Remainder中花费的时间很小),那么就不值得付出努力(和MSalters在评论中提到的钱一样)。

    如果它是<< 100%(并且该工具未报告其他任何惩罚),则可以重构代码以“添加伪造的迭代”(罕见的用户可以负担得起),或者应该尝试使用 #pragma loop_count 最典型的#iterations值(取决于典型的halfWidth值)。

    如果halfWIdth是完全是随机的(没有共同值或平均值),那么您实际上无法解决此问题。

    关于c++ - 我的代码中的 “Peel/Remainder”循环无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43778590/

    相关文章:

    arrays - 向量化访问非连续内存位置的循环

    c++ - 使用 lambda 初始化包含 std::function 的类

    c++ - std::stable_sort 根本不稳定?

    c++ - 如何避免 C++ 类模板中的无限递归

    unit-testing - NUnit(和MSTest)如何处理更改静态/共享变量的测试?

    python - 并行 Python : Passing a function written in another module to 'submit'

    c++ - 如何在 .h 和 .cpp 中为类中的私有(private)字符数组编写 get 函数?

    parallel-processing - 由具有不同延迟的操作组成的非常长的指令

    r - R中不同维数组的矢量化

    python - 向量化 numpy 数组 for 循环