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/field-to-low-set.sml
ViewVC logotype

View of /branches/charisee/src/compiler/mid-to-low/field-to-low-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: 5030 byte(s)
adding header comments in prep for merge
(* field-to-low-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.
 *)

 (* Converts Ein expression thats uses fields to Low-IL ops.
* Calls evalImg to evaluate Image expression
*Calls evalKrn to evaluate Kernel expressions
*Then multiplies the Image and kernel evalutes
*) 
structure FieldToLow = struct
    local
  
    structure IL = LowIL
    structure Op = LowOps
    structure Var = LowIL.Var
    structure E = Ein
    structure P = Printer
    structure evalImg = EvalImg
    structure evalKrn = EvalKrn
    structure H = Helper

    in

    fun mkImg e = evalImg.mkImg e
    fun mkkrns e = evalKrn.mkkrns e
    fun assgnCons e = H.assgnCons e
    fun mkDotVec e = H.mkDotVec e
    fun err str = raise Fail (str) 

     (**** Helpers used in main evaluation ****) 
     (* sortK:E.ein_exp list* (E.image_id*E.alpha*E.pos list)  list* (E.kernel_id*  (E.mu*E.mu)  list * E.pos)  list
    * sort expression into kernels and images
    *) 
    fun sortK ([], [imgs], ks) = (imgs, List.rev ks)
      | sortK ([], _, _) = raise Fail"More than one image in Summation expression"
      | sortK (e::es, imgs, ks) = (case e
        of E.Krn k1 => sortK (es, imgs, k1::ks)
        | E.Img img1 => sortK (es, img1::imgs, ks)
        | _ => raise Fail"Non Image or Krn in summation expression"
         (*end case*)) 

     (*getArgs:Var list* (E.image_id*E.alpha*E.pos list) * (E.kernel_id*  (E.mu*E.mu)  list * E.pos) 
    * ->var*image*kernel
    * get image variable, image.info, and kernel
    *) 
    fun getArgs  (args, (Vid, _, _), (Hid, _, _)) = let
         (*Get Image bindings*) 
        fun getImageSrcD x  = (case IL.Var.binding x
            of IL.VB_RHS (IL.OP (Op.LoadImage (_, _, img), _ ))  =>  img
            | vb =>   (err  (String.concat["\n -- Not an image, ", IL.Var.toString x, " found ", IL.vbToString vb, "\n"])) 
             (* end case *)) 
        val imgarg = List.nth (args, Vid) 
        val v = getImageSrcD imgarg
         (*Get kernel  bindings*) 
        val harg = List.nth (args, Hid) 
        val h  = (case IL.Var.binding harg
            of IL.VB_RHS (IL.OP (Op.Kernel (h, _ ), _ )) =>  h
            | vb =>   (err  (String.concat["\n -- Not a kernel, ", IL.Var.toString harg, " found ", IL.vbToString vb, "\n"])) 
             (* end case *)) 
        in
             (imgarg, v, h) 
        end

     (*prodImgKrn:Var list* Var list * int*int->Var*LowIL.assgn list 
    * Product of Image and Kernel
    *) 
    fun prodImgKrn (avail, imgArg, krnArg, range0, range1) = let
        fun ConsInt (avail, args) = assgnCons (avail, "", [range1], args) 
        fun mul2d (avail, [], rest, hy) = ConsInt (avail, List.rev rest)
          | mul2d (avail, e::es, rest, hy) = let
            val  (avail, vA) = mkDotVec (avail, range1, [e, hy])
            in mul2d (avail, es, vA::rest, hy) end

        fun mul3d (avail, [], _ , _, rest, hz) = (avail, rest)
          | mul3d (avail, e1::es, rest, 0, consrest, hz) = let
            val  (avail, vA) = mkDotVec (avail, range1, [hz, e1])
            val  (avail, vD) = ConsInt  (avail, rest@[vA])
            in
                mul3d (avail, es, [], range0, consrest@[vD], hz)
            end
        | mul3d (avail, e1::es, rest, n, consrest, hz) = let
            val  (avail, vA) = mkDotVec (avail, range1, [hz, e1])
            in
                mul3d (avail, es, rest@[vA], n-1, consrest, hz)
            end
     (*Create Product by doing case analysis of the dimension*) 
    in  (case  (krnArg, imgArg) 
        of  ([h0], [i]) =>  mkDotVec (avail, range1, [i, h0])   (*1-D case*) 
        |  ([h0, h1], _) => let
            val  (avail, vA) = mul2d (avail, imgArg, [], h0)
            in
                mkDotVec (avail, range1, [vA, h1])
            end
        |   ([h0, h1, h2], _) => let
            val  (avail, restZ ) = mul3d (avail, imgArg, [], range0, [], h0)
            val  (avail, restY) = mul2d (avail, restZ, [], h1)
            in
                mkDotVec (avail, range1, [h2, restY])
            end
        | _ =>  raise Fail "Kernel dimensions not between 1-3"
         (*end case*)) 
    end

     (*evalField:dict* (E.body* (string*E.Ein*Var list)) ->Var*LowIL.assgn list
    *Main function to evaluate image and fields and multiple the results
    *) 
    fun evalField (avail, mapp, (E.Sum (sx, E.Opn (E.Prod, p)), (e, args))) = let
        val params = E.params e
        val  (img1, k1) = sortK (p, [], []) 
        val  (imgarg, v, h) = getArgs (args, img1, List.hd (k1)) 
        val  (E.V sid, lb, ub) = hd (sx) 
        val range0 = (ub-lb) 
        val range1 = range0+1
        val  (avail, imgArg) = mkImg (avail, mapp,  params, args, sx, img1, v, imgarg, lb, range0, range1)
        val  (avail, krnArg) = mkkrns (avail, mapp, params, args, k1, h, sid, lb, range0, range1)
        in
            prodImgKrn (avail, imgArg, krnArg, range0, range1)
        end


end  (* local *) 

end

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