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.sml
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3174 - (download) (annotate)
Mon Mar 30 11:46:58 2015 UTC (3 years, 11 months ago) by cchiw
File size: 4969 byte(s)
hack
(* 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 DstIL = LowIL
    structure DstOp = LowOps
    structure LowToS= LowToString
    structure Var = LowIL.Var
    structure E = Ein
    structure P=Printer
    structure evalImg=EvalImg
    structure evalKrn =EvalKrn
    structure H=Helper

        in
    
    val testing=0
    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 iTos n=Int.toString n
    fun err str=raise Fail(str)
    fun testp n =(case testing
        of 0 => 1
        | _  => (print(String.concat n);1)
        (*end case *))

    (**** 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,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,ks@[k1])
        | E.Img img1=>sortK(es,imgs@[img1],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 DstIL.Var.binding x
            of DstIL.VB_RHS(DstIL.OP(DstOp.LoadImage(_, _, img), _ )) => img
            | vb => (err (String.concat["\n -- Not an image, ", DstIL.Var.toString x," found ", DstIL.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 DstIL.Var.binding harg
            of DstIL.VB_RHS(DstIL.OP(DstOp.Kernel(h, _ ) ,_ ))=> h
            | vb => (err (String.concat["\n -- Not a kernel, ", DstIL.Var.toString harg," found ", DstIL.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(imgArg,krnArg,range0,range1)=let

        fun ConsInt(pre,args)= assgnCons("",[range1], args)

        fun mul2d([],rest,code,hy)=   let
            val (vD,D)=ConsInt("",rest)
            in (vD,code@D) end
          | mul2d(e::es,rest,code,hy)=let
            val (vA,A)=mkDotVec("",range1,[e,hy])
            in mul2d(es,rest@[vA],code@A,hy)end

        fun mul3d([] , _ ,code,_,rest,hz)=(rest,code)
          | mul3d(e1::es,rest,code,0,consrest,hz)=let
            val (vA,A)=mkDotVec("",range1,[hz,e1])
            val (vD,D)=ConsInt ("",rest@[vA])
            in
                mul3d(es,[],code@A@D,range0,consrest@[vD],hz)
            end
        | mul3d(e1::es,rest,code,n,consrest,hz)=let
            val (vA,A)=mkDotVec("",range1,[hz,e1])
            in
                mul3d(es,rest@[vA],code@A,n-1,consrest,hz)
            end

    (*Create Product by doing case analysis of the dimension*)
    in (case (krnArg,imgArg)
        of ([h0],[i])=> mkDotVec("VH_",range1,[i,h0]) (*1-D case*)
        | ([h0,h1],_)=>let
            val (vA,A)=mul2d(imgArg,[],[],h0)
            val (vB,B)=mkDotVec("VH_",range1,[vA,h1])
            in
                (vB,A@B)
            end
        |  ([h0,h1,h2],_)=>let
            val (restZ,codeZ)=mul3d(imgArg,[],[],range0,[],h0)
            val (restY,codeY)=mul2d(restZ,[],[],h1)
            val (vA,A)=mkDotVec("VH_",range1,[h2,restY])
            in
                (vA,codeZ@codeY@A)
            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(mapp,(E.Sum(sx,E.Prod p),(lhs,e,args)))=let

        val _= testp ["\n************** new direction **********"]

        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 (imgArg,imgCode)= mkImg(mapp,lhs,params,args,sx,img1,v,imgarg,lb,range0,range1)
        val (krnArg, krnCode)= mkkrns(mapp,lhs,params,args,k1,h,sid,lb,range0,range1)
        val (vA,A)=prodImgKrn(imgArg,krnArg,range0,range1)
        val _=testp["Number of Assignments  in prodImgArg returned",iTos(length(imgArg))]
        in
            (vA,imgCode@krnCode@A)
        end


end (* local *)

end

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