Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/charisee/src/compiler/translate/translate-basis.sml
ViewVC logotype

View of /branches/charisee/src/compiler/translate/translate-basis.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3112 - (download) (annotate)
Wed Mar 18 19:28:13 2015 UTC (4 years, 4 months ago) by cchiw
File size: 18691 byte(s)
sqrt fields
(* translate-basis.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * Translation for basis operations in Simple AST to HighIL code
 *)

structure TranslateBasis : sig

  (* translate(lhs, f, mvs, args) translates the application of f (specialized
   * to the instantiated meta variables mvs) to a list of SSA assignments in
   * reverse order.
   *)
    val translate : (HighIL.var * Var.var * SimpleTypes.meta_arg list * HighIL.var list)
          -> HighIL.assignment list

  end = struct

    structure BV = BasisVars
    structure IL = HighIL
    structure DstTy = HighILTypes
    structure Op = HighOps
    structure Ty = SimpleTypes
    structure VTbl = Var.Tbl
    structure Mk = MkOperators

    fun trType (Ty.TY ty) = TranslateTy.tr ty
      | trType _ = raise Fail "expected type"
    fun dimVarToInt (Ty.DIM d) = d
      | dimVarToInt _ = raise Fail "expected dim"
    fun dimVarToTensor dv = DstTy.tensorTy[dimVarToInt dv]
    fun dimVarToMatrix dv = let
          val d = dimVarToInt dv
          in
            DstTy.tensorTy[d, d]        (* square matrix type *)
          end
    fun shapeVarToTensor (Ty.SHAPE shp) = DstTy.tensorTy shp
      | shapeVarToTensor _ = raise Fail "expected shape"

    fun assign (y, rator, xs) = [IL.ASSGN(y, IL.OP(rator, xs))]

    fun basisFn name (y, [], xs) = [IL.ASSGN(y, IL.APPLY(name, xs))]

    fun simpleOp rator (y, [], xs) = assign (y, rator, xs)

    fun tensorOp rator (y, [sv], xs) = assign (y, rator(shapeVarToTensor sv), xs)

    fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs)

    fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), [])

    val cnt = ref 0
    fun genName prefix = let
        val n = !cnt
        in
            cnt := n+1;
            String.concat[prefix, "_", Int.toString n]
        end
  (* utility functions for synthesizing eigenvector/eigenvalue code *)
    fun eigenVec (rator, dim) = let
          val ty = DstTy.SeqTy(DstTy.realTy, dim)
          in
            fn (y, _, [m]) => let
                val v = IL.Var.new("evals", ty)
                in
                  [IL.MASSGN([v, y], rator, [m])]
                end
          end
    fun eigenVal (rator, dim) = let
          val ty = DstTy.SeqTy(DstTy.vecTy dim, dim)
          in
            fn (y, _, [m]) => let
                val v = IL.Var.new("evecs", ty)
                in
                  [IL.MASSGN([y, v], rator, [m])]
                end
          end

    fun assignEin (y, rator, xs) = IL.ASSGN(y, IL.EINAPP(rator, xs))

    fun  simpleEOp rator (y, _,xs) = [assignEin(y, rator, xs)]

