Re: [eigen] Lazy evaluation bug, feature, or PEBKAC?

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


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



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/