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 2394 - (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 : jhr 2389 fun createTenEin (operator, sv) = let
90 :     val shape=shapeVarToTensor
91 :     in
92 :     S.transform(operator,[len(shape), shape])
93 :     end
94 :     fun createFldEin(operator, f)=S.transform(operator,[])
95 : cchiw 2378
96 :    
97 : jhr 2389 fun assignEin (y, rator, xs) = [IL.ASSGN(y, IL.EINAPP(R.evalEinApp(rator, xs)))]
98 :     fun simpleEinOp rator (y, [], xs) = assignEin (y, S.transform(rator,[]), xs)
99 :     fun tensorEinOp rator (y, [sv], xs) = assignEin(y, createTenEin(rator, sv), xs)
100 : cchiw 2382
101 : jhr 184 (* build a table that maps Basis variables to their translation functions *)
102 : jhr 1640 val tbl : ((IL.var * Ty.meta_var list * IL.var list) -> IL.assignment list) VTbl.hash_table = let
103 : jhr 2356 val tbl = VTbl.mkTable (128, Fail "Translate table")
104 :     val insert = VTbl.insert tbl
105 :     in
106 :     List.app insert [
107 :     (BV.lt_ii, simpleOp(Op.LT DstTy.IntTy)),
108 :     (BV.lt_rr, simpleOp(Op.LT DstTy.realTy)),
109 :     (BV.lte_ii, simpleOp(Op.LTE DstTy.IntTy)),
110 :     (BV.lte_rr, simpleOp(Op.LTE DstTy.realTy)),
111 :     (BV.gte_ii, simpleOp(Op.GTE DstTy.IntTy)),
112 :     (BV.gte_rr, simpleOp(Op.GTE(DstTy.realTy))),
113 :     (BV.gt_ii, simpleOp(Op.GT DstTy.IntTy)),
114 :     (BV.gt_rr, simpleOp(Op.GT(DstTy.realTy))),
115 :     (BV.equ_bb, simpleOp(Op.EQ DstTy.BoolTy)),
116 :     (BV.equ_ii, simpleOp(Op.EQ DstTy.IntTy)),
117 :     (BV.equ_ss, simpleOp(Op.EQ DstTy.StringTy)),
118 :     (BV.equ_rr, simpleOp(Op.EQ(DstTy.realTy))),
119 :     (BV.neq_bb, simpleOp(Op.NEQ DstTy.BoolTy)),
120 :     (BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)),
121 :     (BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)),
122 :     (BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))),
123 : cchiw 2378
124 :     (* Changed*)
125 :    
126 : cchiw 2386 (BV.add_ii, simpleEinOp(EinOp.addScalars)),
127 :     (BV.add_tt, tensorEinOp(EinOp.addTensors)),
128 : cchiw 2378
129 : cchiw 2383
130 : cchiw 2380 (BV.add_ff, fn (y, _, [f, g])
131 :     =>assignEin(y, createFldEin(EinOp.addField,f ), [f, g])),
132 :     (BV.add_fr, fn (y, _, [f, s])
133 :     => assignEin(y, createFldEin(EinOp.addTenField,f ), [f, s])),
134 :     (BV.add_rf, fn (y, _, [s, f])
135 :     => assignEin(y, createFldEin(EinOp.addTenField,f ), [f, s])),
136 : cchiw 2378
137 : cchiw 2388 (BV.sub_ii, simpleEinOp(EinOp.subScalars)),
138 : cchiw 2386 (BV.sub_tt, tensorEinOp(EinOp.subTensor)),
139 : cchiw 2380 (BV.sub_ff, fn (y, _, [f, g])
140 :     => assignEin(y, createFldEin(EinOp.subField,f), [f, g])),
141 :    
142 : cchiw 2379 (* UnChanged*)
143 : jhr 2358 (BV.sub_fr, fn (y, _, [f, s]) => let
144 :     val s' = IL.Var.copy s
145 :     in [
146 :     IL.ASSGN(s', IL.OP(Op.Neg DstTy.realTy, [s])),
147 :     IL.ASSGN(y, IL.OP(Op.OffsetField, [f, s']))
148 :     ] end),
149 : cchiw 2378
150 : jhr 2358 (BV.sub_rf, fn (y, _, [s, f]) => let
151 :     val f' = IL.Var.copy f
152 :     in [
153 :     IL.ASSGN(f', IL.OP(Op.NegField, [f])),
154 :     IL.ASSGN(y, IL.OP(Op.OffsetField, [f', s]))
155 :     ] end),
156 : cchiw 2378 (* Changed*)
157 : cchiw 2388 (BV.mul_ii, simpleEinOp(EinOp.scalarxscalar)),
158 : cchiw 2386 (BV.mul_rr, simpleEinOp(EinOp.scalarxscalar)),
159 : cchiw 2388 (BV.mul_rt, tensorEinOp(EinOp.scaleTensor)),
160 : cchiw 2378
161 : cchiw 2380 (BV.mul_tr, fn (y, sv, [t, r])
162 : cchiw 2388 =>assignEin(y,createTenEin(EinOp.scaleTensor, sv), [r,t])),
163 : cchiw 2380
164 :    
165 : cchiw 2386 (BV.mul_rf, fn (y, _, [s, f])
166 :     => assignEin(y, createFldEin(EinOp.scaleField,f), [s, f])),
167 :     (BV.mul_fr, fn (y, _, [f, s])
168 :     => assignEin(y, createFldEin(EinOp.scaleField,f), [s, f])),
169 : cchiw 2380
170 : cchiw 2378 (* UnChanged*)
171 :    
172 : jhr 2356 (BV.div_ii, simpleOp(Op.Div DstTy.IntTy)),
173 :     (BV.div_rr, simpleOp(Op.Div DstTy.realTy)),
174 : cchiw 2394 (* Changed *)
175 :     (BV.div_tr, fn (y, [sv], [x, s]) =>
176 :     assignEin(y, createTenEin(EinOp.divideTensor,sv), [x,s])),
177 :     (BV.div_fr, fn (y, _, [f, s]) =>
178 :     assignEin(y, createFldEin(EinOp.divideField,sv), [x,s])),
179 :     (*UnChanged*)
180 : jhr 2356 (BV.exp_ri, simpleOp(Op.Power)),
181 :     (BV.exp_rr, basisFn MathFuns.pow),
182 : cchiw 2378
183 : cchiw 2379 (* Changed*)
184 : cchiw 2384 (*Note curl is in Ein and does not need to normalize.*)
185 : cchiw 2379 (BV.curl2D, fn (y, _, xs) => assignEin(y, EinOp.Curl2d, xs)),
186 :     (BV.curl3D, fn (y, _, xs) => assignEin(y, EinOp.Curl3d, xs)),
187 : cchiw 2378
188 :    
189 : cchiw 2379 (* UnChanged*)
190 : jhr 2356 (BV.convolve_vk, fn (y, [_, dv, _], xs) =>
191 :     assign(y, Op.Field(pruneDim(MV.toDim dv)), xs)),
192 :     (BV.convolve_kv, fn (y, [_, dv, _], [k, v]) =>
193 :     assign(y, Op.Field(pruneDim(MV.toDim dv)), [v, k])),
194 : cchiw 2379
195 :    
196 : jhr 2356 (BV.neg_i, simpleOp(Op.Neg DstTy.IntTy)),
197 :     (BV.neg_t, tensorOp Op.Neg),
198 :     (BV.neg_f, fn (y, _, xs) => assign(y, Op.NegField, xs)),
199 :     (BV.op_probe, fn (y, [_, dv, sv], xs) =>
200 :     assign(y, Op.Probe(dimVarToTensor dv, shapeVarToTensor sv), xs)),
201 : cchiw 2379
202 : cchiw 2381 (* Changed*)
203 : cchiw 2384 (*Um what do I give as the argument to Normalize?*)
204 : cchiw 2381 (BV.op_D, fn (y, _, xs) => assignEin(y, createFldEin(EinOp.Grad,xs), xs)),
205 :     (BV.op_Dotimes, fn (y, _, xs) => assignEin(y, createFldEin(EinOp.Divergence,xs), xs)),
206 : cchiw 2379
207 :     (* UnChanged*)
208 : jhr 2356 (BV.op_norm, fn (y, [sv], xs) => (case shapeVarToTensor sv
209 :     of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, xs)
210 :     | ty => assign(y, Op.Norm ty, xs)
211 :     (* end case *))),
212 :     (BV.op_not, simpleOp Op.Not),
213 : cchiw 2379
214 :     (* Changed*)
215 : cchiw 2388 (BV.op_cross, fn(y, _ , xs )=> assignEin(y,EinOp.crossProductE ,xs))
216 : cchiw 2381 (BV.op_outer, fn (y, [dv1, dv2], xs) => let
217 : jhr 2356 val d1 = pruneDim(MV.toDim dv1)
218 :     val d2 = pruneDim(MV.toDim dv2)
219 : cchiw 2381 in assignEin( y, S.transform(EinOp.outerProduct,[(length(d1),d1), (length(d2),d2)]),xs)
220 : cchiw 2388 end)
221 : cchiw 2381 (*Here, d1, d2 is an int, but we want a list.*)
222 : cchiw 2379
223 : cchiw 2381 (* Assuming dd1, ddd2 are lists of dimensions*)
224 : cchiw 2380 (BV.op_inner, fn (y, [sh1, sh2, _], xs) => let
225 : jhr 2356 val ty1 as DstTy.TensorTy dd1 = pruneShape sh1
226 :     val ty2 as DstTy.TensorTy dd2 = pruneShape sh2
227 : cchiw 2380 val ilist= [(length(dd1)-1, List.take(dd1, length(dd1)-1) ) ,
228 : cchiw 2381 ( length(dd2)-1, tl(dd2)),
229 :     ( 0, [hd(dd2)]) ]
230 : cchiw 2380 in assignEin(y, S.tranform(EinOp.innerProduct, ilist),xs) end),
231 :    
232 :    
233 :     (BV.op_colon, fn (y, [sh1, sh2, _], xs) => let
234 : jhr 2358 val ty1 as DstTy.TensorTy dd1 = pruneShape sh1
235 :     val ty2 as DstTy.TensorTy dd2 = pruneShape sh2
236 : cchiw 2380 val ilist= [ (length(dd1)-2 , List.take(q, length(q)-2)) ,
237 : cchiw 2381 ( length(dd2)-2 , tl(tl(dd2))),
238 : cchiw 2388 (0, List.take(dd2,2))]
239 : cchiw 2380 in assignEin(y, S.transform(EinOp.doubleDot, ilist),xs) end),
240 :    
241 :    
242 : cchiw 2379 (* UnChanged*)
243 : jhr 2356 (BV.fn_inside, fn (y, [_, dv, _], xs) =>
244 :     assign(y, Op.Inside(pruneDim(MV.toDim dv)), xs)),
245 :     (BV.clamp_rrr, simpleOp (Op.Clamp DstTy.realTy)),
246 :     (BV.clamp_vvv, vectorOp Op.Clamp),
247 :     (BV.lerp3, tensorOp Op.Lerp),
248 :     (BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) => let
249 :     val t1 = IL.Var.new("t1", DstTy.realTy)
250 :     val t2 = IL.Var.new("t2", DstTy.realTy)
251 :     val t3 = IL.Var.new("t3", DstTy.realTy)
252 :     in [
253 :     IL.ASSGN(t1, IL.OP(Op.Sub DstTy.realTy, [x, x0])),
254 :     IL.ASSGN(t2, IL.OP(Op.Sub DstTy.realTy, [x1, x0])),
255 :     IL.ASSGN(t3, IL.OP(Op.Div DstTy.realTy, [t1, t2])),
256 :     IL.ASSGN(y, IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3]))
257 :     ] end),
258 :     (BV.evals2x2, eigenVal (Op.Eigen2x2, 2)),
259 :     (BV.evals3x3, eigenVal (Op.Eigen3x3, 3)),
260 :     (BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)),
261 :     (BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)),
262 :     (BV.fn_max, simpleOp Op.Max),
263 :     (BV.fn_min, simpleOp Op.Min),
264 :     (BV.fn_modulate, vectorOp Op.Mul),
265 :     (BV.fn_normalize, vectorOp Op.Normalize),
266 :     (BV.fn_principleEvec, vectorOp Op.PrincipleEvec),
267 : cchiw 2379
268 :     (* Changed*)
269 : cchiw 2381 (BV.fn_trace, fn (y, [dv], xs) => (let
270 :     val i=pruneDim(MV.toDim dv) (* (dimVarToMatrix dv)*)
271 :     (*we need i to be the dimension n of an n xn matrix*)
272 : cchiw 2388 in assignEin(y,S.transform(EinOp.trace, [1,i]),xs) end))
273 : cchiw 2379
274 :     (* UnChanged*)
275 :     (BV.fn_transpose, fn (y, [dv1, dv2], xs) =>
276 : jhr 2358 assign(y, Op.Transpose(dimVarToInt dv1, dimVarToInt dv2), xs)),
277 : jhr 2356 (BV.kn_bspln3, kernel Kernel.bspln3),
278 :     (BV.kn_bspln5, kernel Kernel.bspln5),
279 :     (BV.kn_ctmr, kernel Kernel.ctmr),
280 :     (BV.kn_c2ctmr, kernel Kernel.ctmr),
281 : jhr 2358 (BV.kn_c4hexic, kernel Kernel.c4hexic),
282 : jhr 2356 (BV.kn_tent, kernel Kernel.tent),
283 :     (BV.kn_c1tent, kernel Kernel.tent),
284 :     (BV.i2r, simpleOp Op.IntToReal),
285 : cchiw 2379
286 :     (* Changed*)
287 : cchiw 2381 (BV.identity, fn (y, [dv], []) =>(let
288 :     val i=pruneDim(MV.toDim dv)
289 :     (*we need i to be the dimension n of an n xn matrix*)
290 :     in assignEin(y, S.transform(EinOp.identity,[(1,i),(1,i)]),[]) end)),
291 : cchiw 2380
292 : cchiw 2379 (* UnChanged*)
293 : jhr 2356 (BV.zero, fn (y, [sv], []) =>
294 :     assign(y, Op.Zero(shapeVarToTensor sv), [])),
295 : jhr 1640 (BV.subscript, fn (y, [tv, dv], args) =>
296 :     assign(y,
297 : jhr 2358 Op.SeqSub(DstTy.SeqTy(pruneTy tv, pruneDim(MV.toDim dv))),
298 : jhr 1640 args))
299 : cchiw 2388 ];
300 :    
301 : jhr 2356 (* add C math functions *)
302 :     List.app (fn (n, x) => insert(x, basisFn n)) BV.mathFns;
303 :     tbl
304 :     end
305 : jhr 180
306 : jhr 194 fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
307 : jhr 2356 of SOME transFn => transFn(y, mvs, xs)
308 :     | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f)
309 :     (* end case *))
310 : jhr 1116 handle ex => (print(concat["translate (", IL.Var.toString y, ", ",
311 :     Var.uniqueNameOf f, ", ...)\n"]); raise ex)
312 : jhr 180
313 :     end

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