Mikhail
Mikhail

Reputation: 8028

Inner if statement kills vectorization

I have the following for loop. When I switch on the type array the code doesn't vectorize. When I fix type to '1' gcc performs a primitize vectorization vectorizes. Does anybody have any reccomendtions to trigger some kind of vectorization?

#define type(M,N) type[(M)*sizeX + (N)]
for (int i = 0; i < sizeY - 1; i++)
{
    for (int j = 0; j < sizeX - 1; j++)
    {
       const int id = type(i, j);
       //const int id = 1; //vectorizes
       const float A = this->A[id];
       const float B = this->B[id];
       a(i, j) = A * a(i, j) + B * (b(i, j) - b(i + 1, j))*(p[i]);
    }
 }

The approximate error from gcc 4.7.1

45: not vectorized: not suitable for gather A_26 = *D.14145_25;

Edit 1

All of the arrays are stored as pointers and are defined with the restrict keyword as members of some class.

Edit 2

Is there something I can do if 'type' is small?

Edit 3

Small means 8.

Upvotes: 2

Views: 801

Answers (1)

Mysticial
Mysticial

Reputation: 471379

The difference is the memory access.

When id = 1, the following array loads become a single element vector broadcast.

 const float A = this->A[id];
 const float B = this->B[id];

But when id = type[(i)*sizeX + (k)], the memory access is strided (not contiguous).

Vector loads and stores in SSE and AVX can only be done on:

  1. Contiguous memory blocks.
  2. Or from a single element that is broadcast to the entire vector.

They cannot handle strided memory access where you load each of the vector elements from different parts of memory.

AVX2 will have support for such "gather/scatter" instructions.


To address the edits:

If the i in type(i, j) is anything other than zero, the memory access is still strided. So it will be difficult to vectorize. (I say "difficult" instead of "impossible", because it is possible for very small compile-time-deterimined strides - albeit at reduced efficiency.)

The core problem you have here is that you're iterating down rows of a matrix. Not only is this not vectorizable without gather/scatter support, but it's also bad for cache.

I'm not sure what task you're trying to accomplish, but you may need to consider a different data-layout to get maximum performance.

Upvotes: 4

Related Questions