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 2492 - (download) (annotate)
Tue Oct 22 15:25:50 2013 UTC (7 years, 9 months ago) by jhr
File size: 13832 byte(s)
  porting changes from trunk
(* 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 EinOp = Operators
    structure S = Specialize

    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
            DstTy.tensorTy[d, d]        (* square matrix type *)
    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), [])

  (* utility functions for synthesizing eigenvector/eigenvalue code *)
    fun eigenVec (rator, dim) = let
          val ty = DstTy.SeqTy(DstTy.realTy, dim)
            fn (y, _, [m]) => let
                val v = IL.Var.new("evals", ty)
                  [IL.MASSGN([v, y], rator, [m])]
    fun eigenVal (rator, dim) = let
          val ty = DstTy.SeqTy(DstTy.vecTy dim, dim)
            fn (y, _, [m]) => let
                val v = IL.Var.new("evecs", ty)
                  [IL.MASSGN([y, v], rator, [m])]

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

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

    fun simpleEinOp rator (y, _,xs) = assignEin(y, rator, xs)

    fun peelFieldSK SK = [2]  (*(case TU.pruneShape(MV.toShape SK)
            of Ty.Shape dd =>  List.map pruneDim dd
            | shp => raise Fail("unresolved shape " ^ TU.shapeToString shp)
            (* end case *))*)

    fun tensorEinOp einop (y, [shp], xs) = let
          val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
          val rator =S.transform(einop, [dd1], [])
            assignEin(y, rator,xs)

    fun scalarField (einop, paramarg, y, xs) = let
          val paramarg' = List.map dimVarToInt paramarg(*[2]*)
          val rator = S.transform(einop, [[]], paramarg')
            assignEin(y, rator, xs)

    fun singleField (SK, einop, paramarg, y, xs) = let
          val s=peelFieldSK SK
          val paramarg'= List.map dimVarToInt paramarg (*[2]*)
          val rator= S.transform(einop, [s], paramarg')
            assignEin(y, rator, xs)

(****MV.toDim returns int, and DstTy.TensorTy returns intlist *)

(*DK- SK-shape,NK-dim *)

(* 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
            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,             tensorEinOp EinOp.addTensor),
                (BV.add_ff,             fn (y, [_,NK,SK], xs) => singleField(SK,EinOp.addField, [NK,NK], y, xs)),
                (BV.add_fr,             fn (y, [_,NK], xs) => scalarField(EinOp.addTenField,[NK],y, xs)),
                (BV.add_rf,             fn (y, [_,NK], [s, f]) => scalarField(EinOp.addTenField,[NK],y,[f,s])),
                (BV.sub_ii,             simpleOp Op.ISub),
                (BV.sub_tt,             tensorEinOp EinOp.subTensor),
                (BV.sub_ff,             fn (y, [_,NK,SK], xs) => singleField(SK,EinOp.subField, [NK,NK],y,xs)),
                (BV.sub_fr,             fn (y, [_,NK], xs) => scalarField(EinOp.subField,[NK], y,xs)),
                (BV.sub_rf,             fn (y, [_,NK], [s, f]) => scalarField(EinOp.subField,[NK], y,[f, s])),
                (* CHANGE create negative subtraction*)
                (BV.mul_ii,             simpleOp Op.IMul),
                (BV.mul_rr,             fn (y,_,args) => assignEin(y, EinOp.prodScalar,args)),
                (BV.mul_rt,             tensorEinOp EinOp.scaleTensor),
                (BV.mul_tr,             fn (y, [shp], [t, r]) => let
                                          val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
                                          val rator = S.transform(EinOp.scaleTensor, [dd1], [])
                                            assignEin(y, rator,[r,t])
                (BV.mul_rf,             fn (y, [_,NK,SK], xs) => singleField(SK,EinOp.scaleField,[NK],y, xs)),
                (BV.mul_fr,             fn (y, [_,NK,SK], [f, s]) => singleField(SK,EinOp.scaleField, [NK],y,[s,f])),
                (BV.div_ii,             simpleOp Op.IDiv),
                (BV.div_rr,             fn (y,_,args) => assignEin(y, EinOp.divScalar,args)),
                (BV.div_tr,             tensorEinOp EinOp.divideTensor),
                (BV.div_fr,             fn (y, [_,NK,SK], xs) => singleField(SK,EinOp.divideField, [NK],y, xs)),
                (BV.exp_ri,             simpleOp(Op.Power)),
                (BV.exp_rr,             basisFn MathFuns.pow),
                (BV.curl2D,             simpleEinOp EinOp.Curl2d),
                (BV.curl3D,             simpleEinOp EinOp.Curl3d),
                (BV.convolve_vk,        fn (y, [_, NK, SK], xs) => singleField(SK,EinOp.conv, [NK],y,xs)),
                (BV.convolve_kv,        fn (y, [_, NK, SK], [k, v]) => singleField(SK,EinOp.conv, [NK],y,[v,k])),
                (BV.neg_i,              simpleOp Op.INeg),
                (BV.neg_t,              tensorEinOp EinOp.negTensor),
                (BV.neg_f,              fn (y, [_,NK,SK], xs) => singleField(SK,EinOp.negField, [NK],y,xs)),
                (BV.op_probe,           fn (y, [_, NK, SK], xs) => let
					  val dv=dimVarToTensor NK  (*Field*)
					  val sv=shapeVarToTensor SK (*Position*)
					  val dv'=peelFieldSK dv
					  val sv'=peelFieldSK sv
					  val einop= S.transform(EinOp.probe,[sv',dv'],[2])
					    assignEin(y, einop,xs)
                (BV.op_D,               fn (y, [_, NK], xs) => scalarField(EinOp.Grad,[NK], y,xs)),
                (BV.op_Dotimes,         fn (y, [_, NK, SK, NK2], xs) => singleField(SK,EinOp.Divergence,[NK], y,xs)),
                (BV.op_norm,            fn (y, [sv], xs) => (case shapeVarToTensor sv
                                           of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, xs)
                                            | ty => assign(y, Op.Norm ty, xs)
                                          (* end case *))),
                (BV.op_not,             simpleOp Op.Not),
                (BV.op_cross,           simpleEinOp EinOp.crossProduct),
                (BV.op_outer,		fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
(* CHECK: should it be [[d1], [d2]]? *)
					  assignEin(y, S.transform(EinOp.outerProduct, [[d1, d2]], []), xs)),
                (BV.op_inner,		fn (y, [sh1, sh2, _], xs) => let
					  val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
					  val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
					  val alpha= List.take(dd1, length(dd1)-1)
					  val beta=  tl(dd2)
					  val i =hd(dd2)
					  val ilist=  [alpha, beta, [i]]
                                            assignEin(y, S.transform(EinOp.innerProduct, ilist,[]),xs)
                (BV.op_colon,           fn (y, [sh1, sh2, _], xs) => let
					  val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
					  val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
					  val i::j::beta = dd2
					  val alpha = List.take(dd1, length(dd1)-2)
					  val ilist = [alpha, beta, [i], [j]]
					    assignEin(y, S.transform(EinOp.doubleDot, ilist,[]),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 
                    (*assignEin(y, EinOp.subScalar,[x,x0])),*)
                                          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 [
                                            assignEin2(t1, EinOp.subScalar,[x,x0]),
                                            assignEin2(t2, EinOp.subScalar,[x1,x0]),
                                            assignEin2(t3, EinOp.divScalar,[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),
(* FIXME: modulate is vector * vector pointwise multiplication *)
                (BV.fn_modulate,        fn (y, _, xs) => assign (y, Op.IMul, xs)),
                (BV.fn_normalize,       vectorOp Op.Normalize),
                (BV.fn_principleEvec,   vectorOp Op.PrincipleEvec),
                (BV.fn_trace,           fn (y, [Ty.DIM d], xs) =>
					  assignEin(y,S.transform(EinOp.trace, [[d]],[]), xs)),
                (BV.fn_transpose,       fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
					  assignEin(y, S.transform(EinOp.transpose, [[d1],[d2]],[]), 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, S.transform(EinOp.identity, [[d],[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;

    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)


ViewVC Help
Powered by ViewVC 1.0.0