我试图优化一个占据程序大部分计算时间的循环。
但是当我打开-O3 -ffast-math -ftree-vectorizer-verbose=6自动矢量化时,GCC输出无法矢量化该循环。
我正在使用GCC 4.4.5
代码:
/// Find the point in the path with the largest v parameter
void prediction::find_knife_edge(
const float * __restrict__ const elevation_path,
float * __restrict__ const diff_path,
const float path_res,
const unsigned a,
const unsigned b,
const float h_a,
const float h_b,
const float f,
const float r_e,
) const
{
float wavelength = (speed_of_light * 1e-6f) / f;
float d_ab = path_res * static_cast<float>(b - a);
for (unsigned n = a + 1; n <= b - 1; n++)
{
float d_an = path_res * static_cast<float>(n - a);
float d_nb = path_res * static_cast<float>(b - n);
float h = elevation_path[n] + (d_an * d_nb) / (2.0f * r_e) - (h_a * d_nb + h_b * d_an) / d_ab;
float v = h * std::sqrt((2.0f * d_ab) / (wavelength * d_an * d_nb));
diff_path[n] = v;
}
}
来自GCC的消息:
note: not vectorized: number of iterations cannot be computed.
note: not vectorized: unhandled data-ref
在有关自动向量化的页面(http://gcc.gnu.org/projects/tree-ssa/vectorization.html)中,它指出支持未知的循环边界。
如果我将for循环替换为
for (unsigned n = 0; n <= 100; n++)
然后它将其向量化。我做错了什么?
关于这些消息的详细文档以及GCC自动向量化的内部机制的缺乏详细说明真是令人恼火。
编辑:感谢David,我将循环更改为以下内容:
for (unsigned n = a + 1; n < b; n++)
现在GCC尝试对循环进行向量化,但出现了以下错误:
note: not vectorized: unhandled data-ref
note: Alignment of access forced using peeling.
note: Vectorizing an unaligned access.
note: vect_model_induction_cost: inside_cost = 1, outside_cost = 2 .
note: not vectorized: relevant stmt not supported: D.76777_65 = (float) n_34;
"D.76777_65 = (float) n_34;" 的意思是什么?
n <= b - 1
改为n < b
。 - David Schwartz