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 2843 - (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 2490 val translate : (HighIL.var * Var.var * SimpleTypes.meta_arg 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 2490 structure Ty = SimpleTypes
25 : jhr 194 structure VTbl = Var.Tbl
26 : cchiw 2383
27 : cchiw 2499 structure mk= mkOperators
28 :    
29 : jhr 2490 fun trType (Ty.TY ty) = TranslateTy.tr ty
30 :     | trType _ = raise Fail "expected type"
31 :     fun dimVarToInt (Ty.DIM d) = d
32 :     | dimVarToInt _ = raise Fail "expected dim"
33 : jhr 2358 fun dimVarToTensor dv = DstTy.tensorTy[dimVarToInt dv]
34 : jhr 1116 fun dimVarToMatrix dv = let
35 : jhr 2358 val d = dimVarToInt dv
36 : jhr 2356 in
37 :     DstTy.tensorTy[d, d] (* square matrix type *)
38 :     end
39 : jhr 2490 fun shapeVarToTensor (Ty.SHAPE shp) = DstTy.tensorTy shp
40 :     | shapeVarToTensor _ = raise Fail "expected shape"
41 : jhr 197
42 : jhr 1640 fun assign (y, rator, xs) = [IL.ASSGN(y, IL.OP(rator, xs))]
43 : jhr 407
44 : jhr 1640 fun basisFn name (y, [], xs) = [IL.ASSGN(y, IL.APPLY(name, xs))]
45 : jhr 1116
46 : jhr 407 fun simpleOp rator (y, [], xs) = assign (y, rator, xs)
47 :    
48 :     fun tensorOp rator (y, [sv], xs) = assign (y, rator(shapeVarToTensor sv), xs)
49 :    
50 :     fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs)
51 :    
52 : jhr 1116 fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), [])
53 : jhr 197
54 : jhr 1640 (* utility functions for synthesizing eigenvector/eigenvalue code *)
55 :     fun eigenVec (rator, dim) = let
56 :     val ty = DstTy.SeqTy(DstTy.realTy, dim)
57 :     in
58 :     fn (y, _, [m]) => let
59 :     val v = IL.Var.new("evals", ty)
60 :     in
61 :     [IL.MASSGN([v, y], rator, [m])]
62 :     end
63 :     end
64 :     fun eigenVal (rator, dim) = let
65 :     val ty = DstTy.SeqTy(DstTy.vecTy dim, dim)
66 :     in
67 :     fn (y, _, [m]) => let
68 :     val v = IL.Var.new("evecs", ty)
69 :     in
70 :     [IL.MASSGN([y, v], rator, [m])]
71 :     end
72 :     end
73 :    
74 : cchiw 2496 fun assignEin (y, rator, xs) = IL.ASSGN(y, IL.EINAPP(rator, xs))
75 : cchiw 2382
76 : cchiw 2499 fun simpleEOp rator (y, _,xs) = [assignEin(y, rator, xs)]
77 : cchiw 2485
78 : cchiw 2414
79 : cchiw 2668 fun mkNorm (shape,A) =let
80 :     val RTy=DstTy.TensorTy []
81 :     val b=IL.Var.new("dot" ,RTy)
82 :     val c=IL.Var.new("sqrt" ,RTy)
83 :     val rator=(case shape
84 :     of [_]=>mk.innerProduct(shape,shape)
85 :     | [_,_]=>mk.doubleDot(shape,shape)
86 :     | _ => raise Fail "unsupported norm"
87 :     (*end case*))
88 :     val dot=assignEin(b,rator, [A,A])
89 :     val rator= IL.OP(Op.Sqrt, [b])
90 :     in (c,rator,dot)
91 :     end
92 : cchiw 2447
93 : cchiw 2668
94 : cchiw 2447
95 : cchiw 2414
96 :     (* shape is an int list, DIM is in int|variable, k-level of differntiation *)
97 :    
98 : cchiw 2412 (* build a table that maps Basis variables to their translation functions *)
99 : jhr 2490 val tbl : ((IL.var * Ty.meta_arg list * IL.var list) -> IL.assignment list) VTbl.hash_table = let
100 : jhr 2356 val tbl = VTbl.mkTable (128, Fail "Translate table")
101 :     val insert = VTbl.insert tbl
102 :     in
103 :     List.app insert [
104 :     (BV.lt_ii, simpleOp(Op.LT DstTy.IntTy)),
105 :     (BV.lt_rr, simpleOp(Op.LT DstTy.realTy)),
106 :     (BV.lte_ii, simpleOp(Op.LTE DstTy.IntTy)),
107 :     (BV.lte_rr, simpleOp(Op.LTE DstTy.realTy)),
108 :     (BV.gte_ii, simpleOp(Op.GTE DstTy.IntTy)),
109 :     (BV.gte_rr, simpleOp(Op.GTE(DstTy.realTy))),
110 :     (BV.gt_ii, simpleOp(Op.GT DstTy.IntTy)),
111 :     (BV.gt_rr, simpleOp(Op.GT(DstTy.realTy))),
112 :     (BV.equ_bb, simpleOp(Op.EQ DstTy.BoolTy)),
113 :     (BV.equ_ii, simpleOp(Op.EQ DstTy.IntTy)),
114 :     (BV.equ_ss, simpleOp(Op.EQ DstTy.StringTy)),
115 :     (BV.equ_rr, simpleOp(Op.EQ(DstTy.realTy))),
116 :     (BV.neq_bb, simpleOp(Op.NEQ DstTy.BoolTy)),
117 :     (BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)),
118 :     (BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)),
119 :     (BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))),
120 : jhr 2492 (BV.add_ii, simpleOp Op.IAdd),
121 : cchiw 2499 (BV.add_tt, fn (y, [shp], xs) => let
122 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
123 :     val rator =mk.addTen(dd1)
124 :     in
125 :     [assignEin(y, rator,xs)]
126 :     end),
127 : cchiw 2496 (BV.add_ff, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
128 : cchiw 2499 [assignEin(y, mk.addField(d, dd),xs)]),
129 :    
130 :     (BV.add_fr, fn (y, [_,Ty.DIM d], [f,s]) =>
131 :     [assignEin(y, mk.addTenField(d),[s,f])]),
132 :    
133 :     (BV.add_rf, fn (y, [_,Ty.DIM d], xs) =>
134 :     [assignEin(y, mk.addTenField(d),xs)]),
135 :    
136 :    
137 : jhr 2492 (BV.sub_ii, simpleOp Op.ISub),
138 : cchiw 2499 (BV.sub_tt, fn (y, [shp], xs) => let
139 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
140 :     val rator =mk.subTen(dd1)
141 :     in
142 :     [assignEin(y, rator,xs)]
143 :     end),
144 : cchiw 2496 (BV.sub_ff, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
145 : cchiw 2499 [assignEin(y, mk.subField(d, dd),xs)]),
146 :    
147 :    
148 : cchiw 2496 (BV.sub_fr, fn (y, [_,Ty.DIM d], xs) =>
149 : cchiw 2499 [assignEin(y, mk.subFieldTen(d),xs)]),
150 :    
151 :     (BV.sub_rf, fn (y, [_,Ty.DIM d], xs) =>
152 :     [assignEin(y, mk.subTenField(d),xs)]),
153 :    
154 : cchiw 2447 (BV.mul_ii, simpleOp Op.IMul),
155 : cchiw 2499 (BV.mul_rr, fn (y,_,args) => [assignEin(y, mk.prodScalar,args)]),
156 :     (BV.mul_rt, fn (y, [shp], xs) => let
157 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
158 :     val rator =mk.scaleTen(dd1)
159 :     in
160 :     [assignEin(y, rator,xs)]
161 :     end),
162 : jhr 2492 (BV.mul_tr, fn (y, [shp], [t, r]) => let
163 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
164 : cchiw 2499 val rator = mk.scaleTen dd1
165 : jhr 2492 in
166 : cchiw 2496 [assignEin(y, rator,[r,t])]
167 : jhr 2492 end),
168 : cchiw 2496 (BV.mul_rf, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
169 : cchiw 2499 [assignEin(y, mk.scaleField(d, dd),xs)]),
170 : cchiw 2496 (BV.mul_fr, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [f, s]) =>
171 : cchiw 2499 [assignEin(y, mk.scaleField(d, dd),[s,f])]),
172 : cchiw 2576 (BV.mul_ss, fn (y, [_,Ty.DIM d], xs) =>
173 :     [assignEin(y, mk.mulFieldss d,xs)]),
174 :     (BV.mul_sf, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
175 :     [assignEin(y, mk.mulFieldsf(d,dd),xs)]),
176 :     (BV.mul_fs, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [f,s]) =>
177 :     [assignEin(y, mk.mulFieldsf(d,dd),[s,f])]),
178 :    
179 : cchiw 2447 (BV.div_ii, simpleOp Op.IDiv),
180 : cchiw 2499 (BV.div_rr, fn (y,_,args) => [assignEin(y, mk.divScalar,args)]),
181 :     (BV.div_tr, fn (y, [shp], xs) => let
182 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
183 :     val rator =mk.divTen(dd1)
184 :     in
185 :     [assignEin(y, rator,xs)]
186 :     end),
187 :     (BV.div_fr, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
188 :     [assignEin(y, mk.divideField(d, dd),xs)]),
189 : cchiw 2608 (BV.div_ss, fn (y, [_,Ty.DIM d], xs) =>
190 :     [assignEin(y, mk.divFieldss d,xs)]),
191 :    
192 : cchiw 2445 (BV.exp_ri, simpleOp(Op.Power)),
193 :     (BV.exp_rr, basisFn MathFuns.pow),
194 : cchiw 2499 (BV.curl2D, simpleEOp mk.curl2d),
195 :     (BV.curl3D, simpleEOp mk.curl3d),
196 : cchiw 2496 (BV.convolve_vk, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
197 : cchiw 2499 [assignEin(y, mk.conv(d, dd),xs)]),
198 : cchiw 2496 (BV.convolve_kv, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [k, v]) =>
199 : cchiw 2499 [assignEin(y, mk.conv(d, dd),[v,k])]),
200 : cchiw 2447 (BV.neg_i, simpleOp Op.INeg),
201 : cchiw 2499 (BV.neg_t, fn (y, [shp], xs) => let
202 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
203 :     val rator =mk.negTen(dd1)
204 :     in
205 :     [assignEin(y, rator,xs)]
206 :     end),
207 :     (BV.neg_f, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
208 :     [assignEin(y, mk.negField(d, dd),xs)]),
209 :     (BV.op_probe, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
210 : cchiw 2510 [assignEin(y, (mk.probe(dd,d)),xs)]),
211 : cchiw 2499 (BV.op_D, fn (y, [_, Ty.DIM d], xs) => [assignEin(y, mk.grad([d]),xs)]),
212 : cchiw 2510
213 : cchiw 2830 (BV.op_Dotimes, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) => [assignEin(y, mk.dotimes(d1, dd@[d2]),xs)]),
214 : cchiw 2585 (BV.op_Ddot, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) =>
215 :     [assignEin(y, mk.divergence(d1, dd),xs)] ),
216 : cchiw 2506
217 :    
218 : cchiw 2668 (BV.op_norm, fn (y, [sv], [x]) => (case shapeVarToTensor sv
219 :     of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, [x])
220 :     | DstTy.TensorTy dd=> let
221 :     val RTy=DstTy.TensorTy []
222 :     val (_,sqrtop,dot)= mkNorm (dd,x)
223 :     in
224 :     [dot ,IL.ASSGN (y,sqrtop)]
225 :     end
226 :    
227 :    
228 :     | ty => assign(y, Op.Norm ty, [x])
229 : jhr 2356 (* end case *))),
230 : jhr 2492 (BV.op_not, simpleOp Op.Not),
231 : cchiw 2499 (BV.op_cross, simpleEOp mk.crossProduct),
232 : cchiw 2843 (BV.op_cross2, simpleEOp mk.crossProduct2),
233 : cchiw 2603 (BV.op_crossField, simpleEOp mk.crossProductField),
234 : cchiw 2499 (BV.op_outer, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
235 : cchiw 2585 [assignEin(y, (mk.outerProduct(d1, d2)), xs)]),
236 :     (* Any Shape fields
237 :     (BV.op_outerField, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd1, Ty.SHAPE dd2], xs)=>
238 :     [assignEin(y, mk.outerField(d1, dd1, dd2), xs)]),
239 :     *)
240 :     (BV.op_outerField, fn (y, [_, Ty.DIM d1], xs)=>
241 :     [assignEin(y, mk.outerField(d1), xs)]),
242 :    
243 :    
244 : cchiw 2499 (BV.op_inner, fn (y, [sh1, sh2, _], xs) => let
245 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
246 :     val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
247 :     in
248 :     [assignEin(y, (mk.innerProduct(dd1,dd2)),xs)]
249 :     end),
250 : cchiw 2584
251 : cchiw 2611 (BV.op_innerField, fn (y, [_,Ty.SHAPE dd1,Ty.DIM d,Ty.SHAPE dd2,_], xs) =>
252 :     [assignEin(y, mk.innerProductField(dd1,d,dd2),xs)]),
253 : jhr 2492 (BV.op_colon, fn (y, [sh1, sh2, _], xs) => let
254 : cchiw 2499 val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
255 :     val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
256 :     in
257 :     [assignEin(y, (mk.doubleDot(dd1,dd2)),xs)]
258 :     end),
259 : cchiw 2611 (BV.op_colonField, fn (y, [_,Ty.SHAPE dd1,_,Ty.SHAPE dd2, _], xs) =>
260 :     [assignEin(y, (mk.doubleDotField(dd1,dd2)),xs)]),
261 : jhr 2492 (BV.fn_inside, fn (y, [_, Ty.DIM d, _], xs) => assign(y, Op.Inside d, xs)),
262 : jhr 2356 (BV.clamp_rrr, simpleOp (Op.Clamp DstTy.realTy)),
263 :     (BV.clamp_vvv, vectorOp Op.Clamp),
264 :     (BV.lerp3, tensorOp Op.Lerp),
265 : cchiw 2499 (BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) => let
266 : jhr 2356 val t1 = IL.Var.new("t1", DstTy.realTy)
267 :     val t2 = IL.Var.new("t2", DstTy.realTy)
268 :     val t3 = IL.Var.new("t3", DstTy.realTy)
269 :     in [
270 : cchiw 2499 assignEin(t1, mk.subScalar,[x,x0]),
271 :     assignEin(t2, mk.subScalar,[x1,x0]),
272 :     assignEin(t3, mk.divScalar,[t1,t2]),
273 : jhr 2356 IL.ASSGN(y, IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3]))
274 :     ] end),
275 :     (BV.evals2x2, eigenVal (Op.Eigen2x2, 2)),
276 :     (BV.evals3x3, eigenVal (Op.Eigen3x3, 3)),
277 :     (BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)),
278 :     (BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)),
279 : cchiw 2668
280 :    
281 : jhr 2356 (BV.fn_max, simpleOp Op.Max),
282 :     (BV.fn_min, simpleOp Op.Min),
283 : cchiw 2496
284 :     (* modulate is vector * vector pointwise multiplication *)
285 : cchiw 2499 (BV.fn_modulate, fn (y,[Ty.DIM dd1], xs) =>
286 :     [assignEin(y, (mk.modulate dd1),xs)]),
287 : cchiw 2668 (*
288 :     vectorOp Op.Normalize),*)
289 : cchiw 2838
290 :     (BV.fn_normalize, vectorOp Op.Normalize),
291 :    
292 :     (* fn (y, [Ty.DIM i], [x]) =>let
293 : cchiw 2668 val (c,sqrtop,dot)= mkNorm ([i],x)
294 :     val d = IL.Var.new ("int", DstTy.intTy)
295 :     val e=IL.Var.new("divide" ,DstTy.TensorTy [])
296 :     in [dot ,IL.ASSGN (c,sqrtop),
297 :     IL.ASSGN (d, IL.LIT(Literal.Int 1)),
298 :     assignEin(e,mk.divScalar,[d,c]),
299 :     assignEin(y,mk.scaleTen [i],[e,x])]
300 : cchiw 2838 end)*)
301 : cchiw 2668
302 : jhr 2356 (BV.fn_principleEvec, vectorOp Op.PrincipleEvec),
303 : jhr 2492 (BV.fn_trace, fn (y, [Ty.DIM d], xs) =>
304 : cchiw 2499 [assignEin(y,(mk.trace d), xs)]),
305 : cchiw 2603 (BV.fn_traceField, fn (y, [_,Ty.DIM d,Ty.SHAPE dd], xs) =>
306 :     [assignEin(y,mk.traceField(d,dd), xs)]),
307 :    
308 : jhr 2492 (BV.fn_transpose, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
309 : cchiw 2499 [assignEin(y, (mk.transpose [d1,d2]), xs)]),
310 : cchiw 2603
311 :     (BV.fn_transposeField, fn (y, [_,Ty.DIM d1, Ty.DIM d2,Ty.DIM d3], xs) =>
312 :     [assignEin(y, (mk.transposeField (d1,d2,d3)), xs)]),
313 :    
314 : jhr 2356 (BV.kn_bspln3, kernel Kernel.bspln3),
315 :     (BV.kn_bspln5, kernel Kernel.bspln5),
316 :     (BV.kn_ctmr, kernel Kernel.ctmr),
317 :     (BV.kn_c2ctmr, kernel Kernel.ctmr),
318 : jhr 2358 (BV.kn_c4hexic, kernel Kernel.c4hexic),
319 : jhr 2356 (BV.kn_tent, kernel Kernel.tent),
320 :     (BV.kn_c1tent, kernel Kernel.tent),
321 :     (BV.i2r, simpleOp Op.IntToReal),
322 : cchiw 2496 (BV.identity, fn (y, [Ty.DIM d], xs) =>
323 : cchiw 2502 [assignEin(y, (mk.identity d), xs)]),
324 : cchiw 2628 (BV.zero, fn (y, [sv], []) =>
325 :     assign(y, Op.Zero(shapeVarToTensor sv), [])),
326 : jhr 2492 (BV.subscript, fn (y, [ty, Ty.DIM d], xs) =>
327 :     assign (y, Op.SeqSub(DstTy.SeqTy(trType ty, d)), xs))
328 : jhr 2490 (*,
329 :     (BV.dynSubscript, fn (y, [tv], args) =>
330 :     assign(y, Op.SeqSub(DstTy.DynSeqTy(pruneTy tv)), args))
331 :     *)
332 : cchiw 2388 ];
333 : jhr 2356 (* add C math functions *)
334 :     List.app (fn (n, x) => insert(x, basisFn n)) BV.mathFns;
335 :     tbl
336 :     end
337 : jhr 180
338 : jhr 194 fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
339 : jhr 2356 of SOME transFn => transFn(y, mvs, xs)
340 :     | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f)
341 :     (* end case *))
342 : jhr 1116 handle ex => (print(concat["translate (", IL.Var.toString y, ", ",
343 :     Var.uniqueNameOf f, ", ...)\n"]); raise ex)
344 : jhr 180
345 :     end

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