1 |
(* translate-basis.sml |
(* translate-basis.sml |
2 |
* |
* |
3 |
* COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu) |
* COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu) |
4 |
* All rights reserved. |
* All rights reserved. |
5 |
* |
* |
6 |
* Translation for basis operations in Simple AST to HighIL code |
* Translation for basis operations in Simple AST to HighIL code |
12 |
* to the instantiated meta variables mvs) to a list of SSA assignments in |
* to the instantiated meta variables mvs) to a list of SSA assignments in |
13 |
* reverse order. |
* reverse order. |
14 |
*) |
*) |
15 |
val translate : (HighIL.var * Var.var * Types.meta_var list * HighIL.var list) |
val translate : (HighIL.var * Var.var * SimpleTypes.meta_arg list * HighIL.var list) |
16 |
-> HighIL.assign list |
-> HighIL.assignment list |
17 |
|
|
18 |
end = struct |
end = struct |
19 |
|
|
21 |
structure IL = HighIL |
structure IL = HighIL |
22 |
structure DstTy = HighILTypes |
structure DstTy = HighILTypes |
23 |
structure Op = HighOps |
structure Op = HighOps |
24 |
structure Ty = Types |
structure Ty = SimpleTypes |
|
structure TU = TypeUtil |
|
|
structure MV = MetaVar |
|
25 |
structure VTbl = Var.Tbl |
structure VTbl = Var.Tbl |
26 |
|
|
27 |
fun pruneDim d = (case TU.pruneDim d |
structure mk= mkOperators |
|
of (Ty.DimConst n) => n |
|
|
| d => raise Fail("unresolved dimension " ^ TU.dimToString d) |
|
|
(* end case *)) |
|
28 |
|
|
29 |
fun pruneShape sv = (case TU.pruneShape(MV.toShape sv) |
fun trType (Ty.TY ty) = TranslateTy.tr ty |
30 |
of Ty.Shape dd => DstTy.TensorTy(List.map pruneDim dd) |
| trType _ = raise Fail "expected type" |
31 |
| shp => raise Fail("unresolved shape " ^ TU.shapeToString shp) |
fun dimVarToInt (Ty.DIM d) = d |
32 |
(* end case *)) |
| dimVarToInt _ = raise Fail "expected dim" |
33 |
|
fun dimVarToTensor dv = DstTy.tensorTy[dimVarToInt dv] |
34 |
|
fun dimVarToMatrix dv = let |
35 |
|
val d = dimVarToInt dv |
36 |
|
in |
37 |
|
DstTy.tensorTy[d, d] (* square matrix type *) |
38 |
|
end |
39 |
|
fun shapeVarToTensor (Ty.SHAPE shp) = DstTy.tensorTy shp |
40 |
|
| shapeVarToTensor _ = raise Fail "expected shape" |
41 |
|
|
42 |
fun dimVarToTensor dv = DstTy.TensorTy[pruneDim(MV.toDim dv)] |
fun assign (y, rator, xs) = [IL.ASSGN(y, IL.OP(rator, xs))] |
|
fun shapeVarToTensor sv = pruneShape sv |
|
43 |
|
|
44 |
fun assign (y, rator, xs) = [(y, IL.OP(rator, xs))] |
fun basisFn name (y, [], xs) = [IL.ASSGN(y, IL.APPLY(name, xs))] |
45 |
|
|
46 |
fun simpleOp rator (y, [], xs) = assign (y, rator, xs) |
fun simpleOp rator (y, [], xs) = assign (y, rator, xs) |
47 |
|
|
49 |
|
|
50 |
fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs) |
fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs) |
51 |
|
|
52 |
fun kernel h (y, [], []) = assign(y, Op.Kernel h, []) |
fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), []) |
53 |
|
|
54 |
|
(* 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 |
|
fun assignEin (y, rator, xs) = IL.ASSGN(y, IL.EINAPP(rator, xs)) |
75 |
|
|
76 |
|
fun simpleEOp rator (y, _,xs) = [assignEin(y, rator, xs)] |
77 |
|
|
78 |
|
|
79 |
|
fun mkNorm (shape,A) =let |
80 |
|
val RTy=DstTy.TensorTy [] |
81 |
|
val b=IL.Var.new("dot" ,RTy) |
82 |
|
val c=IL.Var.new("sqrt" ,RTy) |
83 |
|
val rator=(case shape |
84 |
|
of [_]=>mk.innerProduct(shape,shape) |
85 |
|
| [_,_]=>mk.doubleDot(shape,shape) |
86 |
|
| _ => raise Fail "unsupported norm" |
87 |
|
(*end case*)) |
88 |
|
val dot=assignEin(b,rator, [A,A]) |
89 |
|
val rator= IL.OP(Op.Sqrt, [b]) |
90 |
|
in (c,rator,dot) |
91 |
|
end |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
(* shape is an int list, DIM is in int|variable, k-level of differntiation *) |
97 |
|
|
98 |
(* build a table that maps Basis variables to their translation functions *) |
(* build a table that maps Basis variables to their translation functions *) |
99 |
val tbl : ((IL.var * Ty.meta_var list * IL.var list) -> IL.assign list) VTbl.hash_table = let |
val tbl : ((IL.var * Ty.meta_arg list * IL.var list) -> IL.assignment list) VTbl.hash_table = let |
100 |
val tbl = VTbl.mkTable (128, Fail "Translate table") |
val tbl = VTbl.mkTable (128, Fail "Translate table") |
101 |
|
val insert = VTbl.insert tbl |
102 |
in |
in |
103 |
List.app (VTbl.insert tbl) [ |
List.app insert [ |
|
(BV.add_ii, simpleOp(Op.Add DstTy.IntTy)), |
|
|
(BV.add_tt, tensorOp Op.Add), |
|
|
(BV.sub_ii, simpleOp(Op.Sub DstTy.IntTy)), |
|
|
(BV.sub_tt, tensorOp Op.Sub), |
|
|
(BV.mul_ii, simpleOp(Op.Mul DstTy.IntTy)), |
|
|
(BV.mul_rr, simpleOp(Op.Mul(DstTy.TensorTy[]))), |
|
|
(BV.mul_rt, tensorOp Op.Scale), |
|
|
(BV.mul_tr, fn (y, sv, [t, r]) => tensorOp Op.Scale (y, sv, [r, t])), |
|
|
(BV.div_ii, simpleOp(Op.Div DstTy.IntTy)), |
|
|
(BV.div_rr, simpleOp(Op.Div(DstTy.TensorTy[]))), |
|
|
(BV.div_tr, tensorOp Op.InvScale), |
|
104 |
(BV.lt_ii, simpleOp(Op.LT DstTy.IntTy)), |
(BV.lt_ii, simpleOp(Op.LT DstTy.IntTy)), |
105 |
(BV.lt_rr, simpleOp(Op.LT(DstTy.TensorTy[]))), |
(BV.lt_rr, simpleOp(Op.LT DstTy.realTy)), |
106 |
(BV.lte_ii, simpleOp(Op.LTE DstTy.IntTy)), |
(BV.lte_ii, simpleOp(Op.LTE DstTy.IntTy)), |
107 |
(BV.lte_rr, simpleOp(Op.LTE(DstTy.TensorTy[]))), |
(BV.lte_rr, simpleOp(Op.LTE DstTy.realTy)), |
108 |
(BV.gte_ii, simpleOp(Op.GTE DstTy.IntTy)), |
(BV.gte_ii, simpleOp(Op.GTE DstTy.IntTy)), |
109 |
(BV.gte_rr, simpleOp(Op.GTE(DstTy.TensorTy[]))), |
(BV.gte_rr, simpleOp(Op.GTE(DstTy.realTy))), |
110 |
(BV.gt_ii, simpleOp(Op.GT DstTy.IntTy)), |
(BV.gt_ii, simpleOp(Op.GT DstTy.IntTy)), |
111 |
(BV.gt_rr, simpleOp(Op.GT(DstTy.TensorTy[]))), |
(BV.gt_rr, simpleOp(Op.GT(DstTy.realTy))), |
112 |
(BV.equ_bb, simpleOp(Op.EQ DstTy.BoolTy)), |
(BV.equ_bb, simpleOp(Op.EQ DstTy.BoolTy)), |
113 |
(BV.equ_ii, simpleOp(Op.EQ DstTy.IntTy)), |
(BV.equ_ii, simpleOp(Op.EQ DstTy.IntTy)), |
114 |
(BV.equ_ss, simpleOp(Op.EQ DstTy.StringTy)), |
(BV.equ_ss, simpleOp(Op.EQ DstTy.StringTy)), |
115 |
(BV.equ_rr, simpleOp(Op.EQ(DstTy.TensorTy[]))), |
(BV.equ_rr, simpleOp(Op.EQ(DstTy.realTy))), |
116 |
(BV.neq_bb, simpleOp(Op.NEQ DstTy.BoolTy)), |
(BV.neq_bb, simpleOp(Op.NEQ DstTy.BoolTy)), |
117 |
(BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)), |
(BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)), |
118 |
(BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)), |
(BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)), |
119 |
(BV.neq_rr, simpleOp(Op.NEQ(DstTy.TensorTy[]))), |
(BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))), |
120 |
(BV.neg_i, simpleOp(Op.Neg DstTy.IntTy)), |
(BV.add_ii, simpleOp Op.IAdd), |
121 |
(BV.neg_t, tensorOp Op.Neg), |
(BV.add_tt, fn (y, [shp], xs) => let |
122 |
(BV.neg_f, fn (y, _, xs) => assign(y, Op.NegField, xs)), |
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp |
123 |
(BV.op_at, fn (y, [_, dv, sv], xs) => |
val rator =mk.addTen(dd1) |
124 |
assign(y, Op.Probe(shapeVarToTensor sv, dimVarToTensor dv), xs)), |
in |
125 |
(BV.op_convolve, fn (y, _, xs) => assign(y, Op.Convolve, xs)), |
[assignEin(y, rator,xs)] |
126 |
(BV.op_D, fn (y, _, xs) => assign(y, Op.DiffField, xs)), |
end), |
127 |
(BV.op_norm, tensorOp Op.Norm), |
(BV.add_ff, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => |
128 |
|
[assignEin(y, mk.addField(d, dd),xs)]), |
129 |
|
|
130 |
|
(BV.add_fr, fn (y, [_,Ty.DIM d], [f,s]) => |
131 |
|
[assignEin(y, mk.addTenField(d),[s,f])]), |
132 |
|
|
133 |
|
(BV.add_rf, fn (y, [_,Ty.DIM d], xs) => |
134 |
|
[assignEin(y, mk.addTenField(d),xs)]), |
135 |
|
|
136 |
|
|
137 |
|
(BV.sub_ii, simpleOp Op.ISub), |
138 |
|
(BV.sub_tt, fn (y, [shp], xs) => let |
139 |
|
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp |
140 |
|
val rator =mk.subTen(dd1) |
141 |
|
in |
142 |
|
[assignEin(y, rator,xs)] |
143 |
|
end), |
144 |
|
(BV.sub_ff, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => |
145 |
|
[assignEin(y, mk.subField(d, dd),xs)]), |
146 |
|
|
147 |
|
|
148 |
|
(BV.sub_fr, fn (y, [_,Ty.DIM d], xs) => |
149 |
|
[assignEin(y, mk.subFieldTen(d),xs)]), |
150 |
|
|
151 |
|
(BV.sub_rf, fn (y, [_,Ty.DIM d], xs) => |
152 |
|
[assignEin(y, mk.subTenField(d),xs)]), |
153 |
|
|
154 |
|
(BV.mul_ii, simpleOp Op.IMul), |
155 |
|
(BV.mul_rr, fn (y,_,args) => [assignEin(y, mk.prodScalar,args)]), |
156 |
|
(BV.mul_rt, fn (y, [shp], xs) => let |
157 |
|
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp |
158 |
|
val rator =mk.scaleTen(dd1) |
159 |
|
in |
160 |
|
[assignEin(y, rator,xs)] |
161 |
|
end), |
162 |
|
(BV.mul_tr, fn (y, [shp], [t, r]) => let |
163 |
|
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp |
164 |
|
val rator = mk.scaleTen dd1 |
165 |
|
in |
166 |
|
[assignEin(y, rator,[r,t])] |
167 |
|
end), |
168 |
|
(BV.mul_rf, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => |
169 |
|
[assignEin(y, mk.scaleField(d, dd),xs)]), |
170 |
|
(BV.mul_fr, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [f, s]) => |
171 |
|
[assignEin(y, mk.scaleField(d, dd),[s,f])]), |
172 |
|
(*MARK-mul_ff*) |
173 |
|
(BV.mul_ss, fn (y, [_,Ty.DIM d], xs) => |
174 |
|
[assignEin(y, mk.mulFieldss d,xs)]), |
175 |
|
(BV.mul_sf, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => |
176 |
|
[assignEin(y, mk.mulFieldsf(d,dd),xs)]), |
177 |
|
(BV.mul_fs, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [f,s]) => |
178 |
|
[assignEin(y, mk.mulFieldsf(d,dd),[s,f])]), |
179 |
|
|
180 |
|
(BV.div_ii, simpleOp Op.IDiv), |
181 |
|
(BV.div_rr, fn (y,_,args) => [assignEin(y, mk.divScalar,args)]), |
182 |
|
(BV.div_tr, fn (y, [shp], xs) => let |
183 |
|
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp |
184 |
|
val rator =mk.divTen(dd1) |
185 |
|
in |
186 |
|
[assignEin(y, rator,xs)] |
187 |
|
end), |
188 |
|
(BV.div_fr, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => |
189 |
|
[assignEin(y, mk.divideField(d, dd),xs)]), |
190 |
|
(BV.div_ss, fn (y, [_,Ty.DIM d], xs) => |
191 |
|
[assignEin(y, mk.divFieldss d,xs)]), |
192 |
|
|
193 |
|
(BV.exp_ri, simpleOp(Op.Power)), |
194 |
|
(BV.exp_rr, basisFn MathFuns.pow), |
195 |
|
(BV.curl2D, simpleEOp mk.curl2d), |
196 |
|
(BV.curl3D, simpleEOp mk.curl3d), |
197 |
|
(BV.convolve_vk, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) => |
198 |
|
[assignEin(y, mk.conv(d, dd),xs)]), |
199 |
|
(BV.convolve_kv, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [k, v]) => |
200 |
|
[assignEin(y, mk.conv(d, dd),[v,k])]), |
201 |
|
(BV.neg_i, simpleOp Op.INeg), |
202 |
|
(BV.neg_t, fn (y, [shp], xs) => let |
203 |
|
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp |
204 |
|
val rator =mk.negTen(dd1) |
205 |
|
in |
206 |
|
[assignEin(y, rator,xs)] |
207 |
|
end), |
208 |
|
(BV.neg_f, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => |
209 |
|
[assignEin(y, mk.negField(d, dd),xs)]), |
210 |
|
(BV.op_probe, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) => |
211 |
|
[assignEin(y, (mk.probe(dd,d)),xs)]), |
212 |
|
(BV.op_D, fn (y, [_, Ty.DIM d], xs) => [assignEin(y, mk.grad([d]),xs)]), |
213 |
|
|
214 |
|
(BV.op_Dotimes, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) => let |
215 |
|
val x= print (String.concat[" d1:",Int.toString(d1)," d2:",Int.toString(d2), |
216 |
|
" shape:",Int.toString(length(dd)), "\n"]) |
217 |
|
val g=print "*******\n" |
218 |
|
val gg2=print(Int.toString(length(xs))) |
219 |
|
in |
220 |
|
[assignEin(y, mk.dotimes(d1, dd@[d2]),xs)] end ), |
221 |
|
(BV.op_Ddot, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) => |
222 |
|
[assignEin(y, mk.divergence(d1, dd),xs)] ), |
223 |
|
|
224 |
|
|
225 |
|
(BV.op_norm, fn (y, [sv], [x]) => (case shapeVarToTensor sv |
226 |
|
of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, [x]) |
227 |
|
| DstTy.TensorTy dd=> let |
228 |
|
val RTy=DstTy.TensorTy [] |
229 |
|
val (_,sqrtop,dot)= mkNorm (dd,x) |
230 |
|
in |
231 |
|
[dot ,IL.ASSGN (y,sqrtop)] |
232 |
|
end |
233 |
|
|
234 |
|
|
235 |
|
| ty => assign(y, Op.Norm ty, [x]) |
236 |
|
(* end case *))), |
237 |
(BV.op_not, simpleOp Op.Not), |
(BV.op_not, simpleOp Op.Not), |
238 |
(BV.fn_CL, fn (y, _, xs) => assign(y, Op.CL, xs)), |
(BV.op_cross, simpleEOp mk.crossProduct), |
239 |
(BV.fn_convolve, fn (y, _, [h, img]) => assign(y, Op.Convolve, [img, h])), |
(BV.op_crossField, simpleEOp mk.crossProductField), |
240 |
(BV.fn_cos, simpleOp Op.Cos), |
(BV.op_outer, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) => |
241 |
(BV.fn_dot, vectorOp Op.Dot), |
[assignEin(y, (mk.outerProduct(d1, d2)), xs)]), |
242 |
(BV.fn_inside, fn (y, [_, dv, _], xs) => |
(* Any Shape fields |
243 |
assign(y, Op.Inside(dimVarToTensor dv), xs)), |
(BV.op_outerField, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd1, Ty.SHAPE dd2], xs)=> |
244 |
|
[assignEin(y, mk.outerField(d1, dd1, dd2), xs)]), |
245 |
|
*) |
246 |
|
(BV.op_outerField, fn (y, [_, Ty.DIM d1], xs)=> |
247 |
|
[assignEin(y, mk.outerField(d1), xs)]), |
248 |
|
|
249 |
|
|
250 |
|
(BV.op_inner, fn (y, [sh1, sh2, _], xs) => let |
251 |
|
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1 |
252 |
|
val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2 |
253 |
|
in |
254 |
|
[assignEin(y, (mk.innerProduct(dd1,dd2)),xs)] |
255 |
|
end), |
256 |
|
|
257 |
|
(BV.op_innerField, fn (y, [_,Ty.SHAPE dd1,Ty.DIM d,Ty.SHAPE dd2,_], xs) => |
258 |
|
[assignEin(y, mk.innerProductField(dd1,d,dd2),xs)]), |
259 |
|
(BV.op_colon, 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.doubleDot(dd1,dd2)),xs)] |
264 |
|
end), |
265 |
|
(BV.op_colonField, fn (y, [_,Ty.SHAPE dd1,_,Ty.SHAPE dd2, _], xs) => |
266 |
|
[assignEin(y, (mk.doubleDotField(dd1,dd2)),xs)]), |
267 |
|
(BV.fn_inside, fn (y, [_, Ty.DIM d, _], xs) => assign(y, Op.Inside d, xs)), |
268 |
|
(BV.clamp_rrr, simpleOp (Op.Clamp DstTy.realTy)), |
269 |
|
(BV.clamp_vvv, vectorOp Op.Clamp), |
270 |
|
(BV.lerp3, tensorOp Op.Lerp), |
271 |
|
(BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) => let |
272 |
|
val t1 = IL.Var.new("t1", DstTy.realTy) |
273 |
|
val t2 = IL.Var.new("t2", DstTy.realTy) |
274 |
|
val t3 = IL.Var.new("t3", DstTy.realTy) |
275 |
|
in [ |
276 |
|
assignEin(t1, mk.subScalar,[x,x0]), |
277 |
|
assignEin(t2, mk.subScalar,[x1,x0]), |
278 |
|
assignEin(t3, mk.divScalar,[t1,t2]), |
279 |
|
IL.ASSGN(y, IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3])) |
280 |
|
] end), |
281 |
|
(BV.evals2x2, eigenVal (Op.Eigen2x2, 2)), |
282 |
|
(BV.evals3x3, eigenVal (Op.Eigen3x3, 3)), |
283 |
|
(BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)), |
284 |
|
(BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)), |
285 |
|
|
286 |
|
|
287 |
(BV.fn_max, simpleOp Op.Max), |
(BV.fn_max, simpleOp Op.Max), |
288 |
(BV.fn_min, simpleOp Op.Min), |
(BV.fn_min, simpleOp Op.Min), |
289 |
(BV.fn_modulate, vectorOp Op.Mul), |
|
290 |
(BV.fn_pow, simpleOp Op.Pow), |
(* modulate is vector * vector pointwise multiplication *) |
291 |
|
(BV.fn_modulate, fn (y,[Ty.DIM dd1], xs) => |
292 |
|
[assignEin(y, (mk.modulate dd1),xs)]), |
293 |
|
(* |
294 |
|
vectorOp Op.Normalize),*) |
295 |
|
(*only supports vectors *) |
296 |
|
(BV.fn_normalize, fn (y, [Ty.DIM i], [x]) =>let |
297 |
|
val (c,sqrtop,dot)= mkNorm ([i],x) |
298 |
|
val d = IL.Var.new ("int", DstTy.intTy) |
299 |
|
val e=IL.Var.new("divide" ,DstTy.TensorTy []) |
300 |
|
in [dot ,IL.ASSGN (c,sqrtop), |
301 |
|
IL.ASSGN (d, IL.LIT(Literal.Int 1)), |
302 |
|
assignEin(e,mk.divScalar,[d,c]), |
303 |
|
assignEin(y,mk.scaleTen [i],[e,x])] |
304 |
|
end), |
305 |
|
|
306 |
(BV.fn_principleEvec, vectorOp Op.PrincipleEvec), |
(BV.fn_principleEvec, vectorOp Op.PrincipleEvec), |
307 |
(BV.fn_sin, simpleOp Op.Sin), |
(BV.fn_trace, fn (y, [Ty.DIM d], xs) => |
308 |
|
[assignEin(y,(mk.trace d), xs)]), |
309 |
|
(BV.fn_traceField, fn (y, [_,Ty.DIM d,Ty.SHAPE dd], xs) => |
310 |
|
[assignEin(y,mk.traceField(d,dd), xs)]), |
311 |
|
|
312 |
|
(BV.fn_transpose, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) => |
313 |
|
[assignEin(y, (mk.transpose [d1,d2]), xs)]), |
314 |
|
|
315 |
|
(BV.fn_transposeField, fn (y, [_,Ty.DIM d1, Ty.DIM d2,Ty.DIM d3], xs) => |
316 |
|
[assignEin(y, (mk.transposeField (d1,d2,d3)), xs)]), |
317 |
|
|
318 |
(BV.kn_bspln3, kernel Kernel.bspln3), |
(BV.kn_bspln3, kernel Kernel.bspln3), |
319 |
(BV.kn_bspln5, kernel Kernel.bspln5), |
(BV.kn_bspln5, kernel Kernel.bspln5), |
320 |
(BV.kn_ctmr, kernel Kernel.ctmr), |
(BV.kn_ctmr, kernel Kernel.ctmr), |
321 |
|
(BV.kn_c2ctmr, kernel Kernel.ctmr), |
322 |
|
(BV.kn_c4hexic, kernel Kernel.c4hexic), |
323 |
(BV.kn_tent, kernel Kernel.tent), |
(BV.kn_tent, kernel Kernel.tent), |
324 |
(BV.i2r, simpleOp Op.IntToReal) |
(BV.kn_c1tent, kernel Kernel.tent), |
325 |
|
(BV.i2r, simpleOp Op.IntToReal), |
326 |
|
(BV.identity, fn (y, [Ty.DIM d], xs) => |
327 |
|
[assignEin(y, (mk.identity d), xs)]), |
328 |
|
(BV.zero, fn (y, [sv], []) => |
329 |
|
assign(y, Op.Zero(shapeVarToTensor sv), [])), |
330 |
|
(BV.subscript, fn (y, [ty, Ty.DIM d], xs) => |
331 |
|
assign (y, Op.SeqSub(DstTy.SeqTy(trType ty, d)), xs)) |
332 |
|
(*, |
333 |
|
(BV.dynSubscript, fn (y, [tv], args) => |
334 |
|
assign(y, Op.SeqSub(DstTy.DynSeqTy(pruneTy tv)), args)) |
335 |
|
*) |
336 |
]; |
]; |
337 |
|
(* add C math functions *) |
338 |
|
List.app (fn (n, x) => insert(x, basisFn n)) BV.mathFns; |
339 |
tbl |
tbl |
340 |
end |
end |
341 |
|
|
343 |
of SOME transFn => transFn(y, mvs, xs) |
of SOME transFn => transFn(y, mvs, xs) |
344 |
| NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f) |
| NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f) |
345 |
(* end case *)) |
(* end case *)) |
346 |
|
handle ex => (print(concat["translate (", IL.Var.toString y, ", ", |
347 |
|
Var.uniqueNameOf f, ", ...)\n"]); raise ex) |
348 |
|
|
349 |
end |
end |