File: param.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 (167 lines) | stat: -rw-r--r-- 4,984 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
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
#include "Halide.h"
#include <stdio.h>

using namespace Halide;

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

    Var x("x");

    {
        Func f("f");

        Param<int> u;
        Param<int> u_name("u_name");

        f(x) = u;

        Target target = get_jit_target_from_environment();
        if (target.has_gpu_feature()) {
            Var xo, xi;
            f.gpu_tile(x, xo, xi, 256);
        } else if (target.has_feature(Target::HVX)) {
            f.hexagon().vectorize(x, 32);
        }

        u.set(17);
        u.set_estimate(17);
        Buffer<int> out_17 = f.realize({1024}, target);

        // verify the get method.
        assert(u.get() == 17);

        // Copied Params should still refer to the same underlying Parameter,
        // so setting the copy should be equivalent to setting the original.
        Param<int> u_alias = u;
        u_alias.set(123);
        u_alias.set_estimate(123);
        Buffer<int> out_123 = f.realize({1024}, target);

        // verify the get method, again.
        assert(u.get() == 123);

        for (int i = 0; i < 1024; i++) {
            if (out_17(i) != 17 || out_123(i) != 123) {
                printf("Failed!\n");
                for (int i = 0; i < 1024; i++) {
                    printf("%d %d\n", out_17(i), out_123(i));
                }
                return 1;
            }
        }
    }

    // Now the same tests, but with Param types specified at runtime
    {
        Func f("f");

        Param<> u(Int(32));
        Param<> u_name(Int(32), "u_name");

        f(x) = u;

        Target target = get_jit_target_from_environment();
        if (target.has_gpu_feature()) {
            Var xo, xi;
            f.gpu_tile(x, xo, xi, 256);
        } else if (target.has_feature(Target::HVX)) {
            f.hexagon().vectorize(x, 32);
        }

        // For Param<void>, you must provide an explicit template argument to set(),
        // and it must match the dynamic type of the Param.
        u.set<int32_t>(17);
        u.set_estimate<int32_t>(17);
        Buffer<int32_t> out_17 = f.realize({1024}, target);

        // For Param<void>, you must provide an explicit template argument to get(),
        // and it must match the dynamic type of the Param.
        assert(u.get<int32_t>() == 17);

        // This would fail with a user_assert inside the get() method
        // assert(u.get<int16_t>() == 17);

        // Copied Params should still refer to the same underlying Parameter,
        // so setting the copy should be equivalent to setting the original.
        Param<> u_alias = u;
        u_alias.set(123);
        u_alias.set_estimate(123);
        Buffer<int32_t> out_123 = f.realize({1024}, target);

        assert(u.get<int32_t>() == 123);

        for (int i = 0; i < 1024; i++) {
            if (out_17(i) != 17 || out_123(i) != 123) {
                printf("Failed!\n");
                for (int i = 0; i < 1024; i++) {
                    printf("%d %d\n", out_17(i), out_123(i));
                }
                return 1;
            }
        }
    }

    // Test copy ctor between void and non-void Params
    {
        Param<int32_t> u;

        Func f("f");
        f(x) = u;

        u.set(17);
        u.set_estimate(17);
        Buffer<int32_t> out_17 = f.realize({1});
        assert(out_17(0) == 17);

        // You can always construct a Param<void> from a Param<nonvoid>
        Param<> u_alias = u;
        u_alias.set(123);
        u_alias.set_estimate(123);
        Buffer<int32_t> out_123 = f.realize({1});
        assert(out_123(0) == 123);

        // You can also construct Param<nonvoid> from Param<void>,
        // but only if the runtime type of the RHS matches the static type
        // of the LHS (otherwise, assert-fails)
        Param<int32_t> u_alias2 = u_alias;
        u_alias2.set(124);
        u_alias2.set_estimate(124);
        Buffer<int32_t> out_124 = f.realize({1});
        assert(out_124(0) == 124);
    }

    // Test operator= between void and non-void Params
    {
        Param<int32_t> u;

        Func f("f");
        f(x) = u;

        u.set(17);
        u.set_estimate(17);
        Buffer<int32_t> out_17 = f.realize({1});
        assert(out_17(0) == 17);

        // You can always do Param<void> = Param<nonvoid> (LHS takes on type of RHS)
        Param<> u_alias(Float(64));
        u_alias = u;
        assert(u_alias.type() == Int(32));
        u_alias.set(123);
        u_alias.set_estimate(123);
        Buffer<int32_t> out_123 = f.realize({1});
        assert(out_123(0) == 123);

        // You can also do Param<nonvoid> = Param<void>,
        // but only if the runtime type of the RHS matches the static type
        // of the LHS (otherwise, assert-fails)
        Param<int32_t> u_alias2;
        u_alias2 = u_alias;
        u_alias2.set(124);
        u_alias2.set_estimate(124);
        Buffer<int32_t> out_124 = f.realize({1});
        assert(out_124(0) == 124);
    }

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