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 2929 - (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 2499 structure mk= mkOperators
27 :    
28 : jhr 2490 fun trType (Ty.TY ty) = TranslateTy.tr ty
29 :     | trType _ = raise Fail "expected type"
30 :     fun dimVarToInt (Ty.DIM d) = d
31 :     | dimVarToInt _ = raise Fail "expected dim"
32 : jhr 2358 fun dimVarToTensor dv = DstTy.tensorTy[dimVarToInt dv]
33 : jhr 1116 fun dimVarToMatrix dv = let
34 : jhr 2358 val d = dimVarToInt dv
35 : jhr 2356 in
36 :     DstTy.tensorTy[d, d] (* square matrix type *)
37 :     end
38 : jhr 2490 fun shapeVarToTensor (Ty.SHAPE shp) = DstTy.tensorTy shp
39 :     | shapeVarToTensor _ = raise Fail "expected shape"
40 : jhr 197
41 : jhr 1640 fun assign (y, rator, xs) = [IL.ASSGN(y, IL.OP(rator, xs))]
42 : jhr 407
43 : jhr 1640 fun basisFn name (y, [], xs) = [IL.ASSGN(y, IL.APPLY(name, xs))]
44 : jhr 1116
45 : jhr 407 fun simpleOp rator (y, [], xs) = assign (y, rator, xs)
46 :    
47 :     fun tensorOp rator (y, [sv], xs) = assign (y, rator(shapeVarToTensor sv), xs)
48 :    
49 :     fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs)
50 :    
51 : jhr 1116 fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), [])
52 : jhr 197
53 : cchiw 2845 val cnt = ref 0
54 :     fun genName prefix = let
55 :     val n = !cnt
56 :     in
57 :     cnt := n+1;
58 :     String.concat[prefix, "_", Int.toString n]
59 :     end
60 : jhr 1640 (* utility functions for synthesizing eigenvector/eigenvalue code *)
61 :     fun eigenVec (rator, dim) = let
62 :     val ty = DstTy.SeqTy(DstTy.realTy, dim)
63 :     in
64 :     fn (y, _, [m]) => let
65 :     val v = IL.Var.new("evals", ty)
66 :     in
67 :     [IL.MASSGN([v, y], rator, [m])]
68 :     end
69 :     end
70 :     fun eigenVal (rator, dim) = let
71 :     val ty = DstTy.SeqTy(DstTy.vecTy dim, dim)
72 :     in
73 :     fn (y, _, [m]) => let
74 :     val v = IL.Var.new("evecs", ty)
75 :     in
76 :     [IL.MASSGN([y, v], rator, [m])]
77 :     end
78 :     end
79 :    
80 : cchiw 2496 fun assignEin (y, rator, xs) = IL.ASSGN(y, IL.EINAPP(rator, xs))
81 : cchiw 2382
82 : cchiw 2499 fun simpleEOp rator (y, _,xs) = [assignEin(y, rator, xs)]
83 : cchiw 2485
84 : cchiw 2414 (* shape is an int list, DIM is in int|variable, k-level of differntiation *)
85 :    
86 : cchiw 2412 (* build a table that maps Basis variables to their translation functions *)
87 : jhr 2490 val tbl : ((IL.var * Ty.meta_arg list * IL.var list) -> IL.assignment list) VTbl.hash_table = let
88 : jhr 2356 val tbl = VTbl.mkTable (128, Fail "Translate table")
89 :     val insert = VTbl.insert tbl
90 :     in
91 :     List.app insert [
92 :     (BV.lt_ii, simpleOp(Op.LT DstTy.IntTy)),
93 :     (BV.lt_rr, simpleOp(Op.LT DstTy.realTy)),
94 :     (BV.lte_ii, simpleOp(Op.LTE DstTy.IntTy)),
95 :     (BV.lte_rr, simpleOp(Op.LTE DstTy.realTy)),
96 :     (BV.gte_ii, simpleOp(Op.GTE DstTy.IntTy)),
97 :     (BV.gte_rr, simpleOp(Op.GTE(DstTy.realTy))),
98 :     (BV.gt_ii, simpleOp(Op.GT DstTy.IntTy)),
99 :     (BV.gt_rr, simpleOp(Op.GT(DstTy.realTy))),
100 :     (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 :     (BV.equ_rr, simpleOp(Op.EQ(DstTy.realTy))),
104 :     (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 :     (BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))),
108 : jhr 2492 (BV.add_ii, simpleOp Op.IAdd),
109 : cchiw 2499 (BV.add_tt, fn (y, [shp], xs) => let
110 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
111 : cchiw 2906 val rator =mk.addTT(dd1)
112 : cchiw 2499 in
113 :     [assignEin(y, rator,xs)]
114 :     end),
115 : cchiw 2496 (BV.add_ff, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
116 : cchiw 2906 [assignEin(y, mk.addFF(d, dd),xs)]),
117 : cchiw 2499
118 : cchiw 2906 (BV.add_ft, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [f,s]) =>
119 :     [assignEin(y, mk.addTF(d,dd),[s,f])]),
120 : cchiw 2499
121 : cchiw 2906 (BV.add_tf, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
122 :     [assignEin(y, mk.addTF(d,dd),xs)]),
123 : jhr 2492 (BV.sub_ii, simpleOp Op.ISub),
124 : cchiw 2499 (BV.sub_tt, fn (y, [shp], xs) => let
125 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
126 : cchiw 2906 val rator =mk.subTT dd1
127 : cchiw 2499 in
128 :     [assignEin(y, rator,xs)]
129 :     end),
130 : cchiw 2496 (BV.sub_ff, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
131 : cchiw 2906 [assignEin(y, mk.subFF(d, dd),xs)]),
132 :     (BV.sub_ft, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [x1,x2]) =>
133 :     [assignEin(y, mk.subFT(d,dd),[x2,x1])]),
134 :     (BV.sub_tf, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
135 :     [assignEin(y, mk.subTF(d,dd),xs)]),
136 : cchiw 2447 (BV.mul_ii, simpleOp Op.IMul),
137 : cchiw 2906 (BV.mul_rr, fn (y,_,args) => [assignEin(y, mk.mulRR,args)]),
138 : cchiw 2499 (BV.mul_rt, fn (y, [shp], xs) => let
139 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
140 : cchiw 2906 val rator =mk.mulRT(dd1)
141 : cchiw 2499 in
142 :     [assignEin(y, rator,xs)]
143 :     end),
144 : jhr 2492 (BV.mul_tr, fn (y, [shp], [t, r]) => let
145 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
146 : cchiw 2906 val rator = mk.mulRT dd1
147 : jhr 2492 in
148 : cchiw 2496 [assignEin(y, rator,[r,t])]
149 : jhr 2492 end),
150 : cchiw 2496 (BV.mul_rf, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
151 : cchiw 2906 [assignEin(y, mk.mulRF(d, dd),xs)]),
152 : cchiw 2496 (BV.mul_fr, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [f, s]) =>
153 : cchiw 2906 [assignEin(y, mk.mulRF(d, dd),[s,f])]),
154 : cchiw 2576 (BV.mul_ss, fn (y, [_,Ty.DIM d], xs) =>
155 : cchiw 2906 [assignEin(y, mk.mulSS d,xs)]),
156 : cchiw 2576 (BV.mul_sf, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
157 : cchiw 2906 [assignEin(y, mk.mulSF(d,dd),xs)]),
158 : cchiw 2867 (BV.mul_fs, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [s,f]) =>
159 : cchiw 2906 [assignEin(y, mk.mulSF(d,dd),[f,s])]),
160 : cchiw 2447 (BV.div_ii, simpleOp Op.IDiv),
161 : cchiw 2906 (BV.div_rr, fn (y,_,args) => [assignEin(y, mk.divRR,args)]),
162 : cchiw 2499 (BV.div_tr, fn (y, [shp], xs) => let
163 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
164 : cchiw 2906 val rator =mk.divTR dd1
165 : cchiw 2499 in
166 :     [assignEin(y, rator,xs)]
167 :     end),
168 :     (BV.div_fr, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
169 : cchiw 2906 [assignEin(y, mk.divFR(d, dd),xs)]),
170 : cchiw 2867 (BV.div_fs, fn (y, [_,_,Ty.DIM d, Ty.SHAPE dd], xs) =>
171 : cchiw 2906 [assignEin(y, mk.divFS(d, dd),xs)]),
172 : cchiw 2608 (BV.div_ss, fn (y, [_,Ty.DIM d], xs) =>
173 : cchiw 2906 [assignEin(y, mk.divSS d,xs)]),
174 : cchiw 2445 (BV.exp_ri, simpleOp(Op.Power)),
175 :     (BV.exp_rr, basisFn MathFuns.pow),
176 : cchiw 2499 (BV.curl2D, simpleEOp mk.curl2d),
177 :     (BV.curl3D, simpleEOp mk.curl3d),
178 : cchiw 2496 (BV.convolve_vk, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
179 : cchiw 2499 [assignEin(y, mk.conv(d, dd),xs)]),
180 : cchiw 2496 (BV.convolve_kv, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [k, v]) =>
181 : cchiw 2499 [assignEin(y, mk.conv(d, dd),[v,k])]),
182 : cchiw 2447 (BV.neg_i, simpleOp Op.INeg),
183 : cchiw 2499 (BV.neg_t, fn (y, [shp], xs) => let
184 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
185 : cchiw 2906 val rator =mk.negTT dd1
186 : cchiw 2499 in
187 :     [assignEin(y, rator,xs)]
188 :     end),
189 :     (BV.neg_f, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
190 : cchiw 2906 [assignEin(y, mk.negFF(d, dd),xs)]),
191 : cchiw 2499 (BV.op_probe, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
192 : cchiw 2510 [assignEin(y, (mk.probe(dd,d)),xs)]),
193 : cchiw 2847 (BV.op_D, fn (y, [_, Ty.DIM d], xs) => (case d
194 :     of 2=>[assignEin(y, mk.grad([d]),xs)]
195 :     | 3=>[assignEin(y, mk.grad([d]),xs)]
196 :     | _ => raise Fail "unsupported gradient"
197 :     (*end case*))),
198 : cchiw 2906 (BV.op_Dotimes, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) =>
199 :     [assignEin(y, mk.dotimes(d1, dd@[d2]),xs)]),
200 : cchiw 2585 (BV.op_Ddot, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) =>
201 :     [assignEin(y, mk.divergence(d1, dd),xs)] ),
202 : cchiw 2906 (BV.op_norm_tt, fn (y, [sv], xs) => let
203 : cchiw 2845 val ty=shapeVarToTensor sv
204 :     in (case shapeVarToTensor sv
205 :     of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, xs)
206 : cchiw 2906 | DstTy.TensorTy [_] => assign(y, Op.Norm ty, xs)
207 :     | DstTy.TensorTy[_,_] => assign(y, Op.Norm ty, xs)
208 : cchiw 2845 | DstTy.TensorTy[_,_,_] => assign(y, Op.Norm ty, xs)
209 :     | _ => raise Fail"unsupported norm of TensorTy"
210 :     (* end case *))
211 :     end),
212 : cchiw 2906 (BV.op_norm_ff, fn (y, [ _,Ty.DIM d1, Ty.SHAPE dd], xs) => (case dd
213 :     of [] => [assignEin(y, mk.magnitudeFF(d1, dd),xs)]
214 :     | [i] => [assignEin(y, mk.magnitudeFF(d1, dd),xs@xs)]
215 :     | _ => raise Fail"unsupported norm of fieldTy"
216 : cchiw 2867 (* end case *))),
217 : jhr 2492 (BV.op_not, simpleOp Op.Not),
218 : cchiw 2906 (BV.op_cross3_tt, simpleEOp mk.cross3TT),
219 :     (BV.op_cross2_tt, simpleEOp mk.cross2TT),
220 :     (BV.op_cross2_ff, simpleEOp mk.cross2FF),
221 :     (BV.op_cross3_ff, simpleEOp mk.cross3FF),
222 :     (BV.op_outer_tt, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
223 :     [assignEin(y, (mk.outerTT(d1, d2)), xs)]),
224 :     (BV.op_outer_ff, fn (y, [_, Ty.DIM d1], xs)=>
225 :     [assignEin(y, mk.outerFF d1, xs)]),
226 :     (BV.op_inner_tt, fn (y, [sh1, sh2, _], xs) => let
227 : cchiw 2499 val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
228 :     val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
229 :     in
230 : cchiw 2906 [assignEin(y, (mk.innerTT(dd1,dd2)),xs)]
231 : cchiw 2499 end),
232 : cchiw 2584
233 : cchiw 2929 (BV.op_inner_tf, fn (y, [_,Ty.DIM d,sh1,Ty.SHAPE dd2,_], xs) =>let
234 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
235 :     in
236 :     [assignEin(y, mk.innerTF(dd1,d,dd2),xs)]
237 :     end ),
238 :     (BV.op_inner_ft, fn (y, [_,Ty.DIM d,Ty.SHAPE dd1,sh2,_], xs) =>let
239 :     val ty1 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
240 :     in
241 :     [assignEin(y, mk.innerFT(dd1,d,dd2),xs)]
242 :     end ),
243 : cchiw 2906
244 : cchiw 2929 (BV.op_inner_ff, fn (y, [_,Ty.DIM d,Ty.SHAPE dd1,Ty.SHAPE dd2,_], xs) =>
245 : jhr 2926 [assignEin(y, mk.innerFF(dd1,d,dd2),xs)]),
246 :    
247 : cchiw 2906 (BV.op_colon_tt, fn (y, [sh1, sh2, _], xs) => let
248 : cchiw 2499 val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
249 :     val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
250 :     in
251 : cchiw 2906 [assignEin(y, (mk.colonTT(dd1,dd2)),xs)]
252 : cchiw 2499 end),
253 : cchiw 2906 (BV.op_colon_ff, fn (y, [_,Ty.SHAPE dd1,_,Ty.SHAPE dd2, _], xs) =>
254 :     [assignEin(y, (mk.colonFF(dd1,dd2)),xs)]),
255 : jhr 2492 (BV.fn_inside, fn (y, [_, Ty.DIM d, _], xs) => assign(y, Op.Inside d, xs)),
256 : jhr 2356 (BV.clamp_rrr, simpleOp (Op.Clamp DstTy.realTy)),
257 :     (BV.clamp_vvv, vectorOp Op.Clamp),
258 :     (BV.lerp3, tensorOp Op.Lerp),
259 : cchiw 2499 (BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) => let
260 : jhr 2356 val t1 = IL.Var.new("t1", DstTy.realTy)
261 :     val t2 = IL.Var.new("t2", DstTy.realTy)
262 :     val t3 = IL.Var.new("t3", DstTy.realTy)
263 :     in [
264 : cchiw 2906 assignEin(t1, mk.subRR,[x,x0]),
265 :     assignEin(t2, mk.subRR,[x1,x0]),
266 :     assignEin(t3, mk.divRR,[t1,t2]),
267 : jhr 2356 IL.ASSGN(y, IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3]))
268 :     ] end),
269 :     (BV.evals2x2, eigenVal (Op.Eigen2x2, 2)),
270 :     (BV.evals3x3, eigenVal (Op.Eigen3x3, 3)),
271 :     (BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)),
272 :     (BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)),
273 : cchiw 2668
274 :    
275 : jhr 2356 (BV.fn_max, simpleOp Op.Max),
276 :     (BV.fn_min, simpleOp Op.Min),
277 : cchiw 2496
278 :     (* modulate is vector * vector pointwise multiplication *)
279 : cchiw 2499 (BV.fn_modulate, fn (y,[Ty.DIM dd1], xs) =>
280 :     [assignEin(y, (mk.modulate dd1),xs)]),
281 : cchiw 2906 (BV.fn_normalize_tt, vectorOp Op.Normalize),
282 :     (BV.fn_normalize_ff, fn (y, [ _,Ty.DIM d1, Ty.SHAPE dd], xs) =>
283 : cchiw 2870 (case dd
284 : cchiw 2906 of [] => [assignEin(y, mk.normalizeFF(d1, []),xs)]
285 :     | [i] => [assignEin(y, mk.normalizeFF(d1, [i]),xs@xs)]
286 : cchiw 2870 | _ => raise Fail"unsupported normalized of fieldTy"
287 :     (* end case *))),
288 : jhr 2356 (BV.fn_principleEvec, vectorOp Op.PrincipleEvec),
289 : cchiw 2906 (BV.fn_trace_t, fn (y, [Ty.DIM d], xs) =>
290 :     [assignEin(y,(mk.traceT d), xs)]),
291 :     (BV.fn_trace_f, fn (y, [_,Ty.DIM d,Ty.SHAPE dd], xs) =>
292 :     [assignEin(y,mk.traceF(d,dd), xs)]),
293 :     (BV.fn_transpose_t, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
294 :     [assignEin(y, (mk.transposeT [d1,d2]), xs)]),
295 :     (BV.fn_transpose_f, fn (y, [_,Ty.DIM d1, Ty.DIM d2,Ty.DIM d3], xs) =>
296 :     [assignEin(y, (mk.transposeF (d1,d2,d3)), xs)]),
297 : jhr 2356 (BV.kn_bspln3, kernel Kernel.bspln3),
298 :     (BV.kn_bspln5, kernel Kernel.bspln5),
299 :     (BV.kn_ctmr, kernel Kernel.ctmr),
300 :     (BV.kn_c2ctmr, kernel Kernel.ctmr),
301 : jhr 2358 (BV.kn_c4hexic, kernel Kernel.c4hexic),
302 : jhr 2356 (BV.kn_tent, kernel Kernel.tent),
303 :     (BV.kn_c1tent, kernel Kernel.tent),
304 :     (BV.i2r, simpleOp Op.IntToReal),
305 : cchiw 2496 (BV.identity, fn (y, [Ty.DIM d], xs) =>
306 : cchiw 2502 [assignEin(y, (mk.identity d), xs)]),
307 : cchiw 2628 (BV.zero, fn (y, [sv], []) =>
308 :     assign(y, Op.Zero(shapeVarToTensor sv), [])),
309 : jhr 2492 (BV.subscript, fn (y, [ty, Ty.DIM d], xs) =>
310 :     assign (y, Op.SeqSub(DstTy.SeqTy(trType ty, d)), xs))
311 : jhr 2490 (*,
312 :     (BV.dynSubscript, fn (y, [tv], args) =>
313 :     assign(y, Op.SeqSub(DstTy.DynSeqTy(pruneTy tv)), args))
314 :     *)
315 : cchiw 2388 ];
316 : jhr 2356 (* add C math functions *)
317 :     List.app (fn (n, x) => insert(x, basisFn n)) BV.mathFns;
318 :     tbl
319 :     end
320 : jhr 180
321 : jhr 194 fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
322 : jhr 2356 of SOME transFn => transFn(y, mvs, xs)
323 :     | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f)
324 :     (* end case *))
325 : jhr 1116 handle ex => (print(concat["translate (", IL.Var.toString y, ", ",
326 :     Var.uniqueNameOf f, ", ...)\n"]); raise ex)
327 : jhr 180
328 :     end

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