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

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