SCM Repository
Annotation of /branches/charisee/src/compiler/translate/translate-basis.sml
Parent Directory
|
Revision Log
Revision 2386 - (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 | 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 : | cchiw | 2383 | structure EinOp= Operators |
30 : | structure R=Rewrite | ||
31 : | structure N=Normalize | ||
32 : | |||
33 : | jhr | 2358 | fun pruneTy tv = (case TU.prune(MV.toType tv) |
34 : | of (ty as Ty.T_Var _) => raise Fail("unresolved type " ^ TU.toString ty) | ||
35 : | jhr | 1640 | | ty => TranslateTy.tr ty |
36 : | (* end case *)) | ||
37 : | |||
38 : | jhr | 197 | fun pruneDim d = (case TU.pruneDim d |
39 : | jhr | 2356 | of (Ty.DimConst n) => n |
40 : | | d => raise Fail("unresolved dimension " ^ TU.dimToString d) | ||
41 : | (* end case *)) | ||
42 : | jhr | 197 | |
43 : | jhr | 407 | fun pruneShape sv = (case TU.pruneShape(MV.toShape sv) |
44 : | jhr | 2356 | of Ty.Shape dd => DstTy.tensorTy(List.map pruneDim dd) |
45 : | | shp => raise Fail("unresolved shape " ^ TU.shapeToString shp) | ||
46 : | (* end case *)) | ||
47 : | jhr | 183 | |
48 : | jhr | 2358 | fun dimVarToInt dv = pruneDim(MV.toDim dv) |
49 : | fun dimVarToTensor dv = DstTy.tensorTy[dimVarToInt dv] | ||
50 : | jhr | 1116 | fun dimVarToMatrix dv = let |
51 : | jhr | 2358 | val d = dimVarToInt dv |
52 : | jhr | 2356 | in |
53 : | DstTy.tensorTy[d, d] (* square matrix type *) | ||
54 : | end | ||
55 : | jhr | 407 | fun shapeVarToTensor sv = pruneShape sv |
56 : | jhr | 197 | |
57 : | jhr | 1640 | fun assign (y, rator, xs) = [IL.ASSGN(y, IL.OP(rator, xs))] |
58 : | jhr | 407 | |
59 : | jhr | 1640 | fun basisFn name (y, [], xs) = [IL.ASSGN(y, IL.APPLY(name, xs))] |
60 : | jhr | 1116 | |
61 : | jhr | 407 | fun simpleOp rator (y, [], xs) = assign (y, rator, xs) |
62 : | |||
63 : | fun tensorOp rator (y, [sv], xs) = assign (y, rator(shapeVarToTensor sv), xs) | ||
64 : | |||
65 : | fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs) | ||
66 : | |||
67 : | jhr | 1116 | fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), []) |
68 : | jhr | 197 | |
69 : | jhr | 1640 | (* utility functions for synthesizing eigenvector/eigenvalue code *) |
70 : | fun eigenVec (rator, dim) = let | ||
71 : | val ty = DstTy.SeqTy(DstTy.realTy, dim) | ||
72 : | in | ||
73 : | fn (y, _, [m]) => let | ||
74 : | val v = IL.Var.new("evals", ty) | ||
75 : | in | ||
76 : | [IL.MASSGN([v, y], rator, [m])] | ||
77 : | end | ||
78 : | end | ||
79 : | fun eigenVal (rator, dim) = let | ||
80 : | val ty = DstTy.SeqTy(DstTy.vecTy dim, dim) | ||
81 : | in | ||
82 : | fn (y, _, [m]) => let | ||
83 : | val v = IL.Var.new("evecs", ty) | ||
84 : | in | ||
85 : | [IL.MASSGN([y, v], rator, [m])] | ||
86 : | end | ||
87 : | end | ||
88 : | |||
89 : | cchiw | 2386 | fun createTenEin(operator, sv)=let shape=shapeVarToTensor |
90 : | in S.transform(operator,[len(shape), shape]) end | ||
91 : | fun createFldEin(operator, f)=S.transform(operator,[]) | ||
92 : | cchiw | 2378 | |
93 : | |||
94 : | cchiw | 2386 | fun assignEin (y, rator, xs) = [IL.ASSGN(y, IL.EINAPP(R.evalEinApp(rator, xs)))] |
95 : | fun simpleEinOp rator(y, [],xs)= assignEin (y,S.transform(rator,[]), xs) | ||
96 : | fun tensorEinOp rator(y,[sv],xs)= assignEin(y,createTenEin(rator,sv),xs) | ||
97 : | cchiw | 2382 | |
98 : | jhr | 184 | (* build a table that maps Basis variables to their translation functions *) |
99 : | jhr | 1640 | val tbl : ((IL.var * Ty.meta_var 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 : | cchiw | 2378 | |
121 : | (* Changed*) | ||
122 : | |||
123 : | cchiw | 2386 | (BV.add_ii, simpleEinOp(EinOp.addScalars)), |
124 : | (BV.add_tt, tensorEinOp(EinOp.addTensors)), | ||
125 : | cchiw | 2378 | |
126 : | cchiw | 2383 | |
127 : | cchiw | 2380 | (BV.add_ff, fn (y, _, [f, g]) |
128 : | =>assignEin(y, createFldEin(EinOp.addField,f ), [f, g])), | ||
129 : | (BV.add_fr, fn (y, _, [f, s]) | ||
130 : | => assignEin(y, createFldEin(EinOp.addTenField,f ), [f, s])), | ||
131 : | (BV.add_rf, fn (y, _, [s, f]) | ||
132 : | => assignEin(y, createFldEin(EinOp.addTenField,f ), [f, s])), | ||
133 : | cchiw | 2378 | |
134 : | cchiw | 2386 | (BV.sub_ii, simpleEinOp(EinOp.subScalars)), (*DstTy.IntTy*) |
135 : | (BV.sub_tt, tensorEinOp(EinOp.subTensor)), | ||
136 : | cchiw | 2380 | (BV.sub_ff, fn (y, _, [f, g]) |
137 : | => assignEin(y, createFldEin(EinOp.subField,f), [f, g])), | ||
138 : | |||
139 : | cchiw | 2379 | (* UnChanged*) |
140 : | jhr | 2358 | (BV.sub_fr, fn (y, _, [f, s]) => let |
141 : | val s' = IL.Var.copy s | ||
142 : | in [ | ||
143 : | IL.ASSGN(s', IL.OP(Op.Neg DstTy.realTy, [s])), | ||
144 : | IL.ASSGN(y, IL.OP(Op.OffsetField, [f, s'])) | ||
145 : | ] end), | ||
146 : | cchiw | 2378 | |
147 : | jhr | 2358 | (BV.sub_rf, fn (y, _, [s, f]) => let |
148 : | val f' = IL.Var.copy f | ||
149 : | in [ | ||
150 : | IL.ASSGN(f', IL.OP(Op.NegField, [f])), | ||
151 : | IL.ASSGN(y, IL.OP(Op.OffsetField, [f', s])) | ||
152 : | ] end), | ||
153 : | cchiw | 2378 | (* Changed*) |
154 : | cchiw | 2386 | (BV.mul_ii, simpleEinOp(EinOp.scalarxscalar)), |
155 : | (BV.mul_rr, simpleEinOp(EinOp.scalarxscalar)), | ||
156 : | (BV.mul_rt, tensorEinOp(EinOp.scaleTensor)), (*tensorOp Op.Scale),*) | ||
157 : | cchiw | 2378 | |
158 : | cchiw | 2380 | (BV.mul_tr, fn (y, sv, [t, r]) |
159 : | cchiw | 2381 | =>assignEin(y,createTenEin(EinOp.scaleTensor, sv), [r,t]) |
160 : | cchiw | 2380 | |
161 : | |||
162 : | cchiw | 2386 | (BV.mul_rf, fn (y, _, [s, f]) |
163 : | => assignEin(y, createFldEin(EinOp.scaleField,f), [s, f])), | ||
164 : | (BV.mul_fr, fn (y, _, [f, s]) | ||
165 : | => assignEin(y, createFldEin(EinOp.scaleField,f), [s, f])), | ||
166 : | cchiw | 2380 | |
167 : | cchiw | 2378 | (* UnChanged*) |
168 : | |||
169 : | jhr | 2356 | (BV.div_ii, simpleOp(Op.Div DstTy.IntTy)), |
170 : | (BV.div_rr, simpleOp(Op.Div DstTy.realTy)), | ||
171 : | (BV.div_tr, fn (y, [sv], [x, s]) => let | ||
172 : | val one = IL.Var.new("one", DstTy.realTy) | ||
173 : | val s' = IL.Var.new("sInv", DstTy.realTy) | ||
174 : | in [ | ||
175 : | IL.ASSGN(one, IL.LIT(Literal.Float(FloatLit.one))), | ||
176 : | IL.ASSGN(s', IL.OP(Op.Div DstTy.realTy, [one, s])), | ||
177 : | IL.ASSGN(y, IL.OP(Op.Scale(shapeVarToTensor sv), [s', x])) | ||
178 : | ] end), | ||
179 : | (BV.div_fr, fn (y, _, [f, s]) => let | ||
180 : | val one = IL.Var.new("one", DstTy.realTy) | ||
181 : | val s' = IL.Var.new("sInv", DstTy.realTy) | ||
182 : | in [ | ||
183 : | IL.ASSGN(one, IL.LIT(Literal.Float(FloatLit.one))), | ||
184 : | IL.ASSGN(s', IL.OP(Op.Div DstTy.realTy, [one, s])), | ||
185 : | IL.ASSGN(y, IL.OP(Op.ScaleField, [s', f])) | ||
186 : | ] end), | ||
187 : | (BV.exp_ri, simpleOp(Op.Power)), | ||
188 : | (BV.exp_rr, basisFn MathFuns.pow), | ||
189 : | cchiw | 2378 | |
190 : | cchiw | 2379 | (* Changed*) |
191 : | cchiw | 2384 | (*Note curl is in Ein and does not need to normalize.*) |
192 : | cchiw | 2379 | (BV.curl2D, fn (y, _, xs) => assignEin(y, EinOp.Curl2d, xs)), |
193 : | (BV.curl3D, fn (y, _, xs) => assignEin(y, EinOp.Curl3d, xs)), | ||
194 : | cchiw | 2378 | |
195 : | |||
196 : | cchiw | 2379 | (* UnChanged*) |
197 : | jhr | 2356 | (BV.convolve_vk, fn (y, [_, dv, _], xs) => |
198 : | assign(y, Op.Field(pruneDim(MV.toDim dv)), xs)), | ||
199 : | (BV.convolve_kv, fn (y, [_, dv, _], [k, v]) => | ||
200 : | assign(y, Op.Field(pruneDim(MV.toDim dv)), [v, k])), | ||
201 : | cchiw | 2379 | |
202 : | |||
203 : | jhr | 2356 | (BV.neg_i, simpleOp(Op.Neg DstTy.IntTy)), |
204 : | (BV.neg_t, tensorOp Op.Neg), | ||
205 : | (BV.neg_f, fn (y, _, xs) => assign(y, Op.NegField, xs)), | ||
206 : | (BV.op_probe, fn (y, [_, dv, sv], xs) => | ||
207 : | assign(y, Op.Probe(dimVarToTensor dv, shapeVarToTensor sv), xs)), | ||
208 : | cchiw | 2379 | |
209 : | cchiw | 2381 | (* Changed*) |
210 : | cchiw | 2384 | (*Um what do I give as the argument to Normalize?*) |
211 : | cchiw | 2381 | (BV.op_D, fn (y, _, xs) => assignEin(y, createFldEin(EinOp.Grad,xs), xs)), |
212 : | (BV.op_Dotimes, fn (y, _, xs) => assignEin(y, createFldEin(EinOp.Divergence,xs), xs)), | ||
213 : | cchiw | 2379 | |
214 : | (* UnChanged*) | ||
215 : | jhr | 2356 | (BV.op_norm, fn (y, [sv], xs) => (case shapeVarToTensor sv |
216 : | of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, xs) | ||
217 : | | ty => assign(y, Op.Norm ty, xs) | ||
218 : | (* end case *))), | ||
219 : | (BV.op_not, simpleOp Op.Not), | ||
220 : | cchiw | 2379 | |
221 : | (* Changed*) | ||
222 : | cchiw | 2381 | (BV.op_cross, fn(y, _ , xs )=> assignEin(y,EinOp.crossProductE ,xs) |
223 : | (BV.op_outer, fn (y, [dv1, dv2], xs) => let | ||
224 : | jhr | 2356 | val d1 = pruneDim(MV.toDim dv1) |
225 : | val d2 = pruneDim(MV.toDim dv2) | ||
226 : | cchiw | 2381 | in assignEin( y, S.transform(EinOp.outerProduct,[(length(d1),d1), (length(d2),d2)]),xs) |
227 : | end | ||
228 : | (*Here, d1, d2 is an int, but we want a list.*) | ||
229 : | cchiw | 2379 | |
230 : | cchiw | 2381 | (* Assuming dd1, ddd2 are lists of dimensions*) |
231 : | cchiw | 2380 | (BV.op_inner, fn (y, [sh1, sh2, _], xs) => let |
232 : | jhr | 2356 | val ty1 as DstTy.TensorTy dd1 = pruneShape sh1 |
233 : | val ty2 as DstTy.TensorTy dd2 = pruneShape sh2 | ||
234 : | cchiw | 2380 | val ilist= [(length(dd1)-1, List.take(dd1, length(dd1)-1) ) , |
235 : | cchiw | 2381 | ( length(dd2)-1, tl(dd2)), |
236 : | ( 0, [hd(dd2)]) ] | ||
237 : | cchiw | 2380 | in assignEin(y, S.tranform(EinOp.innerProduct, ilist),xs) end), |
238 : | |||
239 : | |||
240 : | (BV.op_colon, fn (y, [sh1, sh2, _], xs) => let | ||
241 : | jhr | 2358 | val ty1 as DstTy.TensorTy dd1 = pruneShape sh1 |
242 : | val ty2 as DstTy.TensorTy dd2 = pruneShape sh2 | ||
243 : | cchiw | 2380 | val ilist= [ (length(dd1)-2 , List.take(q, length(q)-2)) , |
244 : | cchiw | 2381 | ( length(dd2)-2 , tl(tl(dd2))), |
245 : | (_, List.take(dd2,2))] | ||
246 : | cchiw | 2380 | in assignEin(y, S.transform(EinOp.doubleDot, ilist),xs) end), |
247 : | |||
248 : | |||
249 : | cchiw | 2379 | (* UnChanged*) |
250 : | jhr | 2356 | (BV.fn_inside, fn (y, [_, dv, _], xs) => |
251 : | assign(y, Op.Inside(pruneDim(MV.toDim dv)), xs)), | ||
252 : | (BV.clamp_rrr, simpleOp (Op.Clamp DstTy.realTy)), | ||
253 : | (BV.clamp_vvv, vectorOp Op.Clamp), | ||
254 : | (BV.lerp3, tensorOp Op.Lerp), | ||
255 : | (BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) => let | ||
256 : | val t1 = IL.Var.new("t1", DstTy.realTy) | ||
257 : | val t2 = IL.Var.new("t2", DstTy.realTy) | ||
258 : | val t3 = IL.Var.new("t3", DstTy.realTy) | ||
259 : | in [ | ||
260 : | IL.ASSGN(t1, IL.OP(Op.Sub DstTy.realTy, [x, x0])), | ||
261 : | IL.ASSGN(t2, IL.OP(Op.Sub DstTy.realTy, [x1, x0])), | ||
262 : | IL.ASSGN(t3, IL.OP(Op.Div DstTy.realTy, [t1, t2])), | ||
263 : | IL.ASSGN(y, IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3])) | ||
264 : | ] end), | ||
265 : | (BV.evals2x2, eigenVal (Op.Eigen2x2, 2)), | ||
266 : | (BV.evals3x3, eigenVal (Op.Eigen3x3, 3)), | ||
267 : | (BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)), | ||
268 : | (BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)), | ||
269 : | (BV.fn_max, simpleOp Op.Max), | ||
270 : | (BV.fn_min, simpleOp Op.Min), | ||
271 : | (BV.fn_modulate, vectorOp Op.Mul), | ||
272 : | (BV.fn_normalize, vectorOp Op.Normalize), | ||
273 : | (BV.fn_principleEvec, vectorOp Op.PrincipleEvec), | ||
274 : | cchiw | 2379 | |
275 : | (* Changed*) | ||
276 : | cchiw | 2381 | (BV.fn_trace, fn (y, [dv], xs) => (let |
277 : | val i=pruneDim(MV.toDim dv) (* (dimVarToMatrix dv)*) | ||
278 : | (*we need i to be the dimension n of an n xn matrix*) | ||
279 : | in assignEin(y,S.transform(EinOp.trace, [1,i]),xs) end) | ||
280 : | cchiw | 2379 | |
281 : | (* UnChanged*) | ||
282 : | (BV.fn_transpose, fn (y, [dv1, dv2], xs) => | ||
283 : | jhr | 2358 | assign(y, Op.Transpose(dimVarToInt dv1, dimVarToInt dv2), xs)), |
284 : | jhr | 2356 | (BV.kn_bspln3, kernel Kernel.bspln3), |
285 : | (BV.kn_bspln5, kernel Kernel.bspln5), | ||
286 : | (BV.kn_ctmr, kernel Kernel.ctmr), | ||
287 : | (BV.kn_c2ctmr, kernel Kernel.ctmr), | ||
288 : | jhr | 2358 | (BV.kn_c4hexic, kernel Kernel.c4hexic), |
289 : | jhr | 2356 | (BV.kn_tent, kernel Kernel.tent), |
290 : | (BV.kn_c1tent, kernel Kernel.tent), | ||
291 : | (BV.i2r, simpleOp Op.IntToReal), | ||
292 : | cchiw | 2379 | |
293 : | (* Changed*) | ||
294 : | cchiw | 2381 | (BV.identity, fn (y, [dv], []) =>(let |
295 : | val i=pruneDim(MV.toDim dv) | ||
296 : | (*we need i to be the dimension n of an n xn matrix*) | ||
297 : | in assignEin(y, S.transform(EinOp.identity,[(1,i),(1,i)]),[]) end)), | ||
298 : | cchiw | 2380 | |
299 : | cchiw | 2379 | (* UnChanged*) |
300 : | jhr | 2356 | (BV.zero, fn (y, [sv], []) => |
301 : | assign(y, Op.Zero(shapeVarToTensor sv), [])), | ||
302 : | jhr | 1640 | (BV.subscript, fn (y, [tv, dv], args) => |
303 : | assign(y, | ||
304 : | jhr | 2358 | Op.SeqSub(DstTy.SeqTy(pruneTy tv, pruneDim(MV.toDim dv))), |
305 : | jhr | 1640 | args)) |
306 : | jhr | 2356 | ]; |
307 : | (* add C math functions *) | ||
308 : | List.app (fn (n, x) => insert(x, basisFn n)) BV.mathFns; | ||
309 : | tbl | ||
310 : | end | ||
311 : | jhr | 180 | |
312 : | jhr | 194 | fun translate (y, f, mvs, xs) = (case VTbl.find tbl f |
313 : | jhr | 2356 | of SOME transFn => transFn(y, mvs, xs) |
314 : | | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f) | ||
315 : | (* end case *)) | ||
316 : | jhr | 1116 | handle ex => (print(concat["translate (", IL.Var.toString y, ", ", |
317 : | Var.uniqueNameOf f, ", ...)\n"]); raise ex) | ||
318 : | jhr | 180 | |
319 : | end |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |