Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/translate/translate-basis.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/translate/translate-basis.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3494 - (view) (download)

1 : jhr 3471 (* translate-basis.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 :     * All rights reserved.
7 :     *
8 : jhr 3478 * Translation for basis operations in Simple AST to HighIR code
9 : jhr 3471 *)
10 :    
11 :     structure TranslateBasis : sig
12 :    
13 :     (* translate(lhs, f, mvs, args) translates the application of f (specialized
14 :     * to the instantiated meta variables mvs) to a list of SSA assignments in
15 :     * reverse order.
16 :     *)
17 : jhr 3478 val translate : (HighIR.var * Var.t * SimpleTypes.meta_arg list * HighIR.var list)
18 :     -> HighIR.assignment list
19 : jhr 3471
20 :     end = struct
21 :    
22 :     structure BV = BasisVars
23 : jhr 3478 structure IR = HighIR
24 :     structure DstTy = HighTypes
25 : jhr 3471 structure Op = HighOps
26 :     structure Ty = SimpleTypes
27 :     structure VTbl = Var.Tbl
28 : jhr 3478 structure Mk = MkOperators
29 : jhr 3471
30 :     fun trType (Ty.TY ty) = TranslateTy.tr ty
31 :     | trType _ = raise Fail "expected type"
32 :     fun dimVarToInt (Ty.DIM d) = d
33 :     | dimVarToInt _ = raise Fail "expected dim"
34 :     fun dimVarToTensor dv = DstTy.tensorTy[dimVarToInt dv]
35 :     fun dimVarToMatrix dv = let
36 :     val d = dimVarToInt dv
37 :     in
38 :     DstTy.tensorTy[d, d] (* square matrix type *)
39 :     end
40 :     fun shapeVarToTensor (Ty.SHAPE shp) = DstTy.tensorTy shp
41 :     | shapeVarToTensor _ = raise Fail "expected shape"
42 :    
43 : jhr 3478 fun assign (y, rator, xs) = [IR.ASSGN(y, IR.OP(rator, xs))]
44 : jhr 3471
45 :     fun simpleOp rator (y, [], xs) = assign (y, rator, xs)
46 :    
47 :     fun tensorOp rator (y, [sv], xs) = assign (y, rator(shapeVarToTensor sv), xs)
48 :    
49 :     fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs)
50 :    
51 :     fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), [])
52 :    
53 :     (* utility functions for synthesizing eigenvector/eigenvalue code *)
54 :     fun eigenVec (rator, dim) = let
55 : jhr 3478 val ty = DstTy.SeqTy(DstTy.realTy, SOME dim)
56 : jhr 3471 in
57 :     fn (y, _, [m]) => let
58 : jhr 3478 val v = IR.Var.new("evals", ty)
59 : jhr 3471 in
60 : jhr 3478 [IR.MASSGN([v, y], rator, [m])]
61 : jhr 3471 end
62 :     end
63 :     fun eigenVal (rator, dim) = let
64 : jhr 3478 val ty = DstTy.SeqTy(DstTy.vecTy dim, SOME dim)
65 : jhr 3471 in
66 :     fn (y, _, [m]) => let
67 : jhr 3478 val v = IR.Var.new("evecs", ty)
68 : jhr 3471 in
69 : jhr 3478 [IR.MASSGN([y, v], rator, [m])]
70 : jhr 3471 end
71 :     end
72 :    
73 : jhr 3478 fun assignEin (y, rator, xs) = IR.ASSGN(y, IR.EINAPP(rator, xs))
74 :    
75 :     fun simpleEOp rator (y, _, xs) = [assignEin(y, rator, xs)]
76 :    
77 : cchiw 3494 fun dist(y,dd1,xs)= let
78 :     val t0 = IL.Var.new("t0", DstTy.TensorTy(dd1))
79 :     in [assignEin(t0, Mk.subTT(dd1),xs),assignEin(y, Mk.normTT(dd1),[t0])] end
80 :    
81 :    
82 : jhr 3471 (* build a table that maps Basis variables to their translation functions *)
83 : jhr 3478 val tbl : ((IR.var * Ty.meta_arg list * IR.var list) -> IR.assignment list) VTbl.hash_table = let
84 : jhr 3471 val tbl = VTbl.mkTable (128, Fail "Translate table")
85 : jhr 3478 fun insert (id, def) = (case VTbl.find tbl id
86 :     of NONE => VTbl.insert tbl (id, def)
87 :     | SOME _ => raise Fail("duplicate definition of " ^ Var.nameOf id)
88 :     (* end case *))
89 : jhr 3471 in
90 :     List.app insert [
91 :     (BV.at_Td, fn (y, [tv], args) => assign(y, Op.Prepend(trType tv), args)),
92 :     (BV.at_dT, fn (y, [tv], args) => assign(y, Op.Append(trType tv), args)),
93 :     (BV.at_dd, fn (y, [tv], args) => assign(y, Op.Concat(trType tv), args)),
94 :     (BV.lt_ii, simpleOp(Op.LT DstTy.IntTy)),
95 :     (BV.lt_rr, simpleOp(Op.LT DstTy.realTy)),
96 :     (BV.lte_ii, simpleOp(Op.LTE DstTy.IntTy)),
97 :     (BV.lte_rr, simpleOp(Op.LTE DstTy.realTy)),
98 :     (BV.gte_ii, simpleOp(Op.GTE DstTy.IntTy)),
99 :     (BV.gte_rr, simpleOp(Op.GTE(DstTy.realTy))),
100 :     (BV.gt_ii, simpleOp(Op.GT DstTy.IntTy)),
101 :     (BV.gt_rr, simpleOp(Op.GT(DstTy.realTy))),
102 :     (BV.equ_bb, simpleOp(Op.EQ DstTy.BoolTy)),
103 :     (BV.equ_ii, simpleOp(Op.EQ DstTy.IntTy)),
104 :     (BV.equ_ss, simpleOp(Op.EQ DstTy.StringTy)),
105 :     (BV.equ_rr, simpleOp(Op.EQ(DstTy.realTy))),
106 :     (BV.neq_bb, simpleOp(Op.NEQ DstTy.BoolTy)),
107 :     (BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)),
108 :     (BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)),
109 :     (BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))),
110 : jhr 3476 (BV.add_ii, simpleOp Op.IAdd),
111 :     (BV.sub_ii, simpleOp Op.ISub),
112 :     (BV.mul_ii, simpleOp Op.IMul),
113 :     (BV.div_ii, simpleOp Op.IDiv),
114 : jhr 3478 (BV.op_mod, simpleOp Op.IMod),
115 :     (BV.neg_i, simpleOp Op.INeg),
116 :     (BV.add_tt, fn (y, [shp], xs) => let
117 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
118 :     val rator = Mk.addTT(dd1)
119 :     in
120 :     [assignEin(y, rator, xs)]
121 :     end),
122 :     (BV.add_ff, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
123 :     [assignEin(y, Mk.addFF(d, dd), xs)]),
124 :     (BV.add_ft, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [f, s]) =>
125 :     [assignEin(y, Mk.addTF(d, dd), [s, f])]),
126 :     (BV.add_tf, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
127 :     [assignEin(y, Mk.addTF(d, dd), xs)]),
128 :     (BV.sub_tt, fn (y, [shp], xs) => let
129 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
130 :     val rator = Mk.subTT dd1
131 :     in
132 :     [assignEin(y, rator, xs)]
133 :     end),
134 :     (BV.sub_ff, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
135 :     [assignEin(y, Mk.subFF(d, dd), xs)]),
136 :     (BV.sub_ft, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [x1, x2]) =>
137 :     [assignEin(y, Mk.subFT(d, dd), [x2, x1])]),
138 :     (BV.sub_tf, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
139 :     [assignEin(y, Mk.subTF(d, dd), xs)]),
140 :     (BV.mul_rr, fn (y, _, args) => [assignEin(y, Mk.mulRR, args)]),
141 :     (BV.mul_rt, fn (y, [shp], xs) => let
142 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
143 :     val rator = Mk.mulRT(dd1)
144 :     in
145 :     [assignEin(y, rator, xs)]
146 :     end),
147 :     (BV.mul_tr, fn (y, [shp], [t, r]) => let
148 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
149 :     val rator = Mk.mulRT dd1
150 :     in
151 :     [assignEin(y, rator, [r, t])]
152 :     end),
153 :     (BV.mul_rf, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
154 :     [assignEin(y, Mk.mulRF(d, dd), xs)]),
155 :     (BV.mul_fr, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [f, s]) =>
156 :     [assignEin(y, Mk.mulRF(d, dd), [s, f])]),
157 :     (BV.mul_ss, fn (y, [_, Ty.DIM d], xs) =>
158 :     [assignEin(y, Mk.mulSS d, xs)]),
159 :     (BV.mul_sf, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
160 :     [assignEin(y, Mk.mulSF(d, dd), xs)]),
161 :     (BV.mul_fs, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [s, f]) =>
162 :     [assignEin(y, Mk.mulSF(d, dd), [f, s])]),
163 :     (BV.mul_st, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
164 :     [assignEin(y, Mk.mulST(d, dd), List.rev xs)]),
165 :     (BV.mul_ts, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
166 :     [assignEin(y, Mk.mulST(d, dd), xs)]),
167 :     (BV.div_rr, fn (y, _, args) => [assignEin(y, Mk.divRR, args)]),
168 :     (BV.div_tr, fn (y, [shp], xs) => let
169 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
170 :     val rator = Mk.divTR dd1
171 :     in
172 :     [assignEin(y, rator, xs)]
173 :     end),
174 :     (BV.div_fr, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
175 :     [assignEin(y, Mk.divFR(d, dd), xs)]),
176 :     (BV.div_fs, fn (y, [_, _, Ty.DIM d, Ty.SHAPE dd], xs) =>
177 :     [assignEin(y, Mk.divFS(d, dd), xs)]),
178 :     (BV.div_ss, fn (y, [_, Ty.DIM d], xs) =>
179 :     [assignEin(y, Mk.divSS d, xs)]),
180 : jhr 3482 (BV.pow_ri, simpleOp Op.Power),
181 : jhr 3483 (* FIXME: BV.pos_rr *)
182 :     (BV.pow_si, fn (y, [_, Ty.DIM d1], [f, n]) => let
183 :     fun getN x = (case IR.Var.binding x
184 :     of IR.VB_RHS(IR.LIT(Literal.Int n)) => IntInf.toInt n
185 :     | vb => raise Fail (String.concat[
186 :     (* FIXME: should catch this earlier, where we can generate an error message! *)
187 :     "Not a constant integer, ", IR.Var.toString x,
188 :     " found ", IR.vbToString vb, "\n"
189 :     ])
190 :     (* end case *))
191 :     in
192 :     [assignEin(y, Mk.powF(d1, getN n), [f])]
193 :     end),
194 : jhr 3478 (BV.curl2D, simpleEOp Mk.curl2d),
195 :     (BV.curl3D, simpleEOp Mk.curl3d),
196 : jhr 3471 (BV.convolve_vk, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
197 : jhr 3478 [assignEin(y, Mk.conv(d, dd), xs)]),
198 :     (BV.convolve_kv, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
199 :     [assignEin(y, Mk.conv(d, dd), xs)]),
200 :     (BV.neg_t, fn (y, [shp], xs) => let
201 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
202 :     val rator = Mk.negTT dd1
203 :     in
204 :     [assignEin(y, rator, xs)]
205 :     end),
206 :     (BV.neg_f, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
207 :     [assignEin(y, Mk.negFF(d, dd), xs)]),
208 :     (BV.op_probe, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
209 :     [assignEin(y, (Mk.probe(dd, d)), xs)]),
210 :     (BV.op_D, fn (y, [_, Ty.DIM d], xs) =>
211 :     if (d = 2) orelse (d = 3)
212 :     then [assignEin(y, Mk.grad [d], xs)]
213 :     else raise Fail "unsupported gradient"),
214 :     (BV.op_Dotimes, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) =>
215 :     [assignEin(y, Mk.dotimes(d1, dd@[d2]), xs)]),
216 :     (BV.op_Ddot, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) =>
217 :     [assignEin(y, Mk.divergence(d1, dd), xs)] ),
218 : cchiw 3494 (BV.op_norm_t, fn (y, [sv], xs) =>let
219 :     val DstTy.TensorTy dd1 = shapeVarToTensor sv
220 :     in (case (dd1,xs)
221 :     of ([],[arg0])=> [IL.ASSGN(y,IL.VAR arg0)]
222 :     | _ => [assignEin(y, Mk.normT(dd1),xs)])
223 :     end),
224 :     (BV.op_norm_f, fn (y, [ _,Ty.DIM d1, Ty.SHAPE dd1], xs) =>
225 :     (case (dd1,xs)
226 :     of ([],[arg0])=> [IL.ASSGN(y,IL.VAR arg0)]
227 :     | _ => [assignEin(y, Mk.normF(d1, dd1),xs)])),
228 : jhr 3478 (BV.op_not, simpleOp Op.Not),
229 :     (BV.op_cross3_tt, simpleEOp Mk.cross3TT),
230 :     (BV.op_cross2_tt, simpleEOp Mk.cross2TT),
231 :     (BV.op_cross2_ff, simpleEOp Mk.cross2FF),
232 :     (BV.op_cross3_ff, simpleEOp Mk.cross3FF),
233 :     (BV.op_outer_tt, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
234 :     [assignEin(y, (Mk.outerTT(d1, d2)), xs)]),
235 :     (BV.op_outer_mt, fn (y, [Ty.DIM d1, Ty.DIM d2, Ty.DIM d3], xs) =>
236 :     [assignEin(y, (Mk.outerMT([d1, d2], d3)), xs)]),
237 :     (BV.op_outer_tm, fn (y, [Ty.DIM d1, Ty.DIM d2, Ty.DIM d3], xs) =>
238 :     [assignEin(y, (Mk.outerTM(d1, [ d2, d3])), xs)]),
239 :     (BV.op_outer_ff, fn (y, [_, Ty.DIM d1, Ty.DIM i, Ty.DIM j], xs)=>
240 :     [assignEin(y, Mk.outerFF(d1, i, j), xs)]),
241 :     (BV.op_inner_tt, fn (y, [sh1, sh2, _], xs) => let
242 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
243 :     val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
244 :     in
245 :     [assignEin(y, (Mk.innerTT(dd1, dd2)), xs)]
246 :     end),
247 :     (BV.op_inner_tf, fn (y, [_, Ty.DIM d, sh1, Ty.SHAPE dd2, _], xs) =>let
248 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
249 :     in
250 :     [assignEin(y, Mk.innerTF(dd1, d, dd2), xs)]
251 :     end),
252 :     (BV.op_inner_ft, fn (y, [_, Ty.DIM d, Ty.SHAPE dd1, sh2, _], xs) =>let
253 :     val ty1 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
254 :     in
255 :     [assignEin(y, Mk.innerFT(dd1, d, dd2), xs)]
256 :     end),
257 :     (BV.op_inner_ff, fn (y, [_, _, Ty.DIM d, Ty.SHAPE dd1, Ty.SHAPE dd2, _], xs) =>
258 :     [assignEin(y, Mk.innerFF(dd1, d, dd2), xs)]),
259 :     (BV.op_colon_tt, fn (y, [sh1, sh2, _], xs) => let
260 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
261 :     val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
262 :     in
263 :     [assignEin(y, Mk.colonTT(dd1, dd2), xs)]
264 :     end),
265 :     (BV.op_colon_ft, fn (y, [_, Ty.SHAPE dd1, Ty.DIM d, Ty.SHAPE dd2, _], xs) =>
266 :     [assignEin(y, Mk.colonFT(d, dd1, dd2), xs)]),
267 :     (BV.op_colon_tf, fn (y, [_, Ty.SHAPE dd1, Ty.DIM d, Ty.SHAPE dd2, _], xs) =>
268 :     [assignEin(y, Mk.colonTF(d, dd1, dd2), xs)]),
269 :     (BV.op_colon_ff, fn (y, [_, Ty.SHAPE dd1, Ty.DIM d, Ty.SHAPE dd2, _], xs) =>
270 :     [assignEin(y, Mk.colonFF(d, dd1, dd2), xs)]),
271 :     (* modulate is vector * vector pointwise multiplication *)
272 :     (BV.fn_modulate, fn (y, [Ty.DIM dd1], xs) =>
273 :     [assignEin(y, (Mk.modulate dd1), xs)]),
274 : cchiw 3494 (BV.fn_normalize_t, fn (y, [shp], xs) =>let
275 :     val DstTy.TensorTy dd1 = shapeVarToTensor shp
276 :     in (case (dd1,xs)
277 :     of ([],[arg0])=> [IL.ASSGN(y,IL.VAR arg0)]
278 :     | (_,[arg0]) => [assignEin(y, Mk.normalizeTT(dd1),xs)]
279 :     (*end case*))
280 :     end),
281 :     (BV.fn_normalize_f, fn (y, [ _,Ty.DIM d1, Ty.SHAPE dd1], xs) =>
282 :     (case (dd1,xs)
283 :     of ([],[arg0])=> [IL.ASSGN(y,IL.VAR arg0)]
284 :     | (_,[arg0]) => [assignEin(y, Mk.normalizeFF(d1, dd1),xs@xs)]
285 :     end)),
286 : jhr 3478 (BV.fn_trace_t, fn (y, [Ty.DIM d], xs) =>
287 :     [assignEin(y, (Mk.traceT d), xs)]),
288 :     (BV.fn_trace_f, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
289 :     [assignEin(y, Mk.traceF(d, dd), xs)]),
290 :     (BV.fn_transpose_t, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
291 :     [assignEin(y, (Mk.transposeT [d1, d2]), xs)]),
292 :     (BV.fn_transpose_f, fn (y, [_, Ty.DIM d1, Ty.DIM d2, Ty.DIM d3], xs) =>
293 :     [assignEin(y, (Mk.transposeF (d1, d2, d3)), xs)]),
294 :     (BV.fn_det2_t, simpleEOp (Mk.det2T)),
295 :     (BV.fn_det3_t, simpleEOp (Mk.det3T)),
296 :     (BV.fn_det2_f, simpleEOp (Mk.det2F)),
297 :     (BV.fn_det3_f, simpleEOp (Mk.det3F)),
298 : jhr 3483 (BV.fn_sqrt_s, fn (y, [_, Ty.DIM d1], xs) =>
299 : jhr 3478 [assignEin(y, Mk.sqrtF d1, xs)]),
300 : cchiw 3494 (BV.fn_sqrt_t, fn (y, _, xs) =>
301 :     [assignEin(y, Mk.sqrtR, xs)]),
302 :     (*
303 :     (BV.fn_cos_r, fn (y, _, xs) =>
304 :     [assignEin(y, Mk.cosR, xs)]),
305 :     *)
306 : jhr 3483 (BV.fn_cos_s, fn (y, [_, Ty.DIM d1], xs) =>
307 : jhr 3478 [assignEin(y, Mk.cosF d1, xs)]),
308 : cchiw 3494 (*
309 :     (BV.fn_acos_r, fn (y, _, xs) =>
310 :     [assignEin(y, Mk.acosR, xs)]),
311 :     *)
312 : jhr 3483 (BV.fn_acos_s, fn (y, [_, Ty.DIM d1], xs) =>
313 : jhr 3478 [assignEin(y, Mk.acosF d1, xs)]),
314 : cchiw 3494 (*
315 :     (BV.fn_sin_r, fn (y, _, xs) =>
316 :     [assignEin(y, Mk.sinR, xs)]),
317 :     *)
318 : jhr 3483 (BV.fn_sin_s, fn (y, [_, Ty.DIM d1], xs) =>
319 : jhr 3478 [assignEin(y, Mk.sinF d1, xs)]),
320 : cchiw 3494 (*
321 :     (BV.fn_asin_r, fn (y, _, xs) =>
322 :     [assignEin(y, Mk.asinR, xs)]),
323 :     *)
324 : jhr 3483 (BV.fn_asin_s, fn (y, [_, Ty.DIM d1], xs) =>
325 : jhr 3478 [assignEin(y, Mk.asinF d1, xs)]),
326 : cchiw 3494 (*
327 :     (BV.fn_tan_r, fn (y, _, xs) =>
328 :     [assignEin(y, Mk.tanR, xs)]),
329 :     *)
330 : jhr 3483 (BV.fn_tan_s, fn (y, [_, Ty.DIM d1], xs) =>
331 : jhr 3478 [assignEin(y, Mk.tanF d1, xs)]),
332 : cchiw 3494 (*
333 :     (BV.fn_atan_r, fn (y, _, xs) =>
334 :     [assignEin(y, Mk.atanR, xs)]),
335 :     *)
336 : jhr 3483 (BV.fn_atan_s, fn (y, [_, Ty.DIM d1], xs) =>
337 : jhr 3478 [assignEin(y, Mk.atanF d1, xs)]),
338 : jhr 3483 (BV.fn_exp_r, fn (y, _, xs) =>
339 :     [assignEin(y, Mk.expT, xs)]),
340 :     (BV.fn_exp_s, fn (y, [_, Ty.DIM d1], xs) =>
341 : jhr 3478 [assignEin(y, Mk.expF d1, xs)]),
342 :     (BV.fn_principleEvec, vectorOp Op.PrincipleEvec),
343 :     (BV.fn_inside, fn (y, [_, Ty.DIM d, _], xs) => assign(y, Op.Inside d, xs)),
344 : jhr 3471 (BV.clamp_rrr, simpleOp (Op.Clamp DstTy.realTy)),
345 :     (BV.clamp_vvv, vectorOp Op.Clamp),
346 :     (BV.lerp3, tensorOp Op.Lerp),
347 :     (BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) => let
348 : jhr 3478 val t1 = IR.Var.new("t1", DstTy.realTy)
349 :     val t2 = IR.Var.new("t2", DstTy.realTy)
350 :     val t3 = IR.Var.new("t3", DstTy.realTy)
351 : jhr 3471 in [
352 : jhr 3478 assignEin(t1, Mk.subRR, [x, x0]),
353 :     assignEin(t2, Mk.subRR, [x1, x0]),
354 :     assignEin(t3, Mk.divRR, [t1, t2]),
355 :     IR.ASSGN(y, IR.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3]))
356 : jhr 3471 ] end),
357 :     (BV.evals2x2, eigenVal (Op.Eigen2x2, 2)),
358 :     (BV.evals3x3, eigenVal (Op.Eigen3x3, 3)),
359 :     (BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)),
360 :     (BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)),
361 :     (BV.fn_length, fn (y, [tv], [s]) => assign(y, Op.Length(trType tv), [s])),
362 : jhr 3483 (BV.fn_abs_i, simpleOp (Op.Abs DstTy.IntTy)),
363 :     (BV.fn_abs_r, simpleOp (Op.Abs DstTy.realTy)),
364 :     (BV.fn_max_i, simpleOp (Op.Max DstTy.IntTy)),
365 :     (BV.fn_max_r, simpleOp (Op.Max DstTy.realTy)),
366 :     (BV.fn_min_i, simpleOp (Op.Min DstTy.IntTy)),
367 :     (BV.fn_min_r, simpleOp (Op.Min DstTy.realTy)),
368 : jhr 3471 (BV.kn_bspln3, kernel Kernel.bspln3),
369 :     (BV.kn_bspln5, kernel Kernel.bspln5),
370 :     (BV.kn_ctmr, kernel Kernel.ctmr),
371 :     (BV.kn_c4hexic, kernel Kernel.c4hexic),
372 :     (BV.kn_tent, kernel Kernel.tent),
373 :     (BV.i2r, simpleOp Op.IntToReal),
374 : jhr 3478 (BV.identity, fn (y, [Ty.DIM d], xs) =>
375 :     [assignEin(y, Mk.identity d, xs)]),
376 : jhr 3492 (* FIXME: eventually zero should be implemented by Ein
377 : jhr 3478 (BV.zero, fn (y, [Ty.SHAPE dd], []) =>
378 :     [assignEin(y, Mk.zeros dd, [])]),
379 : jhr 3492 *)
380 :     (BV.zero, fn (y, [sv], []) =>
381 :     assign(y, Op.Zero(shapeVarToTensor sv), [])),
382 : jhr 3471 (BV.nan, fn (y, [Ty.SHAPE dd], []) => let
383 : jhr 3478 val nan = IR.LIT(Literal.Real(RealLit.nan))
384 :     fun mk (y, [], stms) = IR.ASSGN(y, nan) :: stms
385 : jhr 3471 | mk (y, d::dd, stms) = let
386 :     val ty = shapeVarToTensor(Ty.SHAPE dd)
387 : jhr 3478 val zs = List.tabulate(d, fn _ => IR.Var.new("_nan", ty))
388 : jhr 3471 in
389 : jhr 3478 IR.ASSGN(y, IR.CONS(zs, IR.Var.ty y)) ::
390 : jhr 3471 List.foldl (fn (z, stms) => mk(z, dd, stms)) stms zs
391 :     end
392 :     in
393 :     List.rev (mk (y, dd, []))
394 :     end),
395 :     (BV.subscript, fn (y, [tv, Ty.DIM d], args) =>
396 :     assign(y,
397 : jhr 3478 Op.SeqSub(DstTy.SeqTy(trType tv, SOME d)),
398 : jhr 3471 args)),
399 :     (BV.dynSubscript, fn (y, [tv], args) =>
400 : jhr 3478 assign(y, Op.SeqSub(DstTy.SeqTy(trType tv, NONE)), args)),
401 : jhr 3471 (* image operations *)
402 :     (BV.fn_size, fn (y, [Ty.DIM d, _], [img]) => let
403 : jhr 3478 val DstTy.ImageTy info = IR.Var.ty img
404 : jhr 3471 (* we extract each dimension separately and then build the sequence value *)
405 : jhr 3478 val dims = List.tabulate(d, fn i => IR.Var.new("i"^Int.toString i, DstTy.IntTy))
406 : jhr 3471 fun mkStms ([], _, stms) = stms (* in reverse order! *)
407 :     | mkStms (d::dr, i, stms) = mkStms (dr, i+1,
408 : jhr 3478 IR.ASSGN(d, IR.OP(Op.ImageDim(info, i), [img])) :: stms)
409 : jhr 3471 in
410 :     List.revAppend (mkStms (dims, 0, []), [
411 : jhr 3478 IR.ASSGN(y, IR.SEQ(dims, DstTy.SeqTy(DstTy.intTy, SOME d)))
412 : jhr 3471 ])
413 :     end),
414 :     (BV.image_border, fn (y, _, args as [img, _]) => let
415 : jhr 3478 val DstTy.ImageTy info = IR.Var.ty img
416 : jhr 3471 in
417 :     assign(y, Op.BorderCtlDefault info, args)
418 :     end),
419 :     (BV.image_clamp, fn (y, _, args as [img]) => let
420 : jhr 3478 val DstTy.ImageTy info = IR.Var.ty img
421 : jhr 3471 in
422 :     assign(y, Op.BorderCtlClamp info, args)
423 :     end),
424 :     (BV.image_mirror, fn (y, _, args as [img]) => let
425 : jhr 3478 val DstTy.ImageTy info = IR.Var.ty img
426 : jhr 3471 in
427 :     assign(y, Op.BorderCtlMirror info, args)
428 :     end),
429 :     (BV.image_wrap, fn (y, _, args as [img]) => let
430 : jhr 3478 val DstTy.ImageTy info = IR.Var.ty img
431 : jhr 3471 in
432 :     assign(y, Op.BorderCtlWrap info, args)
433 : jhr 3484 end),
434 : cchiw 3494 (BV.dist2_t, fn (y,_,xs)=>dist(y,[2],args)),
435 :     (BV.dist3_t, fn (y,_,xs)=>dist(y,[3],args)),
436 : jhr 3484 (BV.red_all, fn (y, _, args as [e, v, k]) =>
437 :     assign(y, Op.R_All(IR.Var.ty v), args)),
438 :     (BV.red_exists, fn (y, _, args as [e, v, k]) =>
439 :     assign(y, Op.R_Exists(IR.Var.ty v), args)),
440 :     (BV.red_max, fn (y, _, args as [e, v, k]) =>
441 :     assign(y, Op.R_Max(IR.Var.ty v), args)),
442 :     (BV.red_mean, fn (y, _, args as [e, v, k]) =>
443 :     assign(y, Op.R_Mean(IR.Var.ty v), args)),
444 :     (BV.red_min, fn (y, _, args as [e, v, k]) =>
445 :     assign(y, Op.R_Min(IR.Var.ty v), args)),
446 :     (BV.red_product, fn (y, _, args as [e, v, k]) =>
447 :     assign(y, Op.R_Product(IR.Var.ty v),args)),
448 :     (BV.red_sum, fn (y, _, args as [e, v, k]) =>
449 :     assign(y, Op.R_Sum(IR.Var.ty v), args)),
450 :     (BV.red_variance, fn (y, _, args as [e, v, k]) =>
451 :     assign(y, Op.R_Variance(IR.Var.ty v), args)),
452 :     (BV.fn_sphere_im, fn (y, [tv], args as [p, s]) =>
453 :     assign(y, Op.SphereQuery(IR.Var.ty p, trType tv), args)),
454 :     (BV.fn_sphere1_r, fn (y, [tv], args as [p, s]) =>
455 :     assign(y, Op.SphereQuery(IR.Var.ty p, trType tv), args)),
456 :     (BV.fn_sphere2_t, fn (y, [tv], args as [p, s]) =>
457 :     assign(y, Op.SphereQuery(IR.Var.ty p, trType tv), args)),
458 :     (BV.fn_sphere3_t, fn (y, [tv], args as [p, s]) =>
459 :     assign(y, Op.SphereQuery(IR.Var.ty p, trType tv), args))
460 : jhr 3471 ];
461 :     tbl
462 :     end
463 :    
464 :     fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
465 :     of SOME transFn => transFn(y, mvs, xs)
466 :     | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f)
467 :     (* end case *))
468 : jhr 3478 handle ex => (print(concat["translate (", IR.Var.toString y, ", ",
469 : jhr 3471 Var.uniqueNameOf f, ", ...)\n"]); raise ex)
470 :    
471 :     end

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