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

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