File: lazy_convolution.cpp

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (40 lines) | stat: -rw-r--r-- 1,049 bytes parent folder | download | duplicates (3)
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
#include "Halide.h"
#include <stdio.h>

using namespace Halide;

int call_count;
extern "C" HALIDE_EXPORT_SYMBOL float call_counter(float x) {
    call_count++;
    return x;
}

HalidePureExtern_1(float, call_counter, float);

int main(int argc, char **argv) {

    Func f;
    Var x, y;
    f(x, y) = call_counter(sin(x * 3 + y));

    // f contains values in [-1, 1]. Now compute a convolution over f
    // only where f is positive. If f is negative, we'll skip the work
    // and write a zero instead.
    Func blur;
    RDom r(-10, 20, -10, 20);
    blur(x, y) = select(f(x, y) > 0, sum(f(x + r.x, y + r.y)), 0);

    call_count = 0;
    blur.realize({100, 100});

    // If we computed the convolution everywhere, call_count would be
    // 100*100*20*20. Because we only compute it in half of the
    // places, it should be smaller; roughly 100*100*20*20*0.5.
    if (call_count > 2100000) {
        printf("Expected call_count ~= 2000000. Instead it's %d\n", call_count);
        return 1;
    }

    printf("Success!\n");
    return 0;
}