C++ “无效”;“剥皮/剩余物”;在我的代码中循环
我有这个功能:C++ “无效”;“剥皮/剩余物”;在我的代码中循环,c++,parallel-processing,vectorization,simd,intel-advisor,C++,Parallel Processing,Vectorization,Simd,Intel Advisor,我有这个功能: 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;
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
到halfWidth
,但它是为了让您了解我的“伪”迭代策略
关于第二个选项(“增加静态和自动对象的大小,并使用编译器选项添加数据填充”),我不知道如何实现这一点。首先,您必须检查Vector Advisor效率指标,以及与循环体相比,在循环剩余部分花费的相对时间(请参阅Advisor中的热点列表)。若效率接近100%(或者在剩余部分花费的时间非常少),那个么就不值得付出努力(以及评论中提到的MSalters所花的金钱)
如果是“这意味着(如果我没有错的话)数据是32字节对齐的”——不,现在还有一种未对齐的加载操作。不过,您必须针对一个足够新的体系结构,它肯定不在SSE2中。”我想到的唯一解决方案是添加如下“伪”迭代:
if(i>halfWidth)继续;
。您确实注意到了单指令多数据中的#pragma omp simd
,因为您正在提出MIMD解决方案。对于simd,数据可能取决于[i]
,但指令不能。@MSalters抱歉,但我使用的是AVX2机器,这意味着寄存器是256位=32字节=8浮点。我有什么错吗?@MSalters我个人添加了simd
,但我不理解你的评论。我只是说:如果我们强制要求迭代的数量是多个在8个寄存器中,将没有剩余循环,这将更有效,因为它将完全适合寄存器。我缺少什么?您有一个寄存器具有8个浮点数。您不能在半个寄存器中继续该循环。几乎每个AVX2指令都在整个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;
}