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

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/translate/translate-basis.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/translate/translate-basis.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1911 - (view) (download)

1 : jhr 180 (* translate-basis.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 180 * All rights reserved.
5 :     *
6 :     * Translation for basis operations in Simple AST to HighIL code
7 :     *)
8 :    
9 :     structure TranslateBasis : sig
10 :    
11 : jhr 189 (* translate(lhs, f, mvs, args) translates the application of f (specialized
12 :     * to the instantiated meta variables mvs) to a list of SSA assignments in
13 :     * reverse order.
14 :     *)
15 : jhr 180 val translate : (HighIL.var * Var.var * Types.meta_var list * HighIL.var list)
16 : jhr 1640 -> HighIL.assignment list
17 : jhr 180
18 :     end = struct
19 :    
20 : jhr 194 structure BV = BasisVars
21 : jhr 180 structure IL = HighIL
22 : jhr 392 structure DstTy = HighILTypes
23 : jhr 180 structure Op = HighOps
24 : jhr 183 structure Ty = Types
25 :     structure TU = TypeUtil
26 :     structure MV = MetaVar
27 : jhr 194 structure VTbl = Var.Tbl
28 : jhr 180
29 : jhr 1687 fun pruneTy tv = (case TU.prune(MV.toType tv)
30 :     of (ty as Ty.T_Var _) => raise Fail("unresolved type " ^ TU.toString ty)
31 : jhr 1640 | ty => TranslateTy.tr ty
32 :     (* end case *))
33 :    
34 : jhr 197 fun pruneDim d = (case TU.pruneDim d
35 :     of (Ty.DimConst n) => n
36 :     | d => raise Fail("unresolved dimension " ^ TU.dimToString d)
37 :     (* end case *))
38 :    
39 : jhr 407 fun pruneShape sv = (case TU.pruneShape(MV.toShape sv)
40 : jhr 426 of Ty.Shape dd => DstTy.tensorTy(List.map pruneDim dd)
41 : jhr 183 | shp => raise Fail("unresolved shape " ^ TU.shapeToString shp)
42 :     (* end case *))
43 :    
44 : jhr 426 fun dimVarToTensor dv = DstTy.tensorTy[pruneDim(MV.toDim dv)]
45 : jhr 1116 fun dimVarToMatrix dv = let
46 :     val d = pruneDim(MV.toDim dv)
47 :     in
48 :     DstTy.tensorTy[d, d] (* square matrix type *)
49 :     end
50 : jhr 407 fun shapeVarToTensor sv = pruneShape sv
51 : jhr 197
52 : jhr 1640 fun assign (y, rator, xs) = [IL.ASSGN(y, IL.OP(rator, xs))]
53 : jhr 407
54 : jhr 1640 fun basisFn name (y, [], xs) = [IL.ASSGN(y, IL.APPLY(name, xs))]
55 : jhr 1116
56 : jhr 407 fun simpleOp rator (y, [], xs) = assign (y, rator, xs)
57 :    
58 :     fun tensorOp rator (y, [sv], xs) = assign (y, rator(shapeVarToTensor sv), xs)
59 :    
60 :     fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs)
61 :    
62 : jhr 1116 fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), [])
63 : jhr 197
64 : jhr 1640 (* utility functions for synthesizing eigenvector/eigenvalue code *)
65 :     fun eigenVec (rator, dim) = let
66 :     val ty = DstTy.SeqTy(DstTy.realTy, dim)
67 :     in
68 :     fn (y, _, [m]) => let
69 :     val v = IL.Var.new("evals", ty)
70 :     in
71 :     [IL.MASSGN([v, y], rator, [m])]
72 :     end
73 :     end
74 :     fun eigenVal (rator, dim) = let
75 :     val ty = DstTy.SeqTy(DstTy.vecTy dim, dim)
76 :     in
77 :     fn (y, _, [m]) => let
78 :     val v = IL.Var.new("evecs", ty)
79 :     in
80 :     [IL.MASSGN([y, v], rator, [m])]
81 :     end
82 :     end
83 :    
84 : jhr 184 (* build a table that maps Basis variables to their translation functions *)
85 : jhr 1640 val tbl : ((IL.var * Ty.meta_var list * IL.var list) -> IL.assignment list) VTbl.hash_table = let
86 : jhr 194 val tbl = VTbl.mkTable (128, Fail "Translate table")
87 : jhr 180 in
88 : jhr 194 List.app (VTbl.insert tbl) [
89 : jhr 1688 (BV.at_Td, fn (y, [tv], args) => assign(y, Op.Prepend(pruneTy tv), args)),
90 :     (BV.at_dT, fn (y, [tv], args) => assign(y, Op.Append(pruneTy tv), args)),
91 : jhr 1687 (BV.at_dd, fn (y, [tv], args) => assign(y, Op.Concat(pruneTy tv), args)),
92 : jhr 392 (BV.lt_ii, simpleOp(Op.LT DstTy.IntTy)),
93 : jhr 1295 (BV.lt_rr, simpleOp(Op.LT DstTy.realTy)),
94 : jhr 392 (BV.lte_ii, simpleOp(Op.LTE DstTy.IntTy)),
95 : jhr 1295 (BV.lte_rr, simpleOp(Op.LTE DstTy.realTy)),
96 : jhr 392 (BV.gte_ii, simpleOp(Op.GTE DstTy.IntTy)),
97 : jhr 426 (BV.gte_rr, simpleOp(Op.GTE(DstTy.realTy))),
98 : jhr 392 (BV.gt_ii, simpleOp(Op.GT DstTy.IntTy)),
99 : jhr 426 (BV.gt_rr, simpleOp(Op.GT(DstTy.realTy))),
100 : jhr 392 (BV.equ_bb, simpleOp(Op.EQ DstTy.BoolTy)),
101 :     (BV.equ_ii, simpleOp(Op.EQ DstTy.IntTy)),
102 :     (BV.equ_ss, simpleOp(Op.EQ DstTy.StringTy)),
103 : jhr 426 (BV.equ_rr, simpleOp(Op.EQ(DstTy.realTy))),
104 : jhr 392 (BV.neq_bb, simpleOp(Op.NEQ DstTy.BoolTy)),
105 :     (BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)),
106 :     (BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)),
107 : jhr 426 (BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))),
108 : jhr 1116 (BV.add_ii, simpleOp(Op.Add DstTy.IntTy)),
109 :     (BV.add_tt, tensorOp Op.Add),
110 :     (BV.add_ff, fn (y, _, [f, g]) => assign(y, Op.AddField, [f, g])),
111 :     (BV.sub_ii, simpleOp(Op.Sub DstTy.IntTy)),
112 :     (BV.sub_tt, tensorOp Op.Sub),
113 :     (BV.sub_ff, fn (y, _, [f, g]) => assign(y, Op.SubField, [f, g])),
114 :     (BV.mul_ii, simpleOp(Op.Mul DstTy.IntTy)),
115 :     (BV.mul_rr, simpleOp(Op.Mul(DstTy.realTy))),
116 :     (BV.mul_rt, tensorOp Op.Scale),
117 :     (BV.mul_tr, fn (y, sv, [t, r]) => tensorOp Op.Scale (y, sv, [r, t])),
118 :     (BV.mul_rf, fn (y, _, [s, f]) => assign(y, Op.ScaleField, [s, f])),
119 :     (BV.mul_fr, fn (y, _, [f, s]) => assign(y, Op.ScaleField, [s, f])),
120 :     (BV.div_ii, simpleOp(Op.Div DstTy.IntTy)),
121 :     (BV.div_rr, simpleOp(Op.Div DstTy.realTy)),
122 :     (BV.div_tr, fn (y, [sv], [x, s]) => let
123 :     val one = IL.Var.new("one", DstTy.realTy)
124 :     val s' = IL.Var.new("sInv", DstTy.realTy)
125 :     in [
126 : jhr 1640 IL.ASSGN(one, IL.LIT(Literal.Float(FloatLit.one))),
127 :     IL.ASSGN(s', IL.OP(Op.Div DstTy.realTy, [one, s])),
128 :     IL.ASSGN(y, IL.OP(Op.Scale(shapeVarToTensor sv), [s', x]))
129 : jhr 1116 ] end),
130 :     (BV.div_fr, fn (y, _, [f, s]) => let
131 :     val one = IL.Var.new("one", DstTy.realTy)
132 :     val s' = IL.Var.new("sInv", DstTy.realTy)
133 :     in [
134 : jhr 1640 IL.ASSGN(one, IL.LIT(Literal.Float(FloatLit.one))),
135 :     IL.ASSGN(s', IL.OP(Op.Div DstTy.realTy, [one, s])),
136 :     IL.ASSGN(y, IL.OP(Op.ScaleField, [s', f]))
137 : jhr 1116 ] end),
138 :     (BV.exp_ri, simpleOp(Op.Power)),
139 :     (BV.exp_rr, basisFn ILBasis.pow),
140 :     (BV.convolve_vk, fn (y, [_, dv, _], xs) =>
141 :     assign(y, Op.Field(pruneDim(MV.toDim dv)), xs)),
142 :     (BV.convolve_kv, fn (y, [_, dv, _], [k, v]) =>
143 :     assign(y, Op.Field(pruneDim(MV.toDim dv)), [v, k])),
144 : jhr 392 (BV.neg_i, simpleOp(Op.Neg DstTy.IntTy)),
145 : jhr 194 (BV.neg_t, tensorOp Op.Neg),
146 : jhr 197 (BV.neg_f, fn (y, _, xs) => assign(y, Op.NegField, xs)),
147 : jhr 1687 (BV.op_probe, fn (y, [_, dv, sv], xs) =>
148 : jhr 1116 assign(y, Op.Probe(dimVarToTensor dv, shapeVarToTensor sv), xs)),
149 : jhr 197 (BV.op_D, fn (y, _, xs) => assign(y, Op.DiffField, xs)),
150 : jhr 1383 (BV.op_Dotimes, fn (y, _, xs) => assign(y, Op.DiffField, xs)),
151 : jhr 1116 (BV.op_norm, fn (y, [sv], xs) => (case shapeVarToTensor sv
152 :     of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, xs)
153 :     | ty => assign(y, Op.Norm ty, xs)
154 :     (* end case *))),
155 : jhr 194 (BV.op_not, simpleOp Op.Not),
156 : jhr 1116 (BV.fn_atan2, basisFn ILBasis.atan2),
157 :     (BV.fn_cos, basisFn ILBasis.cos),
158 :     (BV.op_cross, simpleOp Op.Cross),
159 :     (BV.op_outer, fn (y, [dv1, dv2], xs) => let
160 :     val d1 = pruneDim(MV.toDim dv1)
161 :     val d2 = pruneDim(MV.toDim dv2)
162 :     in
163 :     assign (y, Op.Outer(DstTy.tensorTy[d1, d2]), xs)
164 :     end),
165 :     (BV.op_inner, fn (y, [sh1, sh2, _], xs) => let
166 :     val ty1 as DstTy.TensorTy dd1 = pruneShape sh1
167 :     val ty2 as DstTy.TensorTy dd2 = pruneShape sh2
168 :     val rator = (case (dd1, dd2)
169 :     of ([d], [d']) => Op.Dot ty1
170 :     | ([d1], [d1', d2]) => Op.MulVecMat ty2
171 :     | ([d1, d2], [d2']) => Op.MulMatVec ty1
172 :     | ([d1, d2], [d2', d3]) => Op.MulMatMat(ty1, ty2)
173 :     | _ => raise Fail "unsupported inner-product type"
174 :     (* end case *))
175 :     in
176 :     assign (y, rator, xs)
177 :     end),
178 : jhr 407 (BV.fn_inside, fn (y, [_, dv, _], xs) =>
179 : jhr 1116 assign(y, Op.Inside(pruneDim(MV.toDim dv)), xs)),
180 : jhr 1295 (BV.clamp_rrr, simpleOp (Op.Clamp DstTy.realTy)),
181 :     (BV.clamp_vvv, vectorOp Op.Clamp),
182 : jhr 1116 (BV.lerp3, tensorOp Op.Lerp),
183 :     (BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) => let
184 :     val t1 = IL.Var.new("t1", DstTy.realTy)
185 :     val t2 = IL.Var.new("t2", DstTy.realTy)
186 :     val t3 = IL.Var.new("t3", DstTy.realTy)
187 :     in [
188 : jhr 1640 IL.ASSGN(t1, IL.OP(Op.Sub DstTy.realTy, [x, x0])),
189 :     IL.ASSGN(t2, IL.OP(Op.Sub DstTy.realTy, [x1, x0])),
190 :     IL.ASSGN(t3, IL.OP(Op.Div DstTy.realTy, [t1, t2])),
191 :     IL.ASSGN(y, IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3]))
192 : jhr 1116 ] end),
193 : jhr 1640 (BV.evals2x2, eigenVal (Op.Eigen2x2, 2)),
194 :     (BV.evals3x3, eigenVal (Op.Eigen3x3, 3)),
195 :     (BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)),
196 :     (BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)),
197 : jhr 1911 (BV.fn_fmod, basisFn ILBasis.fmod),
198 :     (BV.fn_log10, basisFn ILBasis.log10),
199 :     (BV.fn_ln, basisFn ILBasis.ln),
200 : jhr 263 (BV.fn_max, simpleOp Op.Max),
201 :     (BV.fn_min, simpleOp Op.Min),
202 : jhr 197 (BV.fn_modulate, vectorOp Op.Mul),
203 : jhr 1116 (BV.fn_normalize, vectorOp Op.Normalize),
204 : jhr 197 (BV.fn_principleEvec, vectorOp Op.PrincipleEvec),
205 : jhr 1116 (BV.fn_sin, basisFn ILBasis.sin),
206 :     (BV.fn_sqrt, basisFn ILBasis.sqrt),
207 :     (BV.fn_tan, basisFn ILBasis.tan),
208 :     (BV.fn_trace, fn (y, [dv], xs) => assign(y, Op.Trace(dimVarToMatrix dv), xs)),
209 : jhr 197 (BV.kn_bspln3, kernel Kernel.bspln3),
210 :     (BV.kn_bspln5, kernel Kernel.bspln5),
211 :     (BV.kn_ctmr, kernel Kernel.ctmr),
212 : jhr 1116 (BV.kn_c2ctmr, kernel Kernel.ctmr),
213 : jhr 197 (BV.kn_tent, kernel Kernel.tent),
214 : jhr 1116 (BV.kn_c1tent, kernel Kernel.tent),
215 :     (BV.i2r, simpleOp Op.IntToReal),
216 :     (BV.identity, fn (y, [dv], []) =>
217 :     assign(y, Op.Identity(pruneDim(MV.toDim dv)), [])),
218 :     (BV.zero, fn (y, [sv], []) =>
219 : jhr 1640 assign(y, Op.Zero(shapeVarToTensor sv), [])),
220 :     (BV.subscript, fn (y, [tv, dv], args) =>
221 :     assign(y,
222 : jhr 1687 Op.SeqSub(DstTy.SeqTy(pruneTy tv, pruneDim(MV.toDim dv))),
223 : jhr 1640 args))
224 : jhr 180 ];
225 :     tbl
226 :     end
227 :    
228 : jhr 194 fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
229 : jhr 197 of SOME transFn => transFn(y, mvs, xs)
230 : jhr 407 | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f)
231 : jhr 180 (* end case *))
232 : jhr 1116 handle ex => (print(concat["translate (", IL.Var.toString y, ", ",
233 :     Var.uniqueNameOf f, ", ...)\n"]); raise ex)
234 : jhr 180
235 :     end

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