forked from halide/Halide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc_lifetime.cpp
More file actions
75 lines (57 loc) · 1.64 KB
/
func_lifetime.cpp
File metadata and controls
75 lines (57 loc) · 1.64 KB
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
#include <stdio.h>
#include "Halide.h"
#include <iostream>
using namespace Halide;
bool validate(const Image<float> &im, float add)
{
// Check the result was what we expected
for (int i = 0; i < im.width(); i++) {
for (int j = 0; j < im.height(); j++) {
float correct = i*j + add;
if (fabs(im(i, j) - correct) > 0.001f) {
printf("im[%d, %d] = %f instead of %f\n", i, j, im(i, j), correct);
return false;
}
}
}
return true;
}
int main(int argc, char **argv) {
Var x("x"), y("y");
Func f("f");
printf("Defining function f...\n");
f(x, y) = x*y + 1.0f;
Target target = get_jit_target_from_environment();
if (target.has_gpu_feature()) {
f.gpu_tile(x, y, 8, 8);
}
{
printf("Realizing function f...\n");
Image<float> imf = f.realize(32, 32, target);
if (!validate(imf, 1.0f)) {
return -1;
}
}
// Create (and destroy) a new function g.
{
Func g("g");
printf("Defining function g...\n");
g(x, y) = x*y + 2.0f;
if (target.has_gpu_feature()) {
g.gpu_tile(x, y, 8, 8);
}
printf("Realizing function g...\n");
Image<float> img = g.realize(32, 32, target);
if (!validate(img, 2.0f)) {
return -1;
}
}
// Try using f again to ensure it is still valid (after g's destruction).
printf("Realizing function f again...\n");
Image<float> imf2 = f.realize(32, 32, target);
if (!validate(imf2, 1.0f)) {
return -1;
}
printf("Success!\n");
return 0;
}