when to evaluate scalar expressions

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

when to evaluate scalar expressions

Toon Knapen
The expression-template mechanism is used to evaluate the expression at
the assignment as mentioned here:
http://glas.sourceforge.net/doc/papers/introduction/index.html .

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?

toon

_______________________________________________
glas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/glas
Reply | Threaded
Open this post in threaded view
|

Re: when to evaluate scalar expressions

Andreas Pokorny-2
On Thu, Apr 20, 2006 at 10:14:32PM +0200, Toon Knapen <[hidden email]> wrote:

> The expression-template mechanism is used to evaluate the expression at
> the assignment as mentioned here:
> http://glas.sourceforge.net/doc/papers/introduction/index.html .
>
> 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?

Regards,
Andreas Pokorny

 
_______________________________________________
glas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/glas
Reply | Threaded
Open this post in threaded view
|

Re: when to evaluate scalar expressions

Toon Knapen
Andreas Pokorny wrote:
>
> 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.


Partial dispatching, i.e. dispatching only a specific part of an
expression to one backend and the rest of the expression to another
backend, is not directly supported. We thought long and hard about this
but partial dispatching leads to a lot of ambiguous situations.

Thus currently only when assigning a rhs to the lhs we dispatch the
/whole/ expression to a specific backend. However the backend itself can
decide to dispatch parts of the expression to another backend and/or
create temporaries.

For instance we have one 'strict' blas-backend. Dispatching an
expression that does not correspond exactly to a blas-call to this
backend will result in a (compile-)error. Another backend however, say
general blas-backend, will try to evaluate the expression using multiple
blas-calls when there is no blas-call that can evaluate the whole
expression at once. The latter backend will for instance first call
dnrm2 and then daxpy to evaluate the expression 'w=norm2(v)*v' with w
and v being a glas::dense_vector<double> and thus create a temporary.

Since adding backends should be simple, anyone can add another backend,
that builds on all other backends, to adapt policies regarding partial
dispatching, temporaries etc.

toon


_______________________________________________
glas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/glas
Reply | Threaded
Open this post in threaded view
|

Re: when to evaluate scalar expressions

Andreas Pokorny-2
On Sun, Apr 23, 2006 at 10:26:23PM +0200, Toon Knapen <[hidden email]> wrote:

> Andreas Pokorny wrote:
> >
> > 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.
>
>
> Partial dispatching, i.e. dispatching only a specific part of an
> expression to one backend and the rest of the expression to another
> backend, is not directly supported. We thought long and hard about this
> but partial dispatching leads to a lot of ambiguous situations.
>
> Thus currently only when assigning a rhs to the lhs we dispatch the
> /whole/ expression to a specific backend. However the backend itself can
> decide to dispatch parts of the expression to another backend and/or
> create temporaries.

I was asking because, the user would want to control the backend used in
the pre evaluation of the scalar.

Regards
Andreas Pokorny

_______________________________________________
glas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/glas
Reply | Threaded
Open this post in threaded view
|

Re: when to evaluate scalar expressions

Toon Knapen
Andreas Pokorny wrote:

>> Partial dispatching, i.e. dispatching only a specific part of an
>> expression to one backend and the rest of the expression to another
>> backend, is not directly supported. We thought long and hard about this
>> but partial dispatching leads to a lot of ambiguous situations.
>>
> I was asking because, the user would want to control the backend used in
> the pre evaluation of the scalar.
>


good point. This is indeed a drawback of evaluating scalar-expressions
early. So it's probably best not to evaluate scalar-expressions early
and thus dispatch the scalar-expression along with the complete
expression (that contains the scalar-expression).

If users desire to use the _result_ of the scalar-expression in another
expression they can first evaluate the scalar-expression by assigning it
to a scalar and use that scalar in any expression later on.

toon

_______________________________________________
glas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/glas