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

SCM Repository

[diderot] View of /branches/charisee/src/compiler/high-to-mid/Transform.sml
ViewVC logotype

View of /branches/charisee/src/compiler/high-to-mid/Transform.sml

Parent Directory Parent Directory | Revision Log Revision Log

Revision 2845 - (download) (annotate)
Fri Dec 12 06:46:23 2014 UTC (5 years, 9 months ago) by cchiw
File size: 4089 byte(s)
added norm
 *This file does world to image space Transformations
 * Take a look at tex file for examples
 * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.

structure TransformEin = struct

    structure E = Ein
    structure DstIL = MidIL
    structure DstTy = MidILTypes
    structure DstOp = MidOps
    structure DstV = DstIL.Var
    structure P=Printer
    structure mk= mkOperators

    val testing=0
    fun assign (x, rator, args) = (x, DstIL.OP(rator, args))
    fun assignEin (x, rator, args) = ((x, DstIL.EINAPP(rator, args)))
    fun testp n=(case testing
        of 0=> 1
        | _ =>(print(String.concat n);1)
        (*end case*))
    val cnt = ref 0
    fun genName prefix = let
        val n = !cnt
            cnt := n+1;
            String.concat[prefix, "_", Int.toString n]

    (*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= (DstTy.intTy,[],[])
    | getTys dim = (DstTy.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 WorldToImagespace(dim,v,posx,imgArgDst)=let
        val translate=DstOp.Translate v
        val transform=DstOp.Transform v
        val (_ ,fty,pty)=getTys dim
        val mty=DstTy.TensorTy  pty
        val rty=DstTy.TensorTy fty
        val M  = DstV.new (genName "M", mty)   (*transform dim by dim?*)
        val T  = DstV.new (genName "T", rty)
        val x  = DstV.new (genName "x", rty)            (*Image-Space position*)
        val x0  = DstV.new (genName "x0", rty)
        val (PosToImgSpaceA,PosToImgSpaceB)=(case dim
            of 1=>(mk.prodScalar,mk.addScalar)
            | _ => (mk.transformA(dim,dim) ,mk.transformB(dim))
            (*end case*))
        val code=[
            assign(M, transform, [imgArgDst]),
            assign(T, translate, [imgArgDst]),
            assignEin(x0, PosToImgSpaceA,[M,posx]) , (*xo=MX*)
            assignEin(x, PosToImgSpaceB,[x0,T])  (*x=x0+T*)

        -> mid_il var list * mid-il var* code
    *Create fractional, and integer position vectors
    fun transformToImgSpace  (dim,v,posx,imgArgDst)=let
        val (ity,fty,pty)=getTys dim
        val mty=DstTy.TensorTy  pty
        val rty=DstTy.TensorTy fty
        val f  = DstV.new ("f", rty)            (*fractional*)
        val nd = DstV.new ("nd",  rty)           (*real position*)
        val n  = DstV.new ("n", ity)           (*integer position*)
        val P  = DstV.new ("P",mty)   (*transform dim by dim?*)
        val (M,x,code1)=WorldToImagespace(dim,v,posx,imgArgDst)
        val (P,PCode)=(case dim
            of 1=>(M,[])
            | _ =>(P,[assignEin(P, mk.transpose(pty), [M])])
            (*end case*))
        val code=[
            assign(nd, DstOp.Floor dim, [x]),   (*nd *)
            assignEin(f, mk.subTen(fty),[x,nd]),           (*fractional*)
            assign(n, DstOp.RealToInt dim, [nd]) (*real to Int*)

    (*transformToIndexSpace:int*int*mu list * param_id
    *Make Transform Matrices, and returns new indices as mu and sum_indexids
    fun transformToIndexSpace(freshIndex,dim,dx,Pid)=let
        fun iter([],_,newdx,newsx,Ps)=(newdx,newsx,Ps)
        | iter (i::es,n,newdx,newsx,Ps)=(case i
            of E.V v=>  let
            val j=E.V n
            val s=(j,0,dim-1)
            val P=E.Tensor(Pid,[i,j])
                iter (es,n+1,newdx@[j],newsx@[s],Ps@[P])
        | E.C c=> raise Fail"Unsure how to Transform constant differentiation"
        (*end case*))
    val (newdx,newsx,Ps)= iter(dx,freshIndex,[],[],[])


ViewVC Help
Powered by ViewVC 1.0.0