(* shape is an int list, DIM is in int|variable, k-level of differntiation *)

 (* build a table that maps Basis variables to their translation functions *)
    val tbl : ((IL.var * Ty.meta_arg list * IL.var list) -> IL.assignment list) VTbl.hash_table = let
          val tbl = VTbl.mkTable (128, Fail "Translate table")
          val insert = VTbl.insert tbl
          in
            List.app insert [
                (BV.lt_ii,              simpleOp(Op.LT DstTy.IntTy)),
                (BV.lt_rr,              simpleOp(Op.LT DstTy.realTy)),
                (BV.lte_ii,             simpleOp(Op.LTE DstTy.IntTy)),
                (BV.lte_rr,             simpleOp(Op.LTE DstTy.realTy)),
                (BV.gte_ii,             simpleOp(Op.GTE DstTy.IntTy)),
                (BV.gte_rr,             simpleOp(Op.GTE(DstTy.realTy))),
                (BV.gt_ii,              simpleOp(Op.GT DstTy.IntTy)),
                (BV.gt_rr,              simpleOp(Op.GT(DstTy.realTy))),
                (BV.equ_bb,             simpleOp(Op.EQ DstTy.BoolTy)),
                (BV.equ_ii,             simpleOp(Op.EQ DstTy.IntTy)),
                (BV.equ_ss,             simpleOp(Op.EQ DstTy.StringTy)),
                (BV.equ_rr,             simpleOp(Op.EQ(DstTy.realTy))),
                (BV.neq_bb,             simpleOp(Op.NEQ DstTy.BoolTy)),
                (BV.neq_ii,             simpleOp(Op.NEQ DstTy.IntTy)),
                (BV.neq_ss,             simpleOp(Op.NEQ DstTy.StringTy)),
                (BV.neq_rr,             simpleOp(Op.NEQ(DstTy.realTy))),
                (BV.add_ii,             simpleOp Op.IAdd),
                (BV.add_tt,             fn (y, [shp], xs) => let
                                        val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
                                        val rator = Mk.addTT(dd1)
                                        in
                                            [assignEin(y, rator,xs)]
                                        end),
                (BV.add_ff,             fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
                                            [assignEin(y, Mk.addFF(d, dd),xs)]),

                (BV.add_ft,             fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [f,s]) =>
                                            [assignEin(y, Mk.addTF(d,dd),[s,f])]),

                (BV.add_tf,             fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
                                            [assignEin(y, Mk.addTF(d,dd),xs)]),
                (BV.sub_ii,             simpleOp Op.ISub),
                (BV.sub_tt,             fn (y, [shp], xs) => let
                                        val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
                                        val rator = Mk.subTT dd1
                                        in
                                            [assignEin(y, rator,xs)]
                                        end),
                (BV.sub_ff,             fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
                                              [assignEin(y, Mk.subFF(d, dd),xs)]),
                (BV.sub_ft,             fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [x1,x2]) =>
                                            [assignEin(y, Mk.subFT(d,dd),[x2,x1])]),
                (BV.sub_tf,             fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
                                                [assignEin(y, Mk.subTF(d,dd),xs)]),
                (BV.mul_ii,             simpleOp Op.IMul),
                (BV.mul_rr,             fn (y,_,args) => [assignEin(y, Mk.mulRR,args)]),
                (BV.mul_rt,             fn (y, [shp], xs) => let
                                        val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
                                        val rator = Mk.mulRT(dd1)
                                        in
                                            [assignEin(y, rator,xs)]
                                        end),
                (BV.mul_tr,             fn (y, [shp], [t, r]) => let
                                          val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
                                          val rator = Mk.mulRT dd1
                                          in
                                            [assignEin(y, rator,[r,t])]
                                          end),
                (BV.mul_rf,             fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
                                            [assignEin(y, Mk.mulRF(d, dd),xs)]),
                (BV.mul_fr,             fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [f, s]) =>
                                            [assignEin(y, Mk.mulRF(d, dd),[s,f])]),
                (BV.mul_ss,             fn (y, [_,Ty.DIM d], xs) =>
                                        [assignEin(y, Mk.mulSS d,xs)]),
                (BV.mul_sf,             fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
                                            [assignEin(y, Mk.mulSF(d,dd),xs)]),
                (BV.mul_fs,             fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [s,f]) =>
                                        [assignEin(y, Mk.mulSF(d,dd),[f,s])]),
                (BV.mul_st,             fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
                    [assignEin(y, Mk.mulST(d,dd),List.rev xs)]),
                (BV.mul_ts,             fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
                    [assignEin(y, Mk.mulST(d,dd),xs)]),
                (BV.div_ii,             simpleOp Op.IDiv),
                (BV.div_rr,             fn (y,_,args) => [assignEin(y, Mk.divRR,args)]),
                (BV.div_tr,             fn (y, [shp], xs) => let
                                        val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
                                        val rator = Mk.divTR dd1
                                        in
                                            [assignEin(y, rator,xs)]
                                        end),
                (BV.div_fr,             fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
                                            [assignEin(y, Mk.divFR(d, dd),xs)]),
                (BV.div_fs,             fn (y, [_,_,Ty.DIM d, Ty.SHAPE dd], xs) =>
                                        [assignEin(y, Mk.divFS(d, dd),xs)]),
                (BV.div_ss,             fn (y, [_,Ty.DIM d], xs) =>
                                            [assignEin(y, Mk.divSS d,xs)]),
                (BV.exp_ri,             simpleOp(Op.Power)),
                (BV.exp_rr,             basisFn MathFuns.pow),
                (BV.curl2D,              simpleEOp Mk.curl2d),
                (BV.curl3D,              simpleEOp Mk.curl3d),
                (BV.convolve_vk,        fn (y, [_, Ty.DIM d,  Ty.SHAPE dd], xs) =>
                                           [assignEin(y, Mk.conv(d, dd),xs)]),
                (BV.convolve_kv,        fn (y, [_, Ty.DIM d,  Ty.SHAPE dd], [k, v]) =>
                                             [assignEin(y, Mk.conv(d, dd),[v,k])]),
                (BV.neg_i,              simpleOp Op.INeg),
                (BV.neg_t,              fn (y, [shp], xs) => let
                                        val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
                                        val rator = Mk.negTT dd1
                                        in
                                            [assignEin(y, rator,xs)]
                                        end),
                (BV.neg_f,              fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
                                        [assignEin(y, Mk.negFF(d, dd),xs)]),
                (BV.op_probe,           fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) => 
					    [assignEin(y, (Mk.probe(dd,d)),xs)]),
                (BV.op_D,               fn (y, [_, Ty.DIM d], xs) =>  (case d
                                            of 2=>[assignEin(y, Mk.grad([d]),xs)]
                                            |  3=>[assignEin(y, Mk.grad([d]),xs)]
                                            | _ => raise Fail "unsupported gradient"
                                            (*end case*))),
                (BV.op_Dotimes,         fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) =>
                                            [assignEin(y, Mk.dotimes(d1, dd@[d2]),xs)]),
                (BV.op_Ddot,         fn (y, [_, Ty.DIM d1,  Ty.SHAPE dd, Ty.DIM d2], xs) =>
                                        [assignEin(y, Mk.divergence(d1, dd),xs)] ),
                (BV.op_norm_t,            fn (y, [sv], xs) =>  assign(y, Op.Norm(shapeVarToTensor sv), xs)),
                (BV.op_norm_f,            fn (y, [ _,Ty.DIM d1, Ty.SHAPE dd], xs) =>
                                                    [assignEin(y, Mk.magnitudeFF(d1, dd),xs@xs)]),
                (BV.op_not,             simpleOp Op.Not),
                (BV.op_cross3_tt,           simpleEOp Mk.cross3TT),
                (BV.op_cross2_tt,          simpleEOp Mk.cross2TT),
                (BV.op_cross2_ff,       simpleEOp Mk.cross2FF),
                (BV.op_cross3_ff,       simpleEOp Mk.cross3FF),
                (BV.op_outer_tt,           fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
                                            [assignEin(y, (Mk.outerTT(d1, d2)), xs)]),
                (BV.op_outer_ff,      fn (y, [_, Ty.DIM d1,Ty.DIM i,Ty.DIM j], xs)=>
                                            [assignEin(y, Mk.outerFF(d1,i,j), xs)]),
                (BV.op_inner_tt,           fn (y, [sh1, sh2, _], xs) => let
                                            val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
                                            val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
                                        in
                                            [assignEin(y, (Mk.innerTT(dd1,dd2)),xs)]
                                        end),

                (BV.op_inner_tf,      fn (y,  [_,Ty.DIM d,sh1,Ty.SHAPE dd2,_], xs) =>let
                                            val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
                                        in 
                                                [assignEin(y, Mk.innerTF(dd1,d,dd2),xs)]
                                        end ),
                (BV.op_inner_ft,      fn (y,  [_,Ty.DIM d,Ty.SHAPE dd1,sh2,_], xs) =>let
                                            val ty1 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
                                        in
                                                [assignEin(y, Mk.innerFT(dd1,d,dd2),xs)]
                                        end ),

                (BV.op_inner_ff,      fn (y,  [_,_,Ty.DIM d,Ty.SHAPE dd1,Ty.SHAPE dd2,_], xs) =>
                                            [assignEin(y, Mk.innerFF(dd1,d,dd2),xs)]),

                (BV.op_colon_tt,           fn (y, [sh1, sh2, _], xs) => let
                                            val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
                                            val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
                                        in
                                            [assignEin(y, (Mk.colonTT(dd1,dd2)),xs)]
                                        end),
                (BV.op_colon_ff,       fn (y, [_,Ty.SHAPE dd1,_,Ty.SHAPE dd2, _], xs) =>
                                            [assignEin(y, (Mk.colonFF(dd1,dd2)),xs)]),
                (BV.fn_inside,          fn (y, [_, Ty.DIM d, _], xs) => assign(y, Op.Inside d, xs)),
                (BV.clamp_rrr,          simpleOp (Op.Clamp DstTy.realTy)),
                (BV.clamp_vvv,          vectorOp Op.Clamp),
                (BV.lerp3,              tensorOp Op.Lerp),
                (BV.lerp5,              fn (y, [sv], [a, b, x0, x, x1]) => let
                                          val t1 = IL.Var.new("t1", DstTy.realTy)
                                          val t2 = IL.Var.new("t2", DstTy.realTy)
                                          val t3 = IL.Var.new("t3", DstTy.realTy)
                                          in [
                                            assignEin(t1, Mk.subRR,[x,x0]),
                                            assignEin(t2, Mk.subRR,[x1,x0]),
                                            assignEin(t3, Mk.divRR,[t1,t2]),
                                            IL.ASSGN(y,  IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3]))
                                          ] end),
                (BV.evals2x2,           eigenVal (Op.Eigen2x2, 2)),
                (BV.evals3x3,           eigenVal (Op.Eigen3x3, 3)),
                (BV.evecs2x2,           eigenVec (Op.Eigen2x2, 2)),
                (BV.evecs3x3,           eigenVec (Op.Eigen3x3, 3)),
                (BV.fn_max,             simpleOp Op.Max),
                (BV.fn_min,             simpleOp Op.Min),
                (*  modulate is vector * vector pointwise multiplication *)
                (BV.fn_modulate,        fn (y,[Ty.DIM dd1], xs) =>
                                [assignEin(y, (Mk.modulate dd1),xs)]),
                (BV.fn_normalize_t,   vectorOp Op.Normalize),
                (BV.fn_normalize_f,             fn (y, [ _,Ty.DIM d1, Ty.SHAPE dd], xs) =>
                    (case dd
                        of []   => [assignEin(y, Mk.normalizeFF(d1, []),xs)]
                        | _     => [assignEin(y, Mk.normalizeFF(d1, dd),xs@xs)]
                    (* end case *))),
                (BV.fn_principleEvec,   vectorOp Op.PrincipleEvec),
                (BV.fn_trace_t,           fn (y, [Ty.DIM d], xs) =>
					  [assignEin(y,(Mk.traceT d), xs)]),
                (BV.fn_trace_f,           fn (y, [_,Ty.DIM d,Ty.SHAPE dd], xs) =>
                        [assignEin(y,Mk.traceF(d,dd), xs)]),
                (BV.fn_transpose_t,       fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
					  [assignEin(y, (Mk.transposeT [d1,d2]), xs)]),
                (BV.fn_transpose_f,  fn (y, [_,Ty.DIM d1, Ty.DIM d2,Ty.DIM d3], xs) =>
                    [assignEin(y, (Mk.transposeF (d1,d2,d3)), xs)]),
                (BV.fn_det_t2,        simpleEOp (Mk.detT2)),
                (BV.fn_det_f2,        simpleEOp (Mk.detF2)),
                (BV.fn_det_f3,        simpleEOp (Mk.detF3)),
                (BV.fn_sqrt_f,    fn (y, [_,Ty.DIM d1], xs) =>
                    [assignEin(y, Mk.sqrtFF d1, xs)]),
                (BV.fn_sqrt_t,    fn (y, _, xs) =>
                                    assign(y, Op.Sqrt,xs)),
                (BV.kn_bspln3,          kernel Kernel.bspln3),
                (BV.kn_bspln5,          kernel Kernel.bspln5),
                (BV.kn_ctmr,            kernel Kernel.ctmr),
                (BV.kn_c2ctmr,          kernel Kernel.ctmr),
                (BV.kn_c4hexic,         kernel Kernel.c4hexic),
                (BV.kn_tent,            kernel Kernel.tent),
                (BV.kn_c1tent,          kernel Kernel.tent),
                (BV.i2r,                simpleOp Op.IntToReal),
                (BV.identity,           fn (y, [Ty.DIM d], xs) =>
					  [assignEin(y, (Mk.identity d), xs)]),
                (BV.zero,               fn (y, [sv], []) =>
                                          assign(y, Op.Zero(shapeVarToTensor sv), [])),
                (BV.subscript,          fn (y, [ty, Ty.DIM d], xs) =>
					  assign (y, Op.SeqSub(DstTy.SeqTy(trType ty, d)), xs))
(*,
                  (BV.dynSubscript,     fn (y, [tv], args) =>
                                          assign(y, Op.SeqSub(DstTy.DynSeqTy(pruneTy tv)), args))
*)
                ];
          (* add C math functions *)
            List.app (fn (n, x) => insert(x, basisFn n)) BV.mathFns;
            tbl
          end

    fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
           of SOME transFn => transFn(y, mvs, xs)
            | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f)
          (* end case *))
handle ex => (print(concat["translate (", IL.Var.toString y, ", ",
Var.uniqueNameOf f, ", ...)\n"]); raise ex)

  end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0