Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/charisee_dev/src/compiler/high-to-mid/ProbeEin.sml
ViewVC logotype

Annotation of /branches/charisee_dev/src/compiler/high-to-mid/ProbeEin.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3259 - (view) (download)
Original Path: branches/charisee/src/compiler/high-to-mid/ProbeEin.sml

1 : cchiw 2845 (* Expands probe ein
2 : cchiw 2606 *
3 :     * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *)
6 :    
7 :     structure ProbeEin = struct
8 :    
9 :     local
10 :    
11 :     structure E = Ein
12 :     structure DstIL = MidIL
13 :     structure DstOp = MidOps
14 : jhr 3060 structure P = Printer
15 :     structure T = TransformEin
16 :     structure MidToS = MidToString
17 : cchiw 2976 structure DstV = DstIL.Var
18 :     structure DstTy = MidILTypes
19 :    
20 : cchiw 2606 in
21 :    
22 : cchiw 2870 (* This file expands probed fields
23 :     * Take a look at ProbeEin tex file for examples
24 :     *Note that the original field is an EIN operator in the form <V_alpha * H^(deltas)>(midIL.var list )
25 :     * Param_ids are used to note the placement of the argument in the midIL.var list
26 :     * 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 :     * deltas: The deltas in <V_alpha * H^(deltas)>
33 :     * Vid:param_id for V
34 :     * hid:param_id for H
35 :     * nid: integer position param_id
36 :     * fid :fractional position param_id
37 : cchiw 3166 * img-imginfo about V
38 : cchiw 2870 *)
39 : cchiw 3033
40 : cchiw 2923 val testing=0
41 : cchiw 3094 val testlift=0
42 : cchiw 3189
43 : cchiw 2845 val cnt = ref 0
44 : cchiw 2606
45 : cchiw 3030 fun printEINAPP e=MidToString.printEINAPP e
46 : cchiw 2845 fun transformToIndexSpace e=T.transformToIndexSpace e
47 :     fun transformToImgSpace e=T.transformToImgSpace e
48 : cchiw 3259 fun printEINAPP e=MidToString.printEINAPP e
49 : cchiw 3033
50 : cchiw 3048 fun transitionToString(testreplace,a,b)=(case testreplace
51 :     of 0=> 1
52 : cchiw 3094 | 2 => (print(String.concat["\n\n\n Replace probe:\n",P.printbody a,"\n=>",P.printbody b]);1)
53 : cchiw 3166 |_ =>(print(String.concat["\nReplaced:",P.printbody a]);1)
54 : cchiw 3048 (*end case*))
55 : cchiw 3033 fun mkEin(params,index,body)=E.EIN{params=params, index=index,body=body}
56 :     fun mkEinApp(rator,args)=DstIL.EINAPP(rator,args)
57 :     fun getBody(_,DstIL.EINAPP(E.EIN{body,...},_))=body
58 :     fun setBody(body',(y,DstIL.EINAPP(E.EIN{params,index,body},args)))=
59 :     (y,DstIL.EINAPP(E.EIN{params=params,index=index,body=body'},args))
60 : cchiw 3259 fun setBodye(body',E.EIN{params,index,body})=
61 :     E.EIN{params=params,index=index,body=body'}
62 : cchiw 3048
63 : cchiw 2845 fun testp n=(case testing
64 :     of 0=> 1
65 :     | _ =>(print(String.concat n);1)
66 :     (*end case*))
67 : cchiw 3048 fun einapptostring (body,a,b)=(case testlift
68 :     of 0=>1
69 : cchiw 3094 | 2=> (print(String.concat["\n lift probe of ",P.printbody body,"=>\n\t", printEINAPP a, "&\n\t", printEINAPP b]);1)
70 : cchiw 3166 |_ =>(print(String.concat["\nLifted",P.printbody body]);1)
71 : cchiw 3048 (*end case*))
72 :    
73 :    
74 : cchiw 2845 fun getRHSDst x = (case DstIL.Var.binding x
75 :     of DstIL.VB_RHS(DstIL.OP(rator, args)) => (rator, args)
76 :     | DstIL.VB_RHS(DstIL.VAR x') => getRHSDst x'
77 :     | vb => raise Fail(concat[ "expected rhs operator for ", DstIL.Var.toString x, "but found ", DstIL.vbToString vb])
78 :     (* end case *))
79 : cchiw 2838
80 : cchiw 2606
81 : cchiw 2845 (* getArgsDst:MidIL.Var* MidIL.Var->int, ImageInfo, int
82 :     uses the Param_ids for the image, kernel,
83 :     and position tensor to get the Mid-IL arguments
84 :     returns the support of ther kernel, and image
85 :     *)
86 : jhr 3060 fun getArgsDst(hArg,imgArg,args) = (case (getRHSDst hArg, getRHSDst imgArg)
87 :     of ((DstOp.Kernel(h, i), _ ), (DstOp.LoadImage(_, _, img), _ ))=> let
88 : cchiw 2845 in
89 : jhr 3060 ((Kernel.support h) ,img,ImageInfo.dim img)
90 : cchiw 2845 end
91 :     | _ => raise Fail "Expected Image and kernel arguments"
92 :     (*end case*))
93 : cchiw 2606
94 :    
95 : cchiw 2845 (*handleArgs():int*int*int*Mid IL.Var list
96 :     ->int*Mid.ILVars list* code*int* low-il-var
97 :     * uses the Param_ids for the image, kernel, and tensor
98 :     * and gets the mid-IL vars for each.
99 :     *Transforms the position to index space
100 :     *P is the mid-il var for the (transformation matrix)transpose
101 :     *)
102 :     fun handleArgs(Vid,hid,tid,args)=let
103 :     val imgArg=List.nth(args,Vid)
104 :     val hArg=List.nth(args,hid)
105 :     val newposArg=List.nth(args,tid)
106 :     val (s,img,dim) =getArgsDst(hArg,imgArg,args)
107 :     val (argsT,P,code)=transformToImgSpace(dim,img,newposArg,imgArg)
108 : cchiw 2606 in
109 : cchiw 2845 (dim,args@argsT,code, s,P)
110 : cchiw 2606 end
111 : cchiw 2838
112 : cchiw 2845 (*createBody:int*int*int,mu list, param_id, param_id, param_id, param_id
113 :     * expands the body for the probed field
114 :     *)
115 :     fun createBody(dim, s,sx,alpha,deltas,Vid, hid, nid, fid)=let
116 :     (*1-d fields*)
117 :     fun createKRND1 ()=let
118 :     val sum=sx
119 :     val dels=List.map (fn e=>(E.C 0,e)) deltas
120 :     val pos=[E.Add[E.Tensor(fid,[]),E.Value(sum)]]
121 :     val rest= E.Krn(hid,dels,E.Sub(E.Tensor(nid,[]),E.Value(sum)))
122 :     in
123 :     E.Prod [E.Img(Vid,alpha,pos),rest]
124 : cchiw 2843 end
125 : cchiw 2845 (*createKRN Image field and kernels *)
126 :     fun createKRN(0,imgpos,rest)=E.Prod ([E.Img(Vid,alpha,imgpos)] @rest)
127 :     | createKRN(dim,imgpos,rest)=let
128 :     val dim'=dim-1
129 :     val sum=sx+dim'
130 :     val dels=List.map (fn e=>(E.C dim',e)) deltas
131 :     val pos=[E.Add[E.Tensor(fid,[E.C dim']),E.Value(sum)]]
132 :     val rest'= E.Krn(hid,dels,E.Sub(E.Tensor(nid,[E.C dim']),E.Value(sum)))
133 :     in
134 :     createKRN(dim',pos@imgpos,[rest']@rest)
135 :     end
136 :     val exp=(case dim
137 :     of 1 => createKRND1()
138 :     | _=> createKRN(dim, [],[])
139 :     (*end case*))
140 :     (*sumIndex creating summaiton Index for body*)
141 :     val slb=1-s
142 :     val esum=List.tabulate(dim, (fn dim=>(E.V (dim+sx),slb,s)))
143 : cchiw 2843 in
144 : cchiw 2845 E.Sum(esum, exp)
145 : cchiw 2606 end
146 :    
147 : cchiw 2845 (*getsumshift:sum_indexid list* int list-> int
148 :     *get fresh/unused index_id, returns int
149 :     *)
150 :     fun getsumshift(sx,index) =let
151 :     val nsumshift= (case sx
152 :     of []=> length(index)
153 :     | _=>let
154 :     val (E.V v,_,_)=List.hd(List.rev sx)
155 :     in v+1
156 :     end
157 :     (* end case *))
158 :     val aa=List.map (fn (E.V v,_,_)=>Int.toString v) sx
159 :     val _ =testp["\n", "SumIndex" ,(String.concatWith"," aa),
160 :     "\nThink nshift is ", Int.toString nsumshift]
161 :     in
162 :     nsumshift
163 :     end
164 : cchiw 2611
165 : cchiw 2845 (*formBody:ein_exp->ein_exp
166 :     *just does a quick rewrite
167 :     *)
168 :     fun formBody(E.Sum([],e))=formBody e
169 :     | formBody(E.Sum(sx,e))= E.Sum(sx,formBody e)
170 :     | formBody(E.Prod [e])=e
171 :     | formBody e=e
172 : cchiw 2606
173 : cchiw 2976 (* silly change in order of the product to match vis branch WorldtoSpace functions*)
174 :     fun multiPs([P0,P1,P2],sx,body)= formBody(E.Sum(sx, E.Prod([P0,P1,P2,body])))
175 :     | multiPs(Ps,sx,body)=formBody(E.Sum(sx, E.Prod([body]@Ps)))
176 : cchiw 3195
177 : cchiw 2976
178 : cchiw 3195 fun multiMergePs([P0,P1],[sx0,sx1],body)=E.Sum([sx0],E.Prod[P0,E.Sum([sx1],E.Prod[P1,body])])
179 :     | multiMergePs e=multiPs e
180 :    
181 :    
182 : cchiw 2845 (* replaceProbe:ein_exp* params *midIL.var list * int list* sum_id list
183 :     -> ein_exp* *code
184 :     * Transforms position to world space
185 :     * transforms result back to index_space
186 :     * rewrites body
187 :     * replace probe with expanded version
188 :     *)
189 : cchiw 3048 (* fun replaceProbe(testN,y,originalb,b,params,args,index, sx)*)
190 :    
191 :     fun replaceProbe(testN,(y, DstIL.EINAPP(e,args)),p ,sx)
192 :     =let
193 :     val originalb=Ein.body e
194 :     val params=Ein.params e
195 :     val index=Ein.index e
196 : cchiw 3190 val _= ("\n"^P.printbody originalb)
197 : cchiw 3048
198 :     val E.Probe(E.Conv(Vid,alpha,hid,dx),E.Tensor(tid,_))=p
199 : cchiw 2845 val fid=length(params)
200 :     val nid=fid+1
201 :     val Pid=nid+1
202 :     val nshift=length(dx)
203 :     val (dim,argsA,code,s,PArg) = handleArgs(Vid,hid,tid,args)
204 :     val freshIndex=getsumshift(sx,index)
205 :     val (dx,newsx1,Ps)=transformToIndexSpace(freshIndex,dim,dx,Pid)
206 :     val params'=params@[E.TEN(3,[dim]),E.TEN(1,[dim]),E.TEN(1,[dim,dim])]
207 :     val body' = createBody(dim, s,freshIndex+nshift,alpha,dx,Vid, hid, nid, fid)
208 : cchiw 2976 val body' = multiPs(Ps,newsx1,body')
209 : cchiw 3033
210 :     val body'=(case originalb
211 :     of E.Sum(sx, E.Probe _) => E.Sum(sx,body')
212 :     | E.Sum(sx,E.Prod[eps0,E.Probe _ ]) => E.Sum(sx,E.Prod[eps0,body'])
213 :     | _ => body'
214 :     (*end case*))
215 : cchiw 3048 val _=transitionToString(testN,originalb,body')
216 : cchiw 3033
217 : cchiw 2845 val args'=argsA@[PArg]
218 : cchiw 3033 val einapp=(y,mkEinApp(mkEin(params',index,body'),args'))
219 :     in
220 :     code@[einapp]
221 : cchiw 2845 end
222 : cchiw 2976
223 : cchiw 3195 val tsplitvar=true
224 : cchiw 3048 fun createEinApp(originalb,alpha,index,freshIndex,dim,dx,sx)= let
225 : cchiw 2976 val Pid=0
226 :     val tid=1
227 :    
228 : cchiw 3048 val (newdx,newsx,Ps)=transformToIndexSpace(freshIndex,dim,dx,Pid)
229 :    
230 :     (*need to rewrite dx*)
231 :     val (_,sizes,E.Conv(_,_,_,dx))=(case sx@newsx
232 :     of []=> ([],index,E.Conv(9,alpha,7,newdx))
233 :     | _ =>cleanIndex.cleanIndex(E.Conv(9,alpha,7,newdx),index,sx@newsx)
234 :     (*end case*))
235 :    
236 :     val params=[E.TEN(1,[dim,dim]),E.TEN(1,sizes)]
237 : cchiw 3033 val tshape=alpha@newdx
238 :     val t=E.Tensor(tid,tshape)
239 : cchiw 3195 val (splitvar,body)=(case originalb
240 :     of E.Sum(sx, E.Probe _) => (false,E.Sum(sx,multiPs(Ps,newsx,t)))
241 :     | E.Sum(sx,E.Prod[eps0,E.Probe _ ]) => (false,E.Sum(sx,E.Prod[eps0,multiPs(Ps,newsx,t)]))
242 :     | _ => (case tsplitvar
243 : cchiw 3196 of(* true => (true,multiMergePs(Ps,newsx,t)) (*pushes summations in place*)
244 : cchiw 3259 | false*) _ => (true,multiPs(Ps,newsx,t))
245 : cchiw 3195 (*end case*))
246 : cchiw 3048 (*end case*))
247 :    
248 :     val ein0=mkEin(params,index,body)
249 : cchiw 2976 in
250 : cchiw 3195 (splitvar,ein0,sizes,dx)
251 : cchiw 2976 end
252 : cchiw 3048
253 :     fun liftProbe(testN,(y, DstIL.EINAPP(e,args)),p ,sx)=let
254 :     val originalb=Ein.body e
255 :     val params=Ein.params e
256 : cchiw 3189 val index=Ein.index e
257 : cchiw 3259 val _=print("\n Inside Lift: "^P.printbody originalb)
258 : cchiw 2976
259 : cchiw 3048 val E.Probe(E.Conv(Vid,alpha,hid,dx),E.Tensor(tid,_))=p
260 : cchiw 2976 val fid=length(params)
261 :     val nid=fid+1
262 :     val nshift=length(dx)
263 : cchiw 3048 val (dim,args',code,s,PArg) = handleArgs(Vid,hid,tid,args)
264 : cchiw 2976 val freshIndex=getsumshift(sx,index)
265 :    
266 :    
267 :     (*transform T*P*P..Ps*)
268 : cchiw 3195 val (splitvar,ein0,sizes,dx)= createEinApp(originalb,alpha,index,freshIndex,dim,dx,sx)
269 : cchiw 3048 val FArg = DstV.new ("F", DstTy.TensorTy(sizes))
270 :     val einApp0=mkEinApp(ein0,[PArg,FArg])
271 : cchiw 3195 val rtn0=(case splitvar
272 :     of false => [(y,einApp0)]
273 :     | _ => Split.splitEinApp (y,einApp0)
274 :     (*end case*))
275 : cchiw 2976
276 :     (*lifted probe*)
277 :     val params'=params@[E.TEN(3,[dim]),E.TEN(1,[dim])]
278 : cchiw 3048 val body' = createBody(dim, s,freshIndex+nshift,alpha,dx,Vid, hid, nid, fid)
279 : cchiw 3033 val ein1=mkEin(params',sizes,body')
280 : cchiw 2976 val einApp1=mkEinApp(ein1,args')
281 : cchiw 3048 val rtn1=(FArg,einApp1)
282 : cchiw 3195 val rtn=code@[rtn1]@rtn0
283 : cchiw 3259
284 : cchiw 2976 in
285 :     rtn
286 :     end
287 :    
288 : cchiw 2845 (* expandEinOp: code-> code list
289 : cchiw 3259 * A this point we only have simple ein ops
290 :     * Looks to see if the expression has a probe. If so, replaces it.
291 : cchiw 2845 * Note how we keeps eps expressions so only generate pieces that are used
292 :     *)
293 : cchiw 3229 fun expandEinOp( e as (y, DstIL.EINAPP(ein,args)),fieldset)=let
294 : cchiw 3259
295 : cchiw 3066 fun checkConst ([],a) = liftProbe a
296 : cchiw 3259 | checkConst ((E.C _::_),a) =(print("\n \n constant field"^(printEINAPP e));replaceProbe a)
297 : cchiw 3166 | checkConst ((_ ::es),a)= checkConst(es,a)
298 : cchiw 3259
299 : cchiw 2845 fun rewriteBody b=(case b
300 : cchiw 3048 of E.Probe(E.Conv(_,_,_,[]),_)
301 : cchiw 3094 => replaceProbe(0,e,b, [])
302 : cchiw 3066 | E.Probe(E.Conv (_,alpha,_,dx),_)
303 : cchiw 3259 => checkConst(dx,(0,e,b,[])) (*scans dx for contant*)
304 : cchiw 3048 | E.Sum(sx,p as E.Probe(E.Conv(_,_,_,[]),_))
305 : cchiw 3094 => replaceProbe(0,e,p, sx)
306 : cchiw 3066 | E.Sum(sx,p as E.Probe(E.Conv(_,[],_,dx),_))
307 :     => checkConst(dx,(0,e,p,sx))
308 : cchiw 3259 | E.Sum(sx as [(v,_,_)],p as (E.Probe((E.Conv(_,alpha,_,dx),_))))=>
309 :     (print(P.printbody(b));case (List.find (fn x => x = v) dx)
310 :     of NONE=> checkConst(dx,(1,e,p,sx))
311 :     (*need to push summation to lifted exp rather than transform exp.*)
312 : cchiw 3166 | SOME _=> replaceProbe(1,e, p, sx)
313 : cchiw 3259 (*end case*))
314 :     | E.Sum(sx as [(v1,_,_),(v2,_,_)],p as (E.Probe((E.Conv(_,alpha,_,dx),_))))=>
315 :     (print(P.printbody(b));case ((List.find (fn x => x = v1) dx),(List.find (fn x => x = v2) dx))
316 :     of (NONE,NONE)=> checkConst(dx,(1,e,p,sx))
317 :     | ( _ , NONE)=> checkConst(dx,(1,e,p,sx))
318 :     | _=> (*replaceProbe(1,e, p, sx)*) checkConst(dx,(1,e,p,sx))
319 :     (*end case*))
320 : cchiw 3048 | E.Sum(sx,E.Probe p)
321 : cchiw 3094 => replaceProbe(0,e,E.Probe p, sx)
322 : cchiw 3048 | E.Sum(sx,E.Prod[eps,E.Probe p])
323 : cchiw 3094 => replaceProbe(0,e,E.Probe p,sx)
324 : cchiw 3048 | _ => [e]
325 : cchiw 2845 (* end case *))
326 : cchiw 3174
327 :     val (fieldset,var) = einSet.rtnVar(fieldset,y,DstIL.EINAPP(ein,args))
328 :     in (case var
329 : cchiw 3259 of NONE=> (("\n \n not replacing"^(printEINAPP e));(rewriteBody(Ein.body ein),fieldset))
330 :     | SOME v=> (("\n replacing"^(P.printerE ein));( [(y,DstIL.VAR v)] , fieldset))
331 : cchiw 3174 (*end case*))
332 : cchiw 2845 end
333 : cchiw 2843
334 : cchiw 2606 end; (* local *)
335 :    
336 : cchiw 2845 end (* local *)

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