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

1 : jhr 328 (* probe.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 328 * All rights reserved.
5 :     *
6 :     * Expansion of probe operations in the HighIL to MidIL translation.
7 :     *)
8 :    
9 :     structure Probe : sig
10 :    
11 : jhr 1116 val expand : {
12 :     result : MidIL.var, (* result variable for probe *)
13 :     img : MidIL.var, (* probe image argument *)
14 :     v : ImageInfo.info, (* summary info about image *)
15 :     h : Kernel.kernel, (* reconstruction kernel *)
16 :     k : int, (* number of levels of differentiation *)
17 :     pos : MidIL.var (* probe position argument *)
18 :     } -> MidIL.assign list
19 : jhr 328
20 :     end = struct
21 :    
22 :     structure SrcIL = HighIL
23 : jhr 334 structure SrcOp = HighOps
24 : jhr 328 structure DstIL = MidIL
25 : jhr 391 structure DstTy = MidILTypes
26 : jhr 334 structure DstOp = MidOps
27 : jhr 349 structure DstV = DstIL.Var
28 : jhr 328 structure VMap = SrcIL.Var.Map
29 : jhr 349 structure IT = Shape
30 : jhr 328
31 :     (* generate a new variable indexed by dimension *)
32 : jhr 394 fun newVar_dim (prefix, d, ty) =
33 :     DstV.new (prefix ^ Partials.axisToString(Partials.axis d), ty)
34 : jhr 328
35 :     fun assign (x, rator, args) = (x, DstIL.OP(rator, args))
36 : jhr 1116 fun cons (x, args) = (x, DstIL.CONS(DstV.ty x, args))
37 : jhr 334 fun realLit (x, i) = (x, DstIL.LIT(Literal.Float(FloatLit.fromInt i)))
38 : jhr 328 fun intLit (x, i) = (x, DstIL.LIT(Literal.Int(IntInf.fromInt i)))
39 :    
40 : jhr 349 (* generate code for a evaluating a single element of a probe operation *)
41 :     fun probeElem {
42 :     dim, (* dimension of space *)
43 :     h, s, (* kernel h with support s *)
44 :     n, f, (* Dst vars for integer and fractional components of position *)
45 :     voxIter (* iterator over voxels *)
46 :     } (result, pdOp) = let
47 : jhr 1116 val vecsTy = DstTy.vecTy(2*s) (* vectors of coefficients cover support of kernel *)
48 :     val vecDimTy = DstTy.vecTy dim
49 :     (* generate the variables that hold the convolution coefficients. The
50 :     * resulting list is in slowest-to-fastest axes order.
51 :     *)
52 : jhr 349 val convCoeffs = let
53 :     val Partials.D l = pdOp
54 : jhr 1116 fun mkVar (_, [], coeffs) = coeffs
55 :     | mkVar (i, d::dd, coeffs) = (case d
56 :     of 0 => mkVar(i+1, dd, newVar_dim("h", i, vecsTy) :: coeffs)
57 :     | 1 => mkVar(i+1, dd, newVar_dim("dh", i, vecsTy) :: coeffs)
58 :     | _ => mkVar(i+1, dd, newVar_dim(concat["d", Int.toString d, "h"], i, vecsTy) :: coeffs)
59 : jhr 353 (* end case *))
60 : jhr 349 in
61 : jhr 1116 mkVar (0, l, [])
62 : jhr 349 end
63 : jhr 1116 (* for each dimension in space, we evaluate the kernel at the coordinates for that axis.
64 :     * the coefficients are
65 :     * h_{s-i} (f - i) for 1-s <= i <= s
66 :     *)
67 : jhr 349 val coeffCode = let
68 :     fun gen (x, k, (d, code)) = let
69 : jhr 1116 (* note that for 1D images, the f vector is a scalar *)
70 :     val fd = if (dim > 1)
71 :     then newVar_dim ("f", d, DstTy.realTy)
72 :     else f
73 : jhr 394 val a = DstV.new ("a", vecsTy)
74 : jhr 1116 (* note that we reverse the order of the list since the convolution
75 :     * space is flipped from the image space and we want the voxel vector
76 :     * to be in increasing address order.
77 :     *)
78 :     val tmps = List.rev(List.tabulate(2*s,
79 :     fn i => (DstV.new("t"^Int.toString i, DstTy.realTy), i - s)))
80 :     fun mkArg ((t, 0), code) = (t, DstIL.VAR fd) :: code
81 :     | mkArg ((t, n), code) = let
82 :     val (rator, n) = if (n < 0) then (DstOp.Sub, ~n) else (DstOp.Add, n)
83 : jhr 394 val t' = DstV.new ("r", DstTy.realTy)
84 : jhr 349 in
85 :     realLit (t', n) ::
86 : jhr 1116 assign (t, rator DstTy.realTy, [fd, t']) ::
87 : jhr 349 code
88 :     end
89 :     val code =
90 : jhr 353 cons(a, List.map #1 tmps) ::
91 : jhr 349 assign(x, DstOp.EvalKernel(2*s, h, k), [a]) ::
92 : jhr 353 code
93 : jhr 1116 val code = List.foldr mkArg code tmps
94 :     val code = if (dim > 1)
95 : jhr 1640 then assign(fd, DstOp.Index(DstTy.vecTy dim, d), [f]) :: code
96 : jhr 1116 else code
97 : jhr 349 in
98 : jhr 1116 (d+1, code)
99 : jhr 349 end
100 :     val Partials.D l = pdOp
101 :     in
102 : jhr 1116 (* we iterate from fastest to slowest axis *)
103 :     #2 (ListPair.foldr gen (0, []) (convCoeffs, List.rev l))
104 : jhr 349 end
105 : jhr 1116 (* generate the reduction code in reverse order *)
106 : jhr 353 fun genReduce (result, [hh], IT.LF{vox, offsets}, code) =
107 :     assign (result, DstOp.Dot(2*s), [vox, hh]) :: code
108 : jhr 349 | genReduce (result, hh::r, IT.ND(_, kids), code) = let
109 : jhr 394 val tv = DstV.new ("tv", vecsTy)
110 :     val tmps = List.tabulate(2*s, fn i => DstV.new("t"^Int.toString i, DstTy.realTy))
111 : jhr 349 fun lp ([], [], code) = code
112 :     | lp (t::ts, kid::kids, code) = genReduce(t, r, kid, lp(ts, kids, code))
113 :     val code = cons(tv, tmps) :: assign(result, DstOp.Dot(2*s), [hh, tv]) :: code
114 :     in
115 :     lp (tmps, kids, code)
116 :     end
117 : jhr 353 | genReduce _ = raise Fail "genReduce"
118 : jhr 349 val reduceCode = genReduce (result, convCoeffs, voxIter, [])
119 :     in
120 :     coeffCode @ reduceCode
121 :     end
122 :    
123 : jhr 1116 fun doVoxelSample (result, v, k, s, diffIter, {h, n, f, img}, offset) = let
124 :     val stride = ImageInfo.stride
125 :     val dim = ImageInfo.dim v
126 :     val vecsTy = DstTy.vecTy(2*s) (* vectors of coefficients cover support of kernel *)
127 :     (* generate code to load the voxel data; since we use a vector load operation to load the
128 : jhr 353 * fastest dimension, the height of the tree is one less than the dimension of space.
129 :     *)
130 : jhr 328 val voxIter = let
131 :     fun f (i, (offsets, id)) = (i - (s - 1) :: offsets, i::id)
132 :     fun g (offsets, id) = {
133 : jhr 353 offsets = ~(s-1) :: offsets,
134 : jhr 394 vox = DstV.new(String.concat("v" :: List.map Int.toString id), vecsTy)
135 : jhr 328 }
136 :     in
137 : jhr 334 IT.create (dim-1, 2*s, fn _ => (), f, g, ([], []))
138 : jhr 328 end
139 :     val loadCode = let
140 :     fun genCode ({offsets, vox}, code) = let
141 :     fun computeIndices (_, []) = ([], [])
142 :     | computeIndices (i, offset::offsets) = let
143 : jhr 394 val index = newVar_dim("i", i, DstTy.intTy)
144 :     val t1 = DstV.new ("t1", DstTy.intTy)
145 :     val t2 = DstV.new ("t2", DstTy.intTy)
146 : jhr 328 val (indices, code) = computeIndices (i+1, offsets)
147 : jhr 1116 val code = if (dim > 1)
148 :     then
149 :     intLit(t1, offset) ::
150 : jhr 1640 assign(t2, DstOp.Index(DstTy.iVecTy dim, i), [n]) ::
151 : jhr 1116 assign(index, DstOp.Add(DstTy.intTy), [t1, t2]) ::
152 :     code
153 :     else
154 :     intLit(t1, offset) ::
155 :     assign(index, DstOp.Add(DstTy.intTy), [t1, n]) ::
156 :     code
157 : jhr 328 val indices = index::indices
158 :     in
159 :     (indices, code)
160 :     end
161 : jhr 353 val (indices, indicesCode) = computeIndices (0, offsets)
162 : jhr 1116 val a = DstV.new ("a", DstTy.AddrTy v)
163 : jhr 328 in
164 : jhr 349 indicesCode @ [
165 : jhr 1116 assign(a, DstOp.VoxelAddress(v, offset), img::indices),
166 :     assign(vox, DstOp.LoadVoxels(v, 2*s), [a])
167 : jhr 328 ] @ code
168 :     end
169 :     in
170 :     IT.foldr genCode [] voxIter
171 :     end
172 : jhr 349 (* generate code to evaluate and construct the result tensor *)
173 :     val probeElem = probeElem {dim = dim, h = h, s = s, n = n, f = f, voxIter = voxIter}
174 :     fun genProbe (result, IT.ND(_, kids as (IT.LF _)::_), code) = let
175 :     (* the kids will all be leaves *)
176 :     fun genProbeCode (IT.LF arg, code) = probeElem arg @ code
177 :     fun getProbeVar (IT.LF(t, _)) = t
178 :     in
179 : jhr 374 List.foldr genProbeCode (cons (result, List.map getProbeVar kids) :: code) kids
180 : jhr 349 end
181 : jhr 1116 | genProbe (result, IT.ND(ty, kids), code) = let
182 :     (* FIXME: the type of the tmps depends on the types of the kids *)
183 :     val tmps = List.tabulate(dim, fn i => DstV.new("t"^Int.toString i, ty))
184 : jhr 374 val code = cons(result, tmps) :: code
185 : jhr 349 fun lp ([], [], code) = code
186 :     | lp (t::ts, kid::kids, code) = genProbe(t, kid, lp(ts, kids, code))
187 :     in
188 :     lp (tmps, kids, code)
189 :     end
190 : jhr 352 | genProbe (result, IT.LF(t, pdOp), code) = (* for scalar fields *)
191 :     probeElem (result, pdOp) @ code
192 : jhr 1116 val probeCode = if (k > 0)
193 :     then let
194 :     (* for gradients, etc. we have to transform back to world space *)
195 :     val ty = DstV.ty result
196 :     val tensor = DstV.new("tensor", ty)
197 :     val xform = assign(result, DstOp.TensorToWorldSpace(v, ty), [img, tensor])
198 :     in
199 :     genProbe (tensor, diffIter, [xform])
200 :     end
201 :     else genProbe (result, diffIter, [])
202 : jhr 328 in
203 : jhr 450 (* FIXME: for dim > 1 and k > 1, we need to transform the result back into world space *)
204 : jhr 1116 loadCode @ probeCode
205 : jhr 328 end
206 :    
207 : jhr 1116 (* generate code for probing the field (D^k (v * h)) at pos *)
208 :     fun expand {result, img, v, h, k, pos} = let
209 :     val dim = ImageInfo.dim v
210 :     val s = Kernel.support h
211 :     val vecsTy = DstTy.vecTy(2*s) (* vectors of coefficients to cover support of kernel *)
212 :     val vecDimTy = DstTy.vecTy dim
213 :     (* generate the transform code *)
214 :     val x = DstV.new ("x", vecDimTy) (* image-space position *)
215 :     val f = DstV.new ("f", vecDimTy)
216 :     val nd = DstV.new ("nd", vecDimTy)
217 : jhr 1640 val n = DstV.new ("n", DstTy.iVecTy dim)
218 : jhr 1116 val toImgSpaceCode = [
219 :     assign(x, DstOp.PosToImgSpace v, [img, pos]),
220 :     assign(nd, DstOp.Floor dim, [x]),
221 :     assign(f, DstOp.Sub vecDimTy, [x, nd]),
222 :     assign(n, DstOp.RealToInt dim, [nd])
223 :     ]
224 :     (* generate the shape of the differentiation tensor with variables representing
225 :     * the elements
226 :     *)
227 : jhr 1393 fun diffIter () = let
228 : jhr 1116 val partial = Partials.partial dim
229 :     fun f (i, (_::dd, axes)) = (dd, Partials.axis i :: axes)
230 :     fun labelNd (_::dd, _) = DstTy.tensorTy dd
231 :     fun labelLf (_, axes) = let
232 :     val r = DstV.new(
233 :     String.concat("r" :: List.map Partials.axisToString axes),
234 :     DstTy.realTy)
235 :     in
236 :     (r, partial axes)
237 :     end
238 :     in
239 :     IT.create (k, dim, labelNd, f, labelLf, (List.tabulate(k, fn _ => dim), []))
240 : jhr 328 end
241 : jhr 1116 val vars = {h=h, n=n, f=f, img=img}
242 : jhr 328 in
243 : jhr 1116 case ImageInfo.voxelShape v
244 : jhr 1393 of [] => toImgSpaceCode @ doVoxelSample (result, v, k, s, diffIter(), vars, 0)
245 : jhr 1116 | [d] => let
246 : jhr 1393 val sampleTy = DstTy.TensorTy(List.tabulate(k, fn _ => d))
247 : jhr 1116 fun doSamples (offset, xs, code) = if (offset < 0)
248 :     then code @ [cons(result, xs)]
249 :     else let
250 : jhr 1393 val res = DstV.new ("probe" ^ Int.toString offset, sampleTy)
251 :     val code = doVoxelSample (res, v, k, s, diffIter(), vars, offset) @ code
252 : jhr 1116 in
253 :     doSamples (offset-1, res::xs, code)
254 :     end
255 :     in
256 :     toImgSpaceCode @ doSamples (d-1, [], [])
257 :     end
258 :     | _ => raise Fail "image data with order > 1 not supported yet"
259 :     (* end case *)
260 : jhr 328 end
261 :    
262 :     end

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