Re: [eigen] aliasing system

[ Thread Index | Date Index | More Archives ]

On Thu, 6 Sep 2007, Andre Krause wrote:

Hi Andre,

I do understand your concern. Let me first stress that the same problem exists in the other ET libraries. TVMET has a global function alias(). Blitz++ is even worse: not only does it suffer from the same problem, it does not even provide an aliasing system! See here:

It would be impossible to determine exactly when aliasing is needed and when it's not, without paying the price of a constant overhead, which we don't want.

maybe thats a stupid question, but cant the .alias() thing be integrated in an operator=(const Matrix<T,d1,d2>&m) { this->alias()=m; } ??

Hey, you're forgetting the whole reason why we want expression templates in the first place :)

Expression templates eliminate temporaries. This is a big performance improvement.

Let me try to explain what is going on here, because it really is important. Here is an example. Suppose that you have vectors u, v, w, and you do:
	u = v + w;
Then Eigen2 will automatically evaluate this as:
for(int i = 0; i < size; i++) u[i] = v[i] + w[i];
and the for loop can then be unrolled (I'll take care of this later).
So this is fast.

Now Eigen1 (like any library without ETs) does this: it evaluates the above expression as
	operator=(u, operator+(v, w))
So it first calls operator+(v, w), which returns a result by value, so call r this result, and then it calls operator=(u, r).
So all in all, Eigen1 evaluates this expression as:
for(int i = 0; i < size; i++) r[i] = v[i] + w[i];
for(int i = 0; i < size; i++) u[i] = r[i];

As you can see, Eigen1 is inefficient here, because it wastes time with this useless temporary r. So the elimination of the temporary allows Eigen2 to be much faster.

Now alias() negates this. alias() forces the creation of a temporary. So if we followed your suggestion of implementing operator= as
	"this->alias() = m"
then we would negate much of the benefit of using expression templates!

We really want to only use alias() when it's necessary, because it is costly.

I agree that it makes the API harder, but I don't see a better solution.

I think, this will be Item #1 in our FAQ/troubleshooting.


Mail converted by MHonArc 2.6.19+