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

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