File: memoize_cloned.cpp

package info (click to toggle)
halide 14.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 49,124 kB
  • sloc: cpp: 238,722; makefile: 4,303; python: 4,047; java: 1,575; sh: 1,384; pascal: 211; xml: 165; javascript: 43; ansic: 34
file content (51 lines) | stat: -rw-r--r-- 1,209 bytes parent folder | download
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
#include "Halide.h"

using namespace Halide;

#ifdef _WIN32
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT
#endif

int call_count;
extern "C" DLLEXPORT int call_counter(int x) {
    call_count++;
    return x;
}
HalideExtern_1(int, call_counter, int);

int main(int argc, char **argv) {
    Func f, g, h;
    Var x, y;

    // A clone should use the same cache key as the parent, so that
    // computations of the clone can reuse computations of the
    // parent. This pipeline exploits that to compute f per row of one
    // consumer, then retrieve it from cache per row of another
    // consumer.
    //
    // Setting cache size gives you a trade-off between peak memory
    // usage and recompute.

    f(x, y) = call_counter(x);
    g(x, y) = f(x, y) * 2;
    h(x, y) = f(x, y) + g(x, y);

    h.compute_root();
    g.compute_root();
    f.clone_in(h).compute_at(h, y).memoize();
    f.compute_at(g, y).memoize();

    h.bound(x, 0, 1024).bound(y, 0, 32);

    call_count = 0;
    h.realize({1024, 32});
    if (call_count != 1024 * 32) {
        printf("call_count was supposed to be 1024 * 32: %d\n", call_count);
        return 1;
    }

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