File: pyramid_aottest.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 (64 lines) | stat: -rw-r--r-- 2,143 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <math.h>
#include <stdio.h>

#include "HalideBuffer.h"
#include "pyramid.h"

#include <vector>
using std::vector;
using namespace Halide::Runtime;

int main(int argc, char **argv) {
    Buffer<float, 2> input(1024, 1024);

    // Put some junk in the input. Keep it to small integers so the float averaging stays exact.
    for (int y = 0; y < input.height(); y++) {
        for (int x = 0; x < input.width(); x++) {
            input(x, y) = (float)(((x * 17 + y) / 8) % 32);
        }
    }

    vector<Buffer<float, 2>> levels(10);

    for (int l = 0; l < 10; l++) {
        levels[l] = Buffer<float, 2>(1024 >> l, 1024 >> l);
    }

    // Will throw a compiler error if we didn't compile the generator with 10 levels.
    pyramid(input,
            levels[0], levels[1], levels[2], levels[3], levels[4],
            levels[5], levels[6], levels[7], levels[8], levels[9]);

    // The bottom level should be the input
    for (int y = 0; y < input.height(); y++) {
        for (int x = 0; x < input.width(); x++) {
            if (input(x, y) != levels[0](x, y)) {
                printf("input(%d, %d) = %f, but levels[0](%d, %d) = %f\n",
                       x, y, input(x, y), x, y, levels[0](x, y));
                return 1;
            }
        }
    }

    // The remaining levels should be averaging of the levels above them.
    for (int l = 1; l < 10; l++) {
        for (int y = 0; y < (input.height() >> l); y++) {
            for (int x = 0; x < (input.width() >> l); x++) {
                float correct = (levels[l - 1](2 * x, 2 * y) +
                                 levels[l - 1](2 * x + 1, 2 * y) +
                                 levels[l - 1](2 * x, 2 * y + 1) +
                                 levels[l - 1](2 * x + 1, 2 * y + 1)) /
                                4;
                float actual = levels[l](x, y);
                if (correct != actual) {
                    printf("levels[%d](%d, %d) = %f instead of %f\n",
                           l, x, y, actual, correct);
                    return 1;
                }
            }
        }
    }

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