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

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