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 2496 - (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 : jhr 2490 structure EinOp = Operators
27 :     structure S = Specialize
28 : cchiw 2383
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 2496 fun simpleEinOp rator (y, _,xs) = [assignEin(y, rator, xs)]
77 : cchiw 2485
78 : cchiw 2414
79 : jhr 2492 fun tensorEinOp einop (y, [shp], xs) = let
80 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
81 :     val rator =S.transform(einop, [dd1], [])
82 : jhr 2490 in
83 : cchiw 2496 [assignEin(y, rator,xs)]
84 : jhr 2490 end
85 : cchiw 2447
86 :    
87 : cchiw 2496 fun singleField (s, einop, paramarg, y, xs) = let
88 :     val rator= S.transform(einop, [s], paramarg)
89 : jhr 2490 in
90 : cchiw 2496 [assignEin(y, rator, xs)]
91 : jhr 2490 end
92 : cchiw 2414
93 :    
94 :     (* shape is an int list, DIM is in int|variable, k-level of differntiation *)
95 :    
96 : cchiw 2412 (* build a table that maps Basis variables to their translation functions *)
97 : jhr 2490 val tbl : ((IL.var * Ty.meta_arg list * IL.var list) -> IL.assignment list) VTbl.hash_table = let
98 : jhr 2356 val tbl = VTbl.mkTable (128, Fail "Translate table")
99 :     val insert = VTbl.insert tbl
100 :     in
101 :     List.app insert [
102 :     (BV.lt_ii, simpleOp(Op.LT DstTy.IntTy)),
103 :     (BV.lt_rr, simpleOp(Op.LT DstTy.realTy)),
104 :     (BV.lte_ii, simpleOp(Op.LTE DstTy.IntTy)),
105 :     (BV.lte_rr, simpleOp(Op.LTE DstTy.realTy)),
106 :     (BV.gte_ii, simpleOp(Op.GTE DstTy.IntTy)),
107 :     (BV.gte_rr, simpleOp(Op.GTE(DstTy.realTy))),
108 :     (BV.gt_ii, simpleOp(Op.GT DstTy.IntTy)),
109 :     (BV.gt_rr, simpleOp(Op.GT(DstTy.realTy))),
110 :     (BV.equ_bb, simpleOp(Op.EQ DstTy.BoolTy)),
111 :     (BV.equ_ii, simpleOp(Op.EQ DstTy.IntTy)),
112 :     (BV.equ_ss, simpleOp(Op.EQ DstTy.StringTy)),
113 :     (BV.equ_rr, simpleOp(Op.EQ(DstTy.realTy))),
114 :     (BV.neq_bb, simpleOp(Op.NEQ DstTy.BoolTy)),
115 :     (BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)),
116 :     (BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)),
117 :     (BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))),
118 : jhr 2492 (BV.add_ii, simpleOp Op.IAdd),
119 : cchiw 2445 (BV.add_tt, tensorEinOp EinOp.addTensor),
120 : cchiw 2496 (BV.add_ff, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
121 :     singleField(dd,EinOp.addField, [d,d], y, xs)),
122 :     (BV.add_fr, fn (y, [_,Ty.DIM d], xs) =>
123 :     singleField([],EinOp.addTenField,[d],y, xs)),
124 :     (BV.add_rf, fn (y, [_,Ty.DIM d], [s, f]) =>
125 :     singleField([],EinOp.addTenField,[d],y,[f,s])),
126 : jhr 2492 (BV.sub_ii, simpleOp Op.ISub),
127 : cchiw 2445 (BV.sub_tt, tensorEinOp EinOp.subTensor),
128 : cchiw 2496 (BV.sub_ff, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
129 :     singleField(dd,EinOp.subField, [d,d],y,xs)),
130 :     (BV.sub_fr, fn (y, [_,Ty.DIM d], xs) =>
131 :     singleField([],EinOp.subFieldTen,[d], y,xs)),
132 :     (BV.sub_rf, fn (y, [_,Ty.DIM d], [s, f]) =>
133 :     singleField([],EinOp.subTenField,[d], y,[f, s])),
134 :    
135 : cchiw 2447 (BV.mul_ii, simpleOp Op.IMul),
136 : cchiw 2496 (BV.mul_rr, fn (y,_,args) => [assignEin(y, EinOp.prodScalar,args)]),
137 : cchiw 2445 (BV.mul_rt, tensorEinOp EinOp.scaleTensor),
138 : jhr 2492 (BV.mul_tr, fn (y, [shp], [t, r]) => let
139 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
140 :     val rator = S.transform(EinOp.scaleTensor, [dd1], [])
141 :     in
142 : cchiw 2496 [assignEin(y, rator,[r,t])]
143 : jhr 2492 end),
144 : cchiw 2496 (BV.mul_rf, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) =>
145 :     singleField(dd,EinOp.scaleField,[d],y, xs)),
146 :     (BV.mul_fr, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [f, s]) =>
147 :     singleField(dd,EinOp.scaleField, [d],y,[s,f])),
148 : cchiw 2447 (BV.div_ii, simpleOp Op.IDiv),
149 : cchiw 2496 (BV.div_rr, fn (y,_,args) => [assignEin(y, EinOp.divScalar,args)]),
150 : jhr 2492 (BV.div_tr, tensorEinOp EinOp.divideTensor),
151 : cchiw 2496 (BV.div_fr, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => singleField(dd,EinOp.divideField, [d],y, xs)),
152 : cchiw 2445 (BV.exp_ri, simpleOp(Op.Power)),
153 :     (BV.exp_rr, basisFn MathFuns.pow),
154 :     (BV.curl2D, simpleEinOp EinOp.Curl2d),
155 :     (BV.curl3D, simpleEinOp EinOp.Curl3d),
156 : cchiw 2496 (BV.convolve_vk, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
157 :     singleField(dd,EinOp.conv, [d],y,xs)),
158 :     (BV.convolve_kv, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [k, v]) =>
159 :     singleField(dd,EinOp.conv, [d],y,[v,k])),
160 : cchiw 2447 (BV.neg_i, simpleOp Op.INeg),
161 :     (BV.neg_t, tensorEinOp EinOp.negTensor),
162 : cchiw 2496 (BV.neg_f, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => singleField(dd,EinOp.negField, [d],y,xs)),
163 :     (BV.op_probe, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) => let
164 :     val einop= S.transform(EinOp.probe,[dd,[d]],[d])
165 : jhr 2492 in
166 : cchiw 2496 [assignEin(y, einop,xs)]
167 : jhr 2492 end),
168 : cchiw 2496 (BV.op_D, fn (y, [_, Ty.DIM d], xs) => singleField([],EinOp.Grad,[d], y,xs)),
169 :     (BV.op_Dotimes, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) => singleField(dd,EinOp.Divergence,[d1], y,xs)),
170 : jhr 2356 (BV.op_norm, fn (y, [sv], xs) => (case shapeVarToTensor sv
171 :     of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, xs)
172 :     | ty => assign(y, Op.Norm ty, xs)
173 :     (* end case *))),
174 : jhr 2492 (BV.op_not, simpleOp Op.Not),
175 : cchiw 2445 (BV.op_cross, simpleEinOp EinOp.crossProduct),
176 : jhr 2492 (BV.op_outer, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
177 : cchiw 2496 [assignEin(y, S.transform(EinOp.outerProduct, [[d1], [d2]], []), xs)]),
178 : jhr 2492 (BV.op_inner, fn (y, [sh1, sh2, _], xs) => let
179 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
180 :     val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
181 :     val alpha= List.take(dd1, length(dd1)-1)
182 :     val beta= tl(dd2)
183 :     val i =hd(dd2)
184 :     val ilist= [alpha, beta, [i]]
185 : cchiw 2414 in
186 : cchiw 2496 [assignEin(y, S.transform(EinOp.innerProduct, ilist,[]),xs)]
187 : jhr 2492 end),
188 :     (BV.op_colon, fn (y, [sh1, sh2, _], xs) => let
189 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
190 :     val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
191 :     val i::j::beta = dd2
192 :     val alpha = List.take(dd1, length(dd1)-2)
193 :     val ilist = [alpha, beta, [i], [j]]
194 :     in
195 : cchiw 2496 [assignEin(y, S.transform(EinOp.doubleDot, ilist,[]),xs)]
196 : jhr 2492 end),
197 :     (BV.fn_inside, fn (y, [_, Ty.DIM d, _], xs) => assign(y, Op.Inside d, xs)),
198 : jhr 2356 (BV.clamp_rrr, simpleOp (Op.Clamp DstTy.realTy)),
199 :     (BV.clamp_vvv, vectorOp Op.Clamp),
200 :     (BV.lerp3, tensorOp Op.Lerp),
201 : jhr 2492 (BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) => let
202 : jhr 2356 val t1 = IL.Var.new("t1", DstTy.realTy)
203 :     val t2 = IL.Var.new("t2", DstTy.realTy)
204 :     val t3 = IL.Var.new("t3", DstTy.realTy)
205 :     in [
206 : cchiw 2496 assignEin(t1, EinOp.subScalar,[x,x0]),
207 :     assignEin(t2, EinOp.subScalar,[x1,x0]),
208 :     assignEin(t3, EinOp.divScalar,[t1,t2]),
209 : jhr 2356 IL.ASSGN(y, IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3]))
210 :     ] end),
211 :     (BV.evals2x2, eigenVal (Op.Eigen2x2, 2)),
212 :     (BV.evals3x3, eigenVal (Op.Eigen3x3, 3)),
213 :     (BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)),
214 :     (BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)),
215 :     (BV.fn_max, simpleOp Op.Max),
216 :     (BV.fn_min, simpleOp Op.Min),
217 : cchiw 2496
218 :     (* modulate is vector * vector pointwise multiplication *)
219 :     (BV.fn_modulate, fn (y,[sh1], xs) =>let
220 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
221 :     in [assignEin(y, S.transform(EinOp.modulate, [dd1],[]),xs)]
222 :     end),
223 : jhr 2356 (BV.fn_normalize, vectorOp Op.Normalize),
224 :     (BV.fn_principleEvec, vectorOp Op.PrincipleEvec),
225 : jhr 2492 (BV.fn_trace, fn (y, [Ty.DIM d], xs) =>
226 : cchiw 2496 [assignEin(y,S.transform(EinOp.trace, [[d]],[]), xs)]),
227 : jhr 2492 (BV.fn_transpose, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
228 : cchiw 2496 [assignEin(y, S.transform(EinOp.transpose, [[d1],[d2]],[]), 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 : cchiw 2496 (BV.identity, fn (y, [Ty.DIM d], xs) =>
238 :     [assignEin(y, S.transform(EinOp.identity, [[d],[d]],[]), xs)]),
239 : cchiw 2447 (* (BV.zero, fn (y, [sv], []) =>
240 :     assign(y, Op.Zero(shapeVarToTensor sv), [])),*)
241 : jhr 2492 (BV.subscript, fn (y, [ty, Ty.DIM d], xs) =>
242 :     assign (y, Op.SeqSub(DstTy.SeqTy(trType ty, d)), xs))
243 : jhr 2490 (*,
244 :     (BV.dynSubscript, fn (y, [tv], args) =>
245 :     assign(y, Op.SeqSub(DstTy.DynSeqTy(pruneTy tv)), args))
246 :     *)
247 : cchiw 2388 ];
248 : jhr 2356 (* add C math functions *)
249 :     List.app (fn (n, x) => insert(x, basisFn n)) BV.mathFns;
250 :     tbl
251 :     end
252 : jhr 180
253 : jhr 194 fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
254 : jhr 2356 of SOME transFn => transFn(y, mvs, xs)
255 :     | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f)
256 :     (* end case *))
257 : jhr 1116 handle ex => (print(concat["translate (", IL.Var.toString y, ", ",
258 :     Var.uniqueNameOf f, ", ...)\n"]); raise ex)
259 : jhr 180
260 :     end

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