| Start |
|---|
| A basic example illustrating expression templates An expression template is a compile-time optimization technique used mostly in scientific computing. It's main purpose is to avoid unnecessary temporaries and optimize loop calculations using a single pass (typically when performing operations on numerical aggregates). Expression templates were initially devised in order to circumvent the inefficiencies of naïve operator overloading when implementing numerical Before actually diving into expression templates, you should understand why you need them in the first place. To illustrate this, consider the very simple Matrix class given below: Given the previous class definition, you can now write Matrix expressions such as: As illustrated above, being able to overload Unfortunately, the previous implementation is also highly inefficient compared to an equivalent "hand-crafted" version. To understand why, you have to consider what happens when you write an expression such as For example, without operator overloading, you could implement a far more efficient Matrix summation using a single pass: The previous example however has its own disadvantages because it creates a far more convoluted interface for the Matrix class (you would have to consider methods such as Instead let us take a step back and see how we can adapt operator overloading to perform in a more efficient way The problem stems from the fact that the expression This is the core idea behind expression templates: instead of having For example, here is a possible implementation for an expression template corresponding to the summation of 2 types: And here is the updated version of As you can see, In fact, nothing stops you from instantiating the You can however at a later stage, when you actually need the result of the summation, evaluate the expression As you can see, another benefit of using an expression template, is that you have basically managed to evaluate the sum of Also, nothing stops you from combining multiple expression templates. For example, And here again you can evaluate the final result using a single pass: Finally, the last piece of the puzzle is to actually plug your expression template into the |
| Basic expression templates on element-wise algebraic expressions Introduction and motivation Expression templates (denoted as ETs in the following) are a powerful template meta-programming technique, used to speed-up calculations of sometimes quite expensive expressions. It is widely used in different domains, for example in implementation of linear algebra libraries. For this example, consider the context of linear algebraic computations. More specifically, computations involving only element-wise operations. This kind of computations are the most basic applications of ETs, and they serve as a good introduction to how ETs work internally. Let's look at a motivating example. Consider the computation of the expression: Here for the sake of simplicity, I'll assume that the class In a conventional implementation without using ETs (or other similar techniques), at least five constructions of
Implementation using ETs can eliminate the creation of temporary There will also be four constructions of How do ETs work Basically speaking, ETs for any algebraic computations consist of two building blocks:
So, specifically how do we implement ETs in this example? Let's walk through it now. Consider always the following code snippet: The expression to compute result can be decomposed further into two sub-expressions:
What ETs do is the following: there is therefore no need to create a temporary Here are the example codes in action. File vec.hh : wrapper for std::vector, used to show log when a construction is called.File expr.hh : implementation of expression templates for element-wise operations (vector plus and vector inner product)Let's break it down to sections.
File main.cc : test src file |
Read Article
https://develop.horje.com/learn/1434/reference