Re: [eigen] No vectorization in presence of .cast<T>() calls

[ Thread Index | Date Index | More Archives ]

Tough case.

Casting from unsigned char to float is expanding 1 byte to 4 bytes,
which means going from 16 to 4 scalars per 16-byte packet. This change
in the number of scalars per packet is what's troublesome for our
vectorization system.

In general, that's quite hard, but it seems that we can easily
overcome this in you particular case. Since you're only casting from a
small type to a bigger type, the expression returned by cast() could
be vectorizable by implementing packet() by reading LESS THAN a packet
from the original uchar expression, and expanding it to float.

something like this (pseudo code):

packet4f cast<float>(Index i)
   return packet4f(float(src.coeff(i)), float(src.coeff(i+1)),
float(src.coeff(i+2)), float(src.coeff(i+3)));

This is only going to be beneficial if this is used in a complex
enough expression to pay for the cost of this packet() method. We must
make sure not to introduce a performance regression on a simple
dst=src.cast<float>() example.


2010/12/17 Hauke Heibel <hauke.heibel@xxxxxxxxxxxxxx>:
> Hi,
> I have the following piece of code
> MatrixXf od_full = - std::log( img_mat.cast<float>() / 255.0f +
> std::numeric_limits<float>::epsilon() );
> where img_mat is of type
> Map< Array<unsigned char,Dynamic,Dynamic,ColMajor> >.
> The issue is now that I recognized that the code is not vectorized at all..
> As soon as I introduce a temporary
> ArrayXXf tmp = img_mat.cast<float>();
> MatrixXf od_full = - std::log( tmp / 255.0f +
> std::numeric_limits<float>::epsilon() );
> the vectorization works again as expected. Right now, I don't see, why the
> cast should prevent vectorization, though I admit that this might be a
> little bit tricky.
> My use case is that I am "abusing" Eigen for some image processing
> algorithms and most often, images come as "unsigned char" arrays. For
> numerical procedures I want to convert these values to float while trying to
> prevent the introduction of unnecessary temporaries since sometimes my
> images are rather large.
> Do you have any information or ideas about this issue?
> - Hauke

Mail converted by MHonArc 2.6.19+