C++ Expression Templates
Asked Answered
D

3

9

I currently use C for numerical computations. I've heard that using C++ Expression Templates is better for scientific computing. What are C++ Expression Templates in simple terms?

  1. Are there books around that discuss numerical methods/computations using C++ Expression Templates?

  2. In what way, C++ Expression Templates are better than using pure C?

Domett answered 8/4, 2010 at 8:49 Comment(1)
If you are still interested in how expression templates work, Eigen (a Linear Al gebra library) has very good documentation on how they implement them.Neisa
P
12

What are C++ Expression Templates in simple terms?

Expression templates are a category of C++ template meta programming which delays evaluation of subexpressions until the full expression is known, so that optimizations (especially the elimination of temporaries) can be applied.

Are there books around that discuss numerical methods/computations using C++ Expression Templates?

I believe ET's were invented by Todd Veldhuizen who published a paper on it 15 years ago. (It seems that many older links to it are dead by now, but currently here is a version of it.) Some material about it is in David Vandevoorde's and Nicolai Josuttis' C++ Templates: The Complete Guide.

In what way, C++ Expression Templates are better than using pure C?

They allow you to write your code in an expressive high level way without losing performance. For example,

void f(const my_array<double> a1, const my_array<double> a2) 
{ 
  my_array<double> a3 = 1.2 * a1 + a1 * a2; 
  // ..
}

can be optimized all the way down to

for( my_array<double>::size_type idx=0; idx<a1.size(); ++idx ) 
  a3[idx] = 1.2*a1[idx] + a1[idx]*a2[idx]; 

which is faster, but harder to understand.

Potentiometer answered 8/4, 2010 at 8:53 Comment(3)
Cool. Do we have such things with pure C?Domett
I hardly think so, seeing that C has no templates, nor operator overloading.Cheke
@yCalleecharan: Expression templates are all about writing high-level, abstract code and still getting low-level optimizations. C is not at all about high-level, abstract code, so asking about doing this in C makes as little sense as asking for this in assembler. To sum it up: If you want high-level abstractness, and not abandon speed, use C++. If you just want speed and don't care about abstractions, use C or FORTRAN. If you just want high-level abstractness and don't care about speed, use something else completely.Potentiometer
L
3

Adding to sbi's answer, expression templates implement high-level peephole optimizations using templates for pattern matching and synthesis.

They also add syntactic sugar, or make your code more readable, by allowing you to specify the algorithm in terms of simple operations. So, in this case, simplicity and elegance are achieved through optimization by metaprogramming. At least, if you do everything right.

Leonor answered 8/4, 2010 at 9:1 Comment(0)
P
3

There is a nice article on C++ template math in the good old Flipcode archive (sure brings back memories):

http://www.flipcode.com/archives/Faster_Vector_Math_Using_Templates.shtml

Paly answered 8/4, 2010 at 9:1 Comment(4)
Thanks. I shall read the article. Wish there was something similar for pure C.Domett
Why would you want something like that? Why would you avoid C++ so forcefully? C is meant to be barebones and straightforward, simple, without complex ideas.Witchhunt
Usually C is enforced by legacy constraints or platform constraints. @Domett doesn't state his but I am hazarding a guess that the rest of the computation application is in C. [OT]In an app that is a mix of C and C++ I often limit myself to C code in the C legacy section so it feels 'correct'.Gussi
This library in GitHub makes computations with vectors and any STL-compatible containers quite easy. ( github.com/tirimatangi/LazyExpression )Galosh

© 2022 - 2024 — McMap. All rights reserved.