forked from hxmhuang/OpenArray_Dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInternal.hpp
More file actions
305 lines (249 loc) · 7.61 KB
/
Copy pathInternal.hpp
File metadata and controls
305 lines (249 loc) · 7.61 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
295
296
297
298
299
300
301
302
303
304
305
#ifndef __INTERNAL_HPP__
#define __INTERNAL_HPP__
#include <random>
#include "common.hpp"
#include "Box.hpp"
#include "utils/utils.hpp"
#include <vector>
// extern "C"{
// void tic(const char* s);
// void toc(const char* s);
// void show_time(const char* s);
// void show_all();
// }
using namespace std;
namespace oa {
namespace internal {
//int calc_id(int i, int j, int k, int3 S);
template <typename T>
void set_buffer_consts(T *buffer, int size, T val) {
for (int i = 0; i < size; i++) buffer[i] = val;
}
template <typename T>
void set_buffer_local(T* sub_buffer, const Box& box, int x, int y, int z, T val, int sw) {
Shape sp = box.shape_with_stencil(sw);
int M = sp[0];
int N = sp[1];
int P = sp[2];
int cnt = (z + sw) * M * N + (y + sw) * M + x + sw;
sub_buffer[cnt] = val;
}
template <typename T>
T get_buffer_local_sub(T* sub_buffer, const Box& box, int x, int y, int z, int sw) {
Shape sp = box.shape_with_stencil(sw);
int M = sp[0];
int N = sp[1];
int P = sp[2];
int cnt = (z + sw) * M * N + (y + sw) * M + x + sw;
return sub_buffer[cnt];
}
//sub_A = sub(A, box)
template <typename T>
void get_buffer_subarray(T *sub_buffer, T *buffer, const Box &sub_box,
const Box &box, int sw) {
Shape sp = box.shape_with_stencil(sw);
int M = sp[0];
int N = sp[1];
int P = sp[2];
Box bd_box = box.boundary_box(sw);
Box ref_box = sub_box.ref_box(bd_box);
int xs, xe, ys, ye, zs, ze;
ref_box.get_corners_with_stencil(xs, xe, ys, ye, zs, ze, sw);
//ref_box.display("ref_box");
int cnt = 0;
for (int k = zs; k < ze; k++) {
for (int j = ys; j < ye; j++) {
#pragma simd
for (int i = xs; i < xe; i++) {
sub_buffer[(k-zs)*(ye-ys)*(xe-xs)+(j-ys)*(xe-xs)+(i-xs)] = buffer[k * M * N + j * M + i];
//cout<<buffer[cnt-1]<<" ";
}
//cout<<endl;
}
//cout<<endl;
}
}
// set sub(A) = B
template<typename T1, typename T2>
void copy_buffer(
T1* A_buf,
const Shape& A_buf_shape,
const Box& A_window,
T2* B_buf,
const Shape& B_buf_shape,
const Box& B_window) {
Shape sp = A_window.shape();
const int M = sp[0];
const int N = sp[1];
const int P = sp[2];
const int M1 = A_buf_shape[0];
const int N1 = A_buf_shape[1];
const int M2 = B_buf_shape[0];
const int N2 = B_buf_shape[1];
int xs1, xe1, ys1, ye1, zs1, ze1;
A_window.get_corners(xs1, xe1, ys1, ye1, zs1, ze1);
int xs2, xe2, ys2, ye2, zs2, ze2;
B_window.get_corners(xs2, xe2, ys2, ye2, zs2, ze2);
//ref_box.display("ref_box");
int cnt = 0;
for (int k = 0; k < P; k++) {
for (int j = 0; j < N; j++) {
#pragma simd
for (int i = 0; i < M; i++) {
A_buf[(k+zs1)*M1*N1 + (j+ys1)*M1 + i + xs1] =
B_buf[(k+zs2)*M2*N2 + (j+ys2)*M2 + i+xs2];
//cout<<buffer[cnt-1]<<" ";
}
//cout<<endl;
}
//cout<<endl;
}
}
template<typename T1, typename T2, typename T3>
void copy_buffer_with_mask(
T1* A_buf,
const Shape& A_buf_shape,
const Box& A_window,
T2* B_buf,
const Shape& B_buf_shape,
const Box& B_window,
T3* C_buf,
const Shape& C_buf_shape,
const Box& C_window,
bool ifscalar_B
) {
Shape sp = A_window.shape();
const int M = sp[0];
const int N = sp[1];
const int P = sp[2];
const int M1 = A_buf_shape[0];
const int N1 = A_buf_shape[1];
const int M2 = C_buf_shape[0];
const int N2 = C_buf_shape[1];
int xs1, xe1, ys1, ye1, zs1, ze1;
A_window.get_corners(xs1, xe1, ys1, ye1, zs1, ze1);
int xs2, xe2, ys2, ye2, zs2, ze2;
C_window.get_corners(xs2, xe2, ys2, ye2, zs2, ze2);
//ref_box.display("ref_box");
if(!ifscalar_B){
int cnt = 0;
for (int k = 0; k < P; k++) {
for (int j = 0; j < N; j++) {
#pragma simd
for (int i = 0; i < M; i++) {
if(C_buf[(k+zs2)*M2*N2 + (j+ys2)*M2 + i+xs2] > 0)// >0
A_buf[(k+zs1)*M1*N1 + (j+ys1)*M1 + i + xs1] =
B_buf[(k+zs2)*M2*N2 + (j+ys2)*M2 + i+xs2];
}
}
}
}
else{
int cnt = 0;
for (int k = 0; k < P; k++) {
for (int j = 0; j < N; j++) {
#pragma simd
for (int i = 0; i < M; i++) {
if(C_buf[(k+zs2)*M2*N2 + (j+ys2)*M2 + i+xs2] > 0)// >0
A_buf[(k+zs1)*M1*N1 + (j+ys1)*M1 + i + xs1] = B_buf[0];
}
}
}
}
}
// set sub(A) = const
template<typename T1, typename T2>
void set_buffer_subarray_const(T1* buffer, T2 val, const Box &box,
const Box &sub_box, int sw) {
Shape sp = box.shape_with_stencil(sw);
int M = sp[0];
int N = sp[1];
int P = sp[2];
Box bd_box = box.boundary_box(sw);
Box ref_box = sub_box.ref_box(bd_box);
int xs, xe, ys, ye, zs, ze;
ref_box.get_corners_with_stencil(xs, xe, ys, ye, zs, ze, sw);
//ref_box.display("ref_box");
for (int k = zs+sw; k < ze-sw; k++) {
for (int j = ys+sw; j < ye-sw; j++) {
#pragma simd
for (int i = xs+sw; i < xe-sw; i++) {
buffer[k * M * N + j * M + i] = val;
//cout<<buffer[cnt-1]<<" ";
}
//cout<<endl;
}
//cout<<endl;
}
}
template<typename T>
void set_buffer_rand(T *buffer, int size) {
arma::Col<T> v = oa::utils::make_vec(size, buffer);
arma::arma_rng::set_seed_random();
v.randu();
}
//specialized for int type
template<>
void set_buffer_rand(int *buffer, int size);
template<typename T>
void set_buffer_seqs(T *buffer, const Shape& s, Box box, int sw) {
int cnt = 0;
int xs, xe, ys, ye, zs, ze;
int M = s[0];
int N = s[1];
int P = s[2];
//cout<<M<<" "<<N<<" "<<P<<endl;
box.get_corners_with_stencil(xs, xe, ys, ye, zs, ze, sw);
//printf("%d %d %d %d %d %d\n", xs, xe, ys, ye, zs, ze);
for (int k = zs; k < ze; k++) {
for (int j = ys; j < ye; j++) {
#pragma simd
for (int i = xs; i < xe; i++) {
buffer[(k-zs)*(ye-ys)*(xe-xs)+(j-ys)*(xe-xs)+(i-xs)] = k * M * N + j * M + i;
//cout<<buffer[cnt-1]<<" ";
}
//cout<<endl;
}
//cout<<endl;
}
}
template <typename T>
void set_ghost_consts(T *buffer, const Shape &sp, T val, int sw = 1) {
int M = sp[0] + 2 * sw;
int N = sp[1] + 2 * sw;
int P = sp[2] + 2 * sw;
int cnt = 0;
for (int k = 0; k < P; k++) {
for (int j = 0; j < N; j++) {
#pragma simd
for (int i = 0; i < M; i++) {
if ((sw <= k && k < P - sw) &&
(sw <= j && j < N - sw) &&
(sw <= i && i < M - sw)) {
cnt++;
continue;
}
buffer[cnt++] = val;
}
}
}
}
int3 calc_step(Box box, int d, int sw);
// A = B
template<typename T>
void copy_buffer(T *A, T *B, int size) {
#pragma simd
for (int i = 0; i < size; i++) {
A[i] = B[i];
}
}
// template<typename T>
// inline int calc_id(int i, int j, int k, int3 S) {
// int M = S[0];
// int N = S[1];
// int P = S[2];
// return k * M * N + j * M + i;
// }
}
}
#endif