欢迎来到麦多课文档分享! | 帮助中心 海量文档,免费浏览,给你所需,享你所想!
麦多课文档分享
全部分类
  • 标准规范>
  • 教学课件>
  • 考试资料>
  • 办公文档>
  • 学术论文>
  • 行业资料>
  • 易语言源码>
  • ImageVerifierCode 换一换
    首页 麦多课文档分享 > 资源分类 > PPT文档下载
    分享到微信 分享到微博 分享到QQ空间

    Truly Optimal CodeUsing a Metaprogramming Library.ppt

    • 资源ID:373469       资源大小:60.50KB        全文页数:19页
    • 资源格式: PPT        下载积分:2000积分
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    二维码
    微信扫一扫登录
    下载资源需要2000积分(如需开发票,请勿充值!)
    邮箱/手机:
    温馨提示:
    如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如需开发票,请勿充值!如填写123,账号就是123,密码也是123。
    支付方式: 支付宝扫码支付    微信扫码支付   
    验证码:   换一换

    加入VIP,交流精品资源
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    Truly Optimal CodeUsing a Metaprogramming Library.ppt

    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

    10、. Begin with Agner Foggs optimal asm code for DAXPY. Use same loop design and register allocation strategy. Ignore difficult cases fallback to D code.,X87 (stack-based),Convert the infix expression into postfix. Split += into + and =. Swap operands to avoid FMUL latency. A += B - C * D A = (A+B) - (

    11、C*D) C D * A B + - A = Avoid gaps in the instruction set Eg, fewer instructions for 80-bit reals, so load them first whenever possible.,X87 code generation,Directly convert postfix to inline asm.,VEC!(“C+=B*(A+D)“)( 2213.3, vec1, floatvec, vec2); / Postfix : BAD+*C+C=L1:fld double ptr EAX + 8*ESI; /

    12、Bfld double ptr EAX + 8*ESI; /Afadd double ptr EDX + 8*ESI; /D+fmulp ST(1), ST; /*fadd float ptr ECX + 4*ESI; /C+fxch ST(1), ST;fstp float ptr ECX + 4*ESI - 4; / C= L2: inc ESI;jnz L1;,SSE/SSE2 (register-based),Cant do mixed-precision operations. Unroll loop by 2 or 4, to take advantage of SIMD. Ins

    13、truction scheduling is less critical, but register allocation is more complicated than for x87.,GPGPU,Use the GPU in modern video cards to perform massively parallel calculations. Uses OpenGL or DirectX calls, instead of inline asm. Full of hacks (pretend your data is a texture!) but a rational API

    14、should emerge soon. This should NOT be built into a compiler!,Adding a front end,Operator overloading Same limitations as before Mixins eg, mixin(blade(“firstvec+=secondvec*2.38”); clumsy syntax BUT: Can detect aliases Allows better error messages Can unroll small loops inline Closer to proposed mac

    15、ro syntax,Front end using mixins,Lex: first += second * 2.38 A+=B*C. Determine types, resolve aliases, convert constants to literals. Determine precedence and associativity Perform constant foldingWe can do most of this using mixins Compiler help is most required for 4 _traits could help,Determining

    16、 types,char getSymbolTable(char symbols) char result = “;for(int i=0; i0) result =“,“;result = “typeof(“ symbolsi ).stringof, symbolsi .stringof;result = “;return result; ,When mixed in, this creates an array2 of string literals. 0 is the type, 1 is the value,Determining precedence,class AST(char ex

    17、pr) alias expr text;AST!(“(“ text “+” T.text “)“) opAdd(T)(T x) return null; AST!(“(“ text “*” T.text “)“) opMul(T)(T x) return null; AST!( text “(“ T.text “)“ ) opIndex(T)(T x) return null; char getPrecedence(char expr) char code = “typeof(“;for(int i=0; i=A mixin(getPrecedence(“A+B*C*D”) ) “A+(B*C

    18、)*D)”,Conclusion,Implementation and syntactic issues remain Syntax for runtime and compile-time reflection Macros, and an extended _traits syntax should help. How to clean up mixin(), yet retain its power? Yet perfectly optimal code is already possible. Libraries can perform optimisations previously required a compiler back-end.,


    注意事项

    本文(Truly Optimal CodeUsing a Metaprogramming Library.ppt)为本站会员(boatfragile160)主动上传,麦多课文档分享仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文档分享(点击联系客服),我们立即给予删除!




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
    备案/许可证编号:苏ICP备17064731号-1 

    收起
    展开