Boost logo

Glas :

Re: [glas] when to evaluate scalar expressions

From: Andreas Pokorny (andreas.pokorny_at_[hidden])
Date: 2006-04-22 07:52:59

On Thu, Apr 20, 2006 at 10:14:32PM +0200, Toon Knapen <toon.knapen_at_[hidden]> wrote:
> The expression-template mechanism is used to evaluate the expression at
> the assignment as mentioned here:
> .
> In addition to avoiding temporaries and loop fusion etc., evaluating the
> expression at assignment allows us also to dispatch the expression as a
> whole (i.e. lhs and rhs) to a specific backend.
> But should we also delay evaluating scalar-expressions until assignment.
> Suppose following code-snippet
> glas::dense_vector<double> v(10),w(10) ;
> glas::assign(w,mult(norm_2(v),v)) ;
> where we multiply every element in the vector v with the norm_2 of v and
> assign the result to a vector w. If we delay all evaluations until
> assignment and avoid having temporaries, for every element in w we will
> multiply the corresponding element in v with the norm_2 of v which we
> recalculate every time again.
> So should it not be better to evaluate scalar-expression always
> immediately? Or are there scenario's where it would be better to delay
> the evaluation of the scalar-expression?

Multiple evaluations happen in other cases, too. I would rather wonder
whether the frontend should already provide meta information, in the
form of subtree depth, or complexity approximations, to simplify the
decision to preevaluate and cache the result, or evaluate each time
the subtree is accessed.

On the other hand, you already mention that scalar expressions behave
special compared to scalar values, while matrix or vector expressions
behave just like matrices and vectors, when it comes to evaluating
and accessing the values. So if you evaluate immediately this difference
might disappear.

Can you attach a specific backend to a subtree, and have another one
in the final assignment? I just wonder if you have control over the
backend used to preevaluate the scalar value.

I think good backends are supposed to detect these cases, and behave
accordingly. And maybe there will be backends that always create temporaries?

Andreas Pokorny