Re: [eigen] operator overloading vs expression templates, and the need to return references |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] operator overloading vs expression templates, and the need to return references
- From: Rohit Garg <rpg.314@xxxxxxxxx>
- Date: Fri, 11 Sep 2009 22:37:34 +0530
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=GUgPQKLmIy4AHlM0GeOQILdyXI2NbrhwqdXugE+bNso=; b=MW3aNqRBPwnbdDuMwjhGF11JRKLppddtNn5Km9DxCVUa2OsQsngxd8XcHa7NxtPlA+ rHGf9q4FYWCR2yZ3ZLUTA7Q820TiO6/AmRjqH4horVENGB34sBjjkuwaifjClm/y9L5D dIJEEP7XcBw68OvQOVDMvm2qAiLg6agKTsfWg=
- 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=uvnr8lxoVJ6K15jx6JepOH24dE8eb2sU6HqPMI57S/tV3yKG49j6aqvaPJS7TwLbYV RpWh1pC+PWEZV324qHBUb5/l3YqDYcoFucAxGbnKBSAW5AN5pOpwx8jy4DrCQXzgC7sW dTZgHhv2GGXZHUC0+f79IpN+rPPwCBlTyAgBI=
On Fri, Sep 11, 2009 at 9:57 PM, Benoit Jacob <jacob.benoit.1@xxxxxxxxx> wrote:
> 2009/9/11 Rohit Garg <rpg.314@xxxxxxxxx>:
>> Hi all,
>>
>> I made a simple class wrapping vector8s (for shorts) and overloaded
>> the arithmetic and overloaded operators. All was fine until my
>> expressions involving operator overloads got a bit too big and gcc
>> started spewing random crap. It may not be random crap, but I can
>> barely decipher it, so... :(
>>
>> I resorted to simplifying my expressions, and the code quickly
>> exploded in verbosity with temporaries, but it compiled OK. Then I
>> made all the overloads return a reference like this,
>>
>> inline vector8s& operator+(vector8s &other)
>> {
>> vector8s temp;
>> temp.data=_mm_add_epi16(data,other.data);
>> return temp;
>> }
>>
>> and it complies, but it throws warnings at each of these overloaded
>> functions that I am returning references to temporaries. The
>> overloaded functions are pretty small, hence inlined, so I think it
>> should not be a problem, but it is obviously not nice.
>>
>> This naturally raises the question how expression templates avoid this
>> problem, or how eigen suppresses these warnings, if indeed those
>> warnings are in the safe-to-ignore category. If indeed the
>> reference-to-temporary warnings can be safely ignored, then can anyone
>> explain to me how to do it?
>
> First of all, here, expression templates allow to completely avoid
> this problem. Eigen isn't suppressing warnings here, and this isn't a
> place where Eigen has to deal with reference-to-temporary issues. It's
> a completely separate issue.
>
> Looking back at your code:
>
>> inline vector8s& operator+(vector8s &other)
>> {
>> vector8s temp;
>> temp.data=_mm_add_epi16(data,other.data);
>> return temp;
>> }
>
> The way that expression templates work, is that the operator+ does NOT
> do any computation, it does NOT return the computed result. Instead,
> it returns an object that describe the addition operation
> abstractly,and then when the user does c = a+b the actual computation
> will be done by operator=. So the prototype is a bit like this:
>
> Sum<T,OtherT> operator+(const OtherT& other)
> {
> return Sum<T,OtherT>(*this, other);
> }
>
> Then all the job is done by an operator= that takes a const
> Sum<T,OtherT>& as argument.
>
> This is actually explained here:
> http://eigen.tuxfamily.org/dox/InsideEigenExample.html#ConstructionOfSumXpr
I had read that example, so should have kept that in mind before
writing my previous mail. Sorry about that.
Back to my previous question, can somebody explain to me on how to
suppress those warnings in a meaningful way? And it is one of the
reasons I want FULL support (with vectorization, warts and all etc. )
for shorts in eigen.
More OT: I just looked at the code produced by my sucky sucky operator
overloading method, and it is fantastic. I have never seen so tightly
packed and serial-dependency-free code spewed by gcc from my code. All
the more reason to do it with eigen.
--
Rohit Garg
http://rpg-314.blogspot.com/
Senior Undergraduate
Department of Physics
Indian Institute of Technology
Bombay