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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4236 - (download) (annotate)
Wed Jul 20 03:02:00 2016 UTC (3 years, 6 months ago) by cchiw
File size: 7260 byte(s)
added generic cases for trace|det and added test cases
(* 
 *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 TransformEin = struct

    local
   
    structure E = Ein
    structure DstIL = MidIL
    structure DstTy = MidILTypes
    structure DstOp = MidOps
    structure DstV = DstIL.Var
    structure P = Printer
    structure Mk = MkOperators

in
    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
        in
            cnt := n+1;
            String.concat[prefix, "_", Int.toString n]
        end


    fun searchSet(fieldset,codeA,rtn as (lhs,_))=
        (case (einVarSet.rtnVarN(fieldset,rtn))
        of (fieldset,SOME vA) => ("\ntransform-found";(fieldset,vA,codeA))
        | (fieldset,NONE)    =>   ("\ntransform-notfound";(fieldset,lhs,rtn::codeA))
        (*end case*))


    (*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.mulRR,Mk.addRR)
            | _ => (Mk.innerTT([dim,dim],[dim]), Mk.addTT[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*)
        ]
        in
            (M,x,code)
        end

    fun WorldToImagespaceF(fieldset,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.mulRR,Mk.addRR)
            | _ => (Mk.innerTT([dim,dim],[dim]), Mk.addTT[dim])
        (*end case*))

       val (fieldset,M,A)=searchSet(fieldset, [], assign(M, transform, [imgArgDst]))
       val (fieldset,T,A)=searchSet(fieldset, A, assign(T, translate, [imgArgDst]))
       val (fieldset,x0,A)=searchSet(fieldset, A, assignEin(x0, PosToImgSpaceA,[M,posx])) (*xo=MX*)
       val (fieldset,x,code)=searchSet(fieldset, A, assignEin(x, PosToImgSpaceB,[x0,T]))      (*x=x0+T*)

        in
        (fieldset,M,x,List.rev code)
        end


    (*transformToImgSpae:int*imginfo*mid-ilvar*mid-ilvar
        -> 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.transposeT(pty), [M])])
            (*end case*))
        val code=[
            assign(nd, DstOp.Floor dim, [x]),   (*nd *)
            assignEin(f, Mk.subTT(fty),[x,nd]),           (*fractional*)
            assign(n, DstOp.RealToInt dim, [nd]) (*real to Int*)
        ]
        in
            ([n,f],P,code1@PCode@code)
        end


    fun transformToImgSpaceF  (fieldset,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 (fieldset,M,x,code1)=WorldToImagespaceF(fieldset,dim,v,posx,imgArgDst)

        val (fieldset,P,PCode)=(case dim
            of 1=>(fieldset,M,[])
            | _ => searchSet(fieldset, [], assignEin(P, Mk.transposeT(pty), [M]))
            (*end case*))

       val (fieldset,nd,A)=searchSet(fieldset, [],assign(nd, DstOp.Floor dim, [x])) (* nd *)
       val (fieldset,f,A)=searchSet(fieldset, A, assignEin(f, Mk.subTT(fty),[x,nd]))  (*fractional*)
       val (fieldset,n,A)=searchSet(fieldset, A, assign(n, DstOp.RealToInt dim, [nd])) (*real to Int*)

        in
            (fieldset,[n,f],P,code1@PCode@(List.rev A))
        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

* #1 new derivative indices applied to kernel
* #2 tshape, shape of tensor representing probe
* #3 summation indices creating from transformation multiplcation
* #4 expressions for transformation matrix P
*)
    fun transformToIndexSpace (_, 1, dx, Pid) = let
        fun iter ([], newdx, ps) = (rev newdx, [], [], rev ps)
          | iter (i::es, newdx, ps) = let
            val p = E.Tensor(Pid, [])
            in
                iter (es, i::newdx, p::ps)
            end
        in
            iter (dx, [], [])
        end
    | transformToIndexSpace(freshIndex,dim,dx,Pid)=let
        fun iter([],_,newdx,newsx,Ps)=(newdx, newdx,newsx,Ps)
        | iter (i::es,n,newdx,newsx,Ps)=  let
            val j=E.V n
            val s=(j,0,dim-1)
            val P=E.Tensor(Pid,[i,j])
            in
                iter (es,n+1,newdx@[j],newsx@[s],Ps@[P])
            end
            (*raise Fail"Unsure how to Transform constant differentiation"*)
    in  iter(dx,freshIndex,[],[],[])  end



end
end

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