1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
#include "Halide.h"
#include <stdio.h>
using namespace Halide;
int count = 0;
extern "C" HALIDE_EXPORT_SYMBOL int call_counter(int x, int y) {
count++;
return 0;
}
HalideExtern_2(int, call_counter, int, int);
extern "C" void *my_malloc(JITUserContext *, size_t x) {
printf("Malloc wasn't supposed to be called!\n");
exit(1);
}
int main(int argc, char **argv) {
Var x, y;
if (get_jit_target_from_environment().arch == Target::WebAssembly) {
printf("[SKIP] WebAssembly JIT does not support custom allocators.\n");
return 0;
}
for (auto store_in : {MemoryType::Heap, MemoryType::Register}) {
count = 0;
Func f, g;
f(x) = call_counter(x, 0);
g(x) = f(x) + f(x - 1);
f.store_root().compute_at(g, x).store_in(store_in);
// Test that sliding window works when specializing.
g.specialize(g.output_buffer().dim(0).min() == 0);
Buffer<int> im = g.realize({100});
// f should be able to tell that it only needs to compute each value once
if (count != 101) {
printf("f was called %d times instead of %d times\n", count, 101);
return 1;
}
}
// Try two producers used by the same consumer.
for (auto store_in : {MemoryType::Heap, MemoryType::Register}) {
count = 0;
Func f, g, h;
f(x) = call_counter(2 * x + 0, 0);
g(x) = call_counter(2 * x + 1, 0);
h(x) = f(x) + f(x - 1) + g(x) + g(x - 1);
f.store_root().compute_at(h, x).store_in(store_in);
g.store_root().compute_at(h, x).store_in(store_in);
Buffer<int> im = h.realize({100});
if (count != 202) {
printf("f was called %d times instead of %d times\n", count, 202);
return 1;
}
}
// Try a sequence of two sliding windows.
for (auto store_in : {MemoryType::Heap, MemoryType::Register}) {
count = 0;
Func f, g, h;
f(x) = call_counter(2 * x + 0, 0);
g(x) = f(x) + f(x - 1);
h(x) = g(x) + g(x - 1);
f.store_root().compute_at(h, x).store_in(store_in);
g.store_root().compute_at(h, x).store_in(store_in);
Buffer<int> im = h.realize({100});
int correct = store_in == MemoryType::Register ? 103 : 102;
if (count != correct) {
printf("f was called %d times instead of %d times\n", count, correct);
return 1;
}
}
// Try again where there's a containing stage
for (auto store_in : {MemoryType::Heap, MemoryType::Register}) {
count = 0;
Func f, g, h;
f(x) = call_counter(x, 0);
g(x) = f(x) + f(x - 1);
h(x) = g(x);
f.store_root().compute_at(g, x).store_in(store_in);
g.compute_at(h, x);
Buffer<int> im = h.realize({100});
if (count != 101) {
printf("f was called %d times instead of %d times\n", count, 101);
return 1;
}
}
// Add an inner vectorized dimension.
for (auto store_in : {MemoryType::Heap, MemoryType::Register}) {
count = 0;
Func f, g, h;
Var c;
f(x, c) = call_counter(x, c);
g(x, c) = f(x + 1, c) - f(x, c);
h(x, c) = g(x, c);
f.store_root()
.compute_at(h, x)
.store_in(store_in)
.reorder(c, x)
.reorder_storage(c, x)
.bound(c, 0, 4)
.vectorize(c);
g.compute_at(h, x);
h.reorder(c, x).reorder_storage(c, x).bound(c, 0, 4).vectorize(c);
Buffer<int> im = h.realize({100, 4});
if (count != 404) {
printf("f was called %d times instead of %d times\n", count, 404);
return 1;
}
}
// Now try with a reduction
{
count = 0;
RDom r(0, 100);
Func f, g;
f(x, y) = 0;
f(r, y) = call_counter(r, y);
f.store_root().compute_at(g, y);
g(x, y) = f(x, y) + f(x, y - 1);
Buffer<int> im = g.realize({10, 10});
// For each value of y, f should be evaluated over (0 .. 100) in
// x, and (y .. y-1) in y. Sliding window optimization means that
// we can skip the y-1 case in all but the first iteration.
if (count != 100 * 11) {
printf("f was called %d times instead of %d times\n", count, 100 * 11);
return 1;
}
}
{
// Now try sliding over multiple dimensions at once
Func f, g;
count = 0;
f(x, y) = call_counter(x, y);
g(x, y) = f(x - 1, y) + f(x, y) + f(x, y - 1);
f.store_root().compute_at(g, x);
Buffer<int> im = g.realize({10, 10});
if (count != 11 * 11) {
printf("f was called %d times instead of %d times\n", count, 11 * 11);
return 1;
}
}
{
Func f, g;
// Now a trickier example. In order for this to work, Halide would have to slide diagonally. We don't handle this.
count = 0;
f(x, y) = call_counter(x, y);
// When x was two smaller the second term was computed. When y was two smaller the third term was computed.
g(x, y) = f(x + y, x - y) + f((x - 2) + y, (x - 2) - y) + f(x + (y - 2), x - (y - 2));
f.store_root().compute_at(g, x);
Buffer<int> im = g.realize({10, 10});
if (count != 1500) {
printf("f was called %d times instead of %d times\n", count, 1500);
return 1;
}
}
{
// Now make sure Halide folds the example in Func.h down to a stack allocation
Func f, g;
f(x, y) = x * y;
g(x, y) = f(x, y) + f(x + 1, y) + f(x, y + 1) + f(x + 1, y + 1);
f.store_at(g, y).compute_at(g, x);
g.jit_handlers().custom_malloc = my_malloc;
Buffer<int> im = g.realize({10, 10});
}
{
// Sliding where the footprint is actually fixed over the loop
// var. Everything in the producer should be computed in the
// first iteration.
Func f, g;
f(x) = call_counter(x, 0);
g(x) = f(0) + f(5);
f.store_root().compute_at(g, x);
count = 0;
Buffer<int> im = g.realize({100});
// f should be able to tell that it only needs to compute each value once
if (count != 6) {
printf("f was called %d times instead of %d times\n", count, 6);
return 1;
}
}
for (auto store_in : {MemoryType::Heap, MemoryType::Register}) {
// Sliding where we only need a new value every third iteration of the consumer.
Func f, g;
f(x) = call_counter(x, 0);
g(x) = f(x / 3);
f.store_root().compute_at(g, x).store_in(store_in);
count = 0;
Buffer<int> im = g.realize({100});
// f should be able to tell that it only needs to compute each value once
if (count != 34) {
printf("f was called %d times instead of %d times\n", count, 34);
return 1;
}
}
for (auto store_in : {MemoryType::Heap, MemoryType::Register}) {
// Sliding where we only need a new value every third iteration of the consumer.
// This test checks that we don't ask for excessive bounds.
ImageParam f(Int(32), 1);
Func g;
g(x) = f(x / 3);
Var xo;
g.split(x, xo, x, 10);
f.in().store_at(g, xo).compute_at(g, x).store_in(store_in);
Buffer<int> buf(33);
f.set(buf);
Buffer<int> im = g.realize({98});
}
for (auto store_in : {MemoryType::Heap, MemoryType::Register}) {
// Sliding with an unrolled producer
Var x, xi;
Func f, g;
f(x) = call_counter(x, 0) + x * x;
g(x) = f(x) + f(x - 1);
g.split(x, x, xi, 10);
f.store_root().compute_at(g, x).store_in(store_in).unroll(x);
count = 0;
Buffer<int> im = g.realize({100});
if (count != 101) {
printf("f was called %d times instead of %d times\n", count, 101);
return 1;
}
}
{
// Sliding with a vectorized producer and consumer.
count = 0;
Func f, g;
f(x) = call_counter(x, 0);
g(x) = f(x + 1) + f(x - 1);
f.store_root().compute_at(g, x).vectorize(x, 4);
g.vectorize(x, 4);
Buffer<int> im = g.realize({100});
if (count != 104) {
printf("f was called %d times instead of %d times\n", count, 104);
return 1;
}
}
{
// Sliding with a vectorized producer and consumer, trying to rotate
// cleanly in registers.
count = 0;
Func f, g;
f(x) = call_counter(x, 0);
g(x) = f(x + 1) + f(x - 1);
// This currently requires a trick to get everything to be aligned
// nicely. This exploits the fact that ShiftInwards splits are
// aligned to the end of the original loop (and extending before the
// min if necessary).
Var xi("xi");
f.store_root().compute_at(g, x).store_in(MemoryType::Register).split(x, x, xi, 8).vectorize(xi, 4).unroll(xi);
g.vectorize(x, 4, TailStrategy::RoundUp);
Buffer<int> im = g.realize({100});
if (count != 102) {
printf("f was called %d times instead of %d times\n", count, 102);
return 1;
}
}
{
// A sequence of stencils, all computed at the output.
count = 0;
Func f, g, h, u, v;
f(x, y) = call_counter(x, y);
g(x, y) = f(x, y - 1) + f(x, y + 1);
h(x, y) = g(x - 1, y) + g(x + 1, y);
u(x, y) = h(x, y - 1) + h(x, y + 1);
v(x, y) = u(x - 1, y) + u(x + 1, y);
u.compute_at(v, y);
h.store_root().compute_at(v, y);
g.store_root().compute_at(v, y);
f.store_root().compute_at(v, y);
v.realize({10, 10});
if (count != 14 * 14) {
printf("f was called %d times instead of %d times\n", count, 14 * 14);
return 1;
}
}
{
// A sequence of stencils, sliding computed at the output.
count = 0;
Func f, g, h, u, v;
f(x, y) = call_counter(x, y);
g(x, y) = f(x, y - 1) + f(x, y + 1);
h(x, y) = g(x - 1, y) + g(x + 1, y);
u(x, y) = h(x, y - 1) + h(x, y + 1);
v(x, y) = u(x - 1, y) + u(x + 1, y);
u.compute_at(v, y);
h.store_root().compute_at(v, y);
g.compute_at(h, y);
f.store_root().compute_at(v, y);
v.realize({10, 10});
if (count != 14 * 14) {
printf("f was called %d times instead of %d times\n", count, 14 * 14);
return 1;
}
}
{
// Sliding a func that has a boundary condition before the beginning
// of the loop. This needs an explicit warmup before we start sliding.
count = 0;
Func f, g;
f(x) = call_counter(x, 0);
g(x) = f(max(x, 3));
f.store_root().compute_at(g, x);
g.realize({10});
if (count != 7) {
printf("f was called %d times instead of %d times\n", count, 7);
return 1;
}
}
{
// Sliding a func that has a boundary condition on both sides.
count = 0;
Func f, g, h;
f(x) = call_counter(x, 0);
g(x) = f(clamp(x, 0, 9));
h(x) = g(x - 1) + g(x + 1);
f.store_root().compute_at(h, x);
g.store_root().compute_at(h, x);
h.realize({10});
if (count != 10) {
printf("f was called %d times instead of %d times\n", count, 10);
return 1;
}
}
printf("Success!\n");
return 0;
}
|