0

I have this function code:

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

Which is part of code compiled with icpc and the following options:

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

In the optimization report file (.optr) there are these lines:

  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) ]

Lines 259 and 263 are the ones where obviously out value is changed.

To my knowledge, this means that the out is unaligned (more info here[https://software.intel.com/en-us/articles/fdiag15126] and here ).

However, this is already weird, because as I alrady found out it here , cv::Mat objects are already aligned. But this could be legit since the compiler could not know of the alignment...However the message appears if I specifically allocate out as an aligned pointer if I add the following line:

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

This should be enough to tell the compiler that the data are aligned. Am I wrong? Otherwise, how can I tell it that out is already aligned?

Cœur
  • 37,241
  • 25
  • 195
  • 267
justHelloWorld
  • 6,478
  • 8
  • 58
  • 138

1 Answers1

0

The compiler sees out++. It can't stay aligned.

(That is to say, it remains aligned for float* access, but the message refers to SIMD vectorization which requires a stricter alignment)

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Thanks for your answer. So supposing that I'm using a AVX2 machine which can process 8 float elements together, should I compute 8 values of out all-together in one iteration cycle and then do `out+=8` in the `for` clause? If I'm not wrong this is loop unrolling and this was done by the compiler automatically. What am I missing? – justHelloWorld May 04 '17 at 10:53
  • (would you please give a look at [this](http://stackoverflow.com/questions/43778590/ineffective-remainder-loop-in-my-code) question too? ) – justHelloWorld May 04 '17 at 10:53