【问题标题】:Unaligned access on aligned pointer?对齐指针的非对齐访问?
【发布时间】:2017-10-02 08:02:57
【问题描述】:

我有这个功能码:

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

这是使用icpc 和以下选项编译的代码的一部分:

INTEL_OPT=-O3 -simd -xCORE-AVX2 -parallel -qopenmp -fargument-noalias -ansi-alias -no-prec-div -fp-model fast=2 -fma -align -finline-functions
INTEL_PROFILE=-g -qopt-report=5 -Bdynamic -shared-intel -debug inline-debug-info -qopenmp-link dynamic -parallel-source-info=2 -ldl

在优化报告文件 (.optr) 中有以下几行:

  remark #15389: vectorization support: reference *out has unaligned access   [ /home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Descriptors/hesaff/helpers.cpp(259,14) ]
  remark #15389: vectorization support: reference *out has unaligned access   [ /home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Descriptors/hesaff/helpers.cpp(263,14) ]
  remark #15389: vectorization support: reference *out has unaligned access   [ /home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Descriptors/hesaff/helpers.cpp(263,14) ]

第 259 行和第 263 行是 out 值明显改变的地方。

据我所知,这意味着 out 未对齐(更多信息在这里 [https://software.intel.com/en-us/articles/fdiag15126] 和 here )。

但是,这已经很奇怪了,因为我已经发现 here , cv::Mat 对象已经对齐。但这可能是合法的,因为编译器无法知道对齐方式...但是如果我专门将 out 添加为对齐指针,则会出现该消息:

out = (float*) _mm_malloc(res.cols*res.rows*sizeof(float), 32);

这应该足以告诉编译器数据已对齐。我错了吗?不然怎么知道out已经对齐了?

【问题讨论】:

    标签: c++ opencv parallel-processing alignment vectorization


    【解决方案1】:

    编译器看到out++。它不能保持对齐。

    (也就是说,对于float*访问,它仍然保持对齐,但消息指的是SIMD矢量化,需要更严格的对齐)

    【讨论】:

    • 感谢您的回答。因此,假设我正在使用可以一起处理 8 个浮点元素的 AVX2 机器,我是否应该在一个迭代周期中一起计算 8 个值,然后在 for 子句中执行 out+=8 ?如果我没记错的话,这是循环展开,这是由编译器自动完成的。我错过了什么?
    • (你也可以看看this的问题吗?)
    猜你喜欢
    • 1970-01-01
    • 2018-01-17
    • 2016-02-14
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 2020-05-09
    相关资源
    最近更新 更多