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 3744 - (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 :     of IR.OP(Op.LoadImage(Ty.ImageTy info, _), []) => (e, info, E.None)
61 :     | IR.OP(Op.BorderCtlDefault info, [imgArg]) => (imgArg, info, E.Default)
62 :     | IR.OP(Op.BorderCtlClamp info, [imgArg]) => (imgArg, info, E.Clamp)
63 :     | IR.OP(Op.BorderCtlMirror info, [imgArg]) => (imgArg, info, E.Mirror)
64 :     | IR.OP(Op.BorderCtlWrap info, [imgArg]) => (imgArg, info, E.Wrap)
65 :     | 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 : jhr 3551 in
93 : cchiw 3742 (ImageInfo.dim info, border, vN, vH, vF, vP)
94 : jhr 3551 end
95 : jhr 3550
96 : cchiw 3735 (*lifted Kernel expressions
97 :     args are axis, ein index_ids that represent differentiation, image dimension, kernel, fractional position, support
98 :     *)
99 :     fun liftKrn (avail, dir, dx, dim, h, vF, s) = let
100 :     val range = 2*s
101 :    
102 :     (* build position vector for EvalKernel *)
103 :     val vX =
104 :     if (dim=1) then vF (* position is a real type*)
105 :     else AvailRHS.addAssign (avail, "vxindexed_dir"^Int.toString(dir)^"_", Ty.realTy, IR.OP(Op.Index(Ty.TensorTy[dim], dir), [vF]))
106 :    
107 :     val vPos = AvailRHS.addAssign (avail, "kernelpos_dir"^Int.toString(dir)^"_", Ty.TensorTy[range], IR.OP(Op.BuildPos s, [vX]))
108 : jhr 3732
109 : cchiw 3735 val nKernEvals = List.length dx + 1
110 :     fun mkEval k = AvailRHS.addAssign (avail, "mkeval_dir"^Int.toString(dir)^"_del"^Int.toString k,
111 :     Ty.TensorTy[range], IR.OP(Op.EvalKernel(range, h, k), [vPos]))
112 :     val vKs = List.tabulate(nKernEvals, (fn k => mkEval k))
113 : jhr 3732 in
114 : cchiw 3735 case vKs
115 :     of [v] => v (* scalar result *)
116 :     | _ => let
117 :     val consTy = Ty.TensorTy[nKernEvals, range]
118 :     in
119 :     AvailRHS.addAssign (avail, "kernelCons_dir_"^Int.toString(dir), consTy, IR.CONS(vKs, consTy))
120 :     end
121 :     (* end case *)
122 :     end
123 : jhr 3732
124 : cchiw 3735 (*fieldReconstruction:int*int*int,mu list, param_id, param_id, kernel, position, param_id
125 : jhr 3550 * expands the body for the probed field
126 :     *)
127 : cchiw 3742 fun fieldReconstruction (avail, dim, sx, alpha, dx, Vid, nid, vH, vF, kid, border) = let
128 : cchiw 3744 val h = getKernelDst vH
129 :     val s = Kernel.support h
130 :    
131 : cchiw 3735 (* image positions for image body *)
132 :     val imgpos =
133 :     if (dim = 1)
134 :     then [E.Opn(E.Add, [E.Tensor(nid, []), E.Value(sx)])]
135 :     else List.tabulate(dim, fn dir=> E.Opn(E.Add, [E.Tensor(nid, [E.C dir]), E.Value(dir+sx)]))
136 :     (* image body *)
137 : cchiw 3744 val imgexp = E.Img(Vid, alpha, imgpos, s, border)
138 : cchiw 3735
139 :     (* create kernel body *)
140 :     fun createKrn (0, krnexp, vAs) = (krnexp, vAs)
141 :     | createKrn (dir, krnexp, vAs) = let
142 :     val dir' = dir-1
143 :     (* ein expression *)
144 :     val deltas = List.map (fn e =>(E.C(dir'), e)) dx
145 : cchiw 3742 val kexp0 = E.Krn(kid+dir, deltas, dir)
146 : cchiw 3735 (* evalkernel operators *)
147 :     val vA = liftKrn (avail, dir, dx, dim, h, vF, s)
148 :     in
149 :     createKrn (dir', kexp0::krnexp, vA::vAs)
150 :     end
151 :    
152 :     (* creating summation Index *)
153 : cchiw 3741 val esum = List.tabulate (dim, fn i => (E.V (i +sx), 1-s, s))
154 : cchiw 3735 (* final ein expression body to represent field reconstruction *)
155 :     val (krnexp, vAs) = createKrn (dim, [], [])
156 :     val exp = E.Sum(esum, E.Opn(E.Prod, imgexp::krnexp))
157 : jhr 3583 in
158 : cchiw 3735 (exp, vAs)
159 : jhr 3583 end
160 : jhr 3550
161 : jhr 3583 (*getsumshift:sum_indexid list* int list-> int
162 : jhr 3550 *get fresh/unused index_id, returns int
163 :     *)
164 : cchiw 3569 fun getsumshift ([], n) = n
165 : cchiw 3580 | getsumshift (sx, n) = let
166 : jhr 3732 val (E.V v, _, _) = List.hd(List.rev sx)
167 : jhr 3583 in
168 :     v+1
169 :     end
170 : jhr 3550
171 : jhr 3583 (*formBody:ein_exp->ein_exp*)
172 : cchiw 3580 fun formBody (E.Sum([],e)) = formBody e
173 :     | formBody (E.Sum(sx,e)) = E.Sum(sx,formBody e)
174 :     | formBody (E.Opn(E.Prod, [e])) = e
175 :     | formBody e = e
176 : jhr 3550
177 : jhr 3583 (* silly change in order of the product to match vis branch WorldtoSpace functions*)
178 :     fun multiPs (Ps, sx, body) = let
179 :     val exp = (case Ps
180 :     of [P0, P1, P2] => [P0, P1, P2, body]
181 :     | [P0, P1, P2, P3] => [P0, P1, P2, P3, body]
182 :     | _ => body::Ps
183 :     (* end case *))
184 :     in
185 :     formBody(E.Sum(sx, E.Opn(E.Prod, exp)))
186 :     end
187 : jhr 3550
188 : jhr 3583 fun arrangeBody (body, Ps, newsx, exp) = (case body
189 :     of E.Sum(sx, E.Probe _ ) => (true, multiPs(Ps, sx@newsx,exp))
190 :     | E.Sum(sx, E.Opn(E.Prod,[eps0,E.Probe _ ])) =>
191 :     (false, E.Sum(sx, E.Opn(E.Prod, [eps0, multiPs(Ps, newsx,exp)])))
192 : cchiw 3581 | E.Probe _ => (true, multiPs(Ps, newsx, exp))
193 :     | _ => raise Fail "impossible"
194 : jhr 3583 (* end case *))
195 : cchiw 3735
196 :    
197 : cchiw 3579
198 : jhr 3577 (* replaceProbe:ein_exp* params *midIR.var list * int list* sum_id list
199 : jhr 3550 -> ein_exp* *code
200 :     * Transforms position to world space
201 :     * transforms result back to index_space
202 :     * rewrites body
203 :     * replace probe with expanded version
204 :     *)
205 : jhr 3732 fun replaceProbe (avail, (y, IR.EINAPP(Ein.EIN{params, index, body}, args)), probe, sx) = let
206 : cchiw 3741
207 : cchiw 3735 (* tensor ids for position, transform matrix P, and kernel terms*)
208 :     val nid = length params
209 :     val pid = nid+1
210 :     val kid = pid+1
211 :    
212 : jhr 3583 val E.Probe(E.Conv(Vid, alpha, hid, dx), E.Tensor(tid, _)) = probe
213 : cchiw 3742 val (dim, border, vN, vH, vF, vP) = handleArgs (avail, Vid, hid, tid, args)
214 : jhr 3583 val freshIndex = getsumshift (sx, length index)
215 : cchiw 3735 val (dx', sx', Ps) = T.imageToWorld (freshIndex, dim, dx, pid)
216 :     val sxn = freshIndex+length dx' (*next available index id *)
217 : cchiw 3742 val (probe', vKs) = fieldReconstruction (avail, dim, sxn, alpha, dx', Vid, nid, vH, vF, pid, border)
218 : cchiw 3735 (* add new params position (nid), transformation matrix (Pid), and kernel ids *)
219 :     val params' = params @ [E.TEN(true, [dim]), E.TEN(true, [dim, dim])] @(List.tabulate(dim,fn _=> E.TEN(true,[])))
220 : jhr 3583 val (_, body') = arrangeBody (body, Ps, sx', probe')
221 : cchiw 3735 val einapp = (y, IR.EINAPP(mkEin(params', index, body'), args @ [vN, vP]@vKs))
222 :     (* val _ = AvailRHS.addAssignToList (avail, einapp)*)(*FIXME: remove this*)
223 : jhr 3732 in
224 : jhr 3733 AvailRHS.addAssignToList (avail, einapp)
225 : jhr 3583 end
226 : jhr 3550
227 : cchiw 3741
228 : cchiw 3581 (*transform T*P*P..Ps*)
229 :     fun createEinApp (body, alpha, index, freshIndex, dim, dx, sx) = let
230 : cchiw 3735
231 : jhr 3583 val Pid = 0
232 :     val tid = 1
233 :     val (dx', newsx, Ps) = T.imageToWorld(freshIndex, dim, dx, Pid)
234 : jhr 3550 (*need to rewrite dx*)
235 : jhr 3583 val sxx = sx@newsx
236 : cchiw 3741
237 :     val (_, sizes, E.Conv(_, alpha', _, dx)) = (case sxx
238 : jhr 3583 (* QUESTION: what is the significance of "9" and "7" in this code? *)
239 :     of [] => ([], index, E.Conv(9, alpha, 7, dx'))
240 :     | _ => CleanIndex.clean(E.Conv(9, alpha, 7, dx'), index, sxx)
241 :     (* end case *))
242 : cchiw 3741
243 : jhr 3583 fun filterAlpha [] = dx'
244 :     | filterAlpha (E.C _::es) = filterAlpha es
245 :     | filterAlpha (e1::es) = e1::(filterAlpha es)
246 : cchiw 3741
247 :     val exp = E.Tensor(tid, filterAlpha alpha')
248 :    
249 : jhr 3583 val (splitvar, body') = arrangeBody(body, Ps, newsx, exp)
250 : cchiw 3735 val params = [E.TEN(true, [dim,dim]), E.TEN(true, sizes)]
251 : jhr 3583 val ein0 = mkEin(params, index, body')
252 : cchiw 3741
253 : jhr 3583 in
254 : cchiw 3741 (splitvar, ein0, sizes, dx, alpha')
255 : jhr 3583 end
256 : cchiw 3741
257 :    
258 : jhr 3583 (* floats the reconstructed field term *)
259 : jhr 3732 fun liftProbe (avail, (y, IR.EINAPP(Ein.EIN{params, index, body}, args)), probe, sx) = let
260 : jhr 3583 val E.Probe(E.Conv(Vid, alpha, hid, dx), E.Tensor(tid, _)) = probe
261 : cchiw 3742 val (dim, border, vN, vH, vF, vP) = handleArgs(avail, Vid, hid, tid, args)
262 : cchiw 3735 val freshIndex = getsumshift(sx, length(index))
263 : cchiw 3741
264 : cchiw 3735 (* transform T*P*P..Ps *)
265 : jhr 3583 val (splitvar, ein0, sizes, dx, alpha') =
266 :     createEinApp (body, alpha, index, freshIndex, dim, dx, sx)
267 : cchiw 3741 val vT = V.new ("TPP", Ty.TensorTy(sizes))
268 :     val einApp0 = IR.EINAPP(ein0, [vP, vT])
269 : jhr 3583 val rtn0 = if splitvar
270 : cchiw 3741 then FloatEin.transform(y, EinSums.transform ein0, [vP, vT])
271 :     else [(y, IR.EINAPP(ein0, [vP, vT]))]
272 :    
273 :    
274 : cchiw 3735 (* reconstruct the lifted probe *)
275 : cchiw 3741 (* making params args: image, position, and kernel ids *)
276 :     val nid = 1 (* transformed image position *)
277 :     val params' = List.nth(params,Vid)::E.TEN(true, [dim])::(List.tabulate(dim,fn _=> E.TEN(true,[])))
278 :     (* create body for ein expression *)
279 :     val sxn = length sizes (*next available index id *)
280 : cchiw 3742 val (probe', vKs) = fieldReconstruction (avail, dim, sxn, alpha', dx, Vid, nid, vH, vF, nid, border)
281 : cchiw 3741 val args' = List.nth(args,Vid) :: vN ::vKs
282 :     val einApp1 = IR.EINAPP(mkEin(params'@[E.TEN(true,[10])], sizes, probe'), args')
283 :    
284 :    
285 : jhr 3583 in
286 : cchiw 3741 List.app (fn e => AvailRHS.addAssignToList(avail, e)) (((vT, einApp1)::(rtn0)))
287 : jhr 3583 end
288 : jhr 3550
289 : cchiw 3741
290 : jhr 3583 (* expandEinOp: code-> code list
291 :     * A this point we only have simple ein ops
292 :     * Looks to see if the expression has a probe. If so, replaces it.
293 :     *)
294 : jhr 3732 fun expand avail (e as (_, IR.EINAPP(Ein.EIN{body, ...}, _))) = (case body
295 :     of (E.Probe(E.Conv(_, _, _, []) ,_)) =>
296 :     replaceProbe (avail, e, body, [])
297 : cchiw 3741 (* | (E.Probe _) =>
298 : cchiw 3735 replaceProbe (avail, e, body, [])
299 :     (*FIXME: Only use replaceProbe while trying to isolate valnum bug*)
300 : cchiw 3741 *)
301 : jhr 3732 | (E.Probe(E.Conv(_, alpha, _, dx) ,_)) =>
302 :     liftProbe (avail, e, body, []) (*scans dx for contant*)
303 : cchiw 3741
304 : jhr 3732 | (E.Sum(sx, p as E.Probe(E.Conv(_, _, _, []), _))) =>
305 :     replaceProbe (avail, e, p, sx) (*no dx*)
306 : cchiw 3741
307 : jhr 3732 | (E.Sum(sx, p as E.Probe(E.Conv(_, [], _, dx), _))) =>
308 :     liftProbe (avail, e, p, sx) (*scalar field*)
309 : cchiw 3741
310 : jhr 3732 | (E.Sum(sx, E.Probe p)) =>
311 :     replaceProbe (avail, e, E.Probe p, sx)
312 :     | (E.Sum(sx, E.Opn(E.Prod, [eps, E.Probe p]))) =>
313 :     replaceProbe (avail, e, E.Probe p, sx)
314 : cchiw 3735
315 : jhr 3734 | _ => AvailRHS.addAssignToList (avail, e)
316 : jhr 3582 (* end case *))
317 : jhr 3550
318 :     end (* ProbeEin *)

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