-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconvolution.cu
More file actions
411 lines (354 loc) · 13.9 KB
/
convolution.cu
File metadata and controls
411 lines (354 loc) · 13.9 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
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
/*
* Created by Rob Golshan
* Demos common image filters using parallel gpu algorithms
* Algorithms based of convolutionSeperable.pdf in cuda samples
* and wjarosz_convolution_2001.pdf --> converted to parallel
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <helper_cuda.h>
#include <helper_functions.h>
#include "convolution.cuh"
// Kernel cannot have radius bigger than 15
__constant__ int d_kernel[1024];
#define BLOCK_SIZE 16
/*
* Converts a uint to a uint3, seperating RGB
* colors by every byte.
* Most significat to least significant:
* Red, Green, Blue
*/
__device__ __forceinline__ int3 d_uintToRGB(unsigned int orig)
{
int3 rgb;
rgb.x = orig & 0xff;
rgb.y = (orig>>8)&0xff;
rgb.z = (orig>>16)&0xff;
return rgb;
}
/*
* Converts a uint3 to an unsigned int
* Assumes each vector member correspond to RGB colors
* Truncates rgb colors bigger than 1 byte
*/
__device__ __forceinline__ unsigned int d_rgbToUint(int3 rgb)
{
if (rgb.x > 0xff) rgb.x = 0xff;
else if (rgb.x < 0) rgb.x = 0;
if (rgb.y > 0xff) rgb.y = 0xff;
else if (rgb.y < 0) rgb.y = 0;
if (rgb.z > 0xff) rgb.z = 0xff;
else if (rgb.z < 0) rgb.z = 0;
return (rgb.x & 0xff) | ((rgb.y & 0xff) << 8) | ((rgb.z & 0xff) << 16);
}
/*
* divides an int3 by an int
* Maybe faster to just multiply by float instead..
*/
__device__ __forceinline__ int3 d_divide(int3 orig, int op)
{
orig.x = orig.x/op;
orig.y = orig.y/op;
orig.z = orig.z/op;
return orig;
}
/* The most basic convolution method in parallel
* Does not take advantage of memory optimizations with a GPU
* Can be used with any (square) kernel filter
* SLOW
* Each output pixel does radius^2 multiplications
* T = O(radius^2)
* W = O(radius^2 * width * height)
*/
__global__ void d_slowConvolution(unsigned int *d_img, unsigned int *d_result, int width, int height, int radius, int weight)
{
int x = blockIdx.x*blockDim.x + threadIdx.x;
int y = blockIdx.y*blockDim.y + threadIdx.y;
const unsigned int loc = x + y*width;
int3 accumulation = make_int3(0,0,0);
int3 value;
if (x >= width || y >= height) return;
assert(x < width);
assert(y < height);
for (int i = -radius; i <= radius; i++) {
for (int j = -radius; j <= radius; j++) {
if ((x + i < 0) || //left side out of bounds
(x + i >= width) || //right side OoB
(y + j < 0) || //top OoB
(y + j >= height)) //bot OoB
continue;
value = d_uintToRGB(d_img[loc + i + j * width]);
int temp = d_kernel[i + radius + (j+radius)*((radius << 1) + 1)];
value *= temp;
accumulation += value;
}
}
accumulation = d_divide(accumulation, weight);
d_result[loc] = d_rgbToUint(accumulation);
}
/* The most basic convolution method in parallel
* Takes advantage of shared memory in a GPU
* Can be used with any (square) kernel filter
* Faster than without shared memory
* Each output pixel does radius^2 multiplications
* T = O(radius^2)
* W = O(radius^2 * width * height)
*/
__global__ void d_sharedSlowConvolution(unsigned int *d_img, unsigned int *d_result, int width, int height, int radius, int weight)
{
// Use a 1d array instead of 2D in order to coalesce memory access
extern __shared__ unsigned int data[];
int x = blockIdx.x*blockDim.x + threadIdx.x;
int y = blockIdx.y*blockDim.y + threadIdx.y;
if (x >= width || y >= height) return;
// memory location in d_img
const unsigned int loc = x + y*width;
int3 accumulation = make_int3(0,0,0);
int3 value;
int w = blockDim.x;
int h = blockDim.y;
/* to convolute the edges of a block, the shared memory must extend outwards of radius */
#pragma unroll 3
for (int i = -w; i <= w; i+= w) {
#pragma unroll 3
for (int j = -h; j <= h; j+= h) {
int x0 = threadIdx.x + i;
int y0 = threadIdx.y + j;
int newLoc = loc + i + j*width;
if (x0 < -radius ||
x0 >= radius + w ||
y0 < -radius ||
y0 >= radius + h ||
newLoc < 0 ||
newLoc >= width*height)
continue;
data[threadIdx.x + i + radius + (threadIdx.y + j + radius)*(blockDim.x+(radius << 1))] = d_img[newLoc];
}
}
__syncthreads();
for (int i = -radius; i <= radius; i++) {
for (int j = -radius; j <= radius; j++) {
unsigned int t = data[threadIdx.x + i + radius + (threadIdx.y + j + radius)*(blockDim.x+(radius << 1))];
int temp = d_kernel[i + radius + (j+radius)*((radius << 1) + 1)];
value = d_uintToRGB(t);
value *= temp;
accumulation += value;
}
}
accumulation = d_divide(accumulation, weight);
d_result[loc] = d_rgbToUint(accumulation);
}
/* VERY FAST convolution method in parallel
* Takes advantage of shared memory in a GPU
* Can be used with ONLY WITH SEPARABLE kernel filters
* Each output pixel does radius+radius multiplications
* T = O(radius + radius)
* W = O(radius * width + radius*height)
*/
__global__ void d_sepRowConvolution(unsigned int *d_img, unsigned int *d_result, int width, int height, int radius)
{
// Use a 1d array instead of 2D in order to coalesce memory access
extern __shared__ unsigned int data[];
int x = blockIdx.x*blockDim.x + threadIdx.x;
int y = blockIdx.y*blockDim.y + threadIdx.y;
if (x >= width || y >= height) return;
// memory location in d_img
const unsigned int loc = (blockIdx.x*blockDim.x + threadIdx.x) + (blockIdx.y*blockDim.y)*width + threadIdx.y*width;
int3 accumulation = make_int3(0,0,0);
int3 value;
int weight = 0;
int w = blockDim.x;
/* to convolute the edges of a block, the shared memory must extend outwards of radius */
#pragma unroll 3
for (int i = -w; i <= w; i+= w) {
int x0 = threadIdx.x + i;
int newLoc = loc + i;
if (x0 < -radius ||
x0 >= radius + w ||
newLoc < 0 ||
newLoc >= width*height)
continue;
data[threadIdx.x + i + radius + (threadIdx.y) *(blockDim.x+(radius << 1))] = d_img[newLoc];
}
__syncthreads();
for (int i = -radius; i <= radius; i++) {
unsigned int t = data[threadIdx.x + i + radius + (threadIdx.y)*(blockDim.x+(radius << 1))];
int temp = d_kernel[i + radius];
value = d_uintToRGB(t);
value *= temp;
weight += temp;
accumulation += value;
}
accumulation = d_divide(accumulation, weight);
d_result[loc] = d_rgbToUint(accumulation);
}
/* VERY FAST convolution method in parallel
* Takes advantage of shared memory in a GPU
* Can be used with ONLY WITH SEPERABLE kernel filters
* Each output pixel does radius^2 multiplications
* T = O(radius + radius)
* W = O(radius * width + radius*height)
*/
__global__ void d_sepColConvolution(unsigned int *d_result, int width, int height, int radius)
{
// Use a 1d array instead of 2D in order to coalesce memory access
extern __shared__ unsigned int data[];
int x = blockIdx.x*blockDim.x + threadIdx.x;
int y = blockIdx.y*blockDim.y + threadIdx.y;
if (x >= width || y >= height) return;
// memory location in d_img
const unsigned int loc = (blockIdx.x*blockDim.x + threadIdx.x) + (blockIdx.y*blockDim.y)*width + threadIdx.y*width;
int3 accumulation = make_int3(0,0,0);
int3 value;
int weight = 0;
int h = blockDim.y;
/* to convolute the edges of a block, the shared memory must extend outwards of radius */
#pragma unroll 3
for (int j = -h; j <= h; j+= h) {
int y0 = threadIdx.y + j;
int newLoc = loc + j*width;
if (y0 < -radius ||
y0 >= radius + h ||
newLoc < 0 ||
newLoc >= width*height)
continue;
data[threadIdx.x + (threadIdx.y + j + radius)*(blockDim.x)] = d_result[newLoc];
}
__syncthreads();
for (int j = -radius; j <= radius; j++) {
unsigned int t = data[threadIdx.x + (threadIdx.y + j + radius)*(blockDim.x)];
float temp = d_kernel[(j + radius)*((radius << 1)+1)];
value = d_uintToRGB(t);
value *= temp;
weight += temp;
accumulation += value;
}
accumulation = d_divide(accumulation, weight);
d_result[loc] = d_rgbToUint(accumulation);
}
/*
* Fast radius independent box filter
* Do Rows followed by Columns
* T = O(width + height)
* W = O(width*height + width*height)
*/
__global__ void d_boxFilterRow(unsigned int *d_img, unsigned int *d_result, int width, int height, int radius)
{
// memory location in d_img
const unsigned int loc = (blockIdx.x*blockDim.x + threadIdx.x) * width;
if (loc > height*width) return;
d_img = d_img + loc;
d_result = d_result + loc;
int3 accumulation;
int bWeight = (radius<<1) + 1; //all values in kernel weighted equally
//initial clamping of left value
accumulation = d_uintToRGB(d_img[0])*radius;
for (int i = 0; i < radius + 1; i++) {
accumulation += d_uintToRGB(d_img[i]);
}
d_result[0] = d_rgbToUint(d_divide(accumulation, bWeight));
for (int i = 1; i < radius + 1; i++) {
accumulation += d_uintToRGB(d_img[i + radius]);
accumulation -= d_uintToRGB(d_img[0]); //clamp left side
d_result[i] = d_rgbToUint(d_divide(accumulation, bWeight));
}
//resuses previous computed value
for (int i = radius + 1; i < width - radius; i++) {
accumulation += d_uintToRGB(d_img[i + radius]);
accumulation -= d_uintToRGB(d_img[i - radius - 1]);
d_result[i] = d_rgbToUint(d_divide(accumulation, bWeight));
}
for (int i = width - radius; i < width; i++){
//clamp right side
accumulation += d_uintToRGB(d_img[width - 1]);
accumulation -= d_uintToRGB(d_img[i - radius - 1]);
d_result[i] = d_rgbToUint(d_divide(accumulation, bWeight));
}
}
/*
* Fast radius independent box filter
* Do Rows followed by Columns
* d_img should be d_result from the row filter
* T = O(width + height)
* W = O(width*height + width*height)
*/
__global__ void d_boxFilterCol(unsigned int *d_img, unsigned int *d_result, int width, int height, int radius)
{
// memory location in d_img
const unsigned int loc = (blockIdx.x*blockDim.x + threadIdx.x);
if (loc >= width) return;
d_img = d_img + loc;
d_result = d_result + loc;
int3 accumulation;
int bWeight = (radius<<1) + 1; //all values in kernel weighted equally
//initial clamping of left value
accumulation = d_uintToRGB(d_img[0])*radius;
for (int i = 0; i < radius + 1; i++) {
accumulation += d_uintToRGB(d_img[i * width]);
}
d_result[0] = d_rgbToUint(d_divide(accumulation, bWeight));
for (int i = 1; i < radius + 1; i++) {
accumulation += d_uintToRGB(d_img[(i + radius) * width]);
accumulation -= d_uintToRGB(d_img[0]); //clamp left side
d_result[i * width] = d_rgbToUint(d_divide(accumulation, bWeight));
}
//resuses previous computed value
for (int i = radius + 1; i < height - radius; i++) {
accumulation += d_uintToRGB(d_img[(i + radius)*width]);
accumulation -= d_uintToRGB(d_img[(i - radius)*width - width]);
d_result[i * width] = d_rgbToUint(d_divide(accumulation, bWeight));
}
for (int i = height - radius; i < height; i++){
//clamp right side
accumulation += d_uintToRGB(d_img[(height - 1)*width]);
accumulation -= d_uintToRGB(d_img[(i - radius)*width - width]);
d_result[i * width] = d_rgbToUint(d_divide(accumulation, bWeight));
}
}
extern StopWatchInterface *timer;
/*
* look at main.cpp kerboard interrupts for descriptions on what type and kernels do
*/
double convolution(unsigned int *d_img, unsigned int *d_result, int *h_kernel, int width, int height,
int radius, int type, int weight, int iterations)
{
checkCudaErrors(cudaDeviceSynchronize());
// threadsPerBlock needs to be a multiple of 32 for proper coalesce
dim3 threadsPerBlock(BLOCK_SIZE, BLOCK_SIZE);
//numBlocks should probably be a multiple of warp size here for proper coalesce..
dim3 numBlocks(ceil((float)width / threadsPerBlock.x), ceil((float)height/threadsPerBlock.y));
//copy kernel to device memory
if (radius < 15)
checkCudaErrors(cudaMemcpyToSymbol(d_kernel, h_kernel, ((radius << 1)+1)*((radius << 1)+1)*sizeof(int)));
unsigned int *d_temp = NULL;
if (type == 3)
checkCudaErrors(cudaMalloc((void **) &d_temp, width*height*sizeof(unsigned int)));
sdkResetTimer(&timer);
sdkStartTimer(&timer);
for (int i = 0; i < iterations; i++) {
switch (type) {
case 0:
d_slowConvolution<<< numBlocks, threadsPerBlock>>>(d_img, d_result, width, height, radius, weight);
break;
case 1:
d_sharedSlowConvolution<<< numBlocks, threadsPerBlock, (BLOCK_SIZE+(radius << 1))*(BLOCK_SIZE+(radius << 1))*sizeof(unsigned int)>>>(d_img, d_result, width, height, radius, weight);
break;
case 2:
d_sepRowConvolution<<< numBlocks, threadsPerBlock, (BLOCK_SIZE+(radius << 1))*(BLOCK_SIZE)*sizeof(unsigned int)>>>(d_img, d_result, width, height, radius);
d_sepColConvolution<<< numBlocks, threadsPerBlock, (BLOCK_SIZE)*(BLOCK_SIZE+(radius << 1))*sizeof(unsigned int)>>>(d_result, width, height, radius);
break;
case 3:
d_boxFilterRow<<< ceil((float)height/BLOCK_SIZE), BLOCK_SIZE>>>(d_img, d_temp, width, height, radius);
d_boxFilterCol<<< ceil((float)width/BLOCK_SIZE), BLOCK_SIZE>>>(d_temp, d_result, width, height, radius);
break;
}
checkCudaErrors(cudaDeviceSynchronize());
d_img = d_result;
}
sdkStopTimer(&timer);
printf("time taken: %f\n", sdkGetTimerValue(&timer));
checkCudaErrors(cudaFree(d_temp));
return 0;
}