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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3553 - (download) (annotate)
Thu Jan 7 17:39:31 2016 UTC (3 years, 7 months ago) by cchiw
File size: 8967 byte(s)
spacing
 (*
* 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 VecToLowSet = struct
    local

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

    fun insert e = H.insert e
    fun assgn e = H.assignOP e
    fun indexTensor e = H.indexTensor e
    fun mkProdVec e = H.mkProdVec e
    fun mkSumVec e = H.mkSumVec e
    fun mkMultiple e = H.mkMultiple e
    fun getTensorTy e = H.getTensorTy e
    fun mapIndex e = H.mapIndex e
    fun assignOP e = H.assignOP e
    fun assgnCons e = H.assgnCons e
    fun iToss e = H.iToss e
    val realTy = DstTy.TensorTy []
    val testing = false
    fun testp n = if  (testing)  then  (print (String.concat n) ;1)  else 1

     (*projTensor:dict* (string* E.params*Var list*int*E.tensor_id*E.alpha) ->Var*code list
    * projects tensor to a vector
    *just used by EintoVecOps but made sense to keep it here
    *) 
    fun projTensor (setA , _ , (_ , _ , args , _ , id , [])) = (setA , List.nth (args , id) , []) 
    | projTensor (setA , mapp , (lhs , params , args , vecIX , id , ix)) = let
        val nU = List.nth (args , id) 
        val ixx = (List.map  (fn  (e1) => mapIndex (e1 , mapp))  ix) 
        val ix' = ixx  (*index Ty*) 
        val argTy = getTensorTy (params , id) 
        val vecTy = DstTy.TensorTy [vecIX]
        val opp = DstOp.ProjectLast (id , vecIX , ix' , argTy) 
        val name = String.concat["ProjLast_" , iToss ixx , "_"]
        val opset = lowSet.LowSet.empty
        val  (setB , vB , B) = assignOP (setA , opp , [nU] , name , vecTy) 
        in
             (setB , vB , B) 
        end

    fun projFirst (setA , mapp , (lhs , params , args , vecIX , id , ix)) = let
        val nU = List.nth (args , id) 
        val argTy = getTensorTy (params , id) 
        val vecTy = DstTy.TensorTy [vecIX]
        val ixx = (List.map  (fn  (e1) => mapIndex (e1 , mapp))  ix) 
        fun f cnt = let
            val ix' = cnt::ixx
            val opp = DstOp.IndexTensor (id , ix' , argTy) 
            val name = String.concat["IndexTensor_" , iToss ixx , "_"]
            in
                assignOP (setA , opp , [nU] , name , realTy) 
            end
        fun iter  ([] , vCs , Cs) = (vCs , Cs) 
          | iter ( (_ , vB , B) ::es , vCs , Cs) = iter (es , vCs@[vB] , Cs@B) 
        val ops = List.tabulate ( vecIX , fn e => f e) 
        val  (vCs , Cs) = iter (ops , [] , []) 
        val  (setD , vD , D) = assgnCons (setA , "projFirstCons" , [vecIX] , vCs) 
        in
             (setD , vD , Cs@D) 
        end

    fun projFirst2 (setA , mapp , (lhs , params , args , opset , vecIX , id , ix)) = let
        val nU = List.nth (args , id) 
        val argTy = getTensorTy (params , id) 
        val vecTy = DstTy.TensorTy [vecIX]
        val ixx = (List.map  (fn  (e1) => mapIndex (e1 , mapp))  ix) 
        val ix' = (*DstTy.index Ty*)  ixx
        val opp = DstOp.ProjectFirst (id , vecIX , ix' , argTy) 
        val name = String.concat["ProjFirst_" , iToss ixx , "_"]
        in
            assignOP (setA , opp , [nU] , name , vecTy) 
        end


    (*--------------------Vectorization Helper Functions--------------------*) 
     (*negN:dict*string*E.params*Var list * int*Var*E.tensor_id*E.alpha
    * ->Var*LowIL.ASSGN list*) 
    fun negV (setT , mapp , (lhs , params , args , vecIX , vA , id2 , ix2)) = let
        val  (setB , vB , B) = projTensor (setT , mapp , (lhs , params , args , vecIX , id2 , ix2)) 
        val  (setC , vC , C) = assgn (setB , DstOp.prodScaV vecIX , [vA , vB] , "prodScaV" , DstTy.TensorTy [vecIX]) 
        in
             (setC , 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 (setT , mapp , (lhs , params , args , vecIX , id1 , ix1 , id2 , ix2)) = let
        val  (setA , vA , A) = projTensor (setT , mapp , (lhs , params , args , vecIX , id1 , ix1)) 
        val  (setB , vB , B) = projTensor (setA , mapp , (lhs , params , args , vecIX , id2 , ix2)) 
        val  (setC , vC , C) = assgn (setB , DstOp.subVec vecIX , [vA , vB] , "subVec" , DstTy.TensorTy [vecIX]) 
        in
             (setC , 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 (setT , mapp , (lhs , params , args , vecIX , rest)) = let
        fun add (setB , [] , rest , code) = (setB , rest , code) 
          | add (setB , (id1 , ix1) ::es , rest , code) = let
            val  (setC , vC , C) = projTensor (setB , mapp , (lhs , params , args , vecIX , id1 , ix1)) 
            in
                add (setC , es , rest@[vC] , code@C) 
            end
        val  (setB , rest , code) = add (setT , rest , [] , []) 
        val  (setC , vC , C) = mkMultiple (setB , rest , DstOp.addVec vecIX , DstTy.TensorTy ([vecIX])) 
        in
             (setC , vC , code@C) 
        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 (setT , mapp , (lhs , params , args , vecIX , id1 , ix1 , id2 , ix2)) = let
        val  (setA , vA , A) = indexTensor (setT , mapp , (lhs , params , args , id1 , ix1 , realTy)) 
        val  (setB , vB , B) = projTensor (setA , mapp , (lhs , params , args , vecIX , id2 , ix2)) 
        val  (setC , vC , C) = assgn (setB , DstOp.prodScaV vecIX , [vA , vB] , "prodScaV" , DstTy.TensorTy ([vecIX])) 
        in
             (setC , 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 (setT , mapp , (lhs , params , args , vecIX , id1 , ix1 , id2 , ix2)) = let
        val  (setA , vA , A) = projTensor (setT , mapp , (lhs , params , args , vecIX , id1 , ix1)) 
        val  (setB , vB , B) = projTensor (setA , mapp , (lhs , params , args , vecIX , id2 , ix2)) 
        val  (setC , vC , C) = mkProdVec (setB , vecIX , [vA , vB]) 
        in
             (setC , 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 (setT , mapp , (lhs , params , args , vecIX , id1 , ix1 , id2 , ix2)) = let
        val  (setD , vD , D) = prodV (setT , mapp , (lhs , params , args , vecIX , id1 , ix1 , id2 , ix2)) 
        val  (setE , vE , E) = mkSumVec (setD , vecIX , [vD]) 
        in
             (setE , vE , D @E) 
        end

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

    fun MM3 (setT , mapp , (lhs , params , args , vecIX , id1 , ix1 , id2 , ix2 , _)) = let
        val  (setA , vA , A) = projTensor (setT , mapp , (lhs , params , args , vecIX , id1 , ix1)) 
        val  (setB , vB , B) = projFirst (setA , mapp , (lhs , params , args , vecIX , id2 , ix2)) 
        val  (setD , vD , D) = mkProdVec (setB , vecIX , [vA , vB]) 
        val  (setE , vE , E) = mkSumVec (setD , vecIX , [vD]) 
        in
               (setE , 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 (setT , 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 (setA , a , 0 , rest , code) = let
            val mapp = insert (v , 0)  a
            val  (setE , vE , E) = dotV (setA , mapp , nextfnargs) 
            val rest' = [vE]@rest
            val  (setF , vF , F) = mkMultiple (setE , rest' , DstOp.addSca , realTy) 
            in
                 (setF , vF , E@code@F) 
            end
          | sumI (setA , a , sx , rest' , code') = let
            val mapp = insert (v , (sx+lb))  a
            val  (setE , vE , E) = dotV (setA , mapp , nextfnargs) 
            in
                sumI (setE , a , sx-1 , [vE]@rest' , E@code') 
            end
        in
            sumI (setT , 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