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

SCM Repository

[diderot] Annotation of /branches/charisee/src/compiler/mid-to-low/evalImg-set.sml
ViewVC logotype

Annotation of /branches/charisee/src/compiler/mid-to-low/evalImg-set.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3544 - (view) (download)

1 : cchiw 3444 structure EvalImgSet = struct
2 :     local
3 :    
4 :     structure DstOp = LowOps
5 :     structure DstTy = LowILTypes
6 :     structure DstIL = LowIL
7 :     structure LowToS= LowToString
8 :     structure Var = LowIL.Var
9 :     structure E = Ein
10 :     structure P=Printer
11 :     structure H=HelperSet
12 :    
13 :     in
14 :    
15 :     fun lookup e =H.lookup e
16 :     fun insert e=H.insertP e
17 :     fun find e=H.find e
18 :     fun mapIndex e=H.mapIndex e
19 :     fun mkInt n =H.mkInt n
20 :     fun assgn e=H.assignOP e
21 :     fun indexTensor e = H.indexTensor e
22 :     fun mkAddInt e= H.mkAddInt e
23 :     fun mkAddPtr e = H.mkAddPtr e
24 :     fun mkProdInt e= H.mkProdInt e
25 :     fun iTos n=Int.toString n
26 :     fun err str=raise Fail(str)
27 :     val intTy=DstTy.IntTy
28 : cchiw 3542 val testing=false
29 :     fun testp n =if (testing) then (print(String.concat n);1) else 1
30 : cchiw 3444 fun psize n=foldl (fn (a,b) => b*a) 1 n
31 :     fun asize n=foldl (fn (a,b) => b+a) 0 n
32 :    
33 :     (* mkImg:dict*string*E.params*var list*sum_id list*()*image*var*int*int*int
34 :     * ->var*lowIL.assgn
35 :     * The image "imgarg" is probed at positions
36 :     * Σ_{sx} V_alpha[pos0::px]
37 :     * sumPos() iterates over the summation indices and creates a mapp for the indicies
38 :     * once mapp(j->2 k->0) is created sumPos() calls createImgVar() to get the addr of Σ_k V_{i}[T_j,T_k]
39 :     * createImgVar() uses mkpos(), getPosAddr() and getImgAddr() to get imgvar
40 :     *)
41 :     fun mkImg(setOrig,mappOrig,lhs,params,args, sx,(_,v_alpha,pos0::px),v,imgarg,lb,range0,range1)=let
42 :     val dim=ImageInfo.dim v
43 :     val ptyTy=DstTy.AddrTy v
44 :     val sizes=ImageInfo.sizes v
45 : cchiw 3542 val (setBase,vBase,base)=assgn(setOrig,DstOp.baseAddr v,[imgarg],"baseAddr",ptyTy)
46 :     (*base address*)
47 :     val (setShapeShift,vShapeShift,ShapeShiftcode)= mkInt(setBase,psize (ImageInfo.voxelShape v))
48 :     (*shift of the image field.*)
49 : cchiw 3444
50 :     (*Since the image is loaded as a vector
51 :     * we evaluate the first position just once
52 :     * Σ_{ij..} V[T+i,T+j...]-> Σ_{j..} V[T+j...]
53 :     * and we drop the first summation index
54 :     * Additionally, summation indices are reversed
55 :     * that inner loop is second(y) axis and outer loop is third(z)axis
56 :     *)
57 :     val (setPos0,vPos0,Pos0code,sxx)=let
58 : cchiw 3448 val E.Opn(E.Add,[E.Tensor(t1,ix1),_ ])=pos0
59 : cchiw 3444 val (setA,vA,A)=indexTensor(setShapeShift,mappOrig,("",params,args, t1,ix1,intTy))
60 :     val (setB,vB,B)= mkInt(setA, lb)
61 :     val (setC,vC,C)= mkAddInt(setB, [vA,vB])
62 :     val _ =testp["\nsxx\n original:"]
63 :     val sxx= List.map(fn (E.V sid,_,_)=> sid) sx
64 :     val _ =List.map (fn e=> testp["-",iTos e]) sxx
65 :     val sxx=List.rev(List.tl(sxx))
66 :     val _ =testp["\n used:"]
67 :     val _ =List.map (fn e=> testp["-",iTos e]) sxx
68 :    
69 :     in
70 :     (setC,vC,A@B@C,sxx)
71 :     end
72 :     (* mkpos:ein_exp list*var list*DstIL.assgn list
73 :     * transform ein_exp to low-il
74 :     * returns var for the position
75 :     *)
76 :     fun mkpos(setPos,e,mapp,rest,code)=(case e
77 :     of [] => (setPos,rest,code)
78 : cchiw 3448 | (E.Opn(E.Add,([E.Tensor(t1,ix1),E.Value v1]))::es)=> let
79 : cchiw 3444 val (setA,vA,A)=indexTensor(setPos,mapp,("",params,args,t1,ix1,intTy))
80 :     val j=find(v1,mapp)
81 :     val (setD,rest',code')=(case j
82 :     of 0 => (setPos,vA,A)
83 :     | _ => let
84 :     val (setB,vB,B)= mkInt(setA, j)
85 :     val (setC,vC,C)=mkAddInt(setB,[vA,vB])
86 :     in (setC,vC,A@B@C) end
87 :     (*end case*))
88 :     in mkpos(setD,es,mapp,rest@[rest'],code@code') end
89 :     | e1::_ => raise Fail("Incorrect pos for Image: "^P.printbody e1)
90 :     (*end case*))
91 :     (* getPosAddr:var list->var*DstIL.assgn list
92 :     * create position addr based on image info's shapeshift,image info's sizes,and args
93 :     * args are the variables for this specific positions. V_([x,y])
94 :     * returns vPosAddr,PosAddrcode
95 :     *)
96 :     fun getPosAddr(setPosAddr, args)=(case (sizes,args)
97 :     of([ _ ],[i]) =>mkProdInt(setPosAddr,[vShapeShift,i]) (*1-d*)
98 :     | ([x, _ ],[i,j]) =>let (*2-d*)
99 :     val (setA,vA,A)= mkInt(setPosAddr, x)
100 :     val (setB,vB,B)= mkProdInt (setA,[vA,j])
101 :     val (setC,vC,C)= mkAddInt(setB, [i,vB])
102 :     val (setD,vD,D)= mkProdInt(setC,[vShapeShift,vC])
103 :     in (setD,vD,A@B@C@D) end
104 :     | ([x,y,_],[i,j,k]) =>let (*3-d*)
105 :     val (setA,vA,A)= mkInt(setPosAddr, y)
106 :     val (setB,vB,B)= mkProdInt(setA, [vA,k])
107 :     val (setC,vC,C)= mkAddInt(setB ,[j,vB])
108 :     val (setD,vD,D)= mkInt(setC, x)
109 :     val (setE,vE,E)= mkProdInt (setD,[vD,vC])
110 :     val (setF,vF,F)= mkAddInt(setE, [i,vE])
111 :     val( setG,vG,G)= mkProdInt(setF,[vShapeShift,vF])
112 :     in (setG,vG,A@B@C@D@E@F@G) end
113 :     (*end case*))
114 :     (* getImgAddr:int list *var->var*DstIL.assgn list
115 :     * creates image address with ^position address,imgType, and base address
116 :     * imgType are image specific indices V[0,1](_)
117 :     * ->returns (vImgAddr,ImgAddrcode)
118 :     *)
119 :     fun getImgAddr (setImgAddr,imgType,vPosAddr) =(case imgType
120 :     of [] => mkAddPtr(setImgAddr,[vBase,vPosAddr],ptyTy)
121 :     | [0] => mkAddPtr(setImgAddr,[vBase,vPosAddr],ptyTy)
122 :     | [_] => let
123 :     val (setA,vA,A)= mkAddPtr(setImgAddr,[vBase,vPosAddr],ptyTy)
124 :     val (setB,vB,B)= mkInt(setA,asize imgType)
125 :     val (setC,vC,C)= mkAddPtr(setB,[vB, vA],ptyTy)
126 :     in (setC,vC,A@B@C)end
127 :     | [i,j] => let
128 :     val [a,b]=ImageInfo.voxelShape v
129 : cchiw 3542
130 : cchiw 3444 val (setA,vA,A)= mkAddPtr(setImgAddr,[vBase,vPosAddr],ptyTy)
131 :     val (setB,vB,B)= mkInt (setA,(b*j)+i)
132 :     val (setC,vC,C)= mkAddPtr(setB,[vB, vA],ptyTy)
133 :     in (setC,vC,A@B@C)end
134 :     (*end case*))
135 :    
136 :     (* createImgVar:dict->var*DstIL.assgn list
137 :     * gets low-il var for loading an image address
138 :     *)
139 :     fun createImgVar(setI, mapp)=let
140 :     val (setA,vA,A)= mkpos(setI,px,mapp,[],[]) (*transforms the probed position to low-il*)
141 :     val posArgs=[vPos0]@vA (*adds intial position to ^*)
142 :     val (setPosAddr,vPosAddr,PosAddrcode)=getPosAddr(setA, posArgs) (*position address*)
143 :     val imgType=List.map (fn (e1)=> mapIndex(e1,mapp)) v_alpha (*img specific index*)
144 :     val (setImgAddr,vImgAddr,ImgAddrcode)=getImgAddr (setPosAddr,imgType,vPosAddr) (*img address*)
145 :     val (setD,vD,D)=assgn(setImgAddr,DstOp.imgLoad(v,dim,range1),[vImgAddr],"imgrng",DstTy.tensorTy([range1]))
146 :     in
147 :     (setD,vD,A@PosAddrcode@ImgAddrcode@D)
148 :     end
149 :    
150 :     val range0List=List.tabulate(range0+1,fn e=>e)
151 :     (* sumPos:index_id * var list*lowil.assgn list*dict*int
152 :     * ->var*lowil.assgn list
153 :     * sumPos iterates over the summation indices and creates mapp
154 :     *)
155 :     fun sumPos(setSx,[],lft,code,dict,_)=let
156 :     val (set',lft', code')= createImgVar(setSx, dict)
157 :     in (set',lft@[lft'],code@code') end
158 :     | sumPos(setSx,[sid],lft,code,dict,[r])=let
159 :     val n'=lb+r
160 :     val _=testp["\n insert",iTos sid, "->",iTos n']
161 :     val mapp=insert (sid, n') dict
162 :     val (set',lft', code')= createImgVar(setSx, mapp)
163 :     in (set',lft@[lft'],code@code') end
164 :     | sumPos(setSx,[sid],lft,code,dict,r::es)=let
165 :     val n'=lb+r
166 :     val _=testp["\n insert",iTos sid, "->",iTos n']
167 :     val mapp=insert (sid, n') dict
168 :     val (set',lft', code')= createImgVar(setSx, mapp)
169 :     in sumPos(set',[sid],lft@[lft'],code@code',dict,es) end
170 :     | sumPos(setSx,sid::sxx,lft,code,dict,[r])=let
171 :     val n'=lb+r
172 :     val _=testp["\n insert",iTos sid, "->",iTos n']
173 :     val mapp=insert (sid, n') dict
174 :     val (set',lft',code')=sumPos(setSx,sxx,lft,[],mapp,range0List)
175 :     in (set',lft',code@code') end
176 :     | sumPos(setSx,sid::sxx,lft,code,dict,r::es)=let
177 :     val n'=lb+r
178 :     val _=testp["\n insert",iTos sid, "->",iTos n']
179 :     val mapp=insert (sid, n') dict
180 :     val (set',lft',code')=sumPos(setSx,sxx,lft,[],mapp,range0List)
181 :     in
182 :     sumPos(set',sid::sxx,lft',code@code',dict,es)
183 :     end
184 :     val(setSx,lft,code)=sumPos(setPos0,sxx,[],[],mappOrig,range0List)
185 :     in
186 :     (setSx,lft,base@ShapeShiftcode@Pos0code@code)
187 :     end
188 :    
189 :    
190 :     end (* local *)
191 :    
192 :     end

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