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/ein-to-vector.sml
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3685 - (download) (annotate)
Wed Feb 24 22:17:45 2016 UTC (4 years, 1 month ago) by jhr
File size: 3021 byte(s)
working on merge
(* ein-to-vector.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 EinToVector : sig

  (* an environment that maps De Bruijn indices to their iteration-index value *)
    type index_env = int IntRedBlackMap.map

    datatype param_kind = Indx | Proj of Ein.index_id

    datatype param = Param of {
	id : int,		(* the parameter's DeBruijn index *)
	arg : LowIR.var,	(* the corresponding argument *)
	ix : Ein.alpha,		(* the multi-index used to subscript the argument *)
	kind : param_kind	(* are we extracting a scalar (Indx) or projecting out
				 * a vector from the last dimension (Proj)?
				 *)
     }

  (* vector negation *)
    val negV : AvailRHS.t * index_env * param -> LowIR.var

  (* vector sum of a sequence of vectors *)
    val addV : AvailRHS.t * index_env * param * param list -> LowIR.var

  end = struct

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

  (* an environment that maps De Bruijn indices to their iteration-index value *)
    type index_env = int IMap.map

    datatype param_kind = Indx | Proj of Ein.index_id

    datatype param = Param of {
	id : int,		(* the parameter's DeBruijn index *)
	arg : IR.var,		(* the corresponding argument *)
	ix : E.alpha,		(* ?? *)
	kind : param_kind
      }

    fun alphaToIdxs (mapp, mus) = let
	  fun lookup (E.V id) = (case IMap.find (mapp, id)
		 of SOME n => n
		  | NONE => raise Fail(concat["alphaToIdxs(_, V ", Int.toString id, "): out of bounds"])
		(* end case *))
	    | lookup (E.C i) = i
	  in
	    List.map lookup mus
	  end

  (* convert a parameter to its LowIL variable equivalent *)
    fun paramToVar (_, _, Param{arg, ix = [], ...}) = arg
      | paramToVar (avail, mapp, Param{id, arg, ix, kind}) = (
	  case (kind, LowIR.Var.ty arg)
	   of (Indx, ty) => AvailRHS.addAssign (
		avail, "indx", Ty.realTy,
		IR.OP(Op.TensorIndex(ty, alphaToIdxs (mapp, ix)), [arg]))
	    | (Proj dim, ty) => AvailRHS.addAssign (
		avail, "proj", Ty.TensorTy[dim],
		IR.OP(Op.ProjectLast(ty, alphaToIdxs (mapp, ix)), [arg]))
	  (* end case *))

  (* vector negation *)
    fun negV (avail, mapp, vec) = let
	  val v = paramToVar (avail, mapp, vec)
	  val ty as Ty.TensorTy[dim] = IR.Var.ty v
	  in
	    AvailRHS.addAssign (avail, "vneg", ty, IR.OP(Op.VNeg dim, [v]))
	  end

  (* vector addition *)
    fun addV (avail, mapp, u, vs) = let
	val u = paramToVar (avail, mapp, u)
	val vs = List.map (fn v => paramToVar (avail, mapp, v)) vs
	val Ty.TensorTy[dim] = IR.Var.ty u
	in
	  Mk.reduce (avail, fn (avail, u, v) => Mk.vecAdd(avail, dim, u, v), u::vs)
	end

  end

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