我正在尝试让VC++ 2012自动矢量化一个看起来有点像这样的循环(实际上有一些有趣的计算正在进行,但为了尽可能地提出问题的目的,它们被省略了)。
parameters:
int period;
unsigned char* out_array;
unsigned char* in_array1;
unsigned char* in_array2;
unsigned char* in_array3;
for (int x = 0; x < width; ++x)
{
int index = period * (x / 2);
out_array[0] = in_array1[x];
out_array[1] = in_array2[index];
out_array[2] = in_array3[index];
out_array += 4;
}我认为唯一阻碍矢量化的是out_array += 4,所以我做了一个内部的“展开”循环,希望至少可以将一个循环向量化:
for (int x = 0; x < width; ++x)
{
for (int xx = 0; xx < 4; ++xx)
{
int index = period * ((xx + x) / 2);
unsigned char* pout_array = out_array + (4 * xx);
pout_array[0] = in_array1[xx + x];
pout_array[1] = in_array2[index];
pout_array[2] = in_array3[index];
}
out_array += 16;
}但是,当我使用/Qvect-report:2运行编译器时,它告诉我,由于错误代码1200,内部循环无法向量化。错误代码1200声明:
循环包含循环携带的数据依赖项,以防止矢量化。循环的不同迭代相互干扰,从而使循环矢量化会产生错误的答案,而自动向量器无法向自己证明不存在这种数据依赖。
我不明白这个。显然,这个循环的每一次迭代都是独立的。如何让Visual将其矢量化?
发布于 2013-05-21 04:02:58
它不能将其向量化的主要原因是,正如所写的,编译器不能排除out_arrayn不是in_arrayXm的可能性,因此它必须坚持您的顺序顺序。
您可以使用"__restrict“或”限制“关键字来解决这个问题,这是对编译器的一个承诺,即您只能以确保out_array与其他三个指针不相同的方式调用它。您还可能希望慷慨地使用"const“修饰符来帮助编译器:
void func(const int period,
unsigned char* __restrict out_array,
const unsigned char* in_array1,
const unsigned char* in_array2,
const unsigned char* in_array3)
{
...
//mark 'width' as 'const' if possible:
const int width = ...;
for (int x = 0; x < width; ++x)
{
const int index = period * (x / 2);
out_array[(x* 4) + 0] = in_array1[x];
out_array[(x* 4) + 1] = in_array2[index];
out_array[(x* 4) + 2] = in_array3[index];
}
}https://stackoverflow.com/questions/16092966
复制相似问题