C++使用Expression Templates

StackOverflow的问答Passing a functor as C++ template parameter的问题很有意思,我以前从来没有想过这样的做法。这个东西专业术语叫:Expression Templates。

Wiki的相关内容由浅入深,解释的很好:

Expression templates are a C++ template metaprogramming technique that builds structures representing a computation at compile time, with structures are evaluated only as needed to produce efficient code for the entire computation.[1]
Expression templates thus allow programmers to bypass the normal order of evaluation of the C++ language and achieve optimizations such as loop fusion.
Expression templates were invented independently by Todd Veldhuizen and David Vandevoorde;[2]:341, [3]
it was Veldhuizen who gave them their name.[2]:341 They are a popular technique for the implementation of linear algebra software.[1]

Motivation and example

Consider a library representing vectors and operations on them. One common mathematical operation is to add two vectors u and v, element-wise, to produce a new vector. The obvious C++ implementation of this operation would be an overloadedoperator+ that returns a new vector object:

class Vec {
    std::vector<double> elems;

  public:
    Vec(size_t n) : elems(n) {}

    double &operator[](size_t i)      { return elems[i]; }
    double operator[](size_t i) const { return elems[i]; }
    size_t size()               const { return elems.size(); }
};

Vec operator+(Vec const &u, Vec const &v) {
    assert(u.size() == v.size());
    Vec sum(u.size());
    for (size_t i = 0; i < u.size(); i++) {
         sum[i] = u[i] + v[i];
    }
    return sum;
}

Users of this class can now write Vec x = a + b; where a and b are both instances of Vec.
A problem with this approach is that more complicated expressions such as Vec x = a + b + c are implemented inefficiently. The implementation first produces a temporary vector to hold a + b, then produces another vector with the elements of cadded in. Even with return value optimization this will allocate memory at least twice and require two loops.
Delayed evaluation solves this problem, and can be implemented in C++ by letting operator+ return an object of a custom type, say VecSum, that represents the unevaluated sum of two vectors, or a vector with a VecSum, etc. Larger expressions then effectively build expression trees that are evaluated only when assigned to an actual Vec variable. But this requires traversing such trees to do the evaluation, which is in itself costly.[4]

Expression templates implement delayed evaluation using expression trees that only exist at compile time. Each assignment to a Vec, such as Vec x = a + b + c, generates a new Vec constructor if needed by template instantiation. This constructor operates on three Vec; it allocates the necessary memory and then performs the computation. Thus only one memory allocation is performed.
An example implementation of expression templates looks like the following. A base class VecExpression represents any vector-valued expression. It is templated on the actual expression type E to be implemented, per the curiously recurring template pattern.

 1 template <typename E>
 2 class VecExpression {
 3   public:
 4     double operator[](size_t i) const { return static_cast<E const&>(*this)[i];     }
 5     size_t size()               const { return static_cast<E const&>(*this).size(); }
 6 
 7     // The following overload conversions to E, the template argument type;
 8     // e.g., for VecExpression<VecSum>, this is a conversion to VecSum.
 9      operator E& () { return static_cast<E&>(*this); }
10      operator const E& () const { return static_cast<const E&>(*this); }
11 };

The Vec class still stores the coordinates of a fully evaluated vector expression, and becomes a subclass of VecExpression.

class Vec : public VecExpression<Vec> {
    std::vector<double> elems;

  public:
    double operator[](size_t i) const { return elems[i]; }
    double &operator[](size_t i)      { return elems[i]; }
    size_t size() const               { return elems.size(); }

    Vec(size_t n) : elems(n) {}

  // construct vector using initializer list 
    Vec(std::initializer_list<double>init){
        for(auto i:init)
            elems.push_back(i);
    }


    // A Vec can be constructed from any VecExpression, forcing its evaluation.
    template <typename E>
    Vec(VecExpression<E> const& vec) : elems(vec.size()) {
        for (size_t i = 0; i != vec.size(); ++i) {
            elems[i] = vec[i];
        }
    }
};

The sum of two vectors is represented by a new type, VecSum, that is templated on the types of the left- and right-hand sides of the sum so that it can be applied to arbitrary pairs of vector expressions. An overloaded operator+ serves as syntactic sugarfor the VecSum constructor.

 1 template <typename E1, typename E2>
 2 class VecSum : public VecExpression<VecSum<E1, E2> > {
 3     E1 const& _u;
 4     E2 const& _v;
 5 
 6 public:
 7     VecSum(E1 const& u, E2 const& v) : _u(u), _v(v) {
 8         assert(u.size() == v.size());
 9     }
10 
11     double operator[](size_t i) const { return _u[i] + _v[i]; }
12     size_t size()               const { return _v.size(); }
13 };
14   
15 template <typename E1, typename E2>
16 VecSum<E1,E2> const
17 operator+(E1 const& u, E2 const& v) {
18    return VecSum<E1, E2>(u, v);
19 }

With the above definitions, the expression a + b + c is of type
VecSum<VecSum<Vec, Vec>, Vec>

so Vec x = a + b + c invokes the templated Vec constructor with this type as its E template argument. Inside this constructor, the loop body
elems[i] = v[i];

is effectively expanded (following the recursive definitions of operator+ and operator[] on this type) to
elems[i] = a.elems[i] + b.elems[i] + c.elems[i];

with no temporary vectors needed and only one pass through each memory block.
Basic Usage :

 1 int main(){
 2 
 3     Vec v0 = {23.4,12.5,144.56,90.56};
 4     Vec v1 = {67.12,34.8,90.34,89.30};
 5     Vec v2 = {34.90,111.9,45.12,90.5};
 6     VecSum<VecSum<Vec, Vec>, Vec> sum = v0+v1+v2;
 7 
 8     Vec v3(4);
 9 
10     for(int i=0;i<4;++i)
11         v3[i]=sum[i];
12 
13     for(int i=0;i<v3.size();++i)
14         std::cout << v3[i] << std::endl;
15 
16 
17 
18 }

Applications

Expression templates have been found especially useful by the authors of libraries for linear algebra, i.e., for dealing with vectors and matrices of numbers. Among libraries employing expression template are Blaze[5]
, Blitz++[6]
, Boost uBLAS[7]
, Eigen[8]
, POOMA[9]
, Stan Math Library[10]
, and xtensor[11]
.
Outside of vector math, the Spirit parser framework uses expression templates to represent formal grammars and compile these into parsers.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • daoxianzai还zai心疼,后悔自己的大意,漠视和拖拉,本来结果不会这样的,如果…… 那天早上到校后,我发现...
    长跑人阅读 651评论 0 1
  • 表面上我们都长大了,但其实破坏的是当初的美好,最初的那点好东西全给破坏了,如果当初的我们出现在我们面前,一定会笑话...
    森林里的小幺姬阅读 171评论 0 0
  • 在很多人的眼里,深圳是一个新兴的经济特区,是一个移民城市,有一个快节奏生活的超级城市,也有人说深圳特区发展只有短短...
    城市分享阅读 469评论 1 2
  • 80后的孩子深受武侠剧的影响,而作为一名女孩子的我也不例外,生长在乡下,获取的外部信息,就来自小小黑白电视屏幕。课...
    笑忘书风铃草阅读 333评论 0 1

友情链接更多精彩内容