forked from halide/Halide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextern_consumer.cpp
More file actions
116 lines (95 loc) · 2.75 KB
/
extern_consumer.cpp
File metadata and controls
116 lines (95 loc) · 2.75 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
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
#include "Halide.h"
#include <stdio.h>
using namespace Halide;
#ifdef _MSC_VER
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT
#endif
extern "C" DLLEXPORT
int dump_to_file(buffer_t *input, const char *filename,
int desired_min, int desired_extent,
buffer_t *) {
// Note the final output buffer argument is unused.
if (input->host == NULL) {
// Request some range of the input buffer
input->min[0] = desired_min;
input->extent[0] = desired_extent;
} else {
FILE *f = fopen(filename, "w");
// Depending on the schedule, other consumers, etc, Halide may
// have evaluated more than we asked for, so don't assume that
// the min and extents match what we requested.
int *base = ((int *)input->host) - input->min[0];
for (int i = desired_min; i < desired_min + desired_extent; i++) {
fprintf(f, "%d\n", base[i]);
}
fclose(f);
}
return 0;
}
bool check_result() {
// Check the right thing happened
const char *correct =
"0\n"
"1\n"
"4\n"
"9\n"
"16\n"
"25\n"
"36\n"
"49\n"
"64\n"
"81\n";
FILE *f = fopen("halide_test_extern_consumer.txt", "r");
char result[1024];
size_t bytes_read = fread(&result[0], 1, 1023, f);
result[bytes_read] = 0;
fclose(f);
if (strncmp(result, correct, 1023)) {
printf("Incorrect output: %s\n", result);
return false;
}
return true;
}
int main(int argc, char **argv) {
// Define a pipeline that dumps some squares to a file using an
// external consumer stage.
Func source;
Var x;
source(x) = x*x;
Param<int> min, extent;
Param<const char *> filename;
Func sink;
std::vector<ExternFuncArgument> args;
args.push_back(source);
args.push_back(filename);
args.push_back(min);
args.push_back(extent);
sink.define_extern("dump_to_file", args, Int(32), 0);
source.compute_root();
sink.compile_jit();
// Dump the first 10 squares to a file
filename.set("halide_test_extern_consumer.txt");
min.set(0);
extent.set(10);
sink.realize();
if (!check_result())
return -1;
// Test ImageParam ExternFuncArgument via passed in image.
Image<int32_t> buf = source.realize(10);
ImageParam passed_in(Int(32), 1);
passed_in.set(buf);
Func sink2;
std::vector<ExternFuncArgument> args2;
args2.push_back(passed_in);
args2.push_back(filename);
args2.push_back(min);
args2.push_back(extent);
sink2.define_extern("dump_to_file", args2, Int(32), 0);
sink2.realize();
if (!check_result())
return -1;
printf("Success!\n");
return 0;
}