Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/vis15/src/compiler/mid-to-low/field-to-low.sml
ViewVC logotype

View of /branches/vis15/src/compiler/mid-to-low/field-to-low.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3744 - (download) (annotate)
Mon Apr 11 22:06:24 2016 UTC (3 years, 5 months ago) by cchiw
File size: 4222 byte(s)
push support to ein expression and added signature
(* field-to-low.sml
 *
 * NOTE: this code will need to be changed if we ever want to support different kernels
 * for different axes
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2016 The University of Chicago
 * All rights reserved.
 *)

structure FieldToLow : sig

  (* expand a MidIR probe to LowIR code.  The arguments are:
   *
   *    avail   -- available LowIR assignments
   *    mapp    -- mapping from iteration indices to deBruijn indices
   *    sx      -- summation bounds
   *    prod    -- body of summation, which is a product of Ein expressions
   *    lowArgs -- the actual arguments of the enclosing Ein expression
   *)
    val expand : {
	    avail : AvailRHS.t,
	    mapp : int IntRedBlackMap.map,
	    sx : Ein.sumrange list,
	    prod : Ein.ein_exp list,
	    lowArgs : LowIR.var list
	  }  -> LowIR.var

  end = struct

    structure IR = LowIR
    structure Ty = LowTypes
    structure Op = LowOps
    structure Var = LowIR.Var
    structure E = Ein
    structure Mk = MkLowIR
    structure IMap = IntRedBlackMap

    (*Index cons at piece*)
    fun getHolder(args, id, piece) = let
        val t = List.nth(args, id)
        in (case IR.Var.binding (t)
            of IR.VB_RHS(IR.CONS(eargs, _)) =>
                if (length(eargs)>piece) then List.nth(eargs, piece)
                else raise Fail "Holder error"
            | IR.VB_RHS(IR.OP(_, _)) => t
            | vb
                => raise Fail(String.concat["\ngetHolder found ",Var.name(t),"=", IR.vbToString vb, " at ", Int.toString(id)])
        (* end case *))
        end

    (* Product of Image and Kernel *)
    fun prodImgKrn (avail, imgArg, krnArg, s) = let
        (* Number of arguments for Cons *)
        val range1 = 2*s
        val range0 = range1-1
         fun ConsInt args = let
            val ty = Ty.TensorTy [range1]
            val rhs = IR.CONS (args, ty)
            in
                 AvailRHS.addAssign (avail,"cons"^"_", ty, rhs)
            end

        fun mkDotVec (a,b) = Mk.vecDot(avail, range1, a, b)
        fun mul2d ([], rest, hy) = ConsInt (List.rev rest)
          | mul2d (e::es, rest, hy) = let
            val vA = mkDotVec (e, hy)
            in
                mul2d (es, vA::rest, hy)
            end
        fun mul3d ([], _ , _, rest, hz) = rest
          | mul3d (e1::es, rest, 0, consrest, hz) = let
            val vA = mkDotVec (hz, e1)
            val vD = ConsInt (rest@[vA])
            in
                mul3d (es, [], range0, consrest@[vD], hz)
            end
        | mul3d (e1::es, rest, n, consrest, hz) = let
            val vA = mkDotVec (hz, e1)
            in
                mul3d (es, rest@[vA], n-1, consrest, hz)
            end
        (*Create Product by doing case analysis of the dimension*)
        in  (case  (krnArg, imgArg)
            of  ([h0], [i]) =>  mkDotVec (i, h0)   (*1-D case*)
            |  ([h0, h1], _) => let
                val vA = mul2d (imgArg, [], h0)
                in
                    mkDotVec (vA, h1)
            end
            |   ([h0, h1, h2], _) => let
                val restZ = mul3d (imgArg, [], range0, [], h0)
                val restY = mul2d (restZ, [], h1)
                in
                    mkDotVec (h2, restY)
                end
        | _ =>  raise Fail "Kernel dimensions not between 1-3"
        (*end case*))
        end


  (* expand a MidIR probe to LowIR code. *)
    fun expand {avail, mapp, sx, prod as E.Img e1::krnexps, lowArgs} = let
        (* evaluate image expression *)
        val imgArgs = EvalImg.expand{avail = avail, mapp = mapp,  sx = sx, imgexp = E.Img e1, args = lowArgs}
        (* get piece for each kernel *)
        fun getf(E.Krn(id, dels, _)) =
            let
                (* evaluate dels to integer *)
                val delta = List.foldl(fn((i, j), y) => Mk.evalDelta(mapp, i, j) + y) 0  dels
            in
                getHolder(lowArgs, id, delta)  (* selects variable in holder/cons list *)
            end
        (* evaluate kernel expression(s) *)
        val krnArgs = List.map getf krnexps  (* doesn't create code *)
	  in
        prodImgKrn (avail, imgArgs, krnArgs, #4 e1)
	  end

  end

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