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 3586 - (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 : jhr 3582 val expand : MidIR.assign -> MidIR.assign list
12 :    
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 : jhr 3583
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 :     of IR.OP(Op.Kernel(h, _), _) => Kernel.support h
62 :     | 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 : cchiw 3569 fun handleArgs (Vid, hid, tid, args) = let
76 : jhr 3583 val imgArg = List.nth (args, Vid)
77 : jhr 3586 val info = getImageDst imgArg
78 :     val s = getKernelDst (List.nth(args, hid))
79 : jhr 3583 val (argsT, P, code) = T.worldToIndex{info = info, img = imgArg, pos = List.nth(args, tid)}
80 : jhr 3551 in
81 : jhr 3583 (ImageInfo.dim info, args@argsT, code, s, P)
82 : jhr 3551 end
83 : jhr 3550
84 : jhr 3583 (*fieldReconstruction:int*int*int,mu list, param_id, param_id, param_id, param_id
85 : jhr 3550 * expands the body for the probed field
86 :     *)
87 : jhr 3583 fun fieldReconstruction (dimO, s, sx, alpha, dx, Vid, hid, nid, fid) = let
88 : jhr 3550 (*1-d fields*)
89 : jhr 3583 fun createKRND1 () = let
90 :     val imgpos = [E.Opn(E.Add,[E.Tensor(fid,[]), E.Value(sx)])]
91 :     val deltas = List.map (fn e =>(E.C 0,e)) dx
92 :     val rest = E.Krn(hid, deltas, E.Op2(E.Sub,E.Tensor(nid,[]), E.Value(sx)))
93 :     in
94 :     E.Opn(E.Prod, [E.Img(Vid,alpha,imgpos),rest])
95 :     end
96 : jhr 3550 (*createKRN Image field and kernels *)
97 : jhr 3583 fun createKRN (0, imgpos, rest) = E.Opn(E.Prod, E.Img(Vid,alpha,imgpos)::rest)
98 :     | createKRN (d, imgpos, rest) = let
99 :     val d' = d-1
100 :     val cx = E.C(d')
101 :     val Vsum = E.Value(sx+d')
102 :     val pos0 = E.Opn(E.Add, [E.Tensor(fid,[cx]), Vsum])
103 :     val deltas = List.map (fn e =>(cx, e)) dx
104 :     val rest0 = E.Krn(hid, deltas, E.Op2(E.Sub,E.Tensor(nid,[cx]), Vsum))
105 :     in
106 :     createKRN (d', pos0::imgpos, rest0::rest)
107 :     end
108 : cchiw 3580 (*sumIndex creating summation Index for body*)
109 : jhr 3583 val esum = List.tabulate (dimO, fn d => (E.V d, 1-s, s))
110 :     val exp = if (dimO = 1) then createKRND1() else createKRN(dimO, [], [])
111 :     in
112 :     E.Sum(esum, exp)
113 :     end
114 : jhr 3550
115 : jhr 3583 (*getsumshift:sum_indexid list* int list-> int
116 : jhr 3550 *get fresh/unused index_id, returns int
117 :     *)
118 : cchiw 3569 fun getsumshift ([], n) = n
119 : cchiw 3580 | getsumshift (sx, n) = let
120 : jhr 3583 val (E.V v,_,_) = List.hd( List.rev sx)
121 :     in
122 :     v+1
123 :     end
124 : jhr 3550
125 : jhr 3583 (*formBody:ein_exp->ein_exp*)
126 : cchiw 3580 fun formBody (E.Sum([],e)) = formBody e
127 :     | formBody (E.Sum(sx,e)) = E.Sum(sx,formBody e)
128 :     | formBody (E.Opn(E.Prod, [e])) = e
129 :     | formBody e = e
130 : jhr 3550
131 : jhr 3583 (* silly change in order of the product to match vis branch WorldtoSpace functions*)
132 :     fun multiPs (Ps, sx, body) = let
133 :     val exp = (case Ps
134 :     of [P0, P1, P2] => [P0, P1, P2, body]
135 :     | [P0, P1, P2, P3] => [P0, P1, P2, P3, body]
136 :     | _ => body::Ps
137 :     (* end case *))
138 :     in
139 :     formBody(E.Sum(sx, E.Opn(E.Prod, exp)))
140 :     end
141 : jhr 3550
142 : jhr 3583 fun arrangeBody (body, Ps, newsx, exp) = (case body
143 :     of E.Sum(sx, E.Probe _ ) => (true, multiPs(Ps, sx@newsx,exp))
144 :     | E.Sum(sx, E.Opn(E.Prod,[eps0,E.Probe _ ])) =>
145 :     (false, E.Sum(sx, E.Opn(E.Prod, [eps0, multiPs(Ps, newsx,exp)])))
146 : cchiw 3581 | E.Probe _ => (true, multiPs(Ps, newsx, exp))
147 :     | _ => raise Fail "impossible"
148 : jhr 3583 (* end case *))
149 : cchiw 3579
150 : jhr 3577 (* replaceProbe:ein_exp* params *midIR.var list * int list* sum_id list
151 : jhr 3550 -> ein_exp* *code
152 :     * Transforms position to world space
153 :     * transforms result back to index_space
154 :     * rewrites body
155 :     * replace probe with expanded version
156 :     *)
157 : jhr 3583 fun replaceProbe ((y, IR.EINAPP(Ein.EIN{params, index, body}, args)), probe, sx) = let
158 :     val fid = length params
159 :     val nid = fid+1
160 :     val Pid = nid+1
161 :     val E.Probe(E.Conv(Vid, alpha, hid, dx), E.Tensor(tid, _)) = probe
162 :     val (dim, argsA, code, s, PArg) = handleArgs (Vid, hid, tid, args)
163 :     val freshIndex = getsumshift (sx, length index)
164 :     val (dx', sx', Ps) = T.imageToWorld (freshIndex, dim, dx, Pid)
165 :     val params' = params @ [E.TEN(true, [dim]), E.TEN(true, [dim]), E.TEN(true, [dim, dim])]
166 :     val probe' = fieldReconstruction (dim, s, freshIndex+length dx', alpha, dx', Vid, hid, nid, fid)
167 :     val (_, body') = arrangeBody (body, Ps, sx', probe')
168 :     val einapp = (y, IR.EINAPP(mkEin(params', index, body'), argsA@[PArg]))
169 :     in
170 : jhr 3550 code@[einapp]
171 : jhr 3583 end
172 : jhr 3550
173 : cchiw 3581 (*transform T*P*P..Ps*)
174 :     fun createEinApp (body, alpha, index, freshIndex, dim, dx, sx) = let
175 : jhr 3583 val Pid = 0
176 :     val tid = 1
177 :     val (dx', newsx, Ps) = T.imageToWorld(freshIndex, dim, dx, Pid)
178 : jhr 3550 (*need to rewrite dx*)
179 : jhr 3583 val sxx = sx@newsx
180 :     val (_, sizes, E.Conv(_, alpha', _, dx')) = (case sxx
181 :     (* QUESTION: what is the significance of "9" and "7" in this code? *)
182 :     of [] => ([], index, E.Conv(9, alpha, 7, dx'))
183 :     | _ => CleanIndex.clean(E.Conv(9, alpha, 7, dx'), index, sxx)
184 :     (* end case *))
185 :     fun filterAlpha [] = dx'
186 :     | filterAlpha (E.C _::es) = filterAlpha es
187 :     | filterAlpha (e1::es) = e1::(filterAlpha es)
188 :     val exp = E.Tensor(tid, filterAlpha alpha')
189 :     val (splitvar, body') = arrangeBody(body, Ps, newsx, exp)
190 :     val params = [E.TEN(true,[dim,dim]), E.TEN(true,sizes)]
191 :     val ein0 = mkEin(params, index, body')
192 :     in
193 :     (splitvar, ein0, sizes, dx', alpha')
194 :     end
195 : cchiw 3581
196 : jhr 3583 (* floats the reconstructed field term *)
197 : cchiw 3581 fun liftProbe ((y, IR.EINAPP(Ein.EIN{params, index, body}, args)), probe, sx) = let
198 : jhr 3583 val fid = length(params)
199 :     val nid = fid+1
200 :     val E.Probe(E.Conv(Vid, alpha, hid, dx), E.Tensor(tid, _)) = probe
201 :     val (dim, args', code, s, PArg) = handleArgs(Vid, hid, tid, args)
202 :     val freshIndex = getsumshift(sx, length(index))
203 :     (* transform T*P*P..Ps *)
204 :     val (splitvar, ein0, sizes, dx, alpha') =
205 :     createEinApp (body, alpha, index, freshIndex, dim, dx, sx)
206 :     val FArg = V.new ("T", Ty.TensorTy(sizes))
207 :     val einApp0 = IR.EINAPP(ein0, [PArg, FArg])
208 :     val rtn0 = if splitvar
209 :     then FloatEin.transform(y, EinSums.transform ein0, [PArg, FArg])
210 :     else [(y, IR.EINAPP(ein0, [PArg, FArg]))]
211 :     (* reconstruct the lifted probe *)
212 :     val params' = params@[E.TEN(true, [dim]), E.TEN(true, [dim])]
213 :     val freshIndex' = length sizes
214 :     val body' = fieldReconstruction (dim, s, freshIndex', alpha', dx, Vid, hid, nid, fid)
215 :     val einApp1 = IR.EINAPP(mkEin(params', sizes, body'), args')
216 :     in
217 :     code @ (FArg, einApp1) :: rtn0
218 :     end
219 : jhr 3550
220 : jhr 3583 (* expandEinOp: code-> code list
221 :     * A this point we only have simple ein ops
222 :     * Looks to see if the expression has a probe. If so, replaces it.
223 :     *)
224 : jhr 3582 fun expand (e as (_, IR.EINAPP(Ein.EIN{body, ...}, _))) = (case body
225 :     of (E.Probe(E.Conv(_, _, _, []) ,_)) => replaceProbe(e, body, [])
226 :     | (E.Probe(E.Conv(_, alpha, _, dx) ,_)) => liftProbe (e, body, []) (*scans dx for contant*)
227 :     | (E.Sum(sx, p as E.Probe(E.Conv(_, _, _, []), _))) => replaceProbe(e, p, sx) (*no dx*)
228 :     | (E.Sum(sx, p as E.Probe(E.Conv(_, [], _, dx), _))) => liftProbe (e, p, sx) (*scalar field*)
229 :     | (E.Sum(sx, E.Probe p)) => replaceProbe(e, E.Probe p, sx)
230 :     | (E.Sum(sx, E.Opn(E.Prod, [eps, E.Probe p]))) => replaceProbe(e ,E.Probe p, sx)
231 : jhr 3550 | _ => [e]
232 : jhr 3582 (* end case *))
233 : jhr 3550
234 :     end (* ProbeEin *)

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