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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2358 - (view) (download)
Original Path: trunk/src/compiler/translate/translate-basis.sml

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 2356 -> 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 2358 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 : jhr 2356 of (Ty.DimConst n) => n
36 :     | d => raise Fail("unresolved dimension " ^ TU.dimToString d)
37 :     (* end case *))
38 : jhr 197
39 : jhr 407 fun pruneShape sv = (case TU.pruneShape(MV.toShape sv)
40 : jhr 2356 of Ty.Shape dd => DstTy.tensorTy(List.map pruneDim dd)
41 :     | shp => raise Fail("unresolved shape " ^ TU.shapeToString shp)
42 :     (* end case *))
43 : jhr 183
44 : jhr 2358 fun dimVarToInt dv = pruneDim(MV.toDim dv)
45 :     fun dimVarToTensor dv = DstTy.tensorTy[dimVarToInt dv]
46 : jhr 1116 fun dimVarToMatrix dv = let
47 : jhr 2358 val d = dimVarToInt dv
48 : jhr 2356 in
49 :     DstTy.tensorTy[d, d] (* square matrix type *)
50 :     end
51 : jhr 407 fun shapeVarToTensor sv = pruneShape sv
52 : jhr 197
53 : jhr 1640 fun assign (y, rator, xs) = [IL.ASSGN(y, IL.OP(rator, xs))]
54 : jhr 407
55 : jhr 1640 fun basisFn name (y, [], xs) = [IL.ASSGN(y, IL.APPLY(name, xs))]
56 : jhr 1116
57 : jhr 407 fun simpleOp rator (y, [], xs) = assign (y, rator, xs)
58 :    
59 :     fun tensorOp rator (y, [sv], xs) = assign (y, rator(shapeVarToTensor sv), xs)
60 :    
61 :     fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs)
62 :    
63 : jhr 1116 fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), [])
64 : jhr 197
65 : jhr 1640 (* utility functions for synthesizing eigenvector/eigenvalue code *)
66 :     fun eigenVec (rator, dim) = let
67 :     val ty = DstTy.SeqTy(DstTy.realTy, dim)
68 :     in
69 :     fn (y, _, [m]) => let
70 :     val v = IL.Var.new("evals", ty)
71 :     in
72 :     [IL.MASSGN([v, y], rator, [m])]
73 :     end
74 :     end
75 :     fun eigenVal (rator, dim) = let
76 :     val ty = DstTy.SeqTy(DstTy.vecTy dim, dim)
77 :     in
78 :     fn (y, _, [m]) => let
79 :     val v = IL.Var.new("evecs", ty)
80 :     in
81 :     [IL.MASSGN([y, v], rator, [m])]
82 :     end
83 :     end
84 :    
85 : jhr 184 (* build a table that maps Basis variables to their translation functions *)
86 : jhr 1640 val tbl : ((IL.var * Ty.meta_var list * IL.var list) -> IL.assignment list) VTbl.hash_table = let
87 : jhr 2356 val tbl = VTbl.mkTable (128, Fail "Translate table")
88 :     val insert = VTbl.insert tbl
89 :     in
90 :     List.app insert [
91 :     (BV.lt_ii, simpleOp(Op.LT DstTy.IntTy)),
92 :     (BV.lt_rr, simpleOp(Op.LT DstTy.realTy)),
93 :     (BV.lte_ii, simpleOp(Op.LTE DstTy.IntTy)),
94 :     (BV.lte_rr, simpleOp(Op.LTE DstTy.realTy)),
95 :     (BV.gte_ii, simpleOp(Op.GTE DstTy.IntTy)),
96 :     (BV.gte_rr, simpleOp(Op.GTE(DstTy.realTy))),
97 :     (BV.gt_ii, simpleOp(Op.GT DstTy.IntTy)),
98 :     (BV.gt_rr, simpleOp(Op.GT(DstTy.realTy))),
99 :     (BV.equ_bb, simpleOp(Op.EQ DstTy.BoolTy)),
100 :     (BV.equ_ii, simpleOp(Op.EQ DstTy.IntTy)),
101 :     (BV.equ_ss, simpleOp(Op.EQ DstTy.StringTy)),
102 :     (BV.equ_rr, simpleOp(Op.EQ(DstTy.realTy))),
103 :     (BV.neq_bb, simpleOp(Op.NEQ DstTy.BoolTy)),
104 :     (BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)),
105 :     (BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)),
106 :     (BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))),
107 :     (BV.add_ii, simpleOp(Op.Add DstTy.IntTy)),
108 :     (BV.add_tt, tensorOp Op.Add),
109 :     (BV.add_ff, fn (y, _, [f, g]) => assign(y, Op.AddField, [f, g])),
110 : jhr 2358 (BV.add_fr, fn (y, _, [f, s]) => assign(y, Op.OffsetField, [f, s])),
111 :     (BV.add_rf, fn (y, _, [s, f]) => assign(y, Op.OffsetField, [f, s])),
112 : jhr 2356 (BV.sub_ii, simpleOp(Op.Sub DstTy.IntTy)),
113 :     (BV.sub_tt, tensorOp Op.Sub),
114 :     (BV.sub_ff, fn (y, _, [f, g]) => assign(y, Op.SubField, [f, g])),
115 : jhr 2358 (BV.sub_fr, fn (y, _, [f, s]) => let
116 :     val s' = IL.Var.copy s
117 :     in [
118 :     IL.ASSGN(s', IL.OP(Op.Neg DstTy.realTy, [s])),
119 :     IL.ASSGN(y, IL.OP(Op.OffsetField, [f, s']))
120 :     ] end),
121 :     (BV.sub_rf, fn (y, _, [s, f]) => let
122 :     val f' = IL.Var.copy f
123 :     in [
124 :     IL.ASSGN(f', IL.OP(Op.NegField, [f])),
125 :     IL.ASSGN(y, IL.OP(Op.OffsetField, [f', s]))
126 :     ] end),
127 : jhr 2356 (BV.mul_ii, simpleOp(Op.Mul DstTy.IntTy)),
128 :     (BV.mul_rr, simpleOp(Op.Mul(DstTy.realTy))),
129 :     (BV.mul_rt, tensorOp Op.Scale),
130 :     (BV.mul_tr, fn (y, sv, [t, r]) => tensorOp Op.Scale (y, sv, [r, t])),
131 :     (BV.mul_rf, fn (y, _, [s, f]) => assign(y, Op.ScaleField, [s, f])),
132 :     (BV.mul_fr, fn (y, _, [f, s]) => assign(y, Op.ScaleField, [s, f])),
133 :     (BV.div_ii, simpleOp(Op.Div DstTy.IntTy)),
134 :     (BV.div_rr, simpleOp(Op.Div DstTy.realTy)),
135 :     (BV.div_tr, fn (y, [sv], [x, s]) => let
136 :     val one = IL.Var.new("one", DstTy.realTy)
137 :     val s' = IL.Var.new("sInv", DstTy.realTy)
138 :     in [
139 :     IL.ASSGN(one, IL.LIT(Literal.Float(FloatLit.one))),
140 :     IL.ASSGN(s', IL.OP(Op.Div DstTy.realTy, [one, s])),
141 :     IL.ASSGN(y, IL.OP(Op.Scale(shapeVarToTensor sv), [s', x]))
142 :     ] end),
143 :     (BV.div_fr, fn (y, _, [f, s]) => let
144 :     val one = IL.Var.new("one", DstTy.realTy)
145 :     val s' = IL.Var.new("sInv", DstTy.realTy)
146 :     in [
147 :     IL.ASSGN(one, IL.LIT(Literal.Float(FloatLit.one))),
148 :     IL.ASSGN(s', IL.OP(Op.Div DstTy.realTy, [one, s])),
149 :     IL.ASSGN(y, IL.OP(Op.ScaleField, [s', f]))
150 :     ] end),
151 :     (BV.exp_ri, simpleOp(Op.Power)),
152 :     (BV.exp_rr, basisFn MathFuns.pow),
153 : jhr 2358 (BV.curl2D, fn (y, _, xs) => assign(y, Op.CurlField 2, xs)),
154 :     (BV.curl3D, fn (y, _, xs) => assign(y, Op.CurlField 3, xs)),
155 : jhr 2356 (BV.convolve_vk, fn (y, [_, dv, _], xs) =>
156 :     assign(y, Op.Field(pruneDim(MV.toDim dv)), xs)),
157 :     (BV.convolve_kv, fn (y, [_, dv, _], [k, v]) =>
158 :     assign(y, Op.Field(pruneDim(MV.toDim dv)), [v, k])),
159 :     (BV.neg_i, simpleOp(Op.Neg DstTy.IntTy)),
160 :     (BV.neg_t, tensorOp Op.Neg),
161 :     (BV.neg_f, fn (y, _, xs) => assign(y, Op.NegField, xs)),
162 :     (BV.op_probe, fn (y, [_, dv, sv], xs) =>
163 :     assign(y, Op.Probe(dimVarToTensor dv, shapeVarToTensor sv), xs)),
164 :     (BV.op_D, fn (y, _, xs) => assign(y, Op.DiffField, xs)),
165 :     (BV.op_Dotimes, fn (y, _, xs) => assign(y, Op.DiffField, xs)),
166 :     (BV.op_norm, fn (y, [sv], xs) => (case shapeVarToTensor sv
167 :     of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, xs)
168 :     | ty => assign(y, Op.Norm ty, xs)
169 :     (* end case *))),
170 :     (BV.op_not, simpleOp Op.Not),
171 :     (BV.op_cross, simpleOp Op.Cross),
172 :     (BV.op_outer, fn (y, [dv1, dv2], xs) => let
173 :     val d1 = pruneDim(MV.toDim dv1)
174 :     val d2 = pruneDim(MV.toDim dv2)
175 :     in
176 :     assign (y, Op.Outer(DstTy.tensorTy[d1, d2]), xs)
177 :     end),
178 :     (BV.op_inner, fn (y, [sh1, sh2, _], xs) => let
179 :     val ty1 as DstTy.TensorTy dd1 = pruneShape sh1
180 :     val ty2 as DstTy.TensorTy dd2 = pruneShape sh2
181 :     val rator = (case (dd1, dd2)
182 :     of ([d], [d']) => Op.Dot ty1
183 :     | ([d1], [d1', d2]) => Op.MulVecMat ty2
184 :     | ([d1, d2], [d2']) => Op.MulMatVec ty1
185 :     | ([d1, d2], [d2', d3]) => Op.MulMatMat(ty1, ty2)
186 : jhr 2358 | ([d1], [d1', d2, d3]) => Op.MulVecTen3 ty2
187 :     | ([d1, d2, d3], [d3']) => Op.MulTen3Vec ty1
188 :     | _ => raise Fail(concat[
189 :     "unsupported inner-product: ",
190 :     DstTy.toString ty1, " * ", DstTy.toString ty2
191 :     ])
192 : jhr 2356 (* end case *))
193 :     in
194 :     assign (y, rator, xs)
195 :     end),
196 : jhr 2358 (BV.op_colon, fn (y, [sh1, sh2, _], xs) => let
197 :     val ty1 as DstTy.TensorTy dd1 = pruneShape sh1
198 :     val ty2 as DstTy.TensorTy dd2 = pruneShape sh2
199 :     in
200 :     assign (y, Op.ColonMul(ty1, ty2), xs)
201 :     end),
202 : jhr 2356 (BV.fn_inside, fn (y, [_, dv, _], xs) =>
203 :     assign(y, Op.Inside(pruneDim(MV.toDim dv)), xs)),
204 :     (BV.clamp_rrr, simpleOp (Op.Clamp DstTy.realTy)),
205 :     (BV.clamp_vvv, vectorOp Op.Clamp),
206 :     (BV.lerp3, tensorOp Op.Lerp),
207 :     (BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) => let
208 :     val t1 = IL.Var.new("t1", DstTy.realTy)
209 :     val t2 = IL.Var.new("t2", DstTy.realTy)
210 :     val t3 = IL.Var.new("t3", DstTy.realTy)
211 :     in [
212 :     IL.ASSGN(t1, IL.OP(Op.Sub DstTy.realTy, [x, x0])),
213 :     IL.ASSGN(t2, IL.OP(Op.Sub DstTy.realTy, [x1, x0])),
214 :     IL.ASSGN(t3, IL.OP(Op.Div DstTy.realTy, [t1, t2])),
215 :     IL.ASSGN(y, IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3]))
216 :     ] end),
217 :     (BV.evals2x2, eigenVal (Op.Eigen2x2, 2)),
218 :     (BV.evals3x3, eigenVal (Op.Eigen3x3, 3)),
219 :     (BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)),
220 :     (BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)),
221 :     (BV.fn_max, simpleOp Op.Max),
222 :     (BV.fn_min, simpleOp Op.Min),
223 :     (BV.fn_modulate, vectorOp Op.Mul),
224 :     (BV.fn_normalize, vectorOp Op.Normalize),
225 :     (BV.fn_principleEvec, vectorOp Op.PrincipleEvec),
226 :     (BV.fn_trace, fn (y, [dv], xs) => assign(y, Op.Trace(dimVarToMatrix dv), xs)),
227 : jhr 2358 (BV.fn_transpose, fn (y, [dv1, dv2], xs) =>
228 :     assign(y, Op.Transpose(dimVarToInt dv1, dimVarToInt dv2), xs)),
229 : jhr 2356 (BV.kn_bspln3, kernel Kernel.bspln3),
230 :     (BV.kn_bspln5, kernel Kernel.bspln5),
231 :     (BV.kn_ctmr, kernel Kernel.ctmr),
232 :     (BV.kn_c2ctmr, kernel Kernel.ctmr),
233 : jhr 2358 (BV.kn_c4hexic, kernel Kernel.c4hexic),
234 : jhr 2356 (BV.kn_tent, kernel Kernel.tent),
235 :     (BV.kn_c1tent, kernel Kernel.tent),
236 :     (BV.i2r, simpleOp Op.IntToReal),
237 :     (BV.identity, fn (y, [dv], []) =>
238 :     assign(y, Op.Identity(pruneDim(MV.toDim dv)), [])),
239 :     (BV.zero, fn (y, [sv], []) =>
240 :     assign(y, Op.Zero(shapeVarToTensor sv), [])),
241 : jhr 1640 (BV.subscript, fn (y, [tv, dv], args) =>
242 :     assign(y,
243 : jhr 2358 Op.SeqSub(DstTy.SeqTy(pruneTy tv, pruneDim(MV.toDim dv))),
244 : jhr 1640 args))
245 : jhr 2356 ];
246 :     (* add C math functions *)
247 :     List.app (fn (n, x) => insert(x, basisFn n)) BV.mathFns;
248 :     tbl
249 :     end
250 : jhr 180
251 : jhr 194 fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
252 : jhr 2356 of SOME transFn => transFn(y, mvs, xs)
253 :     | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f)
254 :     (* end case *))
255 : jhr 1116 handle ex => (print(concat["translate (", IL.Var.toString y, ", ",
256 :     Var.uniqueNameOf f, ", ...)\n"]); raise ex)
257 : jhr 180
258 :     end

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