-
Notifications
You must be signed in to change notification settings - Fork 5
/
mapping_cpu.impala
215 lines (189 loc) · 10.1 KB
/
mapping_cpu.impala
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
fn @set_pixel_fn[T](img: Img) = @|idx: i32, val: T| bitcast[&mut[T]](img.buf.data)(idx) = val;
fn @get_pixel_fn[T](img: Img) = @|idx: i32| bitcast[&[T]](img.buf.data)(idx);
fn @set_pixel_memory_fn[T](buf: Buffer) = @|idx: i32, val: T| bitcast[&mut[T]](buf.data)(idx) = val;
fn @get_pixel_memory_fn[T](buf: Buffer) = @|idx: i32| bitcast[&[T]](buf.data)(idx);
fn @iteration[T](body: StencilFn[T]) = @|out: Img, imgs: ImgList, bh_lower: BoundaryFn[T], bh_upper: BoundaryFn[T]| {
let out_acc = get_acc[T](out, set_pixel_fn[T](out), get_pixel_fn[T](out));
let accs = for img, (ext_x, ext_y) in img_list2acc_map[T](imgs) {
if ext_x == 0 && ext_y == 0 {
get_acc[T](img, set_pixel_fn[T](img), get_pixel_fn[T](img))
} else {
get_acc_bh[T](img, set_pixel_fn[T](img), get_pixel_fn[T](img), (Boundary::Unknown, Boundary::Unknown), bh_lower, bh_upper)
}
};
for benchmark_cpu() {
for y in outer_loop(0, out.height) {
for x in inner_loop(0, out.width) {
@body(x, y, out_acc, accs);
}
}
}
};
fn @iteration_bounds[T](body: StencilFn[T]) = @|out: Img, imgs: ImgList, bh_lower: BoundaryFn[T], bh_upper: BoundaryFn[T]| {
let (ex, ey) = get_max_ext(imgs);
for benchmark_cpu() {
for ((bounds_row_lower, bounds_row_upper), (bounds_col_lower, bounds_col_upper)), region in iterate_2dregion((0, 0), (out.width, out.height), (round_up(ex, get_vector_length()), ey)) {
let out_acc = get_acc[T](out, set_pixel_fn[T](out), get_pixel_fn[T](out));
let accs = for img, (ext_x, ext_y) in img_list2acc_map[T](imgs) {
if ext_x == 0 && ext_y == 0 {
get_acc[T](img, set_pixel_fn[T](img), get_pixel_fn[T](img))
} else {
get_acc_bh[T](img, set_pixel_fn[T](img), get_pixel_fn[T](img), region, bh_lower, bh_upper)
}
};
//let outer_loop_fun = if region == Boundary::Center { outer_loop } else { range };
// gets slower when computed in parallel
for y in range(bounds_col_lower, bounds_col_upper) {
for x in inner_loop(bounds_row_lower, bounds_row_upper) {
@body(x, y, out_acc, accs);
}
}
}
}
};
fn @tiled_loop(xl: i32, xu: i32, yl: i32, yu: i32, body: fn(i32, i32) -> ()) -> () {
fn @get_step(lvl: i32) -> i32 { // size -> 2048 -> 1
if lvl == 0 { 2048 } else { 1 }
}
let x_upper = xu;
let x_lower = xl;
fn @(?cur_lvl) tile(cur_lvl: i32, xl: i32, xu: i32, yl: i32, yu: i32) -> () {
let step = get_step(cur_lvl);
pe_info[i32]("step size", step);
if step == 1 {
for y in range(yl, yu) {
for x in range(xl, min(xu, x_upper)) {
@body(x, y);
}
}
} else {
let step = if xl == x_lower { step - xl } else { step };
for x in range_step(xl, xu, step) {
tile(cur_lvl + 1, x, x + step, yl, yu);
}
}
}
tile(0, xl, xu, yl, yu)
}
fn @iteration_advanced[T](body: StencilFn[T]) = @|out: Img, imgs: ImgList, bh_lower: BoundaryFn[T], bh_upper: BoundaryFn[T]| {
let (ex, ey) = get_max_ext(imgs);
for benchmark_cpu() {
for ((bounds_row_lower, bounds_row_upper), (bounds_col_lower, bounds_col_upper)), region in iterate_2dregion((0, 0), (out.width, out.height), (round_up(ex, get_vector_length()), ey)) {
let out_acc = get_acc[T](out, set_pixel_fn[T](out), get_pixel_fn[T](out));
let accs = for img, (ext_x, ext_y) in img_list2acc_map[T](imgs) {
if ext_x == 0 && ext_y == 0 {
get_acc[T](img, set_pixel_fn[T](img), get_pixel_fn[T](img))
} else {
get_acc_bh[T](img, set_pixel_fn[T](img), get_pixel_fn[T](img), region, bh_lower, bh_upper)
}
};
//let outer_loop_fun = if region == Boundary::Center { outer_loop } else { range };
// gets slower when computed in parallel
match region {
(Boundary::Center, Boundary::Center) => tiled_loop(bounds_row_lower, bounds_row_upper, bounds_col_lower, bounds_col_upper, @|x, y| @body(x, y, out_acc, accs)),
_ =>
for y in range(bounds_col_lower, bounds_col_upper) {
for x in inner_loop(bounds_row_lower, bounds_row_upper) {
@body(x, y, out_acc, accs);
}
}
}
}
}
};
fn @iteration_sep[T](body: StencilSepFn[T]) = @|out: Img, arr: Img, mask_row: MaskSep, mask_col: MaskSep, bh_lower: BoundaryFn[T], bh_upper: BoundaryFn[T]| {
// allocate temporary array
let tmp = alloc_img[T](out, alloc_cpu);
let out_acc = get_acc[T](out, set_pixel_fn[T](out), get_pixel_fn[T](out));
let tmp_acc_w = get_acc[T](tmp, set_pixel_fn[T](tmp), get_pixel_fn[T](tmp));
let tmp_acc_r = get_acc_bh[T](tmp, set_pixel_fn[T](tmp), get_pixel_fn[T](tmp), (Boundary::Unknown, Boundary::Unknown), bh_lower, bh_upper);
let arr_acc = get_acc_bh[T](arr, set_pixel_fn[T](arr), get_pixel_fn[T](arr), (Boundary::Unknown, Boundary::Unknown), bh_lower, bh_upper);
for benchmark_cpu() {
for y in outer_loop(0, out.height) {
for x in inner_loop(0, out.width) {
let is_row = false;
@body(x, y, tmp_acc_w, arr_acc, mask_col, is_row);
}
}
for y in outer_loop(0, out.height) {
for x in inner_loop(0, out.width) {
let is_row = true;
@body(x, y, out_acc, tmp_acc_r, mask_row, is_row);
}
}
}
release(tmp.buf);
};
fn @iteration_sep_bounds[T](body: StencilSepFn[T]) = @|out: Img, arr: Img, mask_row: MaskSep, mask_col: MaskSep, bh_lower: BoundaryFn[T], bh_upper: BoundaryFn[T]| {
// allocate temporary array
let tmp = alloc_img[T](out, alloc_cpu);
// compute the number of vector strides for boundary handling
let bhy = mask_col.size / 2;
let bhx = round_up(mask_row.size / 2, get_vector_length());
for benchmark_cpu() {
for (bounds_lower, bounds_upper), boundary_col in iterate_1dregion(0, out.height, bhy) {
let region = (Boundary::Center, boundary_col);
let arr_acc = get_acc_bh[T](arr, set_pixel_fn[T](arr), get_pixel_fn[T](arr), region, bh_lower, bh_upper);
let tmp_acc = get_acc[T](tmp, set_pixel_fn[T](tmp), get_pixel_fn[T](tmp));
let outer_loop_fun = match boundary_col { Boundary::Center => outer_loop, _ => range };
for y in outer_loop_fun(bounds_lower, bounds_upper) {
for x in inner_loop(0, out.width) {
let is_row = false;
@body(x, y, tmp_acc, arr_acc, mask_col, is_row);
}
}
}
for (bounds_lower, bounds_upper), boundary_row in iterate_1dregion(0, out.width, bhx) {
let region = (boundary_row, Boundary::Center);
let tmp_acc = get_acc_bh[T](tmp, set_pixel_fn[T](tmp), get_pixel_fn[T](tmp), region, bh_lower, bh_upper);
let out_acc = get_acc[T](out, set_pixel_fn[T](out), get_pixel_fn[T](out));
let outer_loop_fun = match boundary_row { Boundary::Center => outer_loop, _ => range };
for y in outer_loop_fun(0, out.height) {
for x in inner_loop(bounds_lower, bounds_upper) {
let is_row = true;
@body(x, y, out_acc, tmp_acc, mask_row, is_row);
}
}
}
}
release(tmp.buf);
};
fn @iteration_sep_advanced[T](body: StencilSepFn[T]) = @|out: Img, arr: Img, mask_row: MaskSep, mask_col: MaskSep, bh_lower: BoundaryFn[T], bh_upper: BoundaryFn[T]| {
// compute the number of vector strides for boundary handling
let coarsening_factor = 1;
let bhy = round_up(mask_col.size / 2, coarsening_factor);
let bhx = round_up(mask_row.size / 2, get_vector_length());
for benchmark_cpu() {
for (bounds_col_lower, bounds_col_upper), boundary_col in iterate_1dregion(0, out.height, bhy) {
let region_col = (Boundary::Center, boundary_col);
let (outer_col, inner_col) = match boundary_col { Boundary::Center => (outer_loop_step, inner_loop), _ => (range_step, range) };
for y in outer_col(bounds_col_lower, bounds_col_upper, coarsening_factor) {
// allocate temporary array per thread
let tmp = alloc_cpu((out.width * coarsening_factor) as i64 * sizeof[T]());
for x in inner_col(0, out.width) {
let is_row = false;
// index space: cache line
let arr_acc = get_acc_bh_offset[T](arr, set_pixel_fn[T](arr), get_pixel_fn[T](arr), 0, y, region_col, bh_lower, bh_upper);
let tmp_acc = get_acc_memory[T](set_pixel_memory_fn[T](tmp), get_pixel_memory_fn[T](tmp), out.width, coarsening_factor);
for i in unroll(0, coarsening_factor) {
@body(x, i, tmp_acc, arr_acc, mask_col, is_row);
}
}
for (bounds_row_lower, bounds_row_upper), boundary_row in iterate_1dregion(0, out.width, bhx) {
let region_row = (boundary_row, Boundary::Center);
let inner_row = match boundary_row { Boundary::Center => inner_loop, _ => range };
for x in inner_row(bounds_row_lower, bounds_row_upper) {
let is_row = true;
// index space: cache line
let tmp_acc = get_acc_bh_memory[T](set_pixel_memory_fn[T](tmp), get_pixel_memory_fn[T](tmp), out.width, coarsening_factor, region_row, bh_lower, bh_upper);
let out_acc = get_acc_offset[T](out, set_pixel_fn[T](out), get_pixel_fn[T](out), 0, y);
for i in unroll(0, coarsening_factor) {
@body(x, i, out_acc, tmp_acc, mask_row, is_row);
}
}
}
release(tmp);
}
}
}
};