我正在做一些图像处理,为此我受益于矢量化。我有一个向量化ok的函数,但我无法让编译器相信输入和输出缓冲区没有重叠,因此不需要别名检查。我应该能够使用__restrict__
来做到这一点,但是如果缓冲区在作为函数参数到达时没有被定义为__restrict__
,那么就没有办法让编译器相信我绝对肯定两个缓冲区永远不会重叠。
这是函数:
__attribute__((optimize("tree-vectorize","tree-vectorizer-verbose=6")))
void threshold(const cv::Mat& inputRoi, cv::Mat& outputRoi, const unsigned char th) {
const int height = inputRoi.rows;
const int width = inputRoi.cols;
for (int j = 0; j < height; j++) {
const uint8_t* __restrict in = (const uint8_t* __restrict) inputRoi.ptr(j);
uint8_t* __restrict out = (uint8_t* __restrict) outputRoi.ptr(j);
for (int i = 0; i < width; i++) {
out[i] = (in[i] < valueTh) ? 255 : 0;
}
}
}
说服编译器不执行别名检查的唯一方法是将内部循环放在一个单独的函数中,在这个函数中,指针被定义为__restrict__
参数。如果我声明这个内部函数是内联的,别名检查也会被激活。
你也可以通过这个例子看到效果,我认为这是一致的:http://goo.gl/7HK5p7
(注意:我知道可能有更好的方法来编写相同的函数,但在这种情况下,我只是想了解如何避免别名检查)
编辑:
问题解决了!!(参见answer below)
使用gcc 4.9.2,here is the complete example。请注意,使用编译器标志-fopt-info-vec-optimized
代替被取代的-ftree-vectorizer-verbose=N
。
所以,对于gcc来说,使用#pragma GCC ivdep
并享受吧!:)
https://stackoverflow.com/questions/25934239
复制相似问题