我有这个功能:
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建议这样做:这是行程计数分析结果:
据我了解,这意味着:
;
是什么意思。 现在我的问题是:如何遵循英特尔顾问的第一个建议?它说“增加对象的大小并添加迭代,以便行程数是 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/