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

SCM Repository

[diderot] View of /branches/vis15/src/compiler/mid-to-low/eval-img.sml
ViewVC logotype

View of /branches/vis15/src/compiler/mid-to-low/eval-img.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3741 - (download) (annotate)
Sun Apr 10 23:33:56 2016 UTC (3 years, 10 months ago) by cchiw
File size: 7769 byte(s)
pushing lifting kernel
structure EvalImg = struct
    local
  
    structure Op = LowOps
    structure Ty = LowTypes
    structure IL = LowIR
    structure Var = LowIR.Var
    structure E = Ein
    structure P = EinPP
    structure Mk = MkLowIR
    structure Ty = LowTypes
    structure IMap = IntRedBlackMap

    in

    fun lookup k d = IMap.find (d, k)
    fun insert  (k, v) d =  IMap.insert (d, k, v)
    fun find  (v, mapp) =  (case IMap.find (mapp, v)
        of NONE => raise Fail (concat["Outside Bound (", Int.toString v, ") "])
        | SOME s => s
        (* end *) )
    fun mapIndex  (e1, mapp) =  (case e1
        of E.V e => find (e, mapp)
        | E.C c => c
        (*end case*) )
    fun mkAddPtr (avail, args, ty) = Mk.assignOP (avail, Op.RAdd, args, "addPtr", ty)
    fun mkProdInt (avail, args) = Mk.assignOP (avail, Op.RMul, args, "prodInt", Ty.intTy)
    fun mkInt  (avail, n) =  Mk.intLit(avail, IntInf.fromInt n)

    fun err str = raise Fail (str)
    fun psize n = foldl (fn (a, b) => b*a)  1 n
    fun asize n = foldl (fn (a, b) => b+a)  0 n

    (* mkImg:
    * 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 (avail, mappOrig, args, sx, (_, v_alpha, pos0::px), v, imgarg, lb, range0) = let
        val range1 = range0+1
        val dim = ImageInfo.dim v
        val ptyTy = Ty.AddrTy v
        val sizes = ImageInfo.sizes v
        (*base address*)
        val vBase = Mk.assignOP (avail, Op.baseAddr v, [imgarg], "baseAddr", ptyTy)
        (*shift of the image field.*)
        val vShapeShift = mkInt (avail, psize (ImageInfo.voxelShape v))


        (*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 (vPos0, sxx) = let
            val E.Opn (E.Add, [E.Tensor (id, ix), _ ]) = pos0
            val vA = Mk.tensorIndex (avail, mappOrig, List.nth (args, id), ix)
            val vB = mkInt (avail, lb)
            val vC = Mk.realAdd (avail, vA, vB)
            val sxx = List.rev (List.tl(List.map (fn (E.V sid, _, _) => sid)  sx))
            in
                (vC, sxx)
            end
        (* mkpos:ein_exp list*var list*IR.Mk.assignOP list
        * transform ein_exp to low-il
        * returns var for the position
        *) 
        fun mkpos (avail, e, mapp, rest) = (case e
            of [] => rest
            | (E.Opn (E.Add, ([E.Tensor (id, ix), E.Value v1])) ::es) => let
                val vA = Mk.tensorIndex (avail, mapp, List.nth (args, id), ix)
                val rest' = (case find (v1, mapp)
                    of 0 => vA
                    | j => let
                        val vB = mkInt (avail, j)
                        val vC = Mk.realAdd (avail, vA, vB)
                    in vC end
                    (*end case*)) 
                in mkpos (avail, es, mapp, rest@[rest'])  end
            | e1::_ => raise Fail ("Incorrect pos for Image: "^P.expToString e1) 
            (*end case*)) 
        (* getPosAddr:var list->var*IR.Mk.assignOP 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 (avail, args) = case (sizes, args)
            of ([ _ ], [i]) => mkProdInt (avail, [vShapeShift, i])  (*1-d*)
            | ([x, _ ], [i, j]) =>let                  (*2-d*) 
                val vA = mkInt (avail, x)
                val vB = mkProdInt (avail, [vA, j])
                val vC = Mk.realAdd (avail, i, vB)
                in mkProdInt (avail, [vShapeShift, vC]) end
            | ([x, y, _], [i, j, k]) =>let                (*3-d*) 
                val vA = mkInt (avail, y)
                val vB = mkProdInt (avail, [vA, k])
                val vC = Mk.realAdd (avail, j, vB)
                val vD = mkInt (avail, x)
                val vE = mkProdInt (avail, [vD, vC])
                val vF = Mk.realAdd (avail, i, vE)
                in mkProdInt (avail, [vShapeShift, vF]) end

        (* getImgAddr:int list *var->var*IR.Mk.assignOP list
        * creates image address with ^position address, imgType, and base address
        * imgType are  image specific indices V[0, 1] (_) 
        * ->returns (vImgAddr, ImgAddrcode) 
        *) 
        fun getImgAddr (avail, imgType, vPosAddr) = case imgType
            of [] => mkAddPtr (avail, [vBase, vPosAddr], ptyTy) 
            | [0] => mkAddPtr (avail, [vBase, vPosAddr], ptyTy) 
            | [_] => let
                val vA = mkAddPtr (avail, [vBase, vPosAddr], ptyTy)
                val vB = mkInt (avail, asize imgType)
                in mkAddPtr (avail, [vB, vA], ptyTy) end
            | [i, j] => let
                val [a, b] = ImageInfo.voxelShape v
                val vA = mkAddPtr (avail, [vBase, vPosAddr], ptyTy)
                val vB = mkInt (avail, (b*j) +i)
                in mkAddPtr (avail, [vB, vA], ptyTy)  end


        (* createImgVar:dict->var*IR.Mk.assignOP list
        *  gets low-il var for loading an image address
        *) 
        fun createImgVar (avail, mapp) = let
            val vA = mkpos (avail, px, mapp, [])      (*transforms the probed position to low-il*)
            val posArgs = vPos0::vA                              (*adds intial position to ^*)
            val vPosAddr = getPosAddr (avail, posArgs)                 (*position address*)
            val imgType = List.map (fn (e1) => mapIndex (e1, mapp))  v_alpha (*img specific index*)
            val vImgAddr = getImgAddr (avail, imgType, vPosAddr)           (*img address*)
            in
                Mk.assignOP (avail, Op.imgLoad (v, dim, range1), [vImgAddr], "imgrng", Ty.tensorTy ([range1]))
            end

        val range0List = List.tabulate (range0+1, fn e =>e) 
        (* sumPos:index_id * var list*LowIR.Mk.assignOP list*dict*int
        * ->var*LowIR.Mk.assignOP list
        * sumPos iterates over the summation indices and creates mapp
        *) 
        fun sumPos (avail, [], rest, dict, _) = let
            val rest' = createImgVar (avail, dict)
            in rest'::rest end
        | sumPos (avail, [sid], rest, dict, [r]) = let
            val n' = lb+r
            val mapp = insert (sid, n')  dict
            val rest' = createImgVar (avail, mapp)
            in rest'::rest  end
        | sumPos (avail, [sid], rest, dict, r::es) = let
            val n' = lb+r
            val mapp = insert (sid, n')  dict
            val rest' = createImgVar (avail, mapp)
            in sumPos (avail, [sid], rest'::rest, dict, es)  end
        | sumPos (avail, sid::sxx, rest, dict, [r]) = let
            val n' = lb+r
            val mapp = insert (sid, n')  dict
            in
                sumPos (avail, sxx, rest, mapp, range0List)
            end
        | sumPos (avail, sid::sxx, rest, dict, r::es) = let
            val n' = lb+r
            val mapp = insert (sid, n')  dict
            val rest' = sumPos (avail, sxx, rest, mapp, range0List)
            in
                sumPos (avail, sid::sxx, rest', dict, es)
            end
        val ids = sumPos (avail, sxx, [], mappOrig, range0List)
        in
            List.rev ids
        end


end (* local *) 

end

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