Re: [eigen] No vectorization in presence of .cast<T>() calls |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] No vectorization in presence of .cast<T>() calls
- From: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>
- Date: Sat, 18 Dec 2010 00:23:12 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=LG+jRnjCZamqK3+XKme49BF2ySFuNG/AeHku/ufVk0s=; b=ndvFDk37FTb7vUoPyEJAWWlJvKufmdPrx5eQYYYj/Cp0nNqKqdeWBRlowBbc6WxpQs +rB2uL7jkC/V0x1302qpT+NrHS+AEbAXdXkz9/o1s0rTHrEXJGpvpEPMvNMYtDiIh00b 1pSCMTcWetP5KRLEyo2n6WjHGrYkxv8jxfp2U=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=OYwatbrTWJLY/+anc4zTTGe20A49SmTZ8w6AblxeFJ3lpJ8K/y1zBXQKKoRU8Jb/oX fyQ3egwjHH3ZMjnqdoTI8Yw7s9vb71rezUorTOkn5GX8FUA2gPq7z7DDswDnNm8KfKO0 MyP3ENo8EezNlUVUhKbacv4a1wlF5gq8YoqAI=
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.
Benoit
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
>