我有这个函数。
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的第一个建议来做?它说要 "增加对象的大小,并增加迭代,使行程数是矢量长度的倍数"......好吧,所以它只是说 "嘿,伙计这样做,所以 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中花费的相对时间(见顾问中的热点列表)。如果效率接近100%(或者花在Remainder上的时间非常少),那么它就不值得花费精力(和MSalters在评论中提到的金钱)。
如果是<<100%(并且工具没有报告其他处罚),那么你可以重构代码来 "添加假的迭代"(罕见的用户可以承受),或者你应该尝试一下。#pragma loop_count 对于 最典型的#迭代值 取决于典型的halfWidth值)。
如果halfWIdth是 完全 随机的(没有共同值或平均值),那么你对这个问题真的无能为力。