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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2522 - (download) (annotate)
Mon Jan 13 18:42:09 2014 UTC (5 years, 9 months ago) by cchiw
File size: 5447 byte(s)
added gentypes to mid-il and split to High-to-mid il
(*hashs Ein Function after substitution*)
structure genKrn = struct
    local
    structure E = Ein
   structure DstOp = LowOps
    structure genHelper=genHelper
    structure evalKrn =evalKrn
structure SrcIL = MidIL
structure SrcOp = MidOps
structure SrcSV = SrcIL.StateVar
structure SrcTy = MidILTypes
structure VTbl = SrcIL.Var.Tbl
structure DstIL = LowIL
structure DstTy = LowILTypes

    in






(*Add,Subtract Scalars*)
fun mkSimpleOp(mapp,e,args)=let

    fun subP e=(case e
        of E.Tensor(t1,ix1)=>genHelper.mkSca(mapp,(t1,ix1,args))
        | E.Value v1=> genHelper.aaV(DstOp.C (List.nth(mapp,v1)),[])
        | E.Const c=> genHelper.aaV(DstOp.C 9,[])
    (*end case*))

    in (case e
        of E.Sub(e1,e2)=> let
            val (vA,A)=subP e1
            val (vB,B)=subP e2
            val (vD,D)=genHelper.aaV(DstOp.subSca,[vA,vB])
            in (vD,A@B@D) end
        | E.Add[e1,e2]=> let
            val (vA,A)=subP e1
            val (vB,B)=subP e2
            val (vD,D)=genHelper.aaV(DstOp.addSca, [vA,vB])
            in (vD,A@B@D) end
        (* ebd case*))
    end


(*con everything on the list, make a vectors*)
fun peanut([],rest, code)=(rest,code)
    | peanut(e::es,rest,code)=let
        val (vA,A)=genHelper.aaV(DstOp.cons,e)
        in peanut(es, [vA]@rest, A@code)
        end


(*sort expression into kernels and images*)
fun sortK(a,b,[])=(a,b)
| sortK(a,b,e::es)=(case e
    of E.Krn k1=>sortK(a,b@[k1],es)
    | E.Img img1=>sortK(a@[img1],b,es)
    (*end case*))



fun printgetRHS x  = (case SrcIL.Var.binding x
    of SrcIL.VB_RHS(SrcIL.OP(SrcOp.Kernel(h, _),_))=> h
    | vb => (raise Fail (String.concat["\n -- ", SrcIL.Var.toString x,"but found ", SrcIL.vbToString vb,"\n"]))
(* end case *))



(*product of images and kernels*)
(*creates vectors of like terms, so does vector products*)
fun  mkkrns(a,(E.Prod e,sx,origargs,args))=let
    val ar=print "in mkkrn"
    val (img1,k1)=sortK([],[],e)
         val ar=print "in A"
    val k= List.map (fn (id,d1,pos)=>(id,genHelper.evalDelta(d1,a),pos)) k1
     val ar=print "B"
    val nk=length(k)
    val g=Int.toString(length(img1))
     val ar=print "C"
    val gg=Int.toString(nk)
       val ggss=Int.toString(length sx)
     val u=print (String.concat["IMG",g, "KRNS", gg,"sumx",ggss])

    fun m([],lft,_,[],mapp,code)= ((lft,code))
        | m([],lft,e::rht,(id1,dels1,pos1)::krns,mapp,code)=let
       
            val (vF,code1)=mkSimpleOp(mapp,pos1,args)
         
            val (vH,H)=genHelper.aaV(DstOp.krn(id1,dels1),[vF])
           
            val e'=[vH]@e
           
            in
            m([],lft@[e'], rht, krns,mapp, code1@H@code)
            end
    | m([(fid,ix,px)],[],e::rht,krns,mapp,code)=let

        fun mkpos([],rest,code)= (rest,code)
            | mkpos(pos1::es,rest,code)= let
                val (vF,code1)=mkSimpleOp(mapp,pos1,args)
                in mkpos(es,rest@[vF],code@code1)
                end
       

        val ix1=List.map (fn (e1)=> genHelper.mapIndex(e1,mapp)) ix
        val (vF,F)= mkpos(px,[],[])
        val (vA,A)=genHelper.aaV(DstOp.img(fid, ix1),vF)
        val e'=[vA]@e
     

        in
            m([],[e'],rht,krns,mapp,F@A@code)
        end
        | m _ =raise Fail "exceptooon"

    val uu=print "post m"

    fun sumI1(left,(0,lb1),[],emp,code)=let
        val mapp=a@left@[lb1]
        val (lft', code')=m(img1,[],emp,k,mapp,[])
        in (lft',code'@code)
        end
    |  sumI1(left,(i,lb1),[],emp,code)=let
        val mapp=a@left@[i+lb1]
        val (lft', code')=m(img1,[],emp,k,mapp,[])
        in sumI1(left,(i-1,lb1),[],lft',code'@code)
        end
    | sumI1(left,(0,lb1),(a,lb2,ub)::sx,emp,code)=
        sumI1(left@[lb1],(ub-lb2,lb2),sx,emp,code)
    | sumI1(left,(s,lb1),(a,lb2,ub)::sx,emp,code)=let
        val (lft',code')=sumI1(left@[s+lb1],(ub-lb2,lb2),sx,emp,code)
        in sumI1(left,(s-1,lb1),(E.V 0,lb2,ub)::sx,lft',code') end
  


    val emp=List.tabulate(nk+1,(fn(e)=>[]))

    val (_,lb,ub)=hd(sx)

    val(lft,code)=sumI1([],(ub-lb,lb),tl(sx),emp,[])
 
    val (rest',code')=peanut(lft,[],[])
    val d=length k
    val (id,_,_)=hd(k)
    val harg=List.nth(origargs,id)
    val h=printgetRHS harg
    fun evalK([],[],n)=[]
    | evalK(kn::kns,x::xs,n)=let
        val (_,dk,_) =kn
        in evalKrn.expandEvalKernel (d, h, dk, x) @ evalK(kns,xs,n+1)
        end
    val mmm=evalK(k,tl(rest'),0)


    val (vD,D)=genHelper.mkMultiple( rest',DstOp.prodVec)
    val (vG,G)=genHelper.aaV(DstOp.sumVec,[vD])

in (vG,code@code'@D@G)
end
    |mkkrns _=raise Fail"Different structure"







(*
fun genfn(Ein.EIN{params, index, body})= let
    val  sx= ref[]
    val A=[DstOp.notDone]
   fun gen body=(case body
        of E.Field _ =>raise Fail(concat["Invalid Field here "]   )
        | E.Partial _ =>raise Fail(concat["Invalid Field here "]   )
        | E.Apply _ =>raise Fail(concat["Invalid Field here "]   )
        | E.Probe _ =>raise Fail(concat["Invalid Field here "]   )
        | E.Conv _ =>raise Fail(concat["Invalid Field here "]   )
        | E.Sum(sx', e)=> (sx:=sx'; gen e)

        (*Images and kernels*)
        | E.Prod(E.Img im::es)=>let
            val ref x=sx
            in prodIter(body,index,x,mkkrns)
            end
        | E.Prod(E.Krn k1::es)=>let
            val ref x=sx
            in prodIter(body,index,x,mkkrns)
            end
        |_ => A 
        (*end case*))
   in gen body end
*)

end (* local *)

end 

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