-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBoundaryConditions.cpp
More file actions
228 lines (189 loc) · 8.92 KB
/
BoundaryConditions.cpp
File metadata and controls
228 lines (189 loc) · 8.92 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
#include "BoundaryConditions.h"
namespace Halide {
namespace BoundaryConditions {
Func repeat_edge(const Func &source,
const Region &bounds) {
std::vector<Var> args(source.args());
user_assert(args.size() >= bounds.size())
<< "repeat_edge called with more bounds (" << bounds.size()
<< ") than dimensions (" << args.size()
<< ") Func " << source.name() << " has.\n";
std::vector<Expr> actuals;
for (size_t i = 0; i < bounds.size(); i++) {
const Var &arg_var = args[i];
Expr min = bounds[i].min;
Expr extent = bounds[i].extent;
if (min.defined() && extent.defined()) {
actuals.push_back(clamp(likely(arg_var), min, min + extent - 1));
} else if (!min.defined() && !extent.defined()) {
actuals.push_back(arg_var);
} else {
user_error << "Partially undefined bounds for dimension " << arg_var
<< " of Func " << source.name() << "\n";
}
}
// If there were fewer bounds than dimensions, regard the ones at the end as unbounded.
actuals.insert(actuals.end(), args.begin() + actuals.size(), args.end());
Func bounded("repeat_edge");
bounded(args) = source(actuals);
return bounded;
}
Func constant_exterior(const Func &source, const Tuple &value,
const Region &bounds) {
std::vector<Var> args(source.args());
user_assert(args.size() >= bounds.size())
<< "constant_exterior called with more bounds (" << bounds.size()
<< ") than dimensions (" << args.size()
<< ") Func " << source.name() << " has.\n";
Expr out_of_bounds = Halide::Internal::make_zero(Bool());
for (size_t i = 0; i < bounds.size(); i++) {
const Var &arg_var = args[i];
Expr min = bounds[i].min;
Expr extent = bounds[i].extent;
if (min.defined() && extent.defined()) {
out_of_bounds = (out_of_bounds ||
arg_var < min ||
arg_var >= min + extent);
} else if (min.defined() || extent.defined()) {
user_error << "Partially undefined bounds for dimension " << arg_var
<< " of Func " << source.name() << "\n";
}
}
Func bounded("constant_exterior");
if (value.as_vector().size() > 1) {
std::vector<Expr> def;
def.reserve(value.as_vector().size());
for (size_t i = 0; i < value.as_vector().size(); i++) {
def.push_back(select(out_of_bounds, value[i], likely(repeat_edge(source, bounds)(args)[i])));
}
bounded(args) = Tuple(def);
} else {
bounded(args) = select(out_of_bounds, value[0], likely(repeat_edge(source, bounds)(args)));
}
return bounded;
}
Func constant_exterior(const Func &source, const Expr &value,
const Region &bounds) {
return constant_exterior(source, Tuple(value), bounds);
}
Func repeat_image(const Func &source,
const Region &bounds) {
std::vector<Var> args(source.args());
user_assert(args.size() >= bounds.size())
<< "repeat_image called with more bounds (" << bounds.size()
<< ") than dimensions (" << args.size()
<< ") Func " << source.name() << " has.\n";
std::vector<Expr> actuals;
for (size_t i = 0; i < bounds.size(); i++) {
const Var &arg_var = args[i];
Expr min = bounds[i].min;
Expr extent = bounds[i].extent;
if (min.defined() && extent.defined()) {
Expr coord = arg_var - min; // Enforce zero origin.
coord = coord % extent; // Range is 0 to w-1
coord = coord + min; // Restore correct min
coord = select(arg_var < min || arg_var >= min + extent, coord,
likely(clamp(likely(arg_var), min, min + extent - 1)));
// In the line above, we want loop partitioning to both cause the
// clamp to go away, and also cause the select to go away. For loop
// partitioning to make one of these constructs go away we need one
// of two things to be true:
//
// 1) One arg has a likely intrinsic buried somewhere within it, and
// the other arg doesn't.
// 2) Both args have likely intrinsics, but in one of the args it is
// not within any inner min/max/select node. This is called an
// 'uncaptured' likely.
//
// The issue with this boundary condition is that the true branch of
// the select (coord) may well have a likely within it somewhere
// introduced by a loop tail strategy, so condition 1 doesn't
// hold. To be more robust, we make condition 2 hold, by introducing
// an uncaptured likely to the false branch.
actuals.push_back(coord);
} else if (!min.defined() && !extent.defined()) {
actuals.push_back(arg_var);
} else {
user_error << "Partially undefined bounds for dimension " << arg_var
<< " of Func " << source.name() << "\n";
}
}
// If there were fewer bounds than dimensions, regard the ones at the end as unbounded.
actuals.insert(actuals.end(), args.begin() + actuals.size(), args.end());
Func bounded("repeat_image");
bounded(args) = source(actuals);
return bounded;
}
Func mirror_image(const Func &source,
const Region &bounds) {
std::vector<Var> args(source.args());
user_assert(args.size() >= bounds.size())
<< "mirror_image called with more bounds (" << bounds.size()
<< ") than dimensions (" << args.size() << ") Func "
<< source.name() << " has.\n";
std::vector<Expr> actuals;
for (size_t i = 0; i < bounds.size(); i++) {
const Var &arg_var = args[i];
Expr min = bounds[i].min;
Expr extent = bounds[i].extent;
if (min.defined() && extent.defined()) {
Expr coord = arg_var - min; // Enforce zero origin.
coord = coord % (2 * extent); // Range is 0 to 2w-1
coord = select(coord >= extent, 2 * extent - 1 - coord, coord); // Range is -w+1, w
coord = coord + min; // Restore correct min
coord = clamp(coord, min, min + extent - 1);
coord = select(arg_var < min || arg_var >= min + extent, coord,
likely(clamp(likely(arg_var), min, min + extent - 1)));
actuals.push_back(coord);
} else if (!min.defined() && !extent.defined()) {
actuals.push_back(arg_var);
} else {
user_error << "Partially undefined bounds for dimension " << arg_var
<< " of Func " << source.name() << "\n";
}
}
// If there were fewer bounds than dimensions, regard the ones at the end as unbounded.
actuals.insert(actuals.end(), args.begin() + actuals.size(), args.end());
Func bounded("mirror_image");
bounded(args) = source(actuals);
return bounded;
}
Func mirror_interior(const Func &source,
const Region &bounds) {
std::vector<Var> args(source.args());
user_assert(args.size() >= bounds.size())
<< "mirror_interior called with more bounds (" << bounds.size()
<< ") than dimensions (" << args.size()
<< ") Func " << source.name() << " has.\n";
std::vector<Expr> actuals;
for (size_t i = 0; i < bounds.size(); i++) {
const Var &arg_var = args[i];
Expr min = bounds[i].min;
Expr extent = bounds[i].extent;
if (min.defined() && extent.defined()) {
Expr limit = extent - 1;
Expr coord = arg_var - min; // Enforce zero origin.
coord = coord % (2 * limit); // Range is 0 to 2w-1
coord = coord - limit; // Range is -w, w
coord = abs(coord); // Range is 0, w
coord = limit - coord; // Range is 0, w
coord = coord + min; // Restore correct min
// The boundary condition probably doesn't apply
coord = select(arg_var < min || arg_var >= min + extent, coord,
likely(clamp(likely(arg_var), min, min + extent - 1)));
actuals.push_back(coord);
} else if (!min.defined() && !extent.defined()) {
actuals.push_back(arg_var);
} else {
user_error << "Partially undefined bounds for dimension " << arg_var
<< " of Func " << source.name() << "\n";
}
}
// If there were fewer bounds than dimensions, regard the ones at the end as unbounded.
actuals.insert(actuals.end(), args.begin() + actuals.size(), args.end());
Func bounded("mirror_interior");
bounded(args) = source(actuals);
return bounded;
}
} // namespace BoundaryConditions
} // namespace Halide