-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessing.cu
368 lines (295 loc) · 14.4 KB
/
Processing.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
//
// Created by kevin on 26/09/21.
//
#include "Processing.h"
#include "CUDA_check.h"
#include "Parameters.h"
#define CUDA_CHECK_RETURN(value) CheckCudaErrorAux(__FILE__,__LINE__, #value, value)
#define BLOCK_SIZE 32
#if !defined CONSTANT_MEMORY && !defined SHARED_MEMORY
__global__ void
kernel(unsigned char *input, const unsigned long long int *__restrict__ krn, unsigned char *output, int height, int width, int channels,
int size, double weight) {
int iy = blockIdx.y * blockDim.y + threadIdx.y;
int ix = blockIdx.x * blockDim.x + threadIdx.x;
if (iy < height && ix < width) {
int kCenter = size / 2;
int dx, dy, px, py;
for (int ic = 0; ic < channels; ic++) {
// vars "i?" identify image's element
unsigned long long int newVal = 0;
for (int ky = 0; ky < size; ky++) {
for (int kx = 0; kx < size; kx++) {
// vars "k?" identify kernel's element
dx = kx - kCenter;
dy = ky - kCenter;
// vars "d?" identify kernel's element's position with respect to the center
px = ix + dx;
py = iy + dy;
// vars "p?" identify the pixel to combine with kernel's element
if (px < 0 || px >= width) { // edge handling: extend
px = (px < 0) ? 0 : (width - 1);
}
if (py < 0 || py >= height) {
py = (py < 0) ? 0 : (height - 1);
}
newVal += (unsigned long long int) input[py * width * channels + px * channels + ic] *
krn[ky * size + kx];
}
}
newVal = (unsigned long long int) ((long double) newVal * weight);
output[iy * width * channels + ix * channels + ic] = (unsigned char) newVal;
}
}
}
Image *process(Image *img, Kernel *krn) {
Image *res = Image_new_empty(img->width, img->height, img->channels);
unsigned char *d_input;
unsigned long long int *d_krn;
unsigned char *d_output;
CUDA_CHECK_RETURN(cudaMalloc((void **) &d_input, sizeof(unsigned char) * img->width * img->height * img->channels));
CUDA_CHECK_RETURN(cudaMalloc((void **) &d_krn, sizeof(unsigned long long int) * krn->size * krn->size));
CUDA_CHECK_RETURN(
cudaMalloc((void **) &d_output, sizeof(unsigned char) * img->width * img->height * img->channels));
CUDA_CHECK_RETURN(cudaMemcpy((void *) d_input, (void *) img->data,
sizeof(unsigned char) * img->width * img->height * img->channels,
cudaMemcpyHostToDevice));
CUDA_CHECK_RETURN(cudaMemcpy((void *) d_krn, (void *) krn->coefficients,
sizeof(unsigned long long int) * krn->size * krn->size,
cudaMemcpyHostToDevice));
dim3 blockDim(BLOCK_SIZE, BLOCK_SIZE);
dim3 gridDim(ceil(((float) img->width) / BLOCK_SIZE), ceil(((float) img->height) / BLOCK_SIZE));
kernel<<<gridDim, blockDim>>>(d_input, d_krn, d_output, img->height, img->width, img->channels, krn->size,
krn->weight);
cudaDeviceSynchronize();
CUDA_CHECK_RETURN(cudaMemcpy((void *) res->data, (void *) d_output,
sizeof(unsigned char) * img->width * img->height * img->channels,
cudaMemcpyDeviceToHost));
cudaFree(d_input);
cudaFree(d_krn);
cudaFree(d_output);
return res;
}
#endif
#if defined CONSTANT_MEMORY && !defined SHARED_MEMORY
__constant__ unsigned long long int KERNEL[25 * 25];
__global__ void
kernelConstant(unsigned char *input, unsigned char *output, int height, int width, int channels,
int size, double weight) {
int iy = blockIdx.y * blockDim.y + threadIdx.y;
int ix = blockIdx.x * blockDim.x + threadIdx.x;
if (iy < height && ix < width) {
int kCenter = size / 2;
int dx, dy, px, py;
for (int ic = 0; ic < channels; ic++) {
// vars "i?" identify image's element
unsigned long long int newVal = 0;
for (int ky = 0; ky < size; ky++) {
for (int kx = 0; kx < size; kx++) {
// vars "k?" identify kernel's element
dx = kx - kCenter;
dy = ky - kCenter;
// vars "d?" identify kernel's element's position with respect to the center
px = ix + dx;
py = iy + dy;
// vars "p?" identify the pixel to combine with kernel's element
if (px < 0 || px >= width) { // edge handling: extend
px = (px < 0) ? 0 : (width - 1);
}
if (py < 0 || py >= height) {
py = (py < 0) ? 0 : (height - 1);
}
newVal += (unsigned long long int) input[py * width * channels + px * channels + ic] *
KERNEL[ky * size + kx];
}
}
newVal = (unsigned long long int) ((long double) newVal * weight);
output[iy * width * channels + ix * channels + ic] = (unsigned char) newVal;
}
}
}
Image *process(Image *img, Kernel *krn) {
Image *res = Image_new_empty(img->width, img->height, img->channels);
unsigned char *d_input;
unsigned char *d_output;
CUDA_CHECK_RETURN(cudaMalloc((void **) &d_input, sizeof(unsigned char) * img->width * img->height * img->channels));
CUDA_CHECK_RETURN(
cudaMalloc((void **) &d_output, sizeof(unsigned char) * img->width * img->height * img->channels));
CUDA_CHECK_RETURN(cudaMemcpy((void *) d_input, (void *) img->data,
sizeof(unsigned char) * img->width * img->height * img->channels,
cudaMemcpyHostToDevice));
CUDA_CHECK_RETURN(cudaMemcpyToSymbol(KERNEL, (void *) krn->coefficients,
sizeof(unsigned long long int) * krn->size * krn->size));
dim3 blockDim(BLOCK_SIZE, BLOCK_SIZE);
dim3 gridDim(ceil(((float) img->width) / BLOCK_SIZE), ceil(((float) img->height) / BLOCK_SIZE));
kernelConstant<<<gridDim, blockDim>>>(d_input, d_output, img->height, img->width, img->channels, krn->size,
krn->weight);
cudaDeviceSynchronize();
CUDA_CHECK_RETURN(cudaMemcpy((void *) res->data, (void *) d_output,
sizeof(unsigned char) * img->width * img->height * img->channels,
cudaMemcpyDeviceToHost));
cudaFree(d_input);
cudaFree(d_output);
return res;
}
#endif
#if !defined CONSTANT_MEMORY && defined SHARED_MEMORY
#define TILE_WIDTH BLOCK_SIZE
__global__ void
kernel(unsigned char *input, const unsigned long long int *__restrict__ krn, unsigned char *output, int height,
int width, int channels,
int size, double weight, int w) {
// shared memory
extern __shared__ unsigned char input_d[];
// pixel to process
int iy = blockIdx.y * blockDim.y + threadIdx.y;
int ix = blockIdx.x * blockDim.x + threadIdx.x;
// utility values
int kRadius = size / 2;
int batches = (w * w) / (TILE_WIDTH * TILE_WIDTH) + 1; // ceiling
// vars instantiation
int dest, destY, destX, srcY, srcX;
unsigned long long int newVal;
for (int ic = 0; ic < channels; ic++) {
// shared memory loading
for (int b = 0; b < batches; b++) {
dest = threadIdx.y * TILE_WIDTH + threadIdx.x + b * TILE_WIDTH * TILE_WIDTH;
destY = dest / w;
destX = dest % w;
srcY = blockIdx.y * TILE_WIDTH + destY - kRadius;
srcX = blockIdx.x * TILE_WIDTH + destX - kRadius;
if (destY < w) {
if (srcX < 0 || srcX >= width) { // edge handling: extend
srcX = (srcX < 0) ? 0 : (width - 1);
}
if (srcY < 0 || srcY >= height) {
srcY = (srcY < 0) ? 0 : (height - 1);
}
input_d[destY * w + destX] = input[srcY * width * channels + srcX * channels + ic];
}
}
__syncthreads();
// convolution
if (iy < height && ix < width) {
newVal = 0;
for (int ky = 0; ky < size; ky++) {
for (int kx = 0; kx < size; kx++) {
newVal += (unsigned long long int) input_d[(threadIdx.y + ky) * w + (threadIdx.x + kx)] *
krn[ky * size + kx];
}
}
newVal = (unsigned long long int) ((long double) newVal * weight);
output[iy * width * channels + ix * channels + ic] = (unsigned char) newVal;
}
__syncthreads();
}
}
Image *process(Image *img, Kernel *krn) {
Image *res = Image_new_empty(img->width, img->height, img->channels);
unsigned char *d_input;
unsigned long long int *d_krn;
unsigned char *d_output;
CUDA_CHECK_RETURN(cudaMalloc((void **) &d_input, sizeof(unsigned char) * img->width * img->height * img->channels));
CUDA_CHECK_RETURN(cudaMalloc((void **) &d_krn, sizeof(unsigned long long int) * krn->size * krn->size));
CUDA_CHECK_RETURN(
cudaMalloc((void **) &d_output, sizeof(unsigned char) * img->width * img->height * img->channels));
CUDA_CHECK_RETURN(cudaMemcpy((void *) d_input, (void *) img->data,
sizeof(unsigned char) * img->width * img->height * img->channels,
cudaMemcpyHostToDevice));
CUDA_CHECK_RETURN(cudaMemcpy((void *) d_krn, (void *) krn->coefficients,
sizeof(unsigned long long int) * krn->size * krn->size,
cudaMemcpyHostToDevice));
dim3 blockDim(TILE_WIDTH, TILE_WIDTH);
dim3 gridDim(ceil(((float) img->width) / TILE_WIDTH), ceil(((float) img->height) / TILE_WIDTH));
int w = TILE_WIDTH + krn->size - 1;
kernel<<<gridDim, blockDim, sizeof(unsigned char) * w * w>>>(d_input, d_krn, d_output, img->height, img->width,
img->channels, krn->size,
krn->weight, w);
cudaDeviceSynchronize();
CUDA_CHECK_RETURN(cudaMemcpy((void *) res->data, (void *) d_output,
sizeof(unsigned char) * img->width * img->height * img->channels,
cudaMemcpyDeviceToHost));
cudaFree(d_input);
cudaFree(d_krn);
cudaFree(d_output);
return res;
}
#endif
#if defined CONSTANT_MEMORY && defined SHARED_MEMORY
#define TILE_WIDTH 16
__constant__ unsigned long long int KERNEL[25 * 25];
__global__ void
kernel(unsigned char *input, unsigned char *output, int height,
int width, int channels,
int size, double weight, int w) {
// shared memory
extern __shared__ unsigned char input_d[];
// pixel to process
int iy = blockIdx.y * blockDim.y + threadIdx.y;
int ix = blockIdx.x * blockDim.x + threadIdx.x;
// utility values
int kRadius = size / 2;
int batches = (w * w) / (TILE_WIDTH * TILE_WIDTH) + 1; // ceiling
int dest, destY, destX, srcY, srcX;
unsigned long long int newVal;
for (int ic = 0; ic < channels; ic++) {
// shared memory loading
for (int b = 0; b < batches; b++) {
dest = threadIdx.y * TILE_WIDTH + threadIdx.x + b * TILE_WIDTH * TILE_WIDTH;
destY = dest / w;
destX = dest % w;
srcY = blockIdx.y * TILE_WIDTH + destY - kRadius;
srcX = blockIdx.x * TILE_WIDTH + destX - kRadius;
if (destY < w) {
if (srcX < 0 || srcX >= width) { // edge handling: extend
srcX = (srcX < 0) ? 0 : (width - 1);
}
if (srcY < 0 || srcY >= height) {
srcY = (srcY < 0) ? 0 : (height - 1);
}
input_d[destY * w + destX] = input[srcY * width * channels + srcX * channels + ic];
}
}
__syncthreads();
// convolution
if (iy < height && ix < width) {
newVal = 0;
for (int ky = 0; ky < size; ky++) {
for (int kx = 0; kx < size; kx++) {
newVal += (unsigned long long int) input_d[(threadIdx.y + ky) * w + (threadIdx.x + kx)] *
KERNEL[ky * size + kx];
}
}
newVal = (unsigned long long int) ((long double) newVal * weight);
output[iy * width * channels + ix * channels + ic] = (unsigned char) newVal;
}
__syncthreads();
}
}
Image *process(Image *img, Kernel *krn) {
Image *res = Image_new_empty(img->width, img->height, img->channels);
unsigned char *d_input;
unsigned char *d_output;
CUDA_CHECK_RETURN(cudaMalloc((void **) &d_input, sizeof(unsigned char) * img->width * img->height * img->channels));
CUDA_CHECK_RETURN(
cudaMalloc((void **) &d_output, sizeof(unsigned char) * img->width * img->height * img->channels));
CUDA_CHECK_RETURN(cudaMemcpy((void *) d_input, (void *) img->data,
sizeof(unsigned char) * img->width * img->height * img->channels,
cudaMemcpyHostToDevice));
CUDA_CHECK_RETURN(cudaMemcpyToSymbol(KERNEL, (void *) krn->coefficients,
sizeof(unsigned long long int) * krn->size * krn->size));
dim3 blockDim(TILE_WIDTH, TILE_WIDTH);
dim3 gridDim(ceil(((float) img->width) / TILE_WIDTH), ceil(((float) img->height) / TILE_WIDTH));
int w = TILE_WIDTH + krn->size - 1;
kernel<<<gridDim, blockDim, sizeof(unsigned char) * w * w>>>(d_input, d_output, img->height, img->width,
img->channels, krn->size,
krn->weight, w);
cudaDeviceSynchronize();
CUDA_CHECK_RETURN(cudaMemcpy((void *) res->data, (void *) d_output,
sizeof(unsigned char) * img->width * img->height * img->channels,
cudaMemcpyDeviceToHost));
cudaFree(d_input);
cudaFree(d_output);
return res;
}
#endif