C++ 为什么矢量化在AVX-512上失败,而在AVX2上失败?
我在我的AVX2机器上测试了以下代码:C++ 为什么矢量化在AVX-512上失败,而在AVX2上失败?,c++,parallel-processing,vectorization,avx2,avx512,C++,Parallel Processing,Vectorization,Avx2,Avx512,我在我的AVX2机器上测试了以下代码: 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 = i
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;
}
该代码看起来非常熟悉;)分割错误是否会出现在最后几个元素上?AVX-512显然比AVX-256多使用256位,如果您的输入大小是256位(32字节)的奇数倍,那么您可能一次读取512位的内容。@m谢谢您的评论,我很高兴您能记住lol。我不明白“输入大小”是什么意思,但是如果你指的是的向量化
中的循环数,那么它是非常不稳定的。一些halfWidth
示例有20、9、17。。。而halfHeight
的值是48,20,9,43。。。如果你指的是im
size,那么width
的一些示例是1368683、62。。。而height
501061,58,…@MSalters我的意思是,如果这是一个多重问题,AVX2也应该有这些值,你不认为吗?此外,我认为omp simd
负责创建提醒和剥离loops@MSalters请注意,我报告的值并不是相互对应的,我只是报告了从打印中看到的随机数。@MSalters您能帮我吗?
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