-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgaussian.cu
executable file
·465 lines (395 loc) · 14.1 KB
/
gaussian.cu
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include "gaussian.cuh"
constexpr int GAUSSIAN_FILTER_SIZE = 3;
__global__ void k_1D_gaussian_filter(unsigned char *input, int rows, int cols, int mask_dim, int thread_load, int channels)
{
int ty = (blockIdx.x * blockDim.x + threadIdx.x) * thread_load;
int tx = (blockIdx.y * blockDim.y + threadIdx.y);
int threadId = (tx * cols + ty);
int offset = GAUSSIAN_FILTER_SIZE / 2;
if(threadId >= channels){
return;
}
int conv_kernel[GAUSSIAN_FILTER_SIZE][GAUSSIAN_FILTER_SIZE] = {{1, 2, 1}, {2, 4, 2}, {1, 2, 1}};
for(int i = 0; i < thread_load; i++){
int newPixelValue = 0;
int _tx = tx;
int _ty = ty + i;
for (int r = 0; r < mask_dim; r++)
{
for (int c = 0; c < mask_dim; c++)
{
if ((_tx > 0 && _tx < rows - 1) && (_ty > 0 && _ty < cols - 1))
{
newPixelValue += conv_kernel[r][c] * input[(_tx - offset + r) * cols + (_ty - offset + c)];
}
else
{
return;
}
}
}
input[(_tx * cols + _ty)] = static_cast<uchar>(newPixelValue / 16);
}
}
__global__ void k_1D_gaussian_filter_shared_mem(unsigned char* input, int rows, int cols, int mask_dim, int thread_load, int channels)
{
__shared__ unsigned char cache[32][32 * 3];
int conv_kernel[GAUSSIAN_FILTER_SIZE][GAUSSIAN_FILTER_SIZE] = {{1, 2, 1}, {2, 4, 2}, {1, 2, 1}};
int ty = (blockIdx.x * blockDim.x + threadIdx.x) * thread_load;
int tx = blockIdx.y * blockDim.y + threadIdx.y;
int threadId = (tx * cols + ty);
int offset = GAUSSIAN_FILTER_SIZE / 2;
unsigned int cy = threadIdx.x * thread_load;
unsigned int cx = threadIdx.y;
for(int i = 0 ; i < thread_load; i++){
int _ty = ty + i;
int _cy = cy + i;
if(_ty < cols -1){
cache[cx][_cy] = input[tx * cols + _ty];
}
}
if (threadId >= channels)
{
return;
}
__syncthreads();
for(int m = 0; m < thread_load; m++){
int _tx = tx;
int _ty = ty + m;
int _cx = cx;
int _cy = cy + m;
int newPixelValue = 0;
for (int i = 0; i < mask_dim; i++)
{
for (int j = 0; j < mask_dim; j++)
{ /*travel on conv matrix*/
if ((_tx > 0 && _tx < rows - 1) && (_ty > 0 && _ty < cols - 1))
{
int x_index = _cx - offset + i;
int y_index = (_cy - offset + j);
if (_cx == 31 || _cx == 0 || _cy == 0 || _cy == 95)
{
newPixelValue += conv_kernel[i][j] * input[(_tx - offset + i) * cols + (_ty - offset + j)];
}
else
{
newPixelValue += conv_kernel[i][j] * cache[x_index][y_index];
}
}
else
{
return;
}
}
}
input[_tx * cols + _ty] = static_cast<uchar>(newPixelValue / 16);
}
}
float gaussian_filter_gpu_3D(cv::Mat input_img, cv::Mat *output_img, bool sm)
{
unsigned char *gpu_input = NULL;
unsigned char *input = input_img.data;
unsigned char *output = output_img->data;
unsigned int cols = input_img.cols;
unsigned int rows = input_img.rows;
unsigned int size = rows * cols * sizeof(unsigned char) * 3;
const uint mask_dim = 3;
dim3 block(32, 32);
dim3 grid((cols + block.x - 1) / block.x, (rows + block.y - 1) / block.y);
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
CHECK_CUDA_ERROR(cudaMalloc((unsigned char **)&gpu_input, size));
CHECK_CUDA_ERROR(cudaMemcpy(gpu_input, input, size, cudaMemcpyHostToDevice));
if(sm){
k_3D_gaussian_filter_shared_mem<<<grid, block>>>(gpu_input, rows, cols, mask_dim);
}
else{
k_3D_gaussian_filter<<<grid, block>>>(gpu_input, rows, cols, mask_dim);
}
CHECK_CUDA_ERROR(cudaMemcpy(output, gpu_input, size, cudaMemcpyDeviceToHost));
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float elapsed = 0.0f;
cudaEventElapsedTime(&elapsed, start, stop);
cudaFree(gpu_input);
cudaDeviceReset();
return elapsed;
}
__global__ void k_3D_gaussian_filter(unsigned char *input, int rows, int cols, int mask_dim)
{
int ty = blockIdx.x * blockDim.x + threadIdx.x;
int tx = blockIdx.y * blockDim.y + threadIdx.y;
int threadId = (tx * cols + ty ) * 3;
int offset = GAUSSIAN_FILTER_SIZE / 2;
int new_red_val = 0;
int new_green_val = 0;
int new_blue_val = 0;
int conv_kernel[GAUSSIAN_FILTER_SIZE][GAUSSIAN_FILTER_SIZE] = {{1, 2, 1}, {2, 4, 2}, {1, 2, 1}};
if (threadId >= rows * cols * 3)
{
return;
}
for (int i = 0; i < mask_dim; i++)
{
for (int j = 0; j < mask_dim; j++)
{
if ((tx > 0 && tx < rows - 1) && (ty > 0 && ty < cols - 1))
{
new_red_val += conv_kernel[i][j] * input[((tx - offset + i) * cols + ty - offset + j) * 3];
new_green_val += conv_kernel[i][j] * input[((tx - offset + i) * cols + ty - offset + j) * 3 + 1];
new_blue_val += conv_kernel[i][j] * input[((tx - offset + i) * cols + ty - offset + j) * 3 + 2];
}
else
{
return;
}
}
}
input[threadId] = static_cast<uchar>(new_red_val / 16);
input[threadId + 1] = static_cast<uchar>(new_green_val / 16);
input[threadId + 2] = static_cast<uchar>(new_blue_val / 16);
}
__global__ void k_3D_gaussian_filter_shared_mem(unsigned char *input, int rows, int cols, int mask_dim)
{
__shared__ unsigned char cache_red[32][32];
__shared__ unsigned char cache_green[32][32];
__shared__ unsigned char cache_blue[32][32];
int conv_kernel[GAUSSIAN_FILTER_SIZE][GAUSSIAN_FILTER_SIZE] = {{1, 2, 1}, {2, 4, 2}, {1, 2, 1}};
int ty = blockIdx.x * blockDim.x + threadIdx.x;
int tx = blockIdx.y * blockDim.y + threadIdx.y;
int threadId = (tx * cols + ty) * 3;
int offset = GAUSSIAN_FILTER_SIZE / 2;
int new_red_val = 0;
int new_green_val = 0;
int new_blue_val = 0;
if (threadId >= rows * cols * 3)
{
return;
}
unsigned int cy = threadIdx.x;
unsigned int cx = threadIdx.y;
cache_red[cx][cy] = input[threadId]; /*load data shared mem*/
cache_green[cx][cy] = input[threadId + 1];
cache_blue[cx][cy] = input[threadId + 2];
__syncthreads();
for (int i = 0; i < mask_dim; i++)
{
for (int j = 0; j < mask_dim; j++)
{ /*travel on conv matrix*/
if ((tx > 0 && tx < rows - 1) && (ty > 0 && ty < cols - 1))
{
int x_index = cx - offset + i;
int y_index = cy - offset + j;
if (cx == 31 || cx == 0 || cy == 0 || cy == 31)
{
new_red_val += conv_kernel[i][j] * input[((tx - offset + i) * cols + (ty - offset + j)) * 3];
new_green_val += conv_kernel[i][j] * input[((tx - offset + i) * cols + (ty - offset + j)) * 3 + 1];
new_blue_val += conv_kernel[i][j] * input[((tx - offset + i) * cols + (ty - offset + j)) * 3 + 2];
}
else
{
new_red_val += conv_kernel[i][j] * cache_red[x_index][y_index];
new_green_val += conv_kernel[i][j] * cache_green[x_index][y_index];
new_blue_val += conv_kernel[i][j] * cache_blue[x_index][y_index];
}
}
else
{
return;
}
}
}
input[threadId] = static_cast<uchar>(new_red_val / 16);
input[threadId + 1] = static_cast<uchar>(new_green_val / 16);
input[threadId + 2] = static_cast<uchar>(new_blue_val / 16);
}
float gaussian_filter_gpu_1D(cv::Mat input_img, cv::Mat *output_img, bool sm)
{
unsigned char *gpu_input = NULL;
unsigned char *input = input_img.data;
unsigned char *output = output_img->data;
int thread_load = 3; /*32 96*/
unsigned int cols = input_img.cols;
unsigned int rows = input_img.rows;
unsigned int pixels = cols * rows;
unsigned int channels = pixels;
unsigned int size = channels * sizeof(unsigned char);
const uint mask_dim = 3;
dim3 block(32, 32);
dim3 grid((cols / thread_load + block.x - 1) / block.x, (rows + block.y - 1) / block.y);
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
CHECK_CUDA_ERROR(cudaMalloc((unsigned char **)&gpu_input, size));
CHECK_CUDA_ERROR(cudaMemcpy(gpu_input, input, size, cudaMemcpyHostToDevice));
if(sm){
k_1D_gaussian_filter_shared_mem<<<grid, block>>>(gpu_input, rows, cols, mask_dim, thread_load, channels);
}
else{
k_1D_gaussian_filter<<<grid, block>>>(gpu_input, rows, cols, mask_dim, thread_load, channels);
}
cudaError_t cudaError = cudaGetLastError();cudaGetLastError();
std::cerr << "CUDA Error: " << cudaGetErrorString(cudaError) << std::endl;
CHECK_CUDA_ERROR(cudaMemcpy(output, gpu_input, size, cudaMemcpyDeviceToHost));
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float elapsed = 0.0f;
cudaEventElapsedTime(&elapsed, start, stop);
cudaFree(gpu_input);
cudaDeviceReset();
return elapsed;
}
float gaussian_filter_cpu_3D(cv::Mat input_img, cv::Mat *output_img)
{
int cols = input_img.cols;
int rows = input_img.rows;
unsigned char* input = input_img.data;
unsigned char* output = output_img->data;
const unsigned short mask_dim = 3;
float kernel[mask_dim][mask_dim] = {{1, 2, 1}, {2, 4, 2}, {1, 2, 1}};
auto start = std::chrono::steady_clock::now();
for (int i = 1; i < rows - 1; i++)
{
for (int j = 1; j < cols - 1; j++)
{
int new_red_val = 0;
int new_green_val = 0;
int new_blue_val = 0;
for (int m = 0; m < mask_dim; m++)
{
for (int n = 0; n < mask_dim; n++)
{
new_red_val += input[(((i + m - 1) * cols + (j + n - 1))) * 3] * kernel[m][n];
new_green_val += input[((i + m - 1) * cols + (j + n - 1)) * 3 + 1] * kernel[m][n];
new_blue_val += input[((i + m - 1) * cols + (j + n - 1) )* 3 + 2] * kernel[m][n];
}
}
output[(i * cols + j) * 3] = new_red_val / 16;
output[(i * cols + j) * 3 + 1] = new_green_val / 16;
output[(i * cols + j) * 3 + 2] = new_blue_val / 16;
}
}
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start) / 1000.0f;
return elapsed.count();
}
float gaussian_filter_cpu_1D(cv::Mat input_img, cv::Mat *output_img)
{
int cols = input_img.cols;
int rows = input_img.rows;
unsigned char* input = input_img.data;
unsigned char* output = output_img->data;
const unsigned short mask_dim = 3;
float kernel[mask_dim][mask_dim] = {{1, 2, 1}, {2, 4, 2}, {1, 2, 1}};
auto start = std::chrono::steady_clock::now();
for (int i = 1; i < rows - 1; i++)
{
for (int j = 1; j < cols - 1; j++)
{
int newPixelValue = 0;
for (int m = 0; m < mask_dim; m++)
{
for (int n = 0; n < mask_dim; n++)
{
newPixelValue += input[(i + m - 1) * cols + (j + n - 1)] * kernel[m][n];
}
}
output[i * cols + j] = newPixelValue / 16;
}
}
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start) / 1000.0f;
return elapsed.count();
}
float gaussian_filter_cpu_parallel_1D(cv::Mat input_img, cv::Mat* output_img)
{
unsigned char *input = input_img.data;
unsigned char *output = output_img->data;
int cols = input_img.cols;
int rows = input_img.rows;
const unsigned short mask_dim = 3;
float kernel[mask_dim][mask_dim] = {{1, 2, 1}, {2, 4, 2}, {1, 2, 1}};
std::vector<std::thread> threads;
const int MAX_THREAD_SUPPORT = std::thread::hardware_concurrency();
int stride = rows / MAX_THREAD_SUPPORT;
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < MAX_THREAD_SUPPORT; i++)
{
threads.push_back(std::thread([&, i](){
int range_start = stride * i;
int range_end = (i == MAX_THREAD_SUPPORT - 1) ? cols : stride * (i + 1);
for (int r = range_start; r < range_end; r++) { /*row loop*/
for (int c = 0; c < cols; c++) { /*col loop*/
if (r > 0 && r < rows - 1 && c > 0 && c < cols - 1) {
int new_pixel_value = 0;
for (int mr = 0; mr < mask_dim; mr++) { /*matrix row*/
for (int mc = 0; mc < mask_dim; mc++) { /*matrix col*/
int r_index = r + mr - 1;
int c_index = c + mc - 1;
new_pixel_value += input[r_index * cols + c_index] * kernel[mr][mc];
}
}
output[r * cols + c] = static_cast<unsigned char>(new_pixel_value / 16);
}
}
} }));
}
for (std::thread &th : threads)
{
th.join();
}
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start) / 1000.0f;
return elapsed.count();
}
float gaussian_filter_cpu_parallel_3D(cv::Mat input_img, cv::Mat* output_img)
{
unsigned char *input = input_img.data;
unsigned char *output = output_img->data;
int cols = input_img.cols;
int rows = input_img.rows;
const unsigned short mask_dim = 3;
float kernel[mask_dim][mask_dim] = {{1, 2, 1}, {2, 4, 2}, {1, 2, 1}};
std::vector<std::thread> threads;
const int MAX_THREAD_SUPPORT = std::thread::hardware_concurrency();
int stride = rows / MAX_THREAD_SUPPORT;
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < MAX_THREAD_SUPPORT; i++)
{
threads.push_back(std::thread([&, i]()
{
int range_start = stride * i;
int range_end = (i == MAX_THREAD_SUPPORT - 1) ? cols : stride * (i + 1);
for (int r = range_start; r < range_end; r++) { /*row loop*/
for (int c = 0; c < cols; c++) { /*col loop*/
if (r > 0 && r < rows - 1 && c > 0 && c < cols - 1) {
int new_pixel_value_red = 0;
int new_pixel_value_green = 0;
int new_pixel_value_blue = 0;
for (int mr = 0; mr < mask_dim; mr++) { /*matrix row*/
for (int mc = 0; mc < mask_dim; mc++) { /*matrix col*/
int r_index = r + mr - 1;
int c_index = c + mc - 1;
new_pixel_value_red += input[(r_index * cols + c_index) * 3] * kernel[mr][mc];
new_pixel_value_green += input[(r_index * cols + c_index) * 3 + 1] * kernel[mr][mc];
new_pixel_value_blue += input[(r_index * cols + c_index) * 3 + 2] * kernel[mr][mc];
}
}
output[(r * cols + c) * 3] = static_cast<unsigned char>(new_pixel_value_red / 16);
output[(r * cols + c) * 3 + 1] = static_cast<unsigned char>(new_pixel_value_green / 16);
output[(r * cols + c) * 3 + 2] = static_cast<unsigned char>(new_pixel_value_blue / 16);
}
}
} }));
}
for (std::thread &th : threads)
{
th.join();
}
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start) / 1000.0f;
return elapsed.count();
}