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

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/high-to-mid/probe-ein.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/high-to-mid/probe-ein.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3784 - (view) (download)

1 : jhr 3550 (* probe-ein.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2016 The University of Chicago
6 :     * All rights reserved.
7 :     *)
8 :    
9 :     structure ProbeEin : sig
10 :    
11 : cchiw 3735 val expand : AvailRHS.t -> MidIR.var * MidIR.rhs -> unit
12 : jhr 3582
13 : jhr 3550 end = struct
14 : cchiw 3581
15 :     structure IR = MidIR
16 : jhr 3582 structure Op = MidOps
17 : cchiw 3581 structure V = IR.Var
18 :     structure Ty = MidTypes
19 : jhr 3550 structure E = Ein
20 : jhr 3577 structure T = CoordSpaceTransform
21 : jhr 3550
22 : jhr 3582 (* This file expands probed fields
23 : jhr 3550 * Take a look at ProbeEin tex file for examples
24 : jhr 3577 * Note that the original field is an EIN operator in the form <V_alpha * H^(deltas)>(midIR.var list )
25 :     * Param_ids are used to note the placement of the argument in the midIR.var list
26 : jhr 3550 * Index_ids keep track of the shape of an Image or differentiation.
27 :     * Mu bind Index_id
28 :     * Generally, we will refer to the following
29 :     * dim:dimension of field V
30 :     * s: support of kernel H
31 :     * alpha: The alpha in <V_alpha * H^(deltas)>
32 : cchiw 3581 * dx: The dx in <V_alpha * nabla_dx H>
33 :     * deltas: The deltas in <V_alpha * h^(deltas) h^(deltas)>
34 : jhr 3550 * Vid:param_id for V
35 :     * hid:param_id for H
36 :     * nid: integer position param_id
37 :     * fid :fractional position param_id
38 :     * img-imginfo about V
39 :     *)
40 :    
41 : jhr 3577 fun mkEin (params, index, body) = Ein.EIN{params = params, index = index, body = body}
42 : cchiw 3735 fun incUse (IR.V{useCnt, ...}) = (useCnt := !useCnt + 1)
43 : jhr 3586 fun getRHSDst x = (case IR.Var.getDef x
44 :     of IR.OP(rator, args) => (rator, args)
45 :     | rhs => raise Fail(concat[
46 : cchiw 3581 "expected rhs operator for ", IR.Var.toString x,
47 : jhr 3586 " but found ", IR.RHS.toString rhs
48 : jhr 3550 ])
49 :     (* end case *))
50 :    
51 : cchiw 3742 fun checkImg imgArg = (case IR.Var.getDef imgArg
52 :     of IR.OP(Op.LoadImage _, _) => imgArg
53 : jhr 3586 | rhs => raise Fail (String.concat[
54 :     "expected image for ", IR.Var.toString imgArg,
55 :     " but found ", IR.RHS.toString rhs
56 :     ])
57 : cchiw 3742 )
58 :    
59 :     fun getImagInfo e = (case IR.Var.getDef e
60 : cchiw 3784 of IR.OP(Op.LoadImage(Ty.ImageTy info, _), []) => (e, info, NONE)
61 :     | IR.OP(Op.BorderCtlDefault info, [imgArg]) => (imgArg, info, raise Fail "Default boarder control")
62 :     | IR.OP(Op.BorderCtlClamp info, [imgArg]) => (imgArg, info, SOME IndexCtl.Clamp)
63 :     | IR.OP(Op.BorderCtlMirror info, [imgArg]) => (imgArg, info, SOME IndexCtl.Mirror)
64 :     | IR.OP(Op.BorderCtlWrap info, [imgArg]) => (imgArg, info, SOME IndexCtl.Wrap)
65 : cchiw 3742 | rhs => raise Fail (String.concat[
66 :     "expected image for ", IR.Var.toString e,
67 :     " but found ", IR.RHS.toString rhs
68 :     ])
69 :     (* end case *))
70 : jhr 3550
71 : jhr 3586 fun getKernelDst hArg = (case IR.Var.getDef hArg
72 : jhr 3732 of IR.OP(Op.Kernel(h, _), _) => h
73 : jhr 3586 | rhs => raise Fail (String.concat[
74 :     "expected kernel for ", IR.Var.toString hArg,
75 :     " but found ", IR.RHS.toString rhs
76 :     ])
77 : jhr 3582 (* end case *))
78 : cchiw 3581
79 : cchiw 3742 (* handleArgs- returns image arguments, info, and border
80 : jhr 3550 * uses the Param_ids for the image, kernel, and tensor
81 : jhr 3577 * and gets the mid-IR vars for each.
82 : cchiw 3742 * Transforms the position to index space
83 :     * P is the mid-il var for the (transformation matrix)transpose
84 : jhr 3550 *)
85 : jhr 3732 fun handleArgs (avail, Vid, hid, tid, args) = let
86 : cchiw 3735 val vI = List.nth (args, Vid)
87 : cchiw 3742 val (vI, info, border) = getImagInfo vI
88 : cchiw 3735 val vH = List.nth(args, hid)
89 :     val (vN, vF, vP) = T.worldToIndex{
90 :     avail = avail, info = info, img = vI, pos = List.nth(args, tid)
91 : jhr 3732 }
92 : cchiw 3777 val dim = ImageInfo.dim info
93 : jhr 3551 in
94 : cchiw 3777 (vI, vH, vN, vF, vP, info, border, dim)
95 : jhr 3551 end
96 : jhr 3550
97 : cchiw 3735 (*lifted Kernel expressions
98 :     args are axis, ein index_ids that represent differentiation, image dimension, kernel, fractional position, support
99 :     *)
100 :     fun liftKrn (avail, dir, dx, dim, h, vF, s) = let
101 :     val range = 2*s
102 :    
103 :     (* build position vector for EvalKernel *)
104 :     val vX =
105 :     if (dim=1) then vF (* position is a real type*)
106 :     else AvailRHS.addAssign (avail, "vxindexed_dir"^Int.toString(dir)^"_", Ty.realTy, IR.OP(Op.Index(Ty.TensorTy[dim], dir), [vF]))
107 :    
108 :     val vPos = AvailRHS.addAssign (avail, "kernelpos_dir"^Int.toString(dir)^"_", Ty.TensorTy[range], IR.OP(Op.BuildPos s, [vX]))
109 : jhr 3732
110 : cchiw 3735 val nKernEvals = List.length dx + 1
111 :     fun mkEval k = AvailRHS.addAssign (avail, "mkeval_dir"^Int.toString(dir)^"_del"^Int.toString k,
112 :     Ty.TensorTy[range], IR.OP(Op.EvalKernel(range, h, k), [vPos]))
113 :     val vKs = List.tabulate(nKernEvals, (fn k => mkEval k))
114 : jhr 3732 in
115 : cchiw 3735 case vKs
116 :     of [v] => v (* scalar result *)
117 :     | _ => let
118 :     val consTy = Ty.TensorTy[nKernEvals, range]
119 :     in
120 :     AvailRHS.addAssign (avail, "kernelCons_dir_"^Int.toString(dir), consTy, IR.CONS(vKs, consTy))
121 :     end
122 :     (* end case *)
123 :     end
124 : jhr 3732
125 : cchiw 3777
126 : cchiw 3784 fun mkLdVoxel (avail, vI, vN, info, alpha, shape, dim, s, border) = let
127 : cchiw 3777 val vLb = AvailRHS.addAssign (avail, "lit", Ty.intTy, IR.LIT(Literal.Int (1-(IntInf.fromInt s))))
128 :     fun f i =
129 :     let
130 :     val vA = AvailRHS.addAssign (avail, "lit", Ty.intTy, IR.LIT(Literal.Int (IntInf.fromInt i)))
131 :     val vB = AvailRHS.addAssign (avail, "subscript", Ty.intTy, IR.OP(Op.Subscript(Ty.TensorTy[dim]), [vN, vA]))
132 :     in
133 :     AvailRHS.addAssign (avail, "add", Ty.intTy, IR.OP(Op.IAdd, [vB, vLb]))
134 :     end
135 :    
136 :     (* image positions *)
137 : cchiw 3784 val s'= 2*s
138 : cchiw 3777 val supportshape = List.tabulate(dim, fn _ => s')
139 : cchiw 3784 val ldty = Ty.TensorTy (shape@supportshape)
140 : cchiw 3777 val vNs = List.tabulate( dim, fn n => f n)
141 : cchiw 3784
142 :     val op1 = (case border
143 :     of NONE => Op.LoadVoxels (info, s)
144 :     | SOME b => Op.LoadVoxelsWithCtl (info, s, b)
145 :     (* end case *))
146 : cchiw 3777 in
147 : cchiw 3784 AvailRHS.addAssign (avail, "ldvox", ldty, IR.OP(op1, [vI, vN]))
148 : cchiw 3777 end
149 :    
150 :    
151 :    
152 :     (*fieldReconstruction expands the body for the probed field*)
153 : cchiw 3784 fun fieldReconstruction (avail, sx, alpha, shape, dx, Vid, Vidnew, kid, hid, tid, args) = let
154 : cchiw 3777 val (vI, vH, vN, vF, vP, info, border, dim) = handleArgs (avail, Vid, hid, tid, args)
155 : cchiw 3744 val h = getKernelDst vH
156 :     val s = Kernel.support h
157 : cchiw 3784
158 :     (* creating summation Index *)
159 :     val vs = List.tabulate (dim, fn i => (i +sx))
160 :     val esum = List.map (fn i => (E.V i, 1-s, s)) vs
161 :    
162 :     (*represent image in ein expression with tensor*)
163 :     val imgexp= E.Img(Vidnew, alpha, List.map (fn i=> E.Value i) vs, s, E.None)
164 :     (*val imgexp = E.Tensor (Vidnew, alpha@(List.map (fn i => E.V i) vs))*)
165 :    
166 :     (* create load voxel operator for image *)
167 :     val vLd = mkLdVoxel (avail, vI, vN, info, alpha, shape, dim, s, border)
168 : cchiw 3735
169 :     (* create kernel body *)
170 :     fun createKrn (0, krnexp, vAs) = (krnexp, vAs)
171 :     | createKrn (dir, krnexp, vAs) = let
172 :     val dir' = dir-1
173 :     (* ein expression *)
174 :     val deltas = List.map (fn e =>(E.C(dir'), e)) dx
175 : cchiw 3742 val kexp0 = E.Krn(kid+dir, deltas, dir)
176 : cchiw 3735 (* evalkernel operators *)
177 :     val vA = liftKrn (avail, dir, dx, dim, h, vF, s)
178 :     in
179 :     createKrn (dir', kexp0::krnexp, vA::vAs)
180 :     end
181 :    
182 :     (* final ein expression body to represent field reconstruction *)
183 : cchiw 3784 val (krnexp, vKs) = createKrn (dim, [], [])
184 : cchiw 3735 val exp = E.Sum(esum, E.Opn(E.Prod, imgexp::krnexp))
185 : jhr 3583 in
186 : cchiw 3784 (vLd::vKs, vP, exp)
187 : jhr 3583 end
188 : jhr 3550
189 : jhr 3583 (*getsumshift:sum_indexid list* int list-> int
190 : jhr 3550 *get fresh/unused index_id, returns int
191 :     *)
192 : cchiw 3569 fun getsumshift ([], n) = n
193 : cchiw 3580 | getsumshift (sx, n) = let
194 : jhr 3732 val (E.V v, _, _) = List.hd(List.rev sx)
195 : jhr 3583 in
196 :     v+1
197 :     end
198 : jhr 3550
199 : jhr 3583 (*formBody:ein_exp->ein_exp*)
200 : cchiw 3580 fun formBody (E.Sum([],e)) = formBody e
201 :     | formBody (E.Sum(sx,e)) = E.Sum(sx,formBody e)
202 :     | formBody (E.Opn(E.Prod, [e])) = e
203 :     | formBody e = e
204 : jhr 3550
205 : jhr 3583 (* silly change in order of the product to match vis branch WorldtoSpace functions*)
206 :     fun multiPs (Ps, sx, body) = let
207 :     val exp = (case Ps
208 :     of [P0, P1, P2] => [P0, P1, P2, body]
209 :     | [P0, P1, P2, P3] => [P0, P1, P2, P3, body]
210 :     | _ => body::Ps
211 :     (* end case *))
212 :     in
213 :     formBody(E.Sum(sx, E.Opn(E.Prod, exp)))
214 :     end
215 : jhr 3550
216 : jhr 3583 fun arrangeBody (body, Ps, newsx, exp) = (case body
217 :     of E.Sum(sx, E.Probe _ ) => (true, multiPs(Ps, sx@newsx,exp))
218 :     | E.Sum(sx, E.Opn(E.Prod,[eps0,E.Probe _ ])) =>
219 :     (false, E.Sum(sx, E.Opn(E.Prod, [eps0, multiPs(Ps, newsx,exp)])))
220 : cchiw 3581 | E.Probe _ => (true, multiPs(Ps, newsx, exp))
221 :     | _ => raise Fail "impossible"
222 : jhr 3583 (* end case *))
223 : cchiw 3735
224 :    
225 : cchiw 3579
226 : jhr 3577 (* replaceProbe:ein_exp* params *midIR.var list * int list* sum_id list
227 : jhr 3550 -> ein_exp* *code
228 :     * Transforms position to world space
229 :     * transforms result back to index_space
230 :     * rewrites body
231 :     * replace probe with expanded version
232 :     *)
233 : jhr 3732 fun replaceProbe (avail, (y, IR.EINAPP(Ein.EIN{params, index, body}, args)), probe, sx) = let
234 : cchiw 3741
235 : cchiw 3735 (* tensor ids for position, transform matrix P, and kernel terms*)
236 : cchiw 3784 val pid = length params
237 :     val Vidnew = pid+1
238 :     val kid = Vidnew
239 : cchiw 3735
240 : jhr 3583 val E.Probe(E.Conv(Vid, alpha, hid, dx), E.Tensor(tid, _)) = probe
241 : cchiw 3777 val E.IMG(dim, shape) = List.nth(params, Vid)
242 : jhr 3583 val freshIndex = getsumshift (sx, length index)
243 : cchiw 3735 val (dx', sx', Ps) = T.imageToWorld (freshIndex, dim, dx, pid)
244 :     val sxn = freshIndex+length dx' (*next available index id *)
245 : cchiw 3784 val (args', vP, probe') = fieldReconstruction (avail, sxn, alpha, shape, dx', Vid, Vidnew, kid, hid, tid, args)
246 : cchiw 3777
247 :    
248 : cchiw 3784 (* add new params transformation matrix (Pid), image param, and kernel ids *)
249 :     val pP = E.TEN(true, [dim, dim])
250 :     val pV = List.nth(params, Vid)
251 :     val pK = List.tabulate(dim,fn _=> E.KRN)
252 :     val params' = params @ (pP::pV::pK)
253 :     val (_, body') = arrangeBody (body, Ps, sx', probe')
254 :     val einapp = (y, IR.EINAPP(mkEin(params', index, body'), args @ (vP::args')))
255 : jhr 3732 in
256 : jhr 3733 AvailRHS.addAssignToList (avail, einapp)
257 : jhr 3583 end
258 : jhr 3550
259 : cchiw 3741
260 : cchiw 3581 (*transform T*P*P..Ps*)
261 :     fun createEinApp (body, alpha, index, freshIndex, dim, dx, sx) = let
262 : cchiw 3735
263 : jhr 3583 val Pid = 0
264 :     val tid = 1
265 :     val (dx', newsx, Ps) = T.imageToWorld(freshIndex, dim, dx, Pid)
266 : jhr 3550 (*need to rewrite dx*)
267 : jhr 3583 val sxx = sx@newsx
268 : cchiw 3741
269 :     val (_, sizes, E.Conv(_, alpha', _, dx)) = (case sxx
270 : jhr 3583 (* QUESTION: what is the significance of "9" and "7" in this code? *)
271 :     of [] => ([], index, E.Conv(9, alpha, 7, dx'))
272 :     | _ => CleanIndex.clean(E.Conv(9, alpha, 7, dx'), index, sxx)
273 :     (* end case *))
274 : cchiw 3741
275 : jhr 3583 fun filterAlpha [] = dx'
276 :     | filterAlpha (E.C _::es) = filterAlpha es
277 :     | filterAlpha (e1::es) = e1::(filterAlpha es)
278 : cchiw 3741
279 :     val exp = E.Tensor(tid, filterAlpha alpha')
280 :    
281 : jhr 3583 val (splitvar, body') = arrangeBody(body, Ps, newsx, exp)
282 : cchiw 3735 val params = [E.TEN(true, [dim,dim]), E.TEN(true, sizes)]
283 : jhr 3583 val ein0 = mkEin(params, index, body')
284 : cchiw 3741
285 : jhr 3583 in
286 : cchiw 3741 (splitvar, ein0, sizes, dx, alpha')
287 : jhr 3583 end
288 : cchiw 3741
289 :    
290 : jhr 3583 (* floats the reconstructed field term *)
291 : jhr 3732 fun liftProbe (avail, (y, IR.EINAPP(Ein.EIN{params, index, body}, args)), probe, sx) = let
292 : jhr 3583 val E.Probe(E.Conv(Vid, alpha, hid, dx), E.Tensor(tid, _)) = probe
293 : cchiw 3735 val freshIndex = getsumshift(sx, length(index))
294 : cchiw 3777 val E.IMG(dim, shape) = List.nth(params, Vid)
295 :    
296 : cchiw 3735 (* transform T*P*P..Ps *)
297 : jhr 3583 val (splitvar, ein0, sizes, dx, alpha') =
298 :     createEinApp (body, alpha, index, freshIndex, dim, dx, sx)
299 : cchiw 3777 val vT = V.new ("TPP", Ty.TensorTy(sizes))
300 : cchiw 3741
301 : cchiw 3735 (* reconstruct the lifted probe *)
302 : cchiw 3741 (* making params args: image, position, and kernel ids *)
303 : cchiw 3784 val kid = 0 (* params used *)
304 :     val params' = List.nth(params,Vid)::(List.tabulate(dim,fn _=> E.KRN))
305 : cchiw 3741 (* create body for ein expression *)
306 :     val sxn = length sizes (*next available index id *)
307 : cchiw 3784 val (args', vP, probe') = fieldReconstruction (avail, sxn, alpha', shape, dx, Vid, Vid, kid, hid, tid, args)
308 :     val einApp1 = IR.EINAPP(mkEin(params', sizes, probe'), args')
309 : cchiw 3741
310 : cchiw 3777 (* transform T*P*P..Ps *)
311 :     val rtn0 = if splitvar
312 :     then FloatEin.transform(y, EinSums.transform ein0, [vP, vT])
313 :     else [(y, IR.EINAPP(ein0, [vP, vT]))]
314 : cchiw 3741
315 : jhr 3583 in
316 : cchiw 3741 List.app (fn e => AvailRHS.addAssignToList(avail, e)) (((vT, einApp1)::(rtn0)))
317 : jhr 3583 end
318 : jhr 3550
319 : cchiw 3741
320 : jhr 3583 (* expandEinOp: code-> code list
321 :     * A this point we only have simple ein ops
322 :     * Looks to see if the expression has a probe. If so, replaces it.
323 :     *)
324 : jhr 3732 fun expand avail (e as (_, IR.EINAPP(Ein.EIN{body, ...}, _))) = (case body
325 :     of (E.Probe(E.Conv(_, _, _, []) ,_)) =>
326 :     replaceProbe (avail, e, body, [])
327 :     | (E.Probe(E.Conv(_, alpha, _, dx) ,_)) =>
328 :     liftProbe (avail, e, body, []) (*scans dx for contant*)
329 : cchiw 3741
330 : jhr 3732 | (E.Sum(sx, p as E.Probe(E.Conv(_, _, _, []), _))) =>
331 :     replaceProbe (avail, e, p, sx) (*no dx*)
332 : cchiw 3741
333 : jhr 3732 | (E.Sum(sx, p as E.Probe(E.Conv(_, [], _, dx), _))) =>
334 :     liftProbe (avail, e, p, sx) (*scalar field*)
335 : cchiw 3741
336 : jhr 3732 | (E.Sum(sx, E.Probe p)) =>
337 :     replaceProbe (avail, e, E.Probe p, sx)
338 :     | (E.Sum(sx, E.Opn(E.Prod, [eps, E.Probe p]))) =>
339 :     replaceProbe (avail, e, E.Probe p, sx)
340 : cchiw 3735
341 : jhr 3734 | _ => AvailRHS.addAssignToList (avail, e)
342 : jhr 3582 (* end case *))
343 : jhr 3550
344 :     end (* ProbeEin *)

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