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 3624 - (download) (annotate)
Fri Jan 29 17:49:01 2016 UTC (3 years, 11 months ago) by jhr
File size: 4200 byte(s)
adding header comments in prep for merge
(* 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
    local

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

    in

    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)

    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
            val ixx = (List.map  (fn  (e1) => mapIndex (e1, mapp))  ix) (*index Ty*)
            val str=String.concat (List.map Int.toString ixx)
            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 *))
            in
                assignOP (avail, opp, [vA], name, ty)
            end
    (*--------------------Vectorization Helper Functions--------------------*)
    fun negV (avail, mapp, (vA, vecIX, vec)) = let
        val  (avail, vB) =  paramToOp (avail, mapp, vec)
        in
            assignOP (avail, Op.prodScaV vecIX, [vA, vB], "prodScaV", Ty.TensorTy [vecIX])
        end
    fun op2 (avail, mapp, (vecIX, dstop, vecA,vecB)) = let
        val  (avail, vA) = paramToOp (avail, mapp, vecA)
        val  (avail, vB) = paramToOp (avail, mapp, vecB)
        in
            assignOP (avail, dstop, [vA, vB], "vectorOp", Ty.TensorTy [vecIX])
        end
    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)
            in
                add (avail, es, vT::rest)
            end
        val  (avail, rest) = add (avail, rest, [])
        in
            mkMultiple (avail, rest, Op.addVec vecIX, Ty.TensorTy [vecIX])
        end
    (*dot product*)
    fun dotV (avail, mapp, (vecIX, vecA, vecB)) = let
        val  (avail, vD) = op2 (avail, mapp, (vecIX, Op.prodVec vecIX, vecA, vecB))
        in
            mkSumVec (avail, vecIX, [vD])
        end
    (*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)
            in
                mkMultiple (avail, vE::rest, Op.addSca, Ty.TensorTy [])
            end
        | sumI (avail, counter, sx, rest) = let
            val mapp = insert (v, (sx+lb))  counter
            val  (avail, vE) = dotV (avail, mapp, nextfnargs)
            in
                sumI (avail, counter, sx-1, vE::rest)
            end
        in
            sumI (avail, 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