Rich Vector Expressions
As discussed in the previous chapter, vector operation can be accelerated by improving their cache locality via expression templates. Cache locality can be further improved in applications when subsequent vector expressions are evaluated in one loop, data dependencies allowing. Unfortunately, this so-called loop fusion cannot be realized with expression templates. At least not when the loops are performed in the assignment.
In collaboration with Karl Meerbergen, we developed expression templates that can be nested, called rich expression templates. The following program shows some examples of rich expression templates:
// File: rich_vector_expr.cpp #include <iostream> #include <boost/numeric/mtl/mtl.hpp> int main(int, char**) { using namespace mtl; typedef std::complex<double> cdouble; dense_vector<cdouble> u(10), v(10); dense_vector<double> w(10), x(10, 4.0); for (unsigned i= 0; i < size(v); i++) v[i]= cdouble(i+1, 10-i), w[i]= 2 * i + 2; // Increment w by x // and assign the sum of--the incremented--w and v to u u= v + (w+= x); std::cout << "u is " << u << "\n"; // w= w * 3; x= 2; v= v + w + x; u= u + v; u+= v+= (w*= 3) + (x= 2); std::cout << "u is " << u << "w is " << w << "\n"; return 0; }
The first example shows the combination of an incremental assignment with a vector addition. The second statement fuses four vector expressions:
Again, all these operations are performed in one loop and each vector element is accessed exactly once.
Return to Vector Expressions Table of Content Proceed to Matrix Expressions
Rich Vector Expressions -- MTL 4 -- Peter Gottschling and Andrew Lumsdaine
-- Gen. with
rev. 7542
on Sat Aug 11 2012 by doxygen 1.7.6.1 -- © 2010 by SimuNova UG.