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

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