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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3577 - (view) (download)

1 : jhr 3577 (*
2 :     *This file does world to image space Transformations
3 :     * Take a look at tex file for examples
4 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
5 :     *
6 :     * COPYRIGHT (c) 2015 The University of Chicago
7 :     * All rights reserved.
8 :     *)
9 :    
10 :    
11 :    
12 :     structure CoordSpaceTransform : sig
13 :    
14 :     (* `worldToImage {info, img, pos}` returns (M, x, stms), where M is a variable
15 :     * bound to the world-space to image-space linear transformation matrix, x is
16 :     * a variable bound to the transformed position (pos), and stms is a list of
17 :     * assignments that computes the world-space to -image-space transformation.
18 :     *)
19 :     val worldToImage : {
20 :     info : ImageInfo.info, (* the image info for the image *)
21 :     img : MidIR.var, (* the variable bound to the image *)
22 :     pos : MidIR.var (* the variable bound to the world-space position *)
23 :     } -> MidIR.var * MidIR.var * MidIR.assign list
24 :    
25 :     val worldToIndex : {
26 :     info : ImageInfo.info, (* the image info for the image *)
27 :     img : MidIR.var, (* the variable bound to the image *)
28 :     pos : MidIR.var (* the variable bound to the world-space position *)
29 :     } -> MidIR.var list * MidIR.var * MidIR.assign list
30 :    
31 :     val imageToWorld : Ein.tensor_id * int * Ein.mu list * Ein.tensor_id
32 :     -> (Ein.mu list * Ein.sumrange list * Ein.ein_exp list)
33 :    
34 :     end = struct
35 :    
36 :     structure E = Ein
37 :     structure IR = MidIR
38 :     structure Ty = MidTypes
39 :     structure Op = MidOps
40 :     structure V = IR.Var
41 :     structure Mk = MkOperators
42 :    
43 :     fun assign (x, rator, args) = (x, IR.OP(rator, args))
44 :     fun assignEin (x, rator, args) = ((x, IR.EINAPP(rator, args)))
45 :    
46 :     (*getTys:int->ty*int list *int list
47 :     *Returns types based on field dimension
48 :     * Really, just done literally so that 1-d fields pass the type-checker
49 :     *)
50 :     fun getTys 1 = (Ty.intTy, [], [])
51 :     | getTys dim = (Ty.iVecTy dim, [dim], [dim, dim])
52 :    
53 :     (* WorldToImagespace:dim* imageinfo* mid-il var* mid-il var
54 :     ->mid-il var * mid-il var *code
55 :     *transform image-space position x to world space position
56 :     *)
57 :     fun worldToImage {info, img, pos} = let
58 :     val dim = ImageInfo.dim info
59 :     val (_, fty, pty) = getTys dim
60 :     val mty = Ty.TensorTy pty
61 :     val rty = Ty.TensorTy fty
62 :     val M = V.new ("M", mty) (*transform dim by dim?*)
63 :     val T = V.new ("T", rty)
64 :     val x = V.new ("x", rty) (*Image-Space position*)
65 :     val x0 = V.new ("x0", rty)
66 :     val (mulOp, addOp) = if (dim = 1)
67 :     then (Mk.mulRR, Mk.addRR)
68 :     else (Mk.innerTT([dim, dim], [dim]), Mk.addTT[dim])
69 :     val code = [
70 :     assign (M, Op.Transform info, [img]),
71 :     assign (T, Op.Translate info, [img]),
72 :     assignEin (x0, mulOp, [M, pos]), (*xo=MX*)
73 :     assignEin (x, addOp, [x0, T]) (*x=x0+T*)
74 :     ]
75 :     in
76 :     (M, x, code)
77 :     end
78 :    
79 :     (*transformToImgSpae:int*imginfo*mid-ilvar*mid-ilvar
80 :     -> mid_il var list * mid-il var* code
81 :     *Create fractional, and integer position vectors
82 :     *)
83 :     fun worldToIndex (arg as {info, img, pos}) = let
84 :     val dim = ImageInfo.dim info
85 :     val (ity, fty, pty) = getTys dim
86 :     val mty = Ty.TensorTy pty
87 :     val rty = Ty.TensorTy fty
88 :     val f = V.new ("f", rty) (* fractional *)
89 :     val nd = V.new ("nd", rty) (* real position *)
90 :     val n = V.new ("n", ity) (* integer position *)
91 :     val (M, x, code1) = worldToImage arg
92 :     val code = [
93 :     assign (nd, Op.Floor dim, [x]), (*nd *)
94 :     assignEin (f, Mk.subTT(fty),[x,nd]), (*fractional*)
95 :     assign (n, Op.RealToInt dim, [nd]) (*real to Int*)
96 :     ]
97 :     val (P, code) = if (dim = 1)
98 :     then (M, code)
99 :     else let
100 :     val P = V.new ("P", mty)
101 :     in
102 :     (P, assignEin(P, Mk.transposeT pty, [M]) :: code)
103 :     end
104 :     in
105 :     ([n, f], P, code1 @ code)
106 :     end
107 :    
108 :     (*transformToIndexSpace:int*int*mu list * param_id
109 :     *Make Transform Matrices, and returns new indices as mu and sum_indexids
110 :     *)
111 :     (* nextIdx -- next available variable ID
112 :     * dim -- dimension of world space
113 :     * dx -- list of indices from the differentiation of the field that was probed
114 :     * Pid -- variable ID of the transformation-matrix parameter
115 :     *)
116 :     fun imageToWorld (nextIdx, dim, dx, Pid) = let
117 :     fun iter ([], _, newdx, newsx, ps) = (rev newdx, rev newsx, rev ps)
118 :     | iter (i::es, n, newdx, newsx, ps) = let
119 :     val j = E.V n
120 :     val sx = (j, 0, dim-1)
121 :     val p = E.Tensor(Pid, [i, j])
122 :     in
123 :     iter (es, n+1, j::newdx, sx::newsx, p::ps)
124 :     end
125 :     in
126 :     iter (dx, nextIdx, [], [], [])
127 :     end
128 :    
129 :     end

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