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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 334 - (view) (download)

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 334 val expand : MidIL.var * FieldDef.field_def * HighIL.var -> HighIL.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 328 structure VMap = SrcIL.Var.Map
20 :    
21 :     (* a tree representation of nested iterations over the image space, where the
22 :     * height of the tree corresponds to the number of dimensions and at each node
23 :     * we have as many children as there are iterations.
24 :     *)
25 :     structure IT =
26 :     struct
27 :     datatype ('nd, 'lf) iter_tree
28 :     = LF of 'lf
29 :     | ND of ('nd * ('nd, 'lf) iter_tree list)
30 :    
31 :     fun create (depth, width, ndAttr, f, lfAttr, init) = let
32 :     fun mk (d, i, arg) = if (d < depth)
33 :     then ND(ndAttr arg, List.tabulate(width, fn j => mk(d+1, j, f(j, arg))))
34 :     else LF(lfAttr arg)
35 :     in
36 :     mk (0, 0, init)
37 :     end
38 :    
39 :     fun map (nd, lf) t = let
40 :     fun mapf (LF x) = LF(lf x)
41 :     | mapf (ND(i, kids)) = ND(nd i, List.map mapf kids)
42 :     in
43 :     mapf t
44 :     end
45 :    
46 :     fun foldr f init t = let
47 :     fun fold (LF x, acc) = f(x, acc)
48 :     | fold (ND(_, kids), acc) = List.foldr fold acc kids
49 :     in
50 :     fold t
51 :     end
52 :    
53 :     end
54 :     (* generate a new variable indexed by dimension *)
55 :     local
56 :     val dimNames = Vector.fromList[ "x", "y", "z" ];
57 :     in
58 :     fun newVar_dim (prefix, d) =
59 :     DstIL.Var.new (prefix ^ Vector.sub(dimNames, d))
60 :    
61 :     fun assign (x, rator, args) = (x, DstIL.OP(rator, args))
62 :     fun cons (x, args) = (x, DstIL.CONS args)
63 : jhr 334 fun realLit (x, i) = (x, DstIL.LIT(Literal.Float(FloatLit.fromInt i)))
64 : jhr 328 fun intLit (x, i) = (x, DstIL.LIT(Literal.Int(IntInf.fromInt i)))
65 :    
66 :     (* generate code for probing the field (D^k (v * h)) at pos *)
67 :     fun probe (result, (k, v, h), pos) = let
68 :     val ImageInfo.ImgInfo{dim, ty=([], ty), ...} = v
69 :     val dimTy = DstOp.VecTy dim
70 :     val s = Kernel.support h
71 :     val sTy = DstOp.VecTy(2*s)
72 :     (* generate the transform code *)
73 :     val x = DstIL.Var.new "x" (* image-space position *)
74 :     val f = DstIL.Var.new "f"
75 :     val nd = DstIL.Var.new "nd"
76 :     val n = DstIL.Var.new "n"
77 :     val transformCode = [
78 : jhr 334 assign(x, DstOp.Transform v, [pos]),
79 :     assign(nd, DstOp.Floor dim, [x]),
80 :     assign(f, DstOp.Sub dimTy, [x, nd]),
81 : jhr 328 assign(n, DstOp.TruncToInt dim, [nd])
82 :     ]
83 :     (* generate code to load the voxel data *)
84 :     val voxIter = let
85 :     fun f (i, (offsets, id)) = (i - (s - 1) :: offsets, i::id)
86 :     fun g (offsets, id) = {
87 :     offsets = offsets,
88 :     vox = DstIL.Var.new(String.concat("v" :: List.map Int.toString id))
89 :     }
90 :     in
91 : jhr 334 IT.create (dim-1, 2*s, fn _ => (), f, g, ([], []))
92 : jhr 328 end
93 :     val loadCode = let
94 :     fun genCode ({offsets, vox}, code) = let
95 :     fun computeIndices (_, []) = ([], [])
96 :     | computeIndices (i, offset::offsets) = let
97 :     val index = newVar_dim("i", i)
98 :     val t1 = DstIL.Var.new "t1"
99 :     val t2 = DstIL.Var.new "t2"
100 :     val (indices, code) = computeIndices (i+1, offsets)
101 :     val code =
102 :     intLit(t1, offset) ::
103 :     assign(t2, DstOp.Select i, [n]) ::
104 :     assign(index, DstOp.Add(DstOp.IntTy), [t1, t2]) ::
105 :     code
106 :     val indices = index::indices
107 :     in
108 :     (indices, code)
109 :     end
110 :     val (indices, indicesCode) = computeIndices (0, ~(s-1) :: offsets)
111 :     val a = DstIL.Var.new "a"
112 :     in
113 :     indicesCode :: [
114 : jhr 334 assign(a, DstOp.VoxelAddress v, indices),
115 :     assign(vox, DstOp.LoadVoxels(ty, 2*s))
116 : jhr 328 ] @ code
117 :     end
118 :     in
119 :     IT.foldr genCode [] voxIter
120 :     end
121 :     val voxVars = IT.foldr (fn ({vox, ...}, vs) => vox::vs) [] voxIter
122 :     (* generate the code for computing the convolution coefficients *)
123 :     val convCoeffs = Vector.tabulate (dim,
124 :     fn d => Vector.tabulate (k+1,
125 :     fn 0 => newVar_dim("h", d)
126 :     | 1 => newVar_dim("dh", d)
127 :     | i => newVar_dim(concat["d", Int.toString i, "h"], d)))
128 :     fun coefficient (d, k) = Vector.sub(Vector.sub(convCoeffs, d), k)
129 :     fun genCoeffCode d = if (d < dim)
130 :     then let
131 :     val code = genCoeffCode (d+1)
132 :     val fd = newVar_dim "f"
133 :     val a = DstIL.Var.new "a"
134 :     val tmps = List.tabulate(2*s,
135 :     fn i => (DstIL.Var.new("t"^Int.toString i), s - (i+1)))
136 :     fun mkArg ((t, n), code) = let
137 :     val t' = DstIL.Var.new "r"
138 :     in
139 :     realLit (t', n) ::
140 : jhr 334 assign (t, DstOp.Add DstOp.realTy, [fd, t']) ::
141 : jhr 328 code
142 :     end
143 :     val coeffCode =
144 :     cons(a, List.map #1 tmps) ::
145 :     List.tabulate (k+1, fn i =>
146 : jhr 334 assign(coefficient(d, i), DstOp.EvalKernel(2*s, h, i), [a]))
147 : jhr 328 in
148 :     assign(fd, DstOp.Select d, f) ::
149 :     (List.foldr mkArg (coeffCode @ code) tmps)
150 :     end
151 :     else []
152 :     val coeffCode = genCoeffCode
153 :     (* generate the reduction code *)
154 :     fun genReduce (d, IT.ND(kids), code) =
155 :     if (d < dim)
156 :     then List.foldr (fn (nd, code) => genReduce(d+1, nd, code)) code kids
157 :     else let (* the kids will all be leaves *)
158 :     val vv = DstIL.Var.new "vv"
159 :     fun getVox (IT.LF{vox, offsets}) = vox
160 :     val hh = coefficient (d, 0) (* FIXME: what is the right value for k? *)
161 :     in
162 :     cons (vv, List.map getVox kids) ::
163 : jhr 334 assign (t, DstOp.Dot, [hh, vv]) :: code
164 : jhr 328 end
165 :     val reduceCode = genReduce (1, voxIter, [])
166 :     in
167 :     transformCode @ loadCode @ coeffCode @ reduceCode
168 :     end
169 :    
170 :     end
171 :    
172 :     fun expand (result, FieldDef.CONV(0, img, h), pos) = let
173 :     fun expand' (result, FieldDef.CONV(k, v, h)) = let
174 :     val x = DstIL.Var.new "x"
175 :     val xformStm = (x, DstIL.OP(DstOp.Transform img, [pos']))
176 :     in
177 :     probe (result, (k, v, h), x) @ [xformStm]
178 :     end
179 :     | expand' (result, FieldDef.NEG fld) = let
180 :     val r = DstIL.Var.new "value"
181 :     val stms = expand' (r, fld)
182 :     in
183 :     expand' (r, fld) @ [assign(r, DstOp.Neg ty, [r])]
184 :     end
185 :     | expand' (result, FieldDef.SUM(fld1, dlf2)) = raise Fail "expandInside: SUM"
186 :     in
187 :     List.rev (expand (result, fld))
188 :     end
189 :    
190 :     end

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