Re: [eigen] Lazy evaluation bug, feature, or PEBKAC? |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] Lazy evaluation bug, feature, or PEBKAC?
- From: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>
- Date: Wed, 2 Mar 2011 13:07:19 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=nynCb51t0n7plLhMvruZWDykiC6xK7xljT2ZJfsXcvs=; b=tng4Iu68+q0QstJTgJTIZnAVay/l9mmTfmpy7llhSG2rdaJouN7r0kZe8g0i9eWBjL zY4F6ghxbk4g1q+JdwUPtQRk0bzzMH7tnU2bz0hIgr0sUmpqMWi6LdTdJ/efZEnzyRfO bRAIYjgdiRDBtXu1xtJiUeHf9+hKF2H9VDZ+I=
- 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=bfqv4QGmKFNPGN7RNZw7eM6LMxWkzRG7/Vr5EJst5zI/HsCFvKE75VYesNc+j4w+1U B0L3p2iGCtE4ESRekKfwkXVZ5HfBr5fwsbP1apyZ7e93wJV/BKIHdTdFRF7uK921RmjJ AHmmLOhpQWSH8OTk6iOSz0bKSASgjvNVN9geY=
2011/3/2 Benoit Jacob <jacob.benoit.1@xxxxxxxxx>:
> 2011/3/2 Dave Steffen <dave.steffen@xxxxxxxxxxx>:
>> On Wednesday, March 02, 2011 02:31:40 am Gael Guennebaud wrote:
>>> This has to be expected. In Eigen2 expressions are nested by
>>> reference, therefore after the line
>>>
>>> auto x = (a+b).col(0);
>>>
>>> the expression of (a+b) has been deleted and x refers to a dead object.
>>
>> Ouch. It looks to me like I just need to make that 'const auto&',
>> to keep the temporaries bound; alternately, maybe I just need to
>> evaluate the expression into a vector, E.G.
>>
>> Vector3d x = (a+b).col(0)
>>
>> ?
>>
>>> Same with:
>>>
>>> auto x = (a+b).eval().col(0);
>>>
>>> In this later case even Eigen3 fails. This can be observed using
>>> VectorXd (instead of Vector3d) and using valgrind.
>>
>> Ouch.
>>
>>>
>>> In Eigen2, the correct way to do this should be:
>>>
>>> auto x = (a+b).nestByValue().col(0);
>>>
>>> however this does not work either. This is a real bug.
>>
>> What's the right way to do it in Eigen 3? I'm going to port our
>> little test project over to that this morning.
>>
>>> Nevertheless, I'd not encourage you to store Eigen's expression this
>>> way, and I would rather advise to evaluate your sub expression to not
>>> lost the benefit of unrolling and vectorization.
>>
>> Well, right, I'd do that if I had the option.
>>
>> In my application, I really *do* need to add two matrices, which
>> might or might not happen to have 1 column, and then operate
>> (read-only) on individual columns. That doesn't strike me as a
>> terribly unusual thing to do.
>
> The unusual thing here is to want to construct the expression object
> once and then reuse it.
>
> Constructing these expression objects has zero runtime cost in
> optimized builds, because we make it very easy for the compiler to
> optimize away the expression objects completely.
>
> Therefore, there is no overhead in doing "a+b" everytime you need this
> sum expression: zero overhead.
>
> That's why practically nobody is trying to do what you're doing here :)
Also, note that your code only looks "natural" because it's using the
C++0x auto keyword. The C++03 version would have to name explicitly
the (very abstruse) expression type, which is another reason why
practically nobody bothers.
Benoit
- References:
- [eigen] Lazy evaluation bug, feature, or PEBKAC?
- Re: [eigen] Lazy evaluation bug, feature, or PEBKAC?
- Re: [eigen] Lazy evaluation bug, feature, or PEBKAC?
- Re: [eigen] Lazy evaluation bug, feature, or PEBKAC?
- Re: [eigen] Lazy evaluation bug, feature, or PEBKAC?