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 4432 - (view) (download)

1 : jhr 4045 (*
2 : jhr 3577 *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 : jhr 4317 avail : AvailRHS.t,
21 : jhr 4432 info : ImageInfo.t, (* the image info for the image *)
22 : jhr 4317 img : MidIR.var, (* the variable bound to the image *)
23 :     pos : MidIR.var (* the variable bound to the world-space position *)
24 :     } -> MidIR.var * MidIR.var
25 : jhr 3577
26 :     val worldToIndex : {
27 : jhr 4317 avail : AvailRHS.t,
28 : jhr 4432 info : ImageInfo.t, (* the image info for the image *)
29 : jhr 4317 img : MidIR.var, (* the variable bound to the image *)
30 :     pos : MidIR.var (* the variable bound to the world-space position *)
31 :     } -> MidIR.var * MidIR.var * MidIR.var
32 : jhr 3577
33 : cchiw 3978 val imageToWorld : Ein.param_id * int * Ein.mu list * Ein.param_id
34 : jhr 4317 -> (Ein.mu list * Ein.mu list * Ein.sumrange list * Ein.ein_exp list)
35 : jhr 3577
36 :     end = struct
37 : jhr 4045
38 : jhr 3577 structure E = Ein
39 :     structure IR = MidIR
40 :     structure Ty = MidTypes
41 :     structure Op = MidOps
42 :     structure V = IR.Var
43 :     structure Mk = MkOperators
44 :    
45 :     fun assign (x, rator, args) = (x, IR.OP(rator, args))
46 :     fun assignEin (x, rator, args) = ((x, IR.EINAPP(rator, args)))
47 : jhr 3754
48 : cchiw 3735 fun incUse (IR.V{useCnt, ...}) = (useCnt := !useCnt + 1)
49 : jhr 3754
50 : jhr 3577 (*getTys:int->ty*int list *int list
51 :     *Returns types based on field dimension
52 :     * Really, just done literally so that 1-d fields pass the type-checker
53 :     *)
54 :     fun getTys 1 = (Ty.intTy, [], [])
55 :     | getTys dim = (Ty.iVecTy dim, [dim], [dim, dim])
56 :    
57 :     (* WorldToImagespace:dim* imageinfo* mid-il var* mid-il var
58 :     ->mid-il var * mid-il var *code
59 :     *transform image-space position x to world space position
60 :     *)
61 : jhr 3732 fun worldToImage {avail, info, img, pos} = let
62 : jhr 4317 val dim = ImageInfo.dim info
63 :     val (_, fty, pty) = getTys dim
64 :     val mty = Ty.TensorTy pty
65 :     val rty = Ty.TensorTy fty
66 :     val M = AvailRHS.addAssign (avail, "Mtransform", mty, IR.OP(Op.Transform info, [img]))
67 :     val (mulOp, addOp) = if (dim = 1)
68 :     then (Mk.mulRR, Mk.addRR)
69 :     else (Mk.innerTT([dim, dim], [dim]), Mk.addTT[dim])
70 : cchiw 4159
71 :    
72 : jhr 4317 val x0 = AvailRHS.addAssign (avail, "MxPos", rty, IR.EINAPP(mulOp, [M, pos])) (*Image-Space position*)
73 :     val T = AvailRHS.addAssign (avail, "Ttranslate", rty, IR.OP(Op.Translate info, [img]))
74 :     val x = AvailRHS.addAssign (avail, "imgPos", rty, IR.EINAPP(addOp, [x0, T]))
75 :     in
76 :     incUse M; incUse x0; incUse T;
77 : jhr 3732 (M, x)
78 : jhr 4317 end
79 : jhr 3577
80 : jhr 4045 (* Create fractional, and integer position vectors *)
81 : jhr 3732 fun worldToIndex (arg as {avail, info, img, pos}) = let
82 : jhr 4317 val dim = ImageInfo.dim info
83 :     val (ity, fty, pty) = getTys dim
84 :     val mty = Ty.TensorTy pty
85 :     val rty = Ty.TensorTy fty
86 :     val (M, x) = worldToImage arg
87 :     val nd = AvailRHS.addAssign (avail, "nd", rty, IR.OP(Op.Floor dim, [x])) (* real position *)
88 :     val f = AvailRHS.addAssign (avail, "f", rty, IR.EINAPP(Mk.subTT fty, [x, nd])) (* fractional *)
89 :     val n = AvailRHS.addAssign (avail, "n", ity, IR.OP(Op.RealToInt dim, [nd])) (* integer position *)
90 :     val P = if (dim = 1)
91 :     then M
92 :     else AvailRHS.addAssign (avail, "P", mty, IR.EINAPP(Mk.transposeT pty, [M]))
93 :     in
94 : jhr 3732 (n, f, P)
95 : jhr 4317 end
96 : jhr 3577
97 :     (*transformToIndexSpace:int*int*mu list * param_id
98 :     *Make Transform Matrices, and returns new indices as mu and sum_indexids
99 :     *)
100 : jhr 4317 (* nextIdx -- next available variable ID
101 :     * dim -- dimension of world space
102 :     * dx -- list of indices from the differentiation of the field that was probed
103 :     * Pid -- variable ID of the transformation-matrix parameter
104 : cchiw 4169
105 :     * #1 new derivative indices applied to kernel
106 :     * #2 tshape, shape of tensor representing probe
107 :     * #3 summation indices creating from transformation multiplcation
108 :     * #4 expressions for transformation matrix P
109 : jhr 3577 *)
110 : cchiw 4169 fun imageToWorld (nextIdx, 1, dx, Pid) = let
111 :     fun iter ([], newdx, ps) = (rev newdx, [], [], rev ps)
112 : cchiw 4159 | iter (i::es, newdx, ps) = let
113 :     val p = E.Tensor(Pid, [])
114 :     in
115 :     iter (es, i::newdx, p::ps)
116 :     end
117 :     in
118 :     iter (dx, [], [])
119 :     end
120 :     | imageToWorld (nextIdx, dim, dx, Pid) = let
121 : cchiw 4169 fun iter ([], _, newdx, newsx, ps) = (rev newdx, rev newdx, rev newsx, rev ps)
122 : jhr 3577 | iter (i::es, n, newdx, newsx, ps) = let
123 :     val j = E.V n
124 : cchiw 3978 val sx = (n, 0, dim-1)
125 : jhr 3577 val p = E.Tensor(Pid, [i, j])
126 :     in
127 : jhr 4317 iter (es, n+1, j::newdx, sx::newsx, p::ps)
128 : jhr 3577 end
129 : jhr 4317 in
130 :     iter (dx, nextIdx, [], [], [])
131 :     end
132 : jhr 3577
133 : jhr 4045 end

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