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

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