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 3624 - (download) (annotate)
Fri Jan 29 17:49:01 2016 UTC (3 years, 11 months ago) by jhr
File size: 8043 byte(s)
adding header comments in prep for merge
(* evalImg-set.sml
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2016 The University of Chicago
 * All rights reserved.
 *)

structure EvalImg = struct
    local
  
    structure Op = LowOps
    structure Ty = LowILTypes
    structure IL = LowIL
    structure Var = LowIL.Var
    structure E = Ein
    structure P = Printer
    structure H = Helper
    structure IMap = IntRedBlackMap

    in

    fun lookup k d = IMap.find (d, k)
    fun insert  (k, v) d =  IMap.insert (d, k, v)
    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 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: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 (avail, mappOrig, params, args, sx, (_, v_alpha, pos0::px), v, imgarg, lb, range0, range1) = let
        val dim = ImageInfo.dim v
        val ptyTy = Ty.AddrTy v
        val sizes = ImageInfo.sizes v
        val (avail, vBase) = assgn (avail, Op.baseAddr v, [imgarg], "baseAddr", ptyTy)
        (*base address*) 
        val (avail, vShapeShift) = mkInt (avail, 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 (avail, vPos0, sxx) = let
            val E.Opn (E.Add, [E.Tensor (t1, ix1), _ ]) = pos0
            val (avail, vA) = indexTensor (avail, mappOrig, ( params, args, t1, ix1, Ty.IntTy))
            val (avail, vB) = mkInt (avail, lb)
            val (avail, vC) = mkAddInt (avail, [vA, vB])
            val sxx = List.rev (List.tl(List.map (fn (E.V sid, _, _) => sid)  sx))
            in
                (avail, vC, sxx)
            end
        (* mkpos:ein_exp list*var list*IL.assgn list
        * transform ein_exp to low-il
        * returns var for the position
        *) 
        fun mkpos (avail, e, mapp, rest) = (case e
            of [] => (avail, rest)
            | (E.Opn (E.Add, ([E.Tensor (t1, ix1), E.Value v1])) ::es) => let
                val (avail, vA) = indexTensor (avail, mapp, (params, args, t1, ix1, Ty.IntTy))
                val (avail, rest') = (case find (v1, mapp)
                    of 0 => (avail, vA)
                    | j => let
                        val (avail, vB) = mkInt (avail, j)
                        val (avail, vC) = mkAddInt (avail, [vA, vB])
                    in (avail, vC)  end
                    (*end case*)) 
                in mkpos (avail, es, mapp, rest@[rest'])  end
            | e1::_ => raise Fail ("Incorrect pos for Image: "^P.printbody e1) 
            (*end case*)) 
        (* getPosAddr:var list->var*IL.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 (avail, args) = case (sizes, args)
            of ([ _ ], [i]) => mkProdInt (avail, [vShapeShift, i])  (*1-d*)
            | ([x, _ ], [i, j]) =>let                  (*2-d*) 
                val (avail, vA) = mkInt (avail, x)
                val (avail, vB) = mkProdInt (avail, [vA, j])
                val (avail, vC) = mkAddInt (avail, [i, vB])
                in mkProdInt (avail, [vShapeShift, vC]) end
            | ([x, y, _], [i, j, k]) =>let                (*3-d*) 
                val (avail, vA) = mkInt (avail, y)
                val (avail, vB) = mkProdInt (avail, [vA, k])
                val (avail, vC) = mkAddInt (avail, [j, vB])
                val (avail, vD) = mkInt (avail, x)
                val (avail, vE) = mkProdInt (avail, [vD, vC])
                val (avail, vF) = mkAddInt (avail, [i, vE])
                in mkProdInt (avail, [vShapeShift, vF]) end

        (* getImgAddr:int list *var->var*IL.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 (avail, imgType, vPosAddr) = case imgType
            of [] => mkAddPtr (avail, [vBase, vPosAddr], ptyTy) 
            | [0] => mkAddPtr (avail, [vBase, vPosAddr], ptyTy) 
            | [_] => let
                val (avail, vA) = mkAddPtr (avail, [vBase, vPosAddr], ptyTy)
                val (avail, vB) = mkInt (avail, asize imgType)
                in mkAddPtr (avail, [vB, vA], ptyTy) end
            | [i, j] => let
                val [a, b] = ImageInfo.voxelShape v
                val (avail, vA) = mkAddPtr (avail, [vBase, vPosAddr], ptyTy)
                val (avail, vB) = mkInt (avail, (b*j) +i)
                in mkAddPtr (avail, [vB, vA], ptyTy)  end


        (* createImgVar:dict->var*IL.assgn list
        *  gets low-il var for loading an image address
        *) 
        fun createImgVar (avail, mapp) = let
            val (avail, vA) = mkpos (avail, px, mapp, [])      (*transforms the probed position to low-il*)
            val posArgs = vPos0::vA                              (*adds intial position to ^*)
            val (avail, vPosAddr) = getPosAddr (avail, posArgs)                 (*position address*)
            val imgType = List.map (fn (e1) => mapIndex (e1, mapp))  v_alpha (*img specific index*)
            val (avail, vImgAddr) = getImgAddr (avail, imgType, vPosAddr)           (*img address*)
            in
                assgn (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*lowil.assgn list*dict*int
        * ->var*lowil.assgn list
        * sumPos iterates over the summation indices and creates mapp
        *) 
        fun sumPos (avail, [], rest, dict, _) = let
            val (avail, rest') = createImgVar (avail, dict)
            in (avail, rest'::rest)  end
        | sumPos (avail, [sid], rest, dict, [r]) = let
            val n' = lb+r
            val mapp = insert (sid, n')  dict
            val (avail, rest') = createImgVar (avail, mapp)
            in (avail, rest'::rest)  end
        | sumPos (avail, [sid], rest, dict, r::es) = let
            val n' = lb+r
            val mapp = insert (sid, n')  dict
            val (avail, 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 (avail, rest') = sumPos (avail, sxx, rest, mapp, range0List)
            in
                sumPos (avail, sid::sxx, rest', dict, es)
            end
        val (avail,ids) = sumPos (avail, sxx, [], mappOrig, range0List)
        in
            (avail, List.rev ids)
        end


end (* local *) 

end

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