expression templates

enuhtac enuhtac_lists at gmx.de
Thu Mar 31 05:33:59 PDT 2011


Am 29.03.2011 21:15, schrieb dsimcha:
> == Quote from bearophile (bearophileHUGS at lycos.com)'s article
>> The operator overloading done with opCmp is too much coarse even if you want to
> implement sets with operators like <= < > >= == for subset, etc.
>
> Can you please give an example of where <=, >, etc. are useful for representing
> set operations?  My naive opinion (i.e. without understanding your use case) is
> that using comparison operators to represent anything besides partial or total
> ordering is a severe abuse of operator overloading.  (Their use to represent
> ordering of corresponding elements of a vector or matrix is a borderline case.)
You're right. You rarely need comparison operators for sets. This is
possible of course and Matlab or Python use this feature which
significantly simplifies some operations, but if you consider
performance (and that is what you do if you think about expression
templates) this is not a good practice as temporary bool vectors are
generated.

In my case the use of expression templates is twofold: I expect to have
some multidimensional templated array variable type, let's call it
'Array', that should support expression templates for algebraic
operations. For this purpose comparison operators are not needed
(normaly you use reductions instead). But additionally I would like to
use expression templates to parse expressions that are used to
automatically generate device depend code. In one of my previous posts I
gave an example. These expression templates do not work on sets but on
scalar expressions. So comparison operators can be useful. E.g.:

auto case1 = v[I] < 0.0;
auto case2 = !case1;
auto result = case1 * v[I] + case2 * v[I+1];

This code mimics the behaviour of an if ... else construct. The
difference is that both cases are computed and combined into the result
so internally no branches are necessary. This is used to circumvent
program path divergence on GPU devices. Also I suppose that this type of
code is more efficient on standard CPUs as there are no branch
prediction failures.

I use scalar expressions to build complex operators that work on arrays.
To give an example that includes both types of expression templates I
imagine code like this:

class Array( T )
{ ... };

class Operator( Expr )
{ ... };

Operator!( result[I] = (v[I+1] - v[I-1])/h ) derivative;

Array!double a, b, c;

b = derivative( a );
c = a + b;

result, v and I need to be predefined (global) variables of whatever
type that encode input and output parameters of operators (maybe result
could be omitted) and the position in the array.

As I now understand, expression templates are normally used for set
operations. For this purpose you normally do not need comparison
operators so D is well suited. But I would like to use expression
templates in a more general way that is obviously not standard. So D
lacks the necessary support. It's a pity...


More information about the Digitalmars-d mailing list