一段时间以来,我一直在努力对特定的应用程序进行矢量化,我已经尝试了所有方法。从自动矢量化到手工编码的SSE内部函数。但不知何故,我无法在基于模板的应用程序上获得加速。
以下是我当前代码的一个片段,我已经使用SSE内部函数对其进行了矢量化。当我使用-vc-report3编译(Intel icc)时,我经常会收到以下消息:
备注:循环未向量化:语句无法向量化
#pragma ivdep
for ( i = STENCIL; i < z - STENCIL; i+=4 )
{
it = it2 + i;
__m128 tmp2i = _mm_mul_ps(_mm_add_ps(_mm_load_ps(&p2[i+j*it_j-it_j4+k*it_k]),_mm_load_ps(&p2[i+j*it_j+it_j4+k*it_k])),X4_i); //loop was not vectorized: statement cannot be vectorized
__m128 tmp3 = _mm_mul_ps(_mm_add_ps(_mm_load_ps(&p2[i+j*it_j-it_j3+k*it_k]),_mm_load_ps(&p2[i+j*it_j+it_j3+k*it_k])),X3_i);
__m128 tmp4 = _mm_mul_ps(_mm_add_ps(_mm_load_ps(&p2[i+j*it_j-it_j2+k*it_k]),_mm_load_ps(&p2[i+j*it_j+it_j2+k*it_k])),X2_i);
__m128 tmp5 = _mm_mul_ps(_mm_add_ps(_mm_load_ps(&p2[i+j*it_j-it_j +k*it_k]),_mm_load_ps(&p2[i+j*it_j+it_j +k*it_k])),X1_i);
__m128 tmp6 = _mm_add_ps(_mm_add_ps(_mm_add_ps(tmp2i,tmp3),_mm_add_ps(tmp4,tmp5)), _mm_mul_ps(_mm_load_ps(&p2[it]),C00_i));
_mm_store_ps(&tmp2[i],tmp6);
}
我是不是错过了一些关键的东西?由于该消息没有详细说明为什么它不能矢量化,我发现很难确定瓶颈。
更新:在仔细考虑了这些建议之后,我按照以下方式对代码进行了调整。我认为最好将其进一步分解,以确定实际导致向量依赖性的语句。
//#pragma ivdep
for ( i = STENCIL; i < z - STENCIL; i+=4 )
{
it = it2 + i;
__m128 center = _mm_mul_ps(_mm_load_ps(&p2[it]),C00_i);
u_j4 = _mm_load_ps(&p2[i+j*it_j-it_j4+k*it_k]); //Line 180
u_j3 = _mm_load_ps(&p2[i+j*it_j-it_j3+k*it_k]);
u_j2 = _mm_load_ps(&p2[i+j*it_j-it_j2+k*it_k]);
u_j1 = _mm_load_ps(&p2[i+j*it_j-it_j +k*it_k]);
u_j8 = _mm_load_ps(&p2[i+j*it_j+it_j4+k*it_k]);
u_j7 = _mm_load_ps(&p2[i+j*it_j+it_j3+k*it_k]);
u_j6 = _mm_load_ps(&p2[i+j*it_j+it_j2+k*it_k]);
u_j5 = _mm_load_ps(&p2[i+j*it_j+it_j +k*it_k]);
__m128 tmp2i = _mm_mul_ps(_mm_add_ps(u_j4,u_j8),X4_i);
__m128 tmp3 = _mm_mul_ps(_mm_add_ps(u_j3,u_j7),X3_i);
__m128 tmp4 = _mm_mul_ps(_mm_add_ps(u_j2,u_j6),X2_i);
__m128 tmp5 = _mm_mul_ps(_mm_add_ps(u_j1,u_j5),X1_i);
__m128 tmp6 = _mm_add_ps(_mm_add_ps(tmp2i,tmp3),_mm_add_ps(tmp4,tmp5));
__m128 tmp7 = _mm_add_ps(tmp6,center);
_mm_store_ps(&tmp2[i],tmp7); //Line 196
}
当我在没有#pragma ivdep
的情况下编译(icc)上述代码时,我得到以下消息:
remark: loop was not vectorized: existence of vector dependence.
vector dependence: assumed FLOW dependence between tmp2 line 196 and tmp2 line 196.
vector dependence: assumed ANTI dependence between tmp2 line 196 and tmp2 line 196.
当我用#pragma ivdep
编译(icc)它时,我得到以下消息:
remark: loop was not vectorized: unsupported data type. //Line 180
为什么196号线有依赖性?如何消除建议的矢量相关性?
问题是您试图将自动向量化与手动向量化代码一起使用。编译器说不能对行进行矢量化,因为你不能对矢量函数进行矢量化。
要么让编译器自动向量化,要么禁用自动向量化并手动向量化代码。如前所述,自动矢量器将计算矢量化的盈利能力:它检查是否值得对代码进行矢量化。