Download presentation
Presentation is loading. Please wait.
Published byAlisha Wells Modified over 9 years ago
1
U Albany SUNY PETE code Review Xingmin Luo 6/12/2003
2
U Albany SUNY 2 Outline Overview –Motivation –Expression Templates –Syntax tree Encoding syntax tree with Expression Templates Trace results of 3 kinds of expressions b = 1; c = 2; d = 3; b = c; a = b + c + d; Future Work and Conclusions
3
U Albany SUNY 3 Motivation Discover how and why PETE works? How to modify PETE code to support Psi calculus; for example, where (which files) we should change to support reverse operation in Convolution.
4
U Albany SUNY 4 Expression Templates const Expression, Reference > > &expr1 = b + c; const Expression, BinaryNode, Reference > > > &expr2 = b + 3 * c; const Expression, BinaryNode, Reference > > > &expr3 = b + c * d;
5
U Albany SUNY 5 Syntax trees + + + b c b * b * 3 c c d (1) (2) (3) b + c b + 3 * c b + c * d
6
U Albany SUNY 6 Encoding syntax trees with expression Templates (ET) - 1 + b c (1) const Expression, Reference > > &expr1 = b + c;
7
U Albany SUNY 7 Encoding syntax trees with expression Templates (ET) - 2 + b * 3 C (2) b + 3 * c const Expression, BinaryNode, Reference > > > &expr2 = b + 3 * c;
8
U Albany SUNY 8 Encoding syntax trees with expression Templates (ET) - 3 + b * c d (3) b + c * d const Expression, BinaryNode, Reference > > > &expr3 = b + c * d;
9
U Albany SUNY 9 Files PETE ( pete.h ) includes the following files #include "PETE/Scalar.h" #include "PETE/TypeComputations.h" #include "PETE/TreeNodes.h" #include "PETE/OperatorTags.h" #include "PETE/Functors.h" #include "PETE/Combiners.h" #include "PETE/ForEach.h" #include "PETE/CreateLeaf.h“ Our implementations Array.cpp Array.h
10
U Albany SUNY 10 Variables declaration and definition Array.cpp vector shape; Array a(shape), b(shape), c(shape), d(shape); Array.h Array(const vector shape) { long i, sum; sum=1; for (i=0; i<shape.size(); i++) { sum = sum * shape[i]; (this->shape).push_back(shape[i]); } this->size = sum; this->d = my_malloc(sum*sizeof(T)); } private: T * d; vector shape; long size;
11
U Albany SUNY 11 Trace result of b=1;c=2;d=3; Array.cpp b=1;c=2;d=3; Array.h Array &operator=(T value) { for(long i=0; i size; i++) d[i] = value; return *this; } private: T * d; vector shape; long size;
12
U Albany SUNY 12 Trace result of b=c; Array.cpp b=c; Array.h Array &operator=(const Array &v) { for(long i=0; i size; i++) d[i] = v.d[i]; return *this; } private: T * d; vector shape; long size;
13
U Albany SUNY 13 Trace result of a = b + c + d; (1) Array.cpp a = b + c + d; Array.h template Array &operator=(const Expression &rhs) { for(long i=0; i size; i++) d[i] = forEach(rhs, EvalLeaf1(i), OpCombine()); return *this; //equal to: a.d[i] = b.d[i]+c.d[i]+d.d[i] } private: T * d; vector shape; long size;
14
U Albany SUNY 14 Trace result of a = b + c + d; (2) ForEach.h CLASS NAME ForEach forEach(Expr& e, FTag& f, CTag& c) //call this function //same as ForEach::apply // Expr is the type of the expression tree. // FTag is the type of the leaf tag.(specifies the operation being applied) // CTag is the type of the combiner tag. // ForEach ::apply(Expr &e,FTag& f,CTag& c) is a function // that traverses the expression tree defined by e, applies the functor f // to each leaf and combines the results together using the combiner c. // The type of object returned is given by: // typename ForEach ::Type_t // the function forEach(Expr& e,FTag& f,CTag& c) duplicates the action // of ForEach::apply
15
U Albany SUNY 15 Trace result of a = b + c + d; (3) ForEach.h template inline typename ForEach ::Type_t forEach(const Expr &e, const FTag &f, const CTag &c) { return ForEach ::apply(e, f, c); } template struct ForEach { typedef typename LeafFunctor ::Type_t Type_t; inline static Type_t apply(const Expr &expr, const FTag &f, const CTag &) { return LeafFunctor ::apply(expr, f); } };
16
U Albany SUNY 16 Trace result of a = b + c + d; (4) Array.h specializes EvalLeaf1 function template struct LeafFunctor, EvalLeaf1> { typedef T Type_t; static Type_t apply(const Array &a, const EvalLeaf1 &f) { return a[f.val1()]; } //here a is b+c+d }; Functors.h (functors define here) // LeafFunctors are used by ForEach to apply operations to the leaves of the // expression tree. Typical functors are evaluators, counters, etc. struct EvalLeaf1 { int i1_m; // Note: forEach(rhs, EvalLeaf1(i), OpCombine()); so i1, i1_m is i inline EvalLeaf1(int i1) : i1_m(i1) { } inline int val1() const { return i1_m; } };
17
U Albany SUNY 17 Trace result of a = b + c + d; (5) Combiners.h (defines combiner tag) struct OpCombine //Actually, this Tag did nothing. { PETE_EMPTY_CONSTRUCTORS(OpCombine) }; PETE.h #define PETE_EMPTY_CONSTRUCTORS(CLASS) \ CLASS() { } \ CLASS(const CLASS &) { } \ CLASS &operator=(const CLASS &) { return *this; } Equals to: OpCombine() { } OpCombine (const OpCombine &) { } OpCombine &operator=(const OpCombine &) { return *this; }
18
U Albany SUNY 18 user defined Expression <> tree examples Array.cpp Array d; const Expression<BinaryNode<OpAdd, Reference, Reference > > &expr1 = b + c; d = expr1; cout << d << endl; int num = forEach(expr1, CountLeaf(), SumCombine()); cout << num << endl; const Expression, BinaryNode, Reference > > > &expr2 = b + 3 * c; num = forEach(expr2, CountLeaf(), SumCombine()); cout << num << endl; const Expression, BinaryNode, Reference > > > &expr3 = b + c * d; num = forEach(expr3, CountLeaf(), SumCombine()); cout << num << endl;
19
U Albany SUNY 19 Future Work Modify PETE code to support Psi operations. Extend our current implementation of the Psi calculus Build high-level Psi calculus tools
20
U Albany SUNY 20 Conclusions PETE’s Expression templates provide the ability to perform compiler preprocessor-style optimizations (expression tree manipulation) The C++ template mechanism can be applied to a wide variety of problems (e.g. tree traversal ala PETE, graph traversal, list traversal) to gain run-time speedup at the expense of compile time/space
21
U Albany SUNY 21 Acknowlegements Prof. Lenore Mullin Prof. Dan Rosenkrantz Prof. Harry Hunt Lawrence Bush
22
U Albany SUNY 22 Defines expression tree CreateLeaf.h (defines expression tree) //Expression - a class that wraps the contents of an expression template class Expression { public: typedef T Expression_t; // Type of the expression. Expression(const T& expr) : expr_m(expr) { } // Construct from an expression. const Expression_t& expression() const // Accessor that returns the expression. { return expr_m; } private: // Store the expression by value since it is a temporary produced // by operator functions. T expr_m; };
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.