39 |
(* generate the variables that hold the convolution coefficients *) |
(* generate the variables that hold the convolution coefficients *) |
40 |
val convCoeffs = let |
val convCoeffs = let |
41 |
val Partials.D l = pdOp |
val Partials.D l = pdOp |
42 |
fun mkVar 0 = newVar_dim("h", dim) |
fun mkVar (_, []) = [] |
43 |
| mkVar 1 = newVar_dim("dh", dim) |
| mkVar (i, d::dd) = (case d |
44 |
| mkVar i = newVar_dim(concat["d", Int.toString i, "h"], dim) |
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 |
in |
in |
49 |
List.map mkVar l |
mkVar (0, l) |
50 |
end |
end |
51 |
|
val _ = print(concat["probeElem: ", Partials.partialToString pdOp, " in ", Int.toString(List.length convCoeffs), "D space\n"]) |
52 |
(* for each dimension, we evaluate the kernel at the coordinates for that axis *) |
(* for each dimension, we evaluate the kernel at the coordinates for that axis *) |
53 |
val coeffCode = let |
val coeffCode = let |
54 |
fun gen (x, k, (d, code)) = let |
fun gen (x, k, (d, code)) = let |
63 |
assign (t, DstOp.Add DstOp.realTy, [fd, t']) :: |
assign (t, DstOp.Add DstOp.realTy, [fd, t']) :: |
64 |
code |
code |
65 |
end |
end |
|
(* code in reverse order *) |
|
66 |
val code = |
val code = |
67 |
|
cons(a, List.map #1 tmps) :: |
68 |
assign(x, DstOp.EvalKernel(2*s, h, k), [a]) :: |
assign(x, DstOp.EvalKernel(2*s, h, k), [a]) :: |
69 |
|
code |
70 |
|
val code = |
71 |
assign(fd, DstOp.Select(dim, d), [f]) :: |
assign(fd, DstOp.Select(dim, d), [f]) :: |
72 |
cons(a, List.map #1 tmps) :: |
List.foldr mkArg code tmps |
|
(List.foldl mkArg [] tmps) |
|
73 |
in |
in |
74 |
(d, List.rev code) |
(d, code) |
75 |
end |
end |
76 |
val Partials.D l = pdOp |
val Partials.D l = pdOp |
77 |
in |
in |
78 |
#2 (ListPair.foldr gen (dim, []) (convCoeffs, l)) |
#2 (ListPair.foldr gen (dim, []) (convCoeffs, l)) |
79 |
end |
end |
80 |
(* generate the reduction code *) |
(* generate the reduction code *) |
81 |
fun genReduce (result, [hh], IT.ND(_, kids), code) = let |
fun genReduce (result, [hh], IT.LF{vox, offsets}, code) = |
82 |
(* the kids will all be leaves *) |
assign (result, DstOp.Dot(2*s), [vox, hh]) :: code |
|
val vv = DstV.new "vv" |
|
|
fun getVox (IT.LF{vox, offsets}) = vox |
|
|
in |
|
|
cons (vv, List.map getVox kids) :: |
|
|
assign (result, DstOp.Dot(2*s), [hh, vv]) :: code |
|
|
end |
|
83 |
| genReduce (result, hh::r, IT.ND(_, kids), code) = let |
| genReduce (result, hh::r, IT.ND(_, kids), code) = let |
84 |
val tv = DstV.new "tv" |
val tv = DstV.new "tv" |
85 |
val tmps = List.tabulate(2*s, fn i => DstV.new("t"^Int.toString i)) |
val tmps = List.tabulate(2*s, fn i => DstV.new("t"^Int.toString i)) |
89 |
in |
in |
90 |
lp (tmps, kids, code) |
lp (tmps, kids, code) |
91 |
end |
end |
92 |
|
| genReduce _ = raise Fail "genReduce" |
93 |
val reduceCode = genReduce (result, convCoeffs, voxIter, []) |
val reduceCode = genReduce (result, convCoeffs, voxIter, []) |
94 |
in |
in |
95 |
coeffCode @ reduceCode |
coeffCode @ reduceCode |
122 |
in |
in |
123 |
IT.create (k-1, dim, fn _ => (), f, g, []) |
IT.create (k-1, dim, fn _ => (), f, g, []) |
124 |
end |
end |
125 |
(* generate code to load the voxel data *) |
(* 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 |
val voxIter = let |
val voxIter = let |
129 |
fun f (i, (offsets, id)) = (i - (s - 1) :: offsets, i::id) |
fun f (i, (offsets, id)) = (i - (s - 1) :: offsets, i::id) |
130 |
fun g (offsets, id) = { |
fun g (offsets, id) = { |
131 |
offsets = offsets, |
offsets = ~(s-1) :: offsets, |
132 |
vox = DstV.new(String.concat("v" :: List.map Int.toString id)) |
vox = DstV.new(String.concat("v" :: List.map Int.toString id)) |
133 |
} |
} |
134 |
in |
in |
135 |
IT.create (dim-1, 2*s, fn _ => (), f, g, ([], [])) |
IT.create (dim-1, 2*s, fn _ => (), f, g, ([], [])) |
136 |
end |
end |
137 |
|
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 |
val loadCode = let |
val loadCode = let |
152 |
fun genCode ({offsets, vox}, code) = let |
fun genCode ({offsets, vox}, code) = let |
153 |
fun computeIndices (_, []) = ([], []) |
fun computeIndices (_, []) = ([], []) |
165 |
in |
in |
166 |
(indices, code) |
(indices, code) |
167 |
end |
end |
168 |
val (indices, indicesCode) = computeIndices (0, ~(s-1) :: offsets) |
val (indices, indicesCode) = computeIndices (0, offsets) |
169 |
val a = DstV.new "a" |
val a = DstV.new "a" |
170 |
in |
in |
171 |
indicesCode @ [ |
indicesCode @ [ |
201 |
end |
end |
202 |
|
|
203 |
fun expand (result, fld, pos) = let |
fun expand (result, fld, pos) = let |
204 |
fun expand' (result, FieldDef.CONV(k, v, h)) = let |
fun expand' (result, FieldDef.CONV(k, v, h)) = probe (result, (k, v, h), pos) |
|
val x = DstV.new "x" |
|
|
val xformStm = (x, DstIL.OP(DstOp.Transform v, [pos])) |
|
|
in |
|
|
probe (result, (k, v, h), x) @ [xformStm] |
|
|
end |
|
205 |
(* should push negation down to probe operation |
(* should push negation down to probe operation |
206 |
| expand' (result, FieldDef.NEG fld) = let |
| expand' (result, FieldDef.NEG fld) = let |
207 |
val r = DstV.new "value" |
val r = DstV.new "value" |
213 |
*) |
*) |
214 |
| expand' (result, FieldDef.SUM(fld1, dlf2)) = raise Fail "expandInside: SUM" |
| expand' (result, FieldDef.SUM(fld1, dlf2)) = raise Fail "expandInside: SUM" |
215 |
in |
in |
216 |
List.rev (expand' (result, fld)) |
expand' (result, fld) |
217 |
end |
end |
218 |
|
|
219 |
end |
end |