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 3636 - (download) (annotate)
Sun Jan 31 19:23:34 2016 UTC (3 years, 6 months ago) by cchiw
File size: 5766 byte(s)
added comments
(* vec-to-low-set.sml
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 * COPYRIGHT (c) 2016 The University of Chicago
 * All rights reserved.

* 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

    structure Ty = LowILTypes
    structure Op = LowOps
    structure E = Ein
    structure H = Helper
    structure IMap = IntRedBlackMap


* Each EIN operator has a list of arguments.
* To reduce code size each argument is grouped together with its index binding (ix), low-il variable (var_a),
* argument type(argTy), and operation type (opTy) in a datatype called paramTy.
* ParamTy =(id,a,argTy,ix, opTy).
* The EIN operator λA<A_ijk>_{ijk} (a)
* has a single argument a.
* It's paramTy is (id,a,argTy,ix, opTy) where id=0 and  argTy=Ty.TensorTy[i,j,k]
* The opTy or operation type determines if second order tensor argument A
* will be treated as a scalar or vector arguement in the next operation.
* If opTy=Proj k then then last index is projected to a vector tensor[k] result= A[i,j,:]
* If opTy=Ind then each index in the the Tensor is indexed.  real result= A[i,j,k]
*paramToOp: converts each paramTy into a low-ir argument.
* ParamTy(id,a,argTy,[i,j], opTy) where opTy= Proj(k) creates a vector
*       => Op.ProjectLast (0,k ixx,Ty.TensorTy[i,j,k])
*          A[i,j,:]
* ParamTy (id,a,argTy,[i,j,k],opTy) where opTy= Indx  creates a scalar
*       => Op.IndexTensor (0, ixx, Ty.TensorTy[i,j,k])
*       A[i,j,:]
* Vector operations(Op.AddVec, Op.SubVec, Op.ProdVec) expects 2 vector arguements and
* Vector operations (Op.scaVec) expects 1 scalar and 1 vector argument.

    datatype opTy=  Indx| Proj of int
    datatype paramTy  (*param id, argument var * argument type* ein binding* result type*)
    = Param of int * LowIL.var * Ty.ty * E.mu list * opTy

    fun mapIndex e = H.mapIndex e
    fun getTensorTy e = H.getTensorTy e
    fun assignOP 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 insert  (k, v) d =  IMap.insert (d, k, v)
    fun iToss e = String.concat (List.map Int.toString e)

    (*paramToOp: converts each parameter to low-ir operator*)
    fun paramToOp(avail, mapp, paramTy)= case paramTy
        of Param(_, vA ,_ , [], Proj _) => (avail,vA)
        |  Param(_, vA, Ty.TensorTy[], _, Indx) =>  (avail, vA)
        |  Param(id,vA ,argTy, ix, opType) => let
            (* binds each index in ix using mapp*)
            val ixx = (List.map  (fn  (e1) => mapIndex (e1, mapp))  ix) (*index Ty*)
            val str=String.concat (List.map Int.toString ixx)
            (*create low-ir operator *)
            val (ty,opp,name)= (case opType
                of (Proj vecIX) => let
                    val vecTy = Ty.TensorTy [vecIX]
                    val opp = Op.ProjectLast (id, vecIX, ixx, argTy)
                    val name = String.concat["ProjLast_", str, "_"]
                    in (vecTy,opp,name) end
                | Indx => let
                    val opp = Op.IndexTensor (id, ixx, argTy)
                    val name = String.concat["Indx_",str, "_"]
                    in (Ty.TensorTy[], opp, name) end
                (* end case *))
                assignOP (avail, opp, [vA], name, ty)
    (*--------------------Vectorization Helper Functions--------------------*)
    (*multiplies vector by negative 1*)
    fun negV (avail, mapp, (vA, vecIX, vec)) = let
        val  (avail, vB) =  paramToOp (avail, mapp, vec)
            assignOP (avail, Op.prodScaV vecIX, [vA, vB], "prodScaV", Ty.TensorTy [vecIX])
    (*creates binary vector operation (ex.scaling, subtraction,product,..)*)
    fun op2 (avail, mapp, (vecIX, dstop, vecA,vecB)) = let
        val  (avail, vA) = paramToOp (avail, mapp, vecA)
        val  (avail, vB) = paramToOp (avail, mapp, vecB)
            assignOP (avail, dstop, [vA, vB], "vectorOp", Ty.TensorTy [vecIX])
    (*adds vectors*)
    fun addV (avail, mapp, (vecIX, rest)) = let
        fun add (avail, [], rest) = (avail, List.rev rest)
          | add (avail, vecT ::es, rest) = let
            val  (avail, vT) = paramToOp (avail, mapp, vecT)
                add (avail, es, vT::rest)
        val  (avail, rest) = add (avail, rest, [])
            mkMultiple (avail, rest, Op.addVec vecIX, Ty.TensorTy [vecIX])
    (*dot product*)
    fun dotV (avail, mapp, (vecIX, vecA, vecB)) = let
        val  (avail, vD) = op2 (avail, mapp, (vecIX, Op.prodVec vecIX, vecA, vecB))
            mkSumVec (avail, vecIX, [vD])
    (*Sum of dot Product*)
    fun sumDotV (avail, mapp, ((E.V v, lb, ub), vecIX, vecA,vecB)) = let
        val nextfnargs = (vecIX, vecA, vecB)
        fun sumI (avail, counter, 0, rest) = let
            val mapp = insert (v, 0)  counter
            val  (avail, vE) = dotV (avail, mapp, nextfnargs)
                mkMultiple (avail, vE::rest, Op.addSca, Ty.TensorTy [])
        | sumI (avail, counter, sx, rest) = let
            val mapp = insert (v, (sx+lb))  counter
            val  (avail, vE) = dotV (avail, mapp, nextfnargs)
                sumI (avail, counter, sx-1, vE::rest)
            sumI (avail, mapp, (ub-lb), [])
    | sumDotV _ = raise Fail "Non-variable index in summation"



ViewVC Help
Powered by ViewVC 1.0.0