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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 3395 - (download) (annotate)
Tue Nov 10 18:23:07 2015 UTC (3 years, 8 months ago) by cchiw
File size: 5565 byte(s)
val-num in evalkrn
(* 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 FieldToLowSet = struct
    structure DstIL = LowIL
    structure DstOp = LowOps
    structure LowToS= LowToString
    structure Var = LowIL.Var
    structure E = Ein
    structure P=Printer
    structure evalImg=EvalImgSet
    structure evalKrn =EvalKrnSet
    structure H=HelperSet

    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 *))

    (*prodImgKrn:Var list* Var list * int*int->Var*LowIL.assgn list 
    * Product of Image and Kernel
    fun prodImgKrn(setP,imgArg,krnArg,range0,range1)=let

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

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

        fun mul3d(setT,[] , _ ,code,_,rest,hz)=(setT,rest,code)
          | mul3d(setT,e1::es,rest,code,0,consrest,hz)=let
            val (setA,vA,A)=mkDotVec(setT,range1,[hz,e1])
            val (setD,vD,D)=ConsInt (setA,rest@[vA])
        | mul3d(setT,e1::es,rest,code,n,consrest,hz)=let
            val (setA,vA,A)=mkDotVec(setT,range1,[hz,e1])

    (*Create Product by doing case analysis of the dimension*)
    in (case (krnArg,imgArg)
        of ([h0],[i])=> mkDotVec(setP,range1,[i,h0]) (*1-D case*)
        | ([h0,h1],_)=>let
            val (setA,vA,A)=mul2d(setP,imgArg,[],[],h0)
            val (setB,vB,B)=mkDotVec(setA,range1,[vA,h1])
        |  ([h0,h1,h2],_)=>let
            val (setZ,restZ,codeZ)=mul3d(setP,imgArg,[],[],range0,[],h0)
            val (setY,restY,codeY)=mul2d(setZ,restZ,[],[],h1)
            val (setA,vA,A)=mkDotVec(setY,range1,[h2,restY])
        | _ => raise Fail "Kernel dimensions not between 1-3"
        (*end case*))

    (*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(setG,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 (setG,imgArg,imgCode)= mkImg(setG,mapp,lhs,params,args, sx,img1,v,imgarg,lb,range0,range1)
        val (setK,krnArg, krnCode)= mkkrns(setI,mapp,lhs,params,args, k1,h,sid,lb,range0,range1)
        val (setA,vA,A)=prodImgKrn(setK,imgArg,krnArg,range0,range1)
val (setG,imgArg,imgCode)= mkImg(setG,mapp,lhs,params,args, sx,img1,v,imgarg,lb,range0,range1)
val (setG,krnArg, krnCode)= mkkrns(setG,mapp,lhs,params,args, k1,h,sid,lb,range0,range1)
val (setA,vA,A)=prodImgKrn(setG,imgArg,krnArg,range0,range1)
val _=(String.concat["\n imgcode:",Int.toString(length(imgCode))," krncode: ",Int.toString(length(krnCode))," progimgk: ",Int.toString(length(A))])

        val _=testp["Number of Assignments  in prodImgArg returned",iTos(length(imgArg))]

end (* local *)


ViewVC Help
Powered by ViewVC 1.0.0