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

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