-
Notifications
You must be signed in to change notification settings - Fork 3
/
dynamic_median_filter.cpp
308 lines (278 loc) · 13.9 KB
/
dynamic_median_filter.cpp
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <HalideRuntimeCuda.h>
#include "dynamic_median_filter.h"
#include "dynamic_median_filter_f32_2x2_v2.h"
#include "dynamic_median_filter_f32_4x4_v2.h"
#include "dynamic_median_filter_f32_8x8_v2.h"
#include "dynamic_median_filter_u16_2x2_v2.h"
#include "dynamic_median_filter_u16_4x4_v2.h"
#include "dynamic_median_filter_u16_8x8_v2.h"
#include "dynamic_median_filter_u8_2x2_v2.h"
#include "dynamic_median_filter_u8_4x4_v2.h"
#include "dynamic_median_filter_u8_8x8_v2.h"
#include "dynamic_median_filter_cuda_f32_2x2_v2.h"
#include "dynamic_median_filter_cuda_f32_4x4_v2.h"
#include "dynamic_median_filter_cuda_f32_8x8_v2.h"
#include "dynamic_median_filter_cuda_u16_2x2_v2.h"
#include "dynamic_median_filter_cuda_u16_4x4_v2.h"
#include "dynamic_median_filter_cuda_u16_8x8_v2.h"
#include "dynamic_median_filter_cuda_u8_2x2_v2.h"
#include "dynamic_median_filter_cuda_u8_4x4_v2.h"
#include "dynamic_median_filter_cuda_u8_8x8_v2.h"
#include "sorting_bytecode.h"
#include <map>
using std::map;
using std::tuple;
using std::vector;
using Halide::Runtime::Buffer;
struct SortingProgram {
vector<Instruction> col_sort_instructions, col_merge_instructions;
Buffer<int> col_sort_instructions_buf, col_merge_instructions_buf;
int working_mem = 0;
int tw = 0, th = 0;
bool initialized = false;
};
SortingProgram &prepare_program(int radius, bool use_cuda, int max_leaf, bool verbose) {
static map<tuple<int, int, bool>, SortingProgram> instruction_cache;
auto &e = instruction_cache[{radius, max_leaf, use_cuda}];
if (!e.initialized) {
e.initialized = true;
// The tile size.
const int tw = (radius >= 30 && !use_cuda) ? 8 :
radius >= 7 ? 4 :
2;
const int th = tw;
const int diameter = radius * 2 + 1;
// First we must construct the sorting networks we're going to
// use, and then encode that into instructions for a sorting
// network interpreter. Constructing this program is outside
// the loop over pixels, and we can assume this gets cached
// per-radius, so we leave it out of the benchmark
vector<Op> col_sort_network = sort_1d(diameter - th + 1, 0, diameter - th);
// The network that extracts one tile's worth of medians from the sorted column pairs
vector<Op> col_merge_network = tiled_median_2d(diameter, diameter, tw, th);
if (verbose) {
print_op_histogram(col_merge_network);
}
// The table of instructions we're going to use. Must be kept in sync with the generator.
auto table = get_instruction_table(max_leaf);
// Trim the program down to instructions that fit within the limit
col_merge_network = limit_max_op_length(col_merge_network, max_leaf);
col_sort_network = limit_max_op_length(col_sort_network, max_leaf);
// Compute how much working memory the interpreter will need
int working_mem = compute_working_memory(col_merge_network);
// printf("Working memory: %d\n", working_mem);
// Encode the instructions into bytecode
for (Op op : col_sort_network) {
if (op.op == AssertSorted ||
op.op == Print) {
continue;
}
e.col_sort_instructions.push_back(encode(op, table));
}
for (Op op : col_merge_network) {
if (op.op == AssertSorted ||
op.op == Print) {
continue;
}
e.col_merge_instructions.push_back(encode(op, table));
}
if (verbose) {
PerformanceCounters col_sort_perf = estimate_performance(col_sort_network);
PerformanceCounters col_merge_perf = estimate_performance(col_merge_network);
printf("Col sort counters: instructions: %d loads: %d stores: %d swaps: %d\n",
(int)e.col_sort_instructions.size(),
col_sort_perf.loads, col_sort_perf.stores, col_sort_perf.swaps);
printf("Col merge counters: instructions: %d loads: %d stores: %d swaps: %d\n",
(int)e.col_merge_instructions.size(),
col_merge_perf.loads, col_merge_perf.stores, col_merge_perf.swaps);
double instructions_per_pixel = (double)e.col_sort_instructions.size() / th + (double)e.col_merge_instructions.size() / (tw * th);
double loads_per_pixel = (double)col_sort_perf.loads / th + (double)col_merge_perf.loads / (tw * th);
double stores_per_pixel = (double)col_sort_perf.stores / th + (double)col_merge_perf.stores / (tw * th);
double swaps_per_pixel = (double)col_sort_perf.swaps / th + (double)col_merge_perf.swaps / (tw * th);
printf("Total per-pixel instructions: %.2f loads: %.2f stores: %.2f swaps: %.2f\n",
instructions_per_pixel,
loads_per_pixel,
stores_per_pixel,
swaps_per_pixel);
vector<Op> combined = col_sort_network;
combined.insert(combined.end(), col_merge_network.begin(), col_merge_network.end());
print_op_histogram(combined);
}
e.col_merge_instructions_buf =
Buffer<int>((int *)e.col_merge_instructions.data(),
sizeof(e.col_merge_instructions[0]) / sizeof(int),
(int)e.col_merge_instructions.size());
e.col_sort_instructions_buf =
Buffer<int>((int *)e.col_sort_instructions.data(),
sizeof(e.col_sort_instructions[0]) / sizeof(int),
(int)e.col_sort_instructions.size());
e.col_sort_instructions_buf.set_host_dirty();
e.col_merge_instructions_buf.set_host_dirty();
e.tw = tw;
e.th = th;
e.working_mem = working_mem;
}
return e;
}
int leaf_size(int max_leaf, bool use_cuda) {
if (max_leaf > 0) {
return max_leaf;
} else if (use_cuda) {
return 24;
} else {
return 14;
}
}
void dynamic_median_filter(Halide::Runtime::Buffer<const uint8_t> src,
int radius,
Halide::Runtime::Buffer<uint8_t> &dst,
bool use_cuda,
int max_leaf,
bool verbose) {
auto &p = prepare_program(radius, use_cuda, leaf_size(max_leaf, use_cuda), verbose);
if (use_cuda) {
if (p.tw == 2 && p.th == 2) {
dynamic_median_filter_cuda_u8_2x2_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
} else if (p.tw == 4 && p.th == 4) {
dynamic_median_filter_cuda_u8_4x4_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
} else {
assert(p.tw == 8 && p.th == 8);
dynamic_median_filter_cuda_u8_8x8_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
}
} else {
if (p.tw == 2 && p.th == 2) {
dynamic_median_filter_u8_2x2_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
} else if (p.tw == 4 && p.th == 4) {
dynamic_median_filter_u8_4x4_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
} else {
assert(p.tw == 8 && p.th == 8);
dynamic_median_filter_u8_8x8_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
}
}
}
void dynamic_median_filter(Halide::Runtime::Buffer<const uint16_t> src,
int radius,
Halide::Runtime::Buffer<uint16_t> &dst,
bool use_cuda,
int max_leaf,
bool verbose) {
auto &p = prepare_program(radius, use_cuda, leaf_size(max_leaf, use_cuda), verbose);
if (use_cuda) {
if (p.tw == 2 && p.th == 2) {
dynamic_median_filter_cuda_u16_2x2_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
} else if (p.tw == 4 && p.th == 4) {
dynamic_median_filter_cuda_u16_4x4_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
} else {
assert(p.tw == 8 && p.th == 8);
dynamic_median_filter_cuda_u16_8x8_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
}
} else {
if (p.tw == 2 && p.th == 2) {
dynamic_median_filter_u16_2x2_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
} else if (p.tw == 4 && p.th == 4) {
dynamic_median_filter_u16_4x4_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
} else {
assert(p.tw == 8 && p.th == 8);
dynamic_median_filter_u16_8x8_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
}
}
}
void dynamic_median_filter(Halide::Runtime::Buffer<const float> src,
int radius,
Halide::Runtime::Buffer<float> &dst,
bool use_cuda,
int max_leaf,
bool verbose) {
auto &p = prepare_program(radius, use_cuda, leaf_size(max_leaf, use_cuda), verbose);
if (use_cuda) {
if (p.tw == 2 && p.th == 2) {
dynamic_median_filter_cuda_f32_2x2_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
} else if (p.tw == 4 && p.th == 4) {
dynamic_median_filter_cuda_f32_4x4_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
} else {
assert(p.tw == 8 && p.th == 8);
dynamic_median_filter_cuda_f32_8x8_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
}
} else {
if (p.tw == 2 && p.th == 2) {
dynamic_median_filter_f32_2x2_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
} else if (p.tw == 4 && p.th == 4) {
dynamic_median_filter_f32_4x4_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
} else {
assert(p.tw == 8 && p.th == 8);
dynamic_median_filter_f32_8x8_v2(radius,
p.col_sort_instructions_buf,
p.col_merge_instructions_buf,
p.working_mem,
src, dst);
}
}
}