Truly Optimal CodeUsing a Metaprogramming Library.ppt
《Truly Optimal CodeUsing a Metaprogramming Library.ppt》由会员分享,可在线阅读,更多相关《Truly Optimal CodeUsing a Metaprogramming Library.ppt(19页珍藏版)》请在麦多课文档分享上搜索。
1、Generating Truly Optimal Code Using a Metaprogramming Library,Don Clugston First D Programming Conference, 24 August 2007,String mixins in D undercooked, but very tasty,Compiles to:Vindicates built-in string operations,char greet(char greeting) return writefln(“ greeting , world!”); void main() mixi
2、n( greet( “Hello” ) ); ,void main() writefln( “Hello, world!” ); ,The Challenge,Fortran: BLAS (a standard set of highly optimised routines). The crucial functions are coded in asm.y += a * xBut BLAS is limited nothing for simple things:x = y - za = r*0.3 + g*0.5 + b*0.2;,void DAXPY(double y, double
3、x, double a) for (i = 0; i y.length; +i)yi += xi * a; ,Operating overloading,Gives ideal syntax, always works Cant operate on built-in types Inefficient because: Creates unnecessary temporaries. Multiple loops, eg a=b+c+d Somehow, we need to get the expression inside the for loop!,double temp1= new
4、double, temp2 = new double;for(int i=0; ib.length; +i) temp1i = bi + ci;for(int i=0, itemp1.length; +i) temp2i = temp1i + di; a = temp2;,The Wizard Solution: Expression Templates (eg, Blitz+),Overloaded operators dont do the calculation: instead, they record the operation as a proxy type, creating a
5、 syntax tree.Example: (a+b)/(c-d): Need a good optimiser. Works in D as well as C+. BUT we are fighting the compiler!,DVExpr, DVExpr, DApDivide,Representing the Syntax Tree in D,In D, any expression can be represented in a single template.Represent types and values in a tuple. Represent expression i
6、n a char . AZ correspond to T0T25.eg: Note that A appears twice in the expression (operator overloading cant represent that).,void vectorOperation(char expression, T)(T values) ,vectorOperation!(“A+=(B*C)/(A+D)”)(x, y, z, u, v);,Finding the vectors in a tuple,Its a vector if you can index it. Imperf
7、ection cant index tuple in CTFE. Workaround create array of results.Usage:if ( isVector!(Tuple)i) ,template isVector(T.) static if (T.length = 0)const bool isVector = ;else static if( is( typeof(T00) ) )const bool isVector = true isVector!(T1$);else const bool isVector = false isVector!(T1$); ,Metap
8、rogramming For Muggles,USAGE: double firstvec, secondvec, thirdvec; VEC!(“A+=B*(C+A*D)“)(firstvec, secondvec, thirdvec, 25.7);,char muggle (char expr, Values.)() char code = “for (int i=0; i= A ,Trivial enhancements,Ensure all vectors are the same length.Assert no aliasing (vectors dont overlap). Eq
9、ualize with hand-coded asm BLAS routines.,foreach(int i, bool b; isVector!(Values)1$) if (b)code = “assert(values“ atoi(i) “.length = values0.length);”; ,static if ( expr = “A+=B*C” ,Asm code via perturbation,Its hard to determine the optimal asm for an algorithm, much easier to modify existing code
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- TRULYOPTIMALCODEUSINGAMETAPROGRAMMINGLIBRARYPPT

链接地址:http://www.mydoc123.com/p-373469.html