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

SCM Repository

[diderot] View of /branches/vis15/src/compiler/high-to-mid/coord-space-transform.sml
ViewVC logotype

View of /branches/vis15/src/compiler/high-to-mid/coord-space-transform.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3732 - (download) (annotate)
Thu Apr 7 20:56:16 2016 UTC (4 years, 1 month ago) by jhr
File size: 4362 byte(s)
  Working on merge (rewrite probe expansion to use BuildPos and EvalKernel).
(* 
 *This file does world to image space Transformations
 * Take a look at tex file for examples
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2015 The University of Chicago
 * All rights reserved.
 *)



structure CoordSpaceTransform : sig

  (* `worldToImage {info, img, pos}` returns (M, x, stms), where M is a variable
   * bound to the world-space to image-space linear transformation matrix, x is
   * a variable bound to the transformed position (pos), and stms is a list of
   * assignments that computes the world-space to -image-space transformation.
   *)
    val worldToImage : {
	    avail : AvailRHS.avail,
	    info : ImageInfo.info,	(* the image info for the image *)
	    img : MidIR.var,		(* the variable bound to the image *)
	    pos : MidIR.var		(* the variable bound to the world-space position *)
	  } -> MidIR.var * MidIR.var

    val worldToIndex : {
	    avail : AvailRHS.avail,
	    info : ImageInfo.info,	(* the image info for the image *)
	    img : MidIR.var,		(* the variable bound to the image *)
	    pos : MidIR.var		(* the variable bound to the world-space position *)
	  } -> MidIR.var * MidIR.var * MidIR.var

    val imageToWorld : Ein.tensor_id * int * Ein.mu list * Ein.tensor_id
	  -> (Ein.mu list * Ein.sumrange list * Ein.ein_exp list)

  end = struct
   
    structure E = Ein
    structure IR = MidIR
    structure Ty = MidTypes
    structure Op = MidOps
    structure V = IR.Var
    structure Mk = MkOperators

    fun assign (x, rator, args) = (x, IR.OP(rator, args))
    fun assignEin (x, rator, args) = ((x, IR.EINAPP(rator, args)))

    (*getTys:int->ty*int list *int list
    *Returns types based on field dimension
    * Really, just done literally so that 1-d fields pass the type-checker
    *)
    fun getTys 1 = (Ty.intTy, [], [])
      | getTys dim = (Ty.iVecTy dim, [dim], [dim, dim])

    (* WorldToImagespace:dim* imageinfo* mid-il var* mid-il var
        ->mid-il var * mid-il var *code
    *transform image-space position x to world space position
    *)
    fun worldToImage {avail, info, img, pos} = let
	  val dim = ImageInfo.dim info
	  val (_, fty, pty) = getTys dim
	  val mty = Ty.TensorTy pty
	  val rty = Ty.TensorTy fty
	  val M = Avail.addAssign (avail, "M", mty, IR.OP(Op.Transform info, [img]))
	  val T = Avail.addAssign (avail, "T", rty, IR.OP(Op.Translate info, [img]))
          val (mulOp, addOp) = if (dim = 1)
		then (Mk.mulRR, Mk.addRR)
		else (Mk.innerTT([dim, dim], [dim]), Mk.addTT[dim])
	  val x0 = Avail.addAssign (avail, "x0", rty, IR.OP(mulOp, [M, pos]))            (*Image-Space position*)
	  val x = Avail.addAssign (avail, "x", rty, IR.OP(addOp, [x0, T]))
	  in
            (M, x)
	  end

    (*transformToImgSpae:int*imginfo*mid-ilvar*mid-ilvar
        -> mid_il var list * mid-il var* code
    *Create fractional, and integer position vectors
    *)
    fun worldToIndex (arg as {avail, info, img, pos}) = let
	  val dim = ImageInfo.dim info
	  val (ity, fty, pty) = getTys dim
	  val mty = Ty.TensorTy  pty
	  val rty = Ty.TensorTy fty
	  val (M, x) = worldToImage arg
	  val nd = Avail.addAssign (avail, "nd", rty, IR.OP(Op.Floor dim, [x]))		(* real position *)
	  val f = Avail.addAssign (avail, "f", rty, IR.OP(Mk.subTT fty, [x, nd]))	(* fractional *)
	  val n = Avail.addAssign (avail, "n", ity, IR.OP(Op.RealToInt dim, [nd]))	(* integer position *)
	  val P = if (dim = 1)
		then M
		else Avail.addAssign (avail, "P", mty, IR.OP(Mk.transposeT pty, [M]))
	  in
            (n, f, P)
	  end

    (*transformToIndexSpace:int*int*mu list * param_id
    *Make Transform Matrices, and returns new indices as mu and sum_indexids
    *)
  (* nextIdx	-- next available variable ID
   * dim 	-- dimension of world space
   * dx		-- list of indices from the differentiation of the field that was probed
   * Pid	-- variable ID of the transformation-matrix parameter
   *)
    fun imageToWorld (nextIdx, dim, dx, Pid) = let
          fun iter ([], _, newdx, newsx, ps) = (rev newdx, rev newsx, rev ps)
            | iter (i::es, n, newdx, newsx, ps) = let
                val j = E.V n
                val sx = (j, 0, dim-1)
                val p = E.Tensor(Pid, [i, j])
                in
		  iter (es, n+1, j::newdx, sx::newsx, p::ps)
                end
	  in
	    iter (dx, nextIdx, [], [], [])
	  end

  end

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