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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 3544 - (download) (annotate)
Tue Jan 5 00:01:44 2016 UTC (3 years, 6 months ago) by cchiw
File size: 8734 byte(s)
code cleanup
structure EvalImgSet = struct
    structure DstOp = LowOps
    structure DstTy = LowILTypes
    structure DstIL = LowIL
    structure LowToS= LowToString
    structure Var = LowIL.Var
    structure E = Ein
    structure P=Printer
    structure H=HelperSet


    fun lookup e =H.lookup e
    fun insert e=H.insertP e
    fun find e=H.find e
    fun mapIndex e=H.mapIndex e
    fun mkInt n =H.mkInt n
    fun assgn e=H.assignOP e
    fun indexTensor e = H.indexTensor e
    fun mkAddInt e= H.mkAddInt e
    fun mkAddPtr e = H.mkAddPtr e
    fun mkProdInt e= H.mkProdInt e
    fun iTos n=Int.toString n
    fun err str=raise Fail(str)
    val intTy=DstTy.IntTy
    val testing=false
    fun testp n =if (testing) then (print(String.concat n);1) else 1
    fun psize n=foldl (fn (a,b) => b*a) 1 n
    fun asize n=foldl (fn (a,b) => b+a) 0 n

    (* mkImg:dict*string*E.params*var list*sum_id list*()*image*var*int*int*int
    * ->var*lowIL.assgn
    * The image "imgarg" is probed at positions
    * Σ_{sx} V_alpha[pos0::px]
    * sumPos() iterates over the summation indices and creates a mapp for the indicies
    * once mapp(j->2 k->0) is created sumPos() calls createImgVar()  to get the addr of Σ_k V_{i}[T_j,T_k]
    * createImgVar() uses  mkpos(), getPosAddr() and getImgAddr() to get imgvar 
    fun mkImg(setOrig,mappOrig,lhs,params,args, sx,(_,v_alpha,pos0::px),v,imgarg,lb,range0,range1)=let
        val dim=ImageInfo.dim v
        val ptyTy=DstTy.AddrTy v
        val sizes=ImageInfo.sizes v
        val (setBase,vBase,base)=assgn(setOrig,DstOp.baseAddr v,[imgarg],"baseAddr",ptyTy)
        (*base address*)
        val (setShapeShift,vShapeShift,ShapeShiftcode)= mkInt(setBase,psize (ImageInfo.voxelShape v))
        (*shift of the image field.*)

        (*Since the image is loaded as a vector
        * we evaluate the first position just once
        * Σ_{ij..} V[T+i,T+j...]-> Σ_{j..} V[T+j...]
        * and we drop the first summation index
        * Additionally, summation indices are reversed
        * that inner loop is second(y) axis and outer loop is third(z)axis
        val (setPos0,vPos0,Pos0code,sxx)=let
            val E.Opn(E.Add,[E.Tensor(t1,ix1),_ ])=pos0
            val (setA,vA,A)=indexTensor(setShapeShift,mappOrig,("",params,args, t1,ix1,intTy))
            val (setB,vB,B)= mkInt(setA, lb)
            val (setC,vC,C)= mkAddInt(setB, [vA,vB])
            val _ =testp["\nsxx\n original:"]
            val sxx= List.map(fn (E.V sid,_,_)=> sid) sx
            val _ =List.map (fn e=> testp["-",iTos e]) sxx
            val sxx=List.rev(List.tl(sxx))
            val _ =testp["\n used:"]
            val _ =List.map (fn e=> testp["-",iTos e]) sxx

        (* mkpos:ein_exp list*var list*DstIL.assgn list
        * transform ein_exp to low-il
        * returns var for the position
        fun mkpos(setPos,e,mapp,rest,code)=(case e
            of [] => (setPos,rest,code)
            | (E.Opn(E.Add,([E.Tensor(t1,ix1),E.Value v1]))::es)=> let
                val (setA,vA,A)=indexTensor(setPos,mapp,("",params,args,t1,ix1,intTy))
                val j=find(v1,mapp)
                val (setD,rest',code')=(case j
                    of 0 => (setPos,vA,A)
                    | _ => let
                        val (setB,vB,B)= mkInt(setA, j)
                        val (setC,vC,C)=mkAddInt(setB,[vA,vB])
                    in (setC,vC,A@B@C) end
                    (*end case*))
                in mkpos(setD,es,mapp,rest@[rest'],code@code') end
            | e1::_ => raise Fail("Incorrect pos for Image: "^P.printbody e1)
            (*end case*))
        (* getPosAddr:var list->var*DstIL.assgn list
        * create position addr based on image info's shapeshift,image info's sizes,and args
        * args are the variables for this specific positions. V_([x,y])
        * returns vPosAddr,PosAddrcode
        fun getPosAddr(setPosAddr, args)=(case (sizes,args)
            of([ _ ],[i]) =>mkProdInt(setPosAddr,[vShapeShift,i]) (*1-d*)
            | ([x, _ ],[i,j]) =>let                  (*2-d*)
                val (setA,vA,A)= mkInt(setPosAddr,  x)
                val (setB,vB,B)= mkProdInt (setA,[vA,j])
                val (setC,vC,C)= mkAddInt(setB, [i,vB])
                val (setD,vD,D)= mkProdInt(setC,[vShapeShift,vC])
                in (setD,vD,A@B@C@D) end
            | ([x,y,_],[i,j,k])  =>let                (*3-d*)
                val (setA,vA,A)= mkInt(setPosAddr,  y)
                val (setB,vB,B)= mkProdInt(setA, [vA,k])
                val (setC,vC,C)= mkAddInt(setB ,[j,vB])
                val (setD,vD,D)= mkInt(setC,  x)
                val (setE,vE,E)= mkProdInt (setD,[vD,vC])
                val (setF,vF,F)= mkAddInt(setE, [i,vE])
                val( setG,vG,G)= mkProdInt(setF,[vShapeShift,vF])
                in (setG,vG,A@B@C@D@E@F@G) end
            (*end case*))
        (* getImgAddr:int list *var->var*DstIL.assgn list
        * creates image address with ^position address,imgType, and base address
        * imgType are  image specific indices V[0,1](_)
        * ->returns (vImgAddr,ImgAddrcode)
        fun getImgAddr (setImgAddr,imgType,vPosAddr) =(case imgType
            of [] => mkAddPtr(setImgAddr,[vBase,vPosAddr],ptyTy)
            | [0] => mkAddPtr(setImgAddr,[vBase,vPosAddr],ptyTy)
            | [_] => let
                val (setA,vA,A)= mkAddPtr(setImgAddr,[vBase,vPosAddr],ptyTy)
                val (setB,vB,B)= mkInt(setA,asize imgType)
                val (setC,vC,C)= mkAddPtr(setB,[vB, vA],ptyTy)
                in (setC,vC,A@B@C)end
            | [i,j] => let
                val [a,b]=ImageInfo.voxelShape v

                val (setA,vA,A)= mkAddPtr(setImgAddr,[vBase,vPosAddr],ptyTy)
                val (setB,vB,B)= mkInt (setA,(b*j)+i)
                val (setC,vC,C)= mkAddPtr(setB,[vB, vA],ptyTy)
                in (setC,vC,A@B@C)end
                (*end case*))

        (* createImgVar:dict->var*DstIL.assgn list
        *  gets low-il var for loading an image address
        fun createImgVar(setI, mapp)=let
            val (setA,vA,A)= mkpos(setI,px,mapp,[],[])     (*transforms the probed position to low-il*)
            val posArgs=[vPos0]@vA                              (*adds intial position to ^*)
            val (setPosAddr,vPosAddr,PosAddrcode)=getPosAddr(setA, posArgs)                (*position address*)
            val imgType=List.map (fn (e1)=> mapIndex(e1,mapp)) v_alpha (*img specific index*)
            val (setImgAddr,vImgAddr,ImgAddrcode)=getImgAddr (setPosAddr,imgType,vPosAddr)          (*img address*)
            val (setD,vD,D)=assgn(setImgAddr,DstOp.imgLoad(v,dim,range1),[vImgAddr],"imgrng",DstTy.tensorTy([range1]))

        val range0List=List.tabulate(range0+1,fn e=>e)
        (* sumPos:index_id * var list*lowil.assgn list*dict*int
        * ->var*lowil.assgn list
        * sumPos iterates over the summation indices and creates mapp
        fun sumPos(setSx,[],lft,code,dict,_)=let
            val (set',lft', code')= createImgVar(setSx, dict)
            in (set',lft@[lft'],code@code') end
        | sumPos(setSx,[sid],lft,code,dict,[r])=let
            val n'=lb+r
            val _=testp["\n insert",iTos sid, "->",iTos n']
            val mapp=insert (sid, n') dict
            val (set',lft', code')= createImgVar(setSx, mapp)
            in (set',lft@[lft'],code@code') end
        | sumPos(setSx,[sid],lft,code,dict,r::es)=let
            val n'=lb+r
            val _=testp["\n insert",iTos sid, "->",iTos n']
            val mapp=insert (sid, n') dict
            val (set',lft', code')= createImgVar(setSx, mapp)
            in sumPos(set',[sid],lft@[lft'],code@code',dict,es) end
        | sumPos(setSx,sid::sxx,lft,code,dict,[r])=let
            val n'=lb+r
            val _=testp["\n insert",iTos sid, "->",iTos n']
            val mapp=insert (sid, n') dict
            val (set',lft',code')=sumPos(setSx,sxx,lft,[],mapp,range0List)
            in (set',lft',code@code') end
        | sumPos(setSx,sid::sxx,lft,code,dict,r::es)=let
            val n'=lb+r
            val _=testp["\n insert",iTos sid, "->",iTos n']
            val mapp=insert (sid, n') dict
            val (set',lft',code')=sumPos(setSx,sxx,lft,[],mapp,range0List)

end (* local *)


ViewVC Help
Powered by ViewVC 1.0.0