forked from halide/Halide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.cpp
More file actions
294 lines (240 loc) · 9.22 KB
/
Image.cpp
File metadata and controls
294 lines (240 loc) · 9.22 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "Image.h"
#include "Type.h"
#include "Util.h"
#include "Uniform.h"
#include "Var.h"
#include <assert.h>
#include "../src/buffer.h"
namespace Halide {
struct DynImage::Contents {
Contents(const Type &t, int a);
Contents(const Type &t, int a, int b);
Contents(const Type &t, int a, int b, int c);
Contents(const Type &t, int a, int b, int c, int d);
Contents(const Type &t, std::vector<int> sizes);
~Contents();
void allocate(size_t bytes);
Type type;
std::vector<int> size, stride;
const std::string name;
unsigned char *data;
std::vector<unsigned char> host_buffer;
buffer_t buf;
mutable void (*copyToHost)(buffer_t*);
mutable void (*freeBuffer)(buffer_t*);
};
DynImage::Contents::Contents(const Type &t, int a) :
type(t), size{a}, stride{1}, name(uniqueName('i')), copyToHost(NULL), freeBuffer(NULL) {
assert(a > 0 && "Images must have positive sizes\n");
allocate(a * (t.bits/8));
}
DynImage::Contents::Contents(const Type &t, int a, int b) :
type(t), size{a, b}, stride{1, a}, name(uniqueName('i')), copyToHost(NULL), freeBuffer(NULL) {
assert(a > 0 && b > 0 && "Images must have positive sizes");
allocate(a * b * (t.bits/8));
}
DynImage::Contents::Contents(const Type &t, int a, int b, int c) :
type(t), size{a, b, c}, stride{1, a, a*b}, name(uniqueName('i')), copyToHost(NULL), freeBuffer(NULL) {
assert(a > 0 && b > 0 && c > 0 && "Images must have positive sizes");
allocate(a * b * c * (t.bits/8));
}
DynImage::Contents::Contents(const Type &t, int a, int b, int c, int d) :
type(t), size{a, b, c, d}, stride{1, a, a*b, a*b*c}, name(uniqueName('i')), copyToHost(NULL), freeBuffer(NULL) {
assert(a > 0 && b > 0 && c > 0 && d > 0 && "Images must have positive sizes");
allocate(a * b * c * d * (t.bits/8));
}
DynImage::Contents::Contents(const Type &t, std::vector<int> sizes) :
type(t), size(sizes), stride(sizes.size()), name(uniqueName('i')), copyToHost(NULL), freeBuffer(NULL) {
size_t total = 1;
for (size_t i = 0; i < sizes.size(); i++) {
assert(sizes[i] > 0 && "Images must have positive sizes");
stride[i] = total;
total *= sizes[i];
}
allocate(total * (t.bits/8));
}
DynImage::Contents::~Contents() {
if (freeBuffer) {
fprintf(stderr, "freeBuffer %p\n", &buf);
freeBuffer(&buf);
}
}
void DynImage::Contents::allocate(size_t bytes) {
host_buffer.resize(bytes+16);
data = &(host_buffer[0]);
unsigned char offset = ((size_t)data) & 0xf;
if (offset) {
data += 16 - offset;
}
assert(size.size() <= 4);
buf.host = data;
buf.dev = 0;
buf.host_dirty = false;
buf.dev_dirty = false;
buf.dims[0] = buf.dims[1] = buf.dims[2] = buf.dims[3] = 1;
for (size_t i = 0; i < size.size(); i++) {
buf.dims[i] = size[i];
}
buf.elem_size = type.bits/8;
}
DynImage::DynImage(const Type &t, int a) : contents(new Contents(t, a)) {}
DynImage::DynImage(const Type &t, int a, int b) : contents(new Contents(t, a, b)) {}
DynImage::DynImage(const Type &t, int a, int b, int c) : contents(new Contents(t, a, b, c)) {}
DynImage::DynImage(const Type &t, int a, int b, int c, int d) : contents(new Contents(t, a, b, c, d)) {}
DynImage::DynImage(const Type &t, std::vector<int> sizes) : contents(new Contents(t, sizes)) {}
DynImage::DynImage(const DynImage &other) : contents(other.contents) {}
const Type &DynImage::type() const {
return contents->type;
}
int DynImage::stride(int i) const {
if (i >= dimensions()) {
fprintf(stderr,
"ERROR: accessing stride of dim %d of %d-dimensional image %s\n",
i, dimensions(), name().c_str());
assert(i < dimensions());
}
return contents->stride[i];
}
int DynImage::size(int i) const {
if (i >= dimensions()) {
fprintf(stderr,
"ERROR: accessing size of dim %d of %d-dimensional image %s\n",
i, dimensions(), name().c_str());
assert(i < dimensions());
}
return contents->size[i];
}
int DynImage::dimensions() const {
return contents->size.size();
}
unsigned char *DynImage::data() const {
return contents->data;
}
const std::string &DynImage::name() const {
return contents->name;
}
buffer_t* DynImage::buffer() const {
return &contents->buf;
}
void DynImage::setRuntimeHooks(void (*copyToHostFn)(buffer_t *), void (*freeFn)(buffer_t *)) const {
contents->copyToHost = copyToHostFn;
contents->freeBuffer = freeFn;
}
void DynImage::copyToHost() const {
// printf("%p->copyToHost...", this);
if (contents->buf.dev_dirty) {
// printf("runs\n");
assert(contents->copyToHost);
contents->copyToHost(&contents->buf);
} else {
// printf("skipped - not dirty\n");
}
}
void DynImage::copyToDev() const {
if (contents->buf.host_dirty) {
// TODO
assert(false);
}
}
void DynImage::markHostDirty() const {
assert(!contents->buf.dev_dirty);
contents->buf.host_dirty = true;
}
void DynImage::markDevDirty() const {
assert(!contents->buf.host_dirty);
contents->buf.dev_dirty = true;
}
bool DynImage::hostDirty() const {
return contents->buf.host_dirty;
}
bool DynImage::devDirty() const {
return contents->buf.dev_dirty;
}
Expr DynImage::operator()(const Expr &a) const {
return ImageRef(*this, {a});
}
Expr DynImage::operator()(const Expr &a, const Expr &b) const {
return ImageRef(*this, {a, b});
}
Expr DynImage::operator()(const Expr &a, const Expr &b, const Expr &c) const {
return ImageRef(*this, {a, b, c});
}
Expr DynImage::operator()(const Expr &a, const Expr &b, const Expr &c, const Expr &d) const {
return ImageRef(*this, {a, b, c, d});
}
struct UniformImage::Contents {
Contents(const Type &t, int dims) :
t(t), name(uniqueName('m')) {
sizes.resize(dims);
for (int i = 0; i < dims; i++) {
sizes[i] = Var(std::string(".") + name + ".dim." + int_to_str(i)); // Connelly: std::ostringstream broken in Python binding, use string + instead
}
}
Contents(const Type &t, int dims, const std::string &name) :
t(t), name(name) {
sizes.resize(dims);
for (int i = 0; i < dims; i++) {
sizes[i] = Var(std::string(".") + name + ".dim." + int_to_str(i)); // Connelly: std::ostringstream broken in Python binding, use string + instead
}
}
Type t;
std::unique_ptr<DynImage> image;
std::vector<Expr> sizes;
const std::string name;
};
UniformImage::UniformImage(const Type &t, int dims) :
contents(new Contents(t, dims)) {
for (int i = 0; i < dims; i++) {
contents->sizes[i].child(*this);
}
}
UniformImage::UniformImage(const Type &t, int dims, const std::string &name) :
contents(new Contents(t, dims, name)) {
for (int i = 0; i < dims; i++) {
contents->sizes[i].child(*this);
}
}
UniformImage::UniformImage(const UniformImage &other) :
contents(other.contents) {
}
void UniformImage::operator=(const DynImage &image) {
assert(image.type() == contents->t);
assert((size_t)image.dimensions() == contents->sizes.size());
contents->image.reset(new DynImage(image));
}
const DynImage &UniformImage::boundImage() const {
assert(contents->image);
return *(contents->image);
}
unsigned char *UniformImage::data() const {
assert(contents->image);
return contents->image->data();
}
bool UniformImage::operator==(const UniformImage &other) const {
return contents == other.contents;
}
Expr UniformImage::operator()(const Expr &a) const {
return UniformImageRef(*this, {a});
}
Expr UniformImage::operator()(const Expr &a, const Expr &b) const {
return UniformImageRef(*this, {a, b});
}
Expr UniformImage::operator()(const Expr &a, const Expr &b, const Expr &c) const {
return UniformImageRef(*this, {a, b, c});
}
Expr UniformImage::operator()(const Expr &a, const Expr &b, const Expr &c, const Expr &d) const {
return UniformImageRef(*this, {a, b, c, d});
}
Type UniformImage::type() const {
return contents->t;
}
const std::string &UniformImage::name() const {
return contents->name;
}
int UniformImage::dimensions() const {
return contents->sizes.size();
}
const Expr &UniformImage::size(int i) const {
return contents->sizes[i];
}
}