我正在尝试优化此功能:
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;
}
我正在使用Intel Advisor对其进行优化,即使内部for
已被矢量化,Intel Advisor也检测到低效的内存访问模式:
特别是在以下三个说明中有4个聚集(不规则)访问:
据我所知,聚集访问的问题发生在所访问元素的类型为a[b]
的情况下,其中b
是不可预测的。 imptr[rowOffset+x]
似乎是这种情况,其中rowOffset
和x
都是不可预测的。
同时,我看到此Vertical Invariant
当以恒定偏移量访问元素时,应该再次发生(根据我的理解)。但是实际上我看不到这个常量偏移量在哪里>>
所以我有3个问题:
[带有下列标志的icpc
2017 Update 3编译:
INTEL_OPT=-O3 -ipo -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
我正在尝试优化此功能:bool插值(const Mat&im,float ofsx,float ofsy,float a11,float a12,float a21,float a22,Mat&res){bool ret = false; //输入...
矢量化(SIMD化)代码不会自动使您的访问模式更好(或更糟)。为了最大化矢量化代码的性能,您必须尝试在代码中使用unit stride