File: for_each_element.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 (84 lines) | stat: -rw-r--r-- 2,706 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "HalideBuffer.h"

#include <stdio.h>

using namespace Halide::Runtime;

int main(int argc, char **argv) {
    // Try several different ways of accessing a the pixels of an image,
    // and check that they all do the same thing.
    Buffer<int> im(1000, 1000, 3);

    // Make the image non-dense in memory to make life more interesting
    im = im.cropped(0, 100, 800).cropped(1, 200, 600);

    im.for_each_element([&](int x, int y, int c) {
        im(x, y, c) = 10 * x + 5 * y + c;
    });

    im.for_each_element([&](const int *pos) {
        int x = pos[0], y = pos[1], c = pos[2];
        int correct = 10 * x + 5 * y + c;
        if (im(x, y, c) != correct) {
            printf("im(%d, %d, %d) = %d instead of %d\n",
                   x, y, c, im(x, y, c), correct);
            abort();
        }
        im(pos) *= 3;
    });

    im.for_each_element([&](int x, int y) {
        for (int c = 0; c < 3; c++) {
            int correct = (10 * x + 5 * y + c) * 3;
            if (im(x, y, c) != correct) {
                printf("im(%d, %d, %d) = %d instead of %d\n",
                       x, y, c, im(x, y, c), correct);
                abort();
            }
            im(x, y, c) *= 2;
        }
    });

    for (int c = im.dim(2).min(); c < im.dim(2).min() + im.dim(2).extent(); c++) {
        for (int y = im.dim(1).min(); y < im.dim(1).min() + im.dim(1).extent(); y++) {
            for (int x = im.dim(0).min(); x < im.dim(0).min() + im.dim(0).extent(); x++) {
                int correct = (10 * x + 5 * y + c) * 6;
                if (im(x, y, c) != correct) {
                    printf("im(%d, %d, %d) = %d instead of %d\n",
                           x, y, c, im(x, y, c), correct);
                    abort();
                }
                im(x, y, c) *= 2;
            }
        }
    }

    for (int c : im.dim(2)) {
        for (int y : im.dim(1)) {
            for (int x : im.dim(0)) {
                int correct = (10 * x + 5 * y + c) * 12;
                if (im(x, y, c) != correct) {
                    printf("im(%d, %d, %d) = %d instead of %d\n",
                           x, y, c, im(x, y, c), correct);
                    abort();
                }
            }
        }
    }

    // Test a zero-dimensional image too.
    Buffer<int> scalar_im = Buffer<int>::make_scalar();
    scalar_im() = 5;

    // Not sure why you'd ever do this, but it verifies that
    // for_each_element does the right thing even in a corner case.
    scalar_im.for_each_element([&]() { scalar_im()++; });

    if (scalar_im() != 6) {
        printf("scalar_im() == %d instead of 6\n", scalar_im());
        return 1;
    }

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