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

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/high-to-mid/probe.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/high-to-mid/probe.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 353 - (view) (download)
Original Path: trunk/src/compiler/high-to-mid/probe.sml

1 : jhr 328 (* probe.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 :     * Expansion of probe operations in the HighIL to MidIL translation.
7 :     *)
8 :    
9 :     structure Probe : sig
10 :    
11 : jhr 349 val expand : MidIL.var * FieldDef.field_def * MidIL.var -> MidIL.assign list
12 : jhr 328
13 :     end = struct
14 :    
15 :     structure SrcIL = HighIL
16 : jhr 334 structure SrcOp = HighOps
17 : jhr 328 structure DstIL = MidIL
18 : jhr 334 structure DstOp = MidOps
19 : jhr 349 structure DstV = DstIL.Var
20 : jhr 328 structure VMap = SrcIL.Var.Map
21 : jhr 349 structure IT = Shape
22 : jhr 328
23 :     (* generate a new variable indexed by dimension *)
24 :     fun newVar_dim (prefix, d) =
25 : jhr 349 DstV.new (prefix ^ Partials.axisToString(Partials.axis d))
26 : jhr 328
27 :     fun assign (x, rator, args) = (x, DstIL.OP(rator, args))
28 :     fun cons (x, args) = (x, DstIL.CONS args)
29 : jhr 334 fun realLit (x, i) = (x, DstIL.LIT(Literal.Float(FloatLit.fromInt i)))
30 : jhr 328 fun intLit (x, i) = (x, DstIL.LIT(Literal.Int(IntInf.fromInt i)))
31 :    
32 : jhr 349 (* generate code for a evaluating a single element of a probe operation *)
33 :     fun probeElem {
34 :     dim, (* dimension of space *)
35 :     h, s, (* kernel h with support s *)
36 :     n, f, (* Dst vars for integer and fractional components of position *)
37 :     voxIter (* iterator over voxels *)
38 :     } (result, pdOp) = let
39 :     (* generate the variables that hold the convolution coefficients *)
40 :     val convCoeffs = let
41 :     val Partials.D l = pdOp
42 : jhr 353 fun mkVar (_, []) = []
43 :     | mkVar (i, d::dd) = (case d
44 :     of 0 => newVar_dim("h", i) :: mkVar(i+1, dd)
45 :     | 1 => newVar_dim("dh", i) :: mkVar(i+1, dd)
46 :     | _ => newVar_dim(concat["d", Int.toString d, "h"], i) :: mkVar(i+1, dd)
47 :     (* end case *))
48 : jhr 349 in
49 : jhr 353 mkVar (0, l)
50 : jhr 349 end
51 : jhr 353 val _ = print(concat["probeElem: ", Partials.partialToString pdOp, " in ", Int.toString(List.length convCoeffs), "D space\n"])
52 : jhr 349 (* for each dimension, we evaluate the kernel at the coordinates for that axis *)
53 :     val coeffCode = let
54 :     fun gen (x, k, (d, code)) = let
55 :     val d = d-1
56 :     val fd = newVar_dim ("f", d)
57 :     val a = DstV.new "a"
58 :     val tmps = List.tabulate(2*s, fn i => (DstV.new("t"^Int.toString i), s - (i+1)))
59 :     fun mkArg ((t, n), code) = let
60 :     val t' = DstV.new "r"
61 :     in
62 :     realLit (t', n) ::
63 :     assign (t, DstOp.Add DstOp.realTy, [fd, t']) ::
64 :     code
65 :     end
66 :     val code =
67 : jhr 353 cons(a, List.map #1 tmps) ::
68 : jhr 349 assign(x, DstOp.EvalKernel(2*s, h, k), [a]) ::
69 : jhr 353 code
70 :     val code =
71 : jhr 349 assign(fd, DstOp.Select(dim, d), [f]) ::
72 : jhr 353 List.foldr mkArg code tmps
73 : jhr 349 in
74 : jhr 353 (d, code)
75 : jhr 349 end
76 :     val Partials.D l = pdOp
77 :     in
78 :     #2 (ListPair.foldr gen (dim, []) (convCoeffs, l))
79 :     end
80 :     (* generate the reduction code *)
81 : jhr 353 fun genReduce (result, [hh], IT.LF{vox, offsets}, code) =
82 :     assign (result, DstOp.Dot(2*s), [vox, hh]) :: code
83 : jhr 349 | genReduce (result, hh::r, IT.ND(_, kids), code) = let
84 :     val tv = DstV.new "tv"
85 :     val tmps = List.tabulate(2*s, fn i => DstV.new("t"^Int.toString i))
86 :     fun lp ([], [], code) = code
87 :     | lp (t::ts, kid::kids, code) = genReduce(t, r, kid, lp(ts, kids, code))
88 :     val code = cons(tv, tmps) :: assign(result, DstOp.Dot(2*s), [hh, tv]) :: code
89 :     in
90 :     lp (tmps, kids, code)
91 :     end
92 : jhr 353 | genReduce _ = raise Fail "genReduce"
93 : jhr 349 val reduceCode = genReduce (result, convCoeffs, voxIter, [])
94 :     in
95 :     coeffCode @ reduceCode
96 :     end
97 :    
98 : jhr 328 (* generate code for probing the field (D^k (v * h)) at pos *)
99 :     fun probe (result, (k, v, h), pos) = let
100 :     val ImageInfo.ImgInfo{dim, ty=([], ty), ...} = v
101 :     val dimTy = DstOp.VecTy dim
102 :     val s = Kernel.support h
103 :     (* generate the transform code *)
104 : jhr 349 val x = DstV.new "x" (* image-space position *)
105 :     val f = DstV.new "f"
106 :     val nd = DstV.new "nd"
107 :     val n = DstV.new "n"
108 : jhr 328 val transformCode = [
109 : jhr 334 assign(x, DstOp.Transform v, [pos]),
110 :     assign(nd, DstOp.Floor dim, [x]),
111 :     assign(f, DstOp.Sub dimTy, [x, nd]),
112 : jhr 328 assign(n, DstOp.TruncToInt dim, [nd])
113 :     ]
114 : jhr 349 (* generate the shape of the differentiation tensor with variables representing
115 :     * the elements
116 :     *)
117 :     val diffIter = let
118 :     val partial = Partials.partial dim
119 :     fun f (i, axes) = Partials.axis i :: axes
120 :     fun g axes =
121 :     (DstV.new(String.concat("y" :: List.map Partials.axisToString axes)), partial axes)
122 :     in
123 :     IT.create (k-1, dim, fn _ => (), f, g, [])
124 :     end
125 : jhr 353 (* generate code to load the voxel data; since we a vector load operation to load the
126 :     * fastest dimension, the height of the tree is one less than the dimension of space.
127 :     *)
128 : jhr 328 val voxIter = let
129 :     fun f (i, (offsets, id)) = (i - (s - 1) :: offsets, i::id)
130 :     fun g (offsets, id) = {
131 : jhr 353 offsets = ~(s-1) :: offsets,
132 : jhr 349 vox = DstV.new(String.concat("v" :: List.map Int.toString id))
133 : jhr 328 }
134 :     in
135 : jhr 334 IT.create (dim-1, 2*s, fn _ => (), f, g, ([], []))
136 : jhr 328 end
137 : jhr 353 val _ = let
138 :     val indentWid = ref 2
139 :     fun inc () = (indentWid := !indentWid + 2)
140 :     fun dec () = (indentWid := !indentWid - 2)
141 :     fun indent () = print(CharVector.tabulate(!indentWid, fn _ => #" "))
142 :     fun nd () = (indent(); print "ND\n");
143 :     fun lf {offsets, vox} = (
144 :     indent(); print "LF{offsets = ["; print(String.concatWith "," (List.map Int.toString offsets));
145 :     print "], vox = "; print(DstV.toString vox); print "}\n")
146 :     fun pr (Shape.ND(attr, kids)) = (nd attr; inc(); List.app pr kids; dec())
147 :     | pr (Shape.LF attr) = lf attr
148 :     in
149 :     pr voxIter
150 :     end
151 : jhr 328 val loadCode = let
152 :     fun genCode ({offsets, vox}, code) = let
153 :     fun computeIndices (_, []) = ([], [])
154 :     | computeIndices (i, offset::offsets) = let
155 :     val index = newVar_dim("i", i)
156 : jhr 349 val t1 = DstV.new "t1"
157 :     val t2 = DstV.new "t2"
158 : jhr 328 val (indices, code) = computeIndices (i+1, offsets)
159 :     val code =
160 :     intLit(t1, offset) ::
161 : jhr 349 assign(t2, DstOp.Select(2*s, i), [n]) ::
162 : jhr 328 assign(index, DstOp.Add(DstOp.IntTy), [t1, t2]) ::
163 :     code
164 :     val indices = index::indices
165 :     in
166 :     (indices, code)
167 :     end
168 : jhr 353 val (indices, indicesCode) = computeIndices (0, offsets)
169 : jhr 349 val a = DstV.new "a"
170 : jhr 328 in
171 : jhr 349 indicesCode @ [
172 : jhr 334 assign(a, DstOp.VoxelAddress v, indices),
173 : jhr 349 assign(vox, DstOp.LoadVoxels(ty, 2*s), [a])
174 : jhr 328 ] @ code
175 :     end
176 :     in
177 :     IT.foldr genCode [] voxIter
178 :     end
179 : jhr 349 (* generate code to evaluate and construct the result tensor *)
180 :     val probeElem = probeElem {dim = dim, h = h, s = s, n = n, f = f, voxIter = voxIter}
181 :     fun genProbe (result, IT.ND(_, kids as (IT.LF _)::_), code) = let
182 :     (* the kids will all be leaves *)
183 :     fun genProbeCode (IT.LF arg, code) = probeElem arg @ code
184 :     fun getProbeVar (IT.LF(t, _)) = t
185 :     in
186 :     cons (result, List.map getProbeVar kids) :: List.foldr genProbeCode code kids
187 :     end
188 :     | genProbe (result, IT.ND(_, kids), code) = let
189 :     val tmps = List.tabulate(dim, fn i => DstV.new("t"^Int.toString i))
190 :     fun lp ([], [], code) = code
191 :     | lp (t::ts, kid::kids, code) = genProbe(t, kid, lp(ts, kids, code))
192 :     val code = cons(result, tmps) :: code
193 :     in
194 :     lp (tmps, kids, code)
195 :     end
196 : jhr 352 | genProbe (result, IT.LF(t, pdOp), code) = (* for scalar fields *)
197 :     probeElem (result, pdOp) @ code
198 : jhr 349 val probeCode = genProbe (result, diffIter, [])
199 : jhr 328 in
200 : jhr 349 transformCode @ loadCode @ probeCode
201 : jhr 328 end
202 :    
203 : jhr 349 fun expand (result, fld, pos) = let
204 : jhr 353 fun expand' (result, FieldDef.CONV(k, v, h)) = probe (result, (k, v, h), pos)
205 : jhr 349 (* should push negation down to probe operation
206 : jhr 328 | expand' (result, FieldDef.NEG fld) = let
207 : jhr 349 val r = DstV.new "value"
208 : jhr 328 val stms = expand' (r, fld)
209 : jhr 349 val ty = ??
210 : jhr 328 in
211 :     expand' (r, fld) @ [assign(r, DstOp.Neg ty, [r])]
212 :     end
213 : jhr 349 *)
214 : jhr 328 | expand' (result, FieldDef.SUM(fld1, dlf2)) = raise Fail "expandInside: SUM"
215 :     in
216 : jhr 353 expand' (result, fld)
217 : jhr 328 end
218 :    
219 :     end

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