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 4014 - (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 : jhr 4014 (* get the image referenced on a RHS and its border control (if any) *)
60 :     fun getImagInfo x = (case V.getDef x
61 :     of IR.GLOBAL gv => let
62 :     val Ty.ImageTy info = IR.GlobalVar.ty gv
63 :     in
64 :     (x, info, NONE)
65 :     end
66 :     | IR.OP(Op.BorderCtlDefault info, [img, v]) =>
67 :     (img, info, raise Fail "Default boarder control")
68 :     | IR.OP(Op.BorderCtlClamp info, [img]) => (img, info, SOME IndexCtl.Clamp)
69 :     | IR.OP(Op.BorderCtlMirror info, [img]) => (img, info, SOME IndexCtl.Mirror)
70 :     | IR.OP(Op.BorderCtlWrap info, [img]) => (img, info, SOME IndexCtl.Wrap)
71 :     | IR.OP(Op.LoadImage(Ty.ImageTy info, _), _) => (x, info, NONE)
72 :     | rhs => raise Fail (String.concat[
73 :     "expected image for ", V.toString x, " but found ", IR.RHS.toString rhs
74 : jhr 3787 ])
75 : jhr 4014 (* end case *))
76 : jhr 3550
77 : jhr 3586 fun getKernelDst hArg = (case IR.Var.getDef hArg
78 : jhr 3787 of IR.OP(Op.Kernel(h, _), _) => h
79 :     | rhs => raise Fail (String.concat[
80 :     "expected kernel for ", IR.Var.toString hArg,
81 :     " but found ", IR.RHS.toString rhs
82 :     ])
83 :     (* end case *))
84 : cchiw 3581
85 : jhr 3791 (* handleArgs- returns image arguments, info, and border
86 :     * uses the Param_ids for the image, kernel, and tensor
87 :     * and gets the mid-IR vars for each.
88 :     * Transforms the position to index space
89 :     * P is the mid-il var for the (transformation matrix)transpose
90 :     *)
91 : jhr 3732 fun handleArgs (avail, Vid, hid, tid, args) = let
92 : jhr 3787 val vI = List.nth (args, Vid)
93 :     val (vI, info, border) = getImagInfo vI
94 :     val vH = List.nth(args, hid)
95 :     val (vN, vF, vP) = T.worldToIndex{
96 :     avail = avail, info = info, img = vI, pos = List.nth(args, tid)
97 :     }
98 :     in
99 :     (vI, vH, vN, vF, vP, info, border, ImageInfo.dim info)
100 :     end
101 : jhr 3550
102 : jhr 3787 (* lifted Kernel expressions
103 : jhr 3791 * args are axis, ein index_ids that represent differentiation, image dimension,
104 :     * kernel, fractional position, support
105 :     *)
106 : cchiw 3735 fun liftKrn (avail, dir, dx, dim, h, vF, s) = let
107 : jhr 3787 val range = 2*s
108 : cchiw 3735 (* build position vector for EvalKernel *)
109 : jhr 3787 val vX = if (dim = 1)
110 :     then vF (* position is a real type*)
111 :     else AvailRHS.addAssign (
112 :     avail, concat["vxindexed_dir", Int.toString dir, "_"],
113 : jhr 3797 Ty.realTy, IR.OP(Op.TensorIndex(Ty.TensorTy[dim], [dir]), [vF]))
114 : jhr 3787 val vPos = AvailRHS.addAssign (
115 :     avail, concat["kernpos_dir", Int.toString dir, "_"],
116 :     Ty.TensorTy[range], IR.OP(Op.BuildPos s, [vX]))
117 :     val nKernEvals = List.length dx + 1
118 :     fun mkEval k = AvailRHS.addAssign (
119 :     avail, concat["mkeval_dir", Int.toString dir, "_del", Int.toString k],
120 : cchiw 3735 Ty.TensorTy[range], IR.OP(Op.EvalKernel(range, h, k), [vPos]))
121 : jhr 3787 val vKs = List.tabulate(nKernEvals, fn k => mkEval k)
122 :     in
123 :     case vKs
124 :     of [v] => v (* scalar result *)
125 :     | _ => let
126 :     val consTy = Ty.TensorTy[nKernEvals, range]
127 :     in
128 :     AvailRHS.addAssign (
129 :     avail, concat["kernelCons_dir_", Int.toString dir, "_"],
130 :     consTy, IR.CONS(vKs, consTy))
131 :     end
132 :     (* end case *)
133 :     end
134 : jhr 3732
135 : cchiw 3789 (* mkLdVoxel creates load voxel operator to represent image addressing *)
136 : cchiw 3784 fun mkLdVoxel (avail, vI, vN, info, alpha, shape, dim, s, border) = let
137 : cchiw 3785 (* creates lb int *)
138 : jhr 3787 val vLb = AvailRHS.addAssign (avail, "lit", Ty.intTy, IR.LIT(Literal.Int (1-(IntInf.fromInt s))))
139 : cchiw 3789 val seqTy = Ty.SeqTy(Ty.intTy, SOME dim)
140 : cchiw 3785 (*created n_0 +lb, n_1+lb*)
141 : jhr 3787 fun f i = let
142 :     val vA = AvailRHS.addAssign (
143 :     avail, "idx", Ty.intTy, IR.LIT(Literal.Int (IntInf.fromInt i)))
144 :     val vB = AvailRHS.addAssign (
145 :     avail, "subscript",
146 : cchiw 3789 Ty.intTy, IR.OP(Op.Subscript seqTy, [vN, vA]))
147 : jhr 3787 in
148 : cchiw 3777 AvailRHS.addAssign (avail, "add", Ty.intTy, IR.OP(Op.IAdd, [vB, vLb]))
149 : jhr 3787 end
150 : cchiw 3777 (* image positions *)
151 : jhr 3787 val s'= 2*s
152 :     val supportshape = List.tabulate(dim, fn _ => s')
153 : jhr 3802 val ldty = Ty.TensorTy(shape@supportshape)
154 : jhr 3787 val vNs = List.tabulate( dim, fn n => f n)
155 : jhr 3790 val vSq = AvailRHS.addAssign (avail, "seq", seqTy, IR.SEQ(vNs, seqTy))
156 : jhr 3787 val op1 = (case border
157 : jhr 3788 of NONE => Op.LoadVoxels (info, 2*s)
158 :     | SOME b => Op.LoadVoxelsWithCtl (info, 2*s, b)
159 : jhr 3787 (* end case *))
160 :     in
161 : cchiw 3785 AvailRHS.addAssign (avail, "ldvox", ldty, IR.OP(op1, [vI, vSq]))
162 : jhr 3787 end
163 :    
164 :     (* fieldReconstruction expands the body for the probed field *)
165 :     fun fieldReconstruction (avail, sx, alpha, shape, dx, Vid, Vidnew, kid, hid, tid, args) = let
166 :     val (vI, vH, vN, vF, vP, info, border, dim) = handleArgs (avail, Vid, hid, tid, args)
167 :     val h = getKernelDst vH
168 :     val s = Kernel.support h
169 : cchiw 3784 (* creating summation Index *)
170 : jhr 3787 val vs = List.tabulate (dim, fn i => (i +sx))
171 : cchiw 3978 val esum = List.map (fn i => (i, 1-s, s)) vs
172 : jhr 3787 (* represent image in ein expression with tensor *)
173 :     val imgexp= E.Img(Vidnew, alpha, List.map (fn i=> E.Value i) vs, s, E.None)
174 : cchiw 3784 (* create load voxel operator for image *)
175 : jhr 3787 val vLd = mkLdVoxel (avail, vI, vN, info, alpha, shape, dim, s, border)
176 : cchiw 3735 (* create kernel body *)
177 : jhr 3787 fun createKrn (0, krnexp, vAs) = (krnexp, vAs)
178 :     | createKrn (dir, krnexp, vAs) = let
179 :     val dir' = dir-1
180 :     (* ein expression *)
181 :     val deltas = List.map (fn e =>(E.C(dir'), e)) dx
182 :     val kexp0 = E.Krn(kid+dir, deltas, dir)
183 :     (* evalkernel operators *)
184 :     val vA = liftKrn (avail, dir, dx, dim, h, vF, s)
185 :     in
186 :     createKrn (dir', kexp0::krnexp, vA::vAs)
187 :     end
188 :     (* final ein expression body to represent field reconstruction *)
189 :     val (krnexp, vKs) = createKrn (dim, [], [])
190 :     val exp = E.Sum(esum, E.Opn(E.Prod, imgexp::krnexp))
191 :     in
192 :     (vLd::vKs, vP, exp)
193 :     end
194 : cchiw 3735
195 : jhr 3787 (* getsumshift:sum_indexid list* int list-> int
196 :     * get fresh/unused index_id, returns int
197 : jhr 3550 *)
198 : cchiw 3569 fun getsumshift ([], n) = n
199 : cchiw 3580 | getsumshift (sx, n) = let
200 : cchiw 3978 val (v, _, _) = List.hd(List.rev sx)
201 : jhr 3787 in
202 :     v+1
203 :     end
204 : jhr 3550
205 : jhr 3787 (* formBody:ein_exp->ein_exp *)
206 : cchiw 3580 fun formBody (E.Sum([],e)) = formBody e
207 :     | formBody (E.Sum(sx,e)) = E.Sum(sx,formBody e)
208 :     | formBody (E.Opn(E.Prod, [e])) = e
209 :     | formBody e = e
210 : jhr 3550
211 : jhr 3787 (* silly change in order of the product to match vis branch WorldtoSpace functions *)
212 : jhr 3583 fun multiPs (Ps, sx, body) = let
213 : jhr 3787 val exp = (case Ps
214 :     of [P0, P1, P2] => [P0, P1, P2, body]
215 :     | [P0, P1, P2, P3] => [P0, P1, P2, P3, body]
216 :     | _ => body::Ps
217 :     (* end case *))
218 :     in
219 :     formBody(E.Sum(sx, E.Opn(E.Prod, exp)))
220 :     end
221 :    
222 : jhr 3980 (* arrangeBody - function changes the ordering of multiplication
223 :     * to match vis12 branch and pass regression tests
224 :     *)
225 : jhr 3583 fun arrangeBody (body, Ps, newsx, exp) = (case body
226 : jhr 3787 of E.Sum(sx, E.Probe _ ) => (true, multiPs(Ps, sx@newsx,exp))
227 : jhr 3583 | E.Sum(sx, E.Opn(E.Prod,[eps0,E.Probe _ ])) =>
228 : jhr 3787 (false, E.Sum(sx, E.Opn(E.Prod, [eps0, multiPs(Ps, newsx,exp)])))
229 : cchiw 3581 | E.Probe _ => (true, multiPs(Ps, newsx, exp))
230 :     | _ => raise Fail "impossible"
231 : jhr 3787 (* end case *))
232 :    
233 : jhr 3577 (* replaceProbe:ein_exp* params *midIR.var list * int list* sum_id list
234 : jhr 3550 -> ein_exp* *code
235 :     * Transforms position to world space
236 :     * transforms result back to index_space
237 : jhr 3787 * rewrites body
238 : jhr 3550 * replace probe with expanded version
239 :     *)
240 : jhr 3787 fun replaceProbe (avail, (y, IR.EINAPP(Ein.EIN{params, index, body}, args)), probe, sx) = let
241 :     (* tensor ids for position, transform matrix P, and kernel terms*)
242 :     val pid = length params
243 :     val Vidnew = pid+1
244 :     val kid = Vidnew
245 :     val E.Probe(E.Conv(Vid, alpha, hid, dx), E.Tensor(tid, _)) = probe
246 :     val E.IMG(dim, shape) = List.nth(params, Vid)
247 :     val freshIndex = getsumshift (sx, length index)
248 :     val (dx', sx', Ps) = T.imageToWorld (freshIndex, dim, dx, pid)
249 :     val sxn = freshIndex + length dx' (*next available index id *)
250 :     val (args', vP, probe') = fieldReconstruction (
251 :     avail, sxn, alpha, shape, dx', Vid, Vidnew, kid, hid, tid, args)
252 :     (* add new params transformation matrix (Pid), image param, and kernel ids *)
253 :     val pP = E.TEN(true, [dim, dim])
254 :     val pV = List.nth(params, Vid)
255 :     val pK = List.tabulate(dim,fn _=> E.KRN)
256 :     val params' = params @ (pP::pV::pK)
257 :     val (_, body') = arrangeBody (body, Ps, sx', probe')
258 :     val einapp = (y, IR.EINAPP(mkEin(params', index, body'), args @ (vP::args')))
259 :     in
260 :     AvailRHS.addAssignToList (avail, einapp)
261 :     end
262 : cchiw 3741
263 : jhr 3787 (* transform T*P*P..Ps *)
264 : cchiw 3581 fun createEinApp (body, alpha, index, freshIndex, dim, dx, sx) = let
265 : jhr 3787 val Pid = 0
266 :     val tid = 1
267 :     val (dx', newsx, Ps) = T.imageToWorld(freshIndex, dim, dx, Pid)
268 :     (* need to rewrite dx *)
269 :     val sxx = sx@newsx
270 :     val (_, sizes, E.Conv(_, alpha', _, dx)) = (case sxx
271 : cchiw 3793 of [] => ([], index, E.Conv(0, alpha, 1, dx'))
272 :     | _ => CleanIndex.clean(E.Conv(0, alpha, 1, dx'), index, sxx)
273 : jhr 3787 (* end case *))
274 :     fun filterAlpha [] = dx'
275 :     | filterAlpha (E.C _::es) = filterAlpha es
276 :     | filterAlpha (e1::es) = e1::(filterAlpha es)
277 :     val exp = E.Tensor(tid, filterAlpha alpha')
278 :     val (splitvar, body') = arrangeBody(body, Ps, newsx, exp)
279 :     val params = [E.TEN(true, [dim,dim]), E.TEN(true, sizes)]
280 :     val ein0 = mkEin(params, index, body')
281 :     in
282 :     (splitvar, ein0, sizes, dx, alpha')
283 :     end
284 : cchiw 3741
285 : jhr 3583 (* floats the reconstructed field term *)
286 : jhr 3732 fun liftProbe (avail, (y, IR.EINAPP(Ein.EIN{params, index, body}, args)), probe, sx) = let
287 : jhr 3787 val E.Probe(E.Conv(Vid, alpha, hid, dx), E.Tensor(tid, _)) = probe
288 :     val freshIndex = getsumshift(sx, length(index))
289 :     val E.IMG(dim, shape) = List.nth(params, Vid)
290 :     (* transform T*P*P..Ps *)
291 :     val (splitvar, ein0, sizes, dx, alpha') =
292 :     createEinApp (body, alpha, index, freshIndex, dim, dx, sx)
293 :     val vT = V.new ("TPP", Ty.TensorTy(sizes))
294 :     (* reconstruct the lifted probe *)
295 :     (* making params args: image, position, and kernel ids *)
296 :     val kid = 0 (* params used *)
297 :     val params' = List.nth(params,Vid)::(List.tabulate(dim,fn _=> E.KRN))
298 :     (* create body for ein expression *)
299 :     val sxn = length sizes (*next available index id *)
300 :     val (args', vP, probe') =
301 :     fieldReconstruction (avail, sxn, alpha', shape, dx, Vid, Vid, kid, hid, tid, args)
302 :     val einApp1 = IR.EINAPP(mkEin(params', sizes, probe'), args')
303 :     (* transform T*P*P..Ps *)
304 :     val rtn0 = if splitvar
305 :     then FloatEin.transform(y, EinSums.transform ein0, [vP, vT])
306 :     else [(y, IR.EINAPP(ein0, [vP, vT]))]
307 :     in
308 :     List.app (fn e => AvailRHS.addAssignToList(avail, e)) (((vT, einApp1)::(rtn0)))
309 :     end
310 : cchiw 3741
311 : jhr 3583 (* expandEinOp: code-> code list
312 :     * A this point we only have simple ein ops
313 :     * Looks to see if the expression has a probe. If so, replaces it.
314 :     *)
315 : jhr 3732 fun expand avail (e as (_, IR.EINAPP(Ein.EIN{body, ...}, _))) = (case body
316 : jhr 3787 of (E.Probe(E.Conv(_, _, _, []) ,_)) =>
317 :     replaceProbe (avail, e, body, [])
318 : jhr 3732 | (E.Probe(E.Conv(_, alpha, _, dx) ,_)) =>
319 : jhr 3787 liftProbe (avail, e, body, []) (*scans dx for contant*)
320 : jhr 3732 | (E.Sum(sx, p as E.Probe(E.Conv(_, _, _, []), _))) =>
321 : jhr 3787 replaceProbe (avail, e, p, sx) (*no dx*)
322 : jhr 3732 | (E.Sum(sx, p as E.Probe(E.Conv(_, [], _, dx), _))) =>
323 : jhr 3787 liftProbe (avail, e, p, sx) (*scalar field*)
324 : jhr 3732 | (E.Sum(sx, E.Probe p)) =>
325 : jhr 3787 replaceProbe (avail, e, E.Probe p, sx)
326 : jhr 3732 | (E.Sum(sx, E.Opn(E.Prod, [eps, E.Probe p]))) =>
327 : jhr 3787 replaceProbe (avail, e, E.Probe p, sx)
328 : jhr 3734 | _ => AvailRHS.addAssignToList (avail, e)
329 : jhr 3787 (* end case *))
330 : jhr 3550
331 :     end (* ProbeEin *)

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