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 2490 - (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 2490 fun tensorEinOp einop (y, [SK], xs) = let
86 :     val ty1 as DstTy.TensorTy dd1 = pruneShape SK
87 :     val rator =S.transform(einop,[dd1],[])
88 :     in
89 :     assignEin(y, rator,xs)
90 :     end
91 : cchiw 2447
92 : jhr 2490 fun scalarField (einop, paramarg, y, xs) = let
93 :     val paramarg' = List.map dimVarToInt paramarg(*[2]*)
94 :     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 :     val rator= S.transform(einop, [s],paramarg')
103 :     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 : cchiw 2447 (BV.add_ii, simpleOp Op.IAdd ),
136 : cchiw 2445 (BV.add_tt, tensorEinOp EinOp.addTensor),
137 : cchiw 2447 (BV.add_ff, fn (y, [_,NK,SK], xs)=> singleField(SK,EinOp.addField, [NK,NK], y, xs)),
138 : cchiw 2445 (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 : cchiw 2447 (BV.sub_ii, simpleOp Op.ISub ),
141 : cchiw 2445 (BV.sub_tt, tensorEinOp EinOp.subTensor),
142 : cchiw 2447 (BV.sub_ff, fn (y, [_,NK,SK], xs)=>singleField(SK,EinOp.subField, [NK,NK],y,xs)),
143 : cchiw 2445 (BV.sub_fr, fn (y, [_,NK], xs) => scalarField(EinOp.subField,[NK], y,xs)),
144 :     (BV.sub_rf, fn (y, [_,NK], [s, f]) => scalarField(EinOp.subField,[NK], y,[f, s])),
145 : cchiw 2485
146 :     (* CHANGE create negative subtraction*)
147 : cchiw 2447 (BV.mul_ii, simpleOp Op.IMul),
148 : cchiw 2485 (BV.mul_rr, fn (y,_,args)=>
149 :     assignEin(y, EinOp.prodScalar,args)),
150 :    
151 : cchiw 2445 (BV.mul_rt, tensorEinOp EinOp.scaleTensor),
152 :     (BV.mul_tr, fn (y, [SK], [t, r])=>let
153 :     val ty1 as DstTy.TensorTy dd1 = pruneShape SK
154 :     val rator =S.transform(EinOp.scaleTensor,[dd1],[])
155 :     in
156 :     assignEin(y, rator,[r,t])
157 :     end),
158 : cchiw 2439
159 :    
160 :    
161 : cchiw 2447 (BV.mul_rf, fn (y,[_,NK,SK], xs)=> singleField(SK,EinOp.scaleField,[NK],y, xs)),
162 : cchiw 2439
163 :    
164 : cchiw 2380
165 : cchiw 2445 (BV.mul_fr, fn (y, [_,NK,SK], [f, s])=> singleField(SK,EinOp.scaleField, [NK],y,[s,f])),
166 :    
167 : cchiw 2447 (BV.div_ii, simpleOp Op.IDiv),
168 : cchiw 2485 (BV.div_rr, fn (y,_,args)=>
169 :     assignEin(y, EinOp.divScalar,args)),
170 : cchiw 2378
171 : cchiw 2445 (BV.div_tr, tensorEinOp EinOp.divideTensor),
172 : cchiw 2378
173 : cchiw 2445 (BV.div_fr, fn (y, [_,NK,SK], xs) =>singleField(SK,EinOp.divideField, [NK],y, xs)),
174 :     (BV.exp_ri, simpleOp(Op.Power)),
175 :     (BV.exp_rr, basisFn MathFuns.pow),
176 :     (BV.curl2D, simpleEinOp EinOp.Curl2d),
177 :     (BV.curl3D, simpleEinOp EinOp.Curl3d),
178 : cchiw 2447 (BV.convolve_vk, fn (y, [_, NK, SK], xs) => singleField(SK,EinOp.conv, [NK],y,xs)),
179 :     (BV.convolve_kv, fn (y, [_, NK, SK], [k, v]) =>singleField(SK,EinOp.conv, [NK],y,[v,k])),
180 :     (BV.neg_i, simpleOp Op.INeg),
181 :     (BV.neg_t, tensorEinOp EinOp.negTensor),
182 : cchiw 2400
183 : cchiw 2447
184 :     (BV.neg_f, fn (y, [_,NK,SK], xs) => singleField(SK,EinOp.negField, [NK],y,xs)),
185 :    
186 : cchiw 2445 (BV.op_probe, fn (y, [_, NK, SK], xs) =>
187 : cchiw 2414 let
188 : cchiw 2447 val dv=dimVarToTensor NK (*Field*)
189 :     val sv=shapeVarToTensor SK (*Position*)
190 :     val dv'=peelFieldSK dv
191 :     val sv'=peelFieldSK sv
192 :     val einop= S.transform(EinOp.probe,[sv',dv'],[2])
193 : cchiw 2445 in assignEin(y, einop,xs)
194 : cchiw 2414 end),
195 : cchiw 2445 (BV.op_D, fn (y, [_, NK], xs) => scalarField(EinOp.Grad,[NK], y,xs)),
196 :     (BV.op_Dotimes, fn (y, [_, NK, SK, NK2], xs) => singleField(SK,EinOp.Divergence,[NK], y,xs)),
197 : jhr 2356 (BV.op_norm, fn (y, [sv], xs) => (case shapeVarToTensor sv
198 :     of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, xs)
199 :     | ty => assign(y, Op.Norm ty, xs)
200 :     (* end case *))),
201 : cchiw 2379
202 : cchiw 2445 (BV.op_not, simpleOp Op.Not),
203 :     (BV.op_cross, simpleEinOp EinOp.crossProduct),
204 : cchiw 2414 (BV.op_outer, fn (y, [dv1, dv2], xs) => let
205 :     val d1 = [pruneDim(MV.toDim dv1)]
206 :     val d2 = [pruneDim(MV.toDim dv2)]
207 :     in
208 :     assignEin( y, S.transform(EinOp.outerProduct,[d1,d2],[]),xs)
209 : cchiw 2447 end),
210 : cchiw 2412 (BV.op_inner, fn (y, [sh1, sh2, _], xs) =>
211 :     let
212 :     val ty1 as DstTy.TensorTy dd1 = pruneShape sh1
213 :     val ty2 as DstTy.TensorTy dd2 = pruneShape sh2
214 :     val alpha= List.take(dd1, length(dd1)-1)
215 :     val beta= tl(dd2)
216 :     val i =hd(dd2)
217 : cchiw 2447 val ilist= [alpha, beta, [i]]
218 : cchiw 2412 in
219 : cchiw 2447 assignEin(y, S.transform(EinOp.innerProduct, ilist,[]),xs)
220 : cchiw 2412 end),
221 : cchiw 2380
222 : cchiw 2412 (BV.op_colon, fn (y, [sh1, sh2, _], xs) =>
223 :     let
224 :     val ty1 as DstTy.TensorTy dd1 = pruneShape sh1
225 :     val ty2 as DstTy.TensorTy dd2 = pruneShape sh2
226 :     val m= List.take(dd2,2)
227 :     val i=hd(m)
228 :     val j=tl(m)
229 :     val alpha= List.take(dd1, length(dd1)-2)
230 :     val beta=tl(tl(dd2))
231 : cchiw 2447 val ilist=[alpha, beta, [i], j]
232 : cchiw 2412 in
233 :     assignEin(y, S.transform(EinOp.doubleDot, ilist,[]),xs)
234 :     end),
235 : cchiw 2380
236 : jhr 2356 (BV.fn_inside, fn (y, [_, dv, _], xs) =>
237 :     assign(y, Op.Inside(pruneDim(MV.toDim dv)), xs)),
238 :     (BV.clamp_rrr, simpleOp (Op.Clamp DstTy.realTy)),
239 :     (BV.clamp_vvv, vectorOp Op.Clamp),
240 :     (BV.lerp3, tensorOp Op.Lerp),
241 : cchiw 2485 (BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) =>let
242 :     (*assignEin(y, EinOp.subScalar,[x,x0])),*)
243 :    
244 : jhr 2356 val t1 = IL.Var.new("t1", DstTy.realTy)
245 :     val t2 = IL.Var.new("t2", DstTy.realTy)
246 :     val t3 = IL.Var.new("t3", DstTy.realTy)
247 :     in [
248 : cchiw 2485 assignEin2(t1, EinOp.subScalar,[x,x0]),
249 :     assignEin2(t2, EinOp.subScalar,[x1,x0]),
250 :     assignEin2(t3, EinOp.divScalar,[t1,t2]),
251 :    
252 : jhr 2356 IL.ASSGN(y, IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3]))
253 :     ] end),
254 :     (BV.evals2x2, eigenVal (Op.Eigen2x2, 2)),
255 :     (BV.evals3x3, eigenVal (Op.Eigen3x3, 3)),
256 :     (BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)),
257 :     (BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)),
258 :     (BV.fn_max, simpleOp Op.Max),
259 :     (BV.fn_min, simpleOp Op.Min),
260 : jhr 2490 (BV.fn_modulate, fn (y, _, xs) => assign (y, Op.IMul, xs)),
261 : jhr 2356 (BV.fn_normalize, vectorOp Op.Normalize),
262 :     (BV.fn_principleEvec, vectorOp Op.PrincipleEvec),
263 : cchiw 2379
264 : jhr 2490 (BV.fn_trace, fn (y, [dv], xs) => let
265 : cchiw 2445 val d=dimVarToInt dv
266 : cchiw 2412 in
267 : cchiw 2447 assignEin(y,S.transform(EinOp.trace, [[d]],[]),xs)
268 : cchiw 2445 end),
269 : jhr 2490 (BV.fn_transpose, fn (y, [dv1, dv2], xs) => let
270 : cchiw 2412 val d1=dimVarToInt dv1
271 :     val d2=dimVarToInt dv2
272 :     in
273 :     assignEin(y,S.transform(EinOp.transpose, [[d1],[d2]],[]),xs)
274 :     end),
275 : jhr 2356 (BV.kn_bspln3, kernel Kernel.bspln3),
276 :     (BV.kn_bspln5, kernel Kernel.bspln5),
277 :     (BV.kn_ctmr, kernel Kernel.ctmr),
278 :     (BV.kn_c2ctmr, kernel Kernel.ctmr),
279 : jhr 2358 (BV.kn_c4hexic, kernel Kernel.c4hexic),
280 : jhr 2356 (BV.kn_tent, kernel Kernel.tent),
281 :     (BV.kn_c1tent, kernel Kernel.tent),
282 :     (BV.i2r, simpleOp Op.IntToReal),
283 : cchiw 2447 (BV.identity, fn (y, [dv], xs) =>let
284 : cchiw 2445 val d=dimVarToInt dv
285 :     in assignEin(y, S.transform(EinOp.identity,[[d],[d]],[]),xs)
286 :     end),
287 : cchiw 2447 (* (BV.zero, fn (y, [sv], []) =>
288 :     assign(y, Op.Zero(shapeVarToTensor sv), [])),*)
289 : jhr 2490 (BV.subscript, fn (y, [tv, dv], xs) =>
290 : jhr 1640 assign(y,
291 : jhr 2358 Op.SeqSub(DstTy.SeqTy(pruneTy tv, pruneDim(MV.toDim dv))),
292 : cchiw 2447 xs))
293 : jhr 2490 (*,
294 :     (BV.dynSubscript, fn (y, [tv], args) =>
295 :     assign(y, Op.SeqSub(DstTy.DynSeqTy(pruneTy tv)), args))
296 :     *)
297 : cchiw 2388 ];
298 : jhr 2356 (* add C math functions *)
299 :     List.app (fn (n, x) => insert(x, basisFn n)) BV.mathFns;
300 :     tbl
301 :     end
302 : jhr 180
303 : jhr 194 fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
304 : jhr 2356 of SOME transFn => transFn(y, mvs, xs)
305 :     | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f)
306 :     (* end case *))
307 : jhr 1116 handle ex => (print(concat["translate (", IL.Var.toString y, ", ",
308 :     Var.uniqueNameOf f, ", ...)\n"]); raise ex)
309 : jhr 180
310 :     end

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