Search code examples
cvectorizationsimdicc

How to overcome "existence of vector dependence" in icc


I want to vectorize following loop in C:

for(k = 0; k < SysData->numOfClaGen; k++)
            A[k] = B[k] * cos(x1[2 * k] - x1[ind0 + k]);

where, there is no alias between variables and ind0 is a constant. None of the other pointers (A or B) point to ind0 and therefore, ind0 remains constant throughout the loop.

When I compile the code with icc, it says that this loop cannot be vectorized due to possible vector dependence. Here is the message:

loop was not vectorized: existence of vector dependence.

I narrowed the problem down and found out that replacing ind0 with a constant number solves the problem. So, I assume that icc thinks A may point to ind0 and therefore, ind0 may change.

I would like to know how I can help the compiler to know that it is safe to vectorized such loop.

Thanks in advance for your help.


Solution

  • Add #pragma ivdep in front of the for loop, it instructs the compiler to ignore assumed vector dependencies.

    #pragma ivdep
    for(k = 0; k < SysData->numOfClaGen; k++)
                A[k] = B[k] * cos(x1[2 * k] - x1[ind0 + k]);
    

    for more info about ivdep, see icc doc