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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3195 - (download) (annotate)
Wed May 20 21:02:12 2015 UTC (4 years, 2 months ago) by cchiw
File size: 5208 byte(s)
does not use vectors, benchmarks all faster
(*
* helper functions used for vector operations.
* At this point every index is bound to an int
* and we are ready to return low-IL code.
* using LowIL vectors ops like subVec,addVec,prodVec..
*
* EIN->scan->iter->here.
*)
structure VecToLow = struct
    local

    structure DstIL = LowIL
    structure DstTy = LowILTypes
    structure DstOp = LowOps
    structure LowToS= LowToString
    structure Var = LowIL.Var
    structure E = Ein
    structure H=Helper
    
    in

    val testing=0

    fun insert e=H.insert e
    fun assgn e=H.assgn e
    fun indexTensor e=H.indexTensor e
    fun projTensor e=H.projTensor e
    fun projFirst e=H.projFirst e
    fun mkProdVec e =H.mkProdVec e
    fun mkSumVec e =H.mkSumVec e
    fun mkMultiple e=H.mkMultiple e

    val realTy=DstTy.TensorTy []
   (*--------------------Vectorization Helper Functions--------------------*)
    (*negN:dict*string*E.params*Var list * int*Var*E.tensor_id*E.alpha
    * ->Var*LowIL.ASSGN list*)
    fun negV(mapp,(lhs,params,args,vecIX ,vA, id2,ix2))=let
        val (vB,B)= projTensor(mapp,(lhs,params,args,vecIX,id2,ix2))
        val (vC, C)=assgn(DstOp.prodScaV vecIX,[vA, vB],"prodScaV",DstTy.TensorTy [vecIX])
        in
            (vC,B@C)
        end

    (*subN:dict*string*E.params*Var list * int*E.tensor_id*E.alpha*E.tensor_id*E.alpha
    * ->Var*LowIL.ASSGN list
    * Vector Subtraction
    *)
    fun subV(mapp,(lhs,params, args,vecIX,id1,ix1,id2,ix2))=let
        val (vA,A)= projTensor(mapp,(lhs,params,args,vecIX,id1,ix1))
        val (vB,B)= projTensor(mapp,(lhs,params,args,vecIX,id2,ix2))
        val (vC,C)= assgn(DstOp.subVec vecIX ,[vA, vB],"subVec",DstTy.TensorTy [vecIX])
        in
            (vC, A@B@C)
        end

    (*Vector Addition
    *addN:dict*string*E.params*Var list * int*(E.tensor_id*E.alpha)list
    * ->Var*LowIL.ASSGN list
    *)
    fun addV(mapp,(lhs,params, args,vecIX,rest))=let
        fun add([],rest,code)=(rest,code)
          | add((id1,ix1)::es,rest,code)=let
            val (vA,A)= projTensor(mapp,(lhs,params,args,vecIX,id1,ix1))
            in
                add(es,rest@[vA],code@A)
            end
        val (rest,code)=add(rest,[],[])
        val (vA,A)=mkMultiple(lhs,rest,DstOp.addVec vecIX,DstTy.TensorTy([vecIX]))
        in
            (vA,code@A)
        end

    (*scaleNN:dict*string*E.params*Var list * int*E.tensor_id*E.alpha*E.tensor_id*E.alpha)
    * ->Var*LowIL.ASSGN list
    *Vector Scaling
    *)
    fun scaleV(mapp,(lhs,params , args,vecIX,id1,ix1,id2,ix2))=let
        val (vA,A)= indexTensor(mapp,(lhs,params,args,id1,ix1,realTy))
        val (vB,B)= projTensor(mapp,(lhs,params,args,vecIX,id2,ix2))
        val (vC,C)= assgn(DstOp.prodScaV vecIX,[vA, vB],"prodScaV",DstTy.TensorTy([vecIX]))
        in
            (vC,A@B@C)
        end

    (*prodN:dict*string*E.params*Var list * int*E.tensor_id*E.alpha*E.tensor_id*E.alpha)
    * ->Var*LowIL.ASSGN list
    *Vector Product
    *)
    fun prodV(mapp,(lhs,params,args,vecIX,id1,ix1,id2,ix2))= let
        val (vA,A)= projTensor(mapp,(lhs,params,args,vecIX,id1,ix1))
        val (vB, B)= projTensor(mapp,(lhs,params,args,vecIX,id2,ix2))
        val (vC, C)=mkProdVec(lhs,vecIX,[vA, vB])
        in
        (vC, A@B@C)
        end

    (*dotN:dict*string*E.params*Var list * int*E.tensor_id*E.alpha*E.tensor_id*E.alpha)
    * ->Var*LowIL.ASSGN list
    *dot product
    *)
    fun dotV(mapp,(lhs,params, args,vecIX,id1,ix1,id2,ix2))=let
        val (vD,D)= prodV(mapp,(lhs,params,args,vecIX,id1,ix1,id2,ix2))
        val (vE, E)= mkSumVec(lhs,vecIX,[vD])
        in
            (vE, D @E)
        end

    fun VM(mapp,(lhs,params, args,vecIX,id1,ix1,id2,ix2))=let
         val vA= List.nth(args,id1)
        val (vB, B)= projTensor(mapp,(lhs,params,args,vecIX,id2,ix2))
        val (vD,D)= mkProdVec(lhs,vecIX,[vA, vB])
        val (vE, E)= mkSumVec(lhs,vecIX,[vD])
        in
            (vE, B@D@E)
        end

    fun MM3(mapp,(lhs,params,args, vecIX,id1,ix1,id2,ix2,_))= let
        val (vA,A)= projTensor(mapp,(lhs,params,args,vecIX,id1,ix1))
        val (vB,B)= projFirst(mapp,(lhs,params,args,vecIX,id2,ix2))
  
        val (vD,D)= mkProdVec(lhs,vecIX,[vA, vB])
        val (vE, E)= mkSumVec(lhs,vecIX,[vD])
        in
              (vE,A@B@D@E)
        end



    (*sumDotN:dict*string*E.params*Var list * (E.nu)*int*E.tensor_id*E.alpha*E.tensor_id*E.alpha)
    * ->Var*LowIL.ASSGN list
    *Sum of dot Product
    *)
    fun sumDotV(mapp,(lhs,params, args,(E.V v,lb,ub),ub1,id1,ix1,id2,ix2))=let
        val nextfnargs=(lhs,params, args,ub1,id1,ix1,id2,ix2)
        fun sumI(a,0,rest,code)=let
            val mapp =insert(v, 0) a
            val (vE, E)=dotV(mapp,nextfnargs)
            val rest'=[vE]@rest
            val (vF, F)=mkMultiple(lhs,rest',DstOp.addSca,realTy)
            in
                (vF,E@code@F)
            end
          | sumI(a,sx,rest',code')=let
            val mapp =insert(v, (sx+lb)) a
            val (vE, E)=dotV(mapp,nextfnargs)
            in
                sumI(a,sx-1,[vE]@rest',E@code')
            end
        in
            sumI(mapp, (ub-lb), [],[])
        end
      | sumDotV _= raise Fail "Non-variable index in summation"

    end


end

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