-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnlm-cuda-shared.cu
203 lines (179 loc) · 6.82 KB
/
nlm-cuda-shared.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include "supplementary.h"
//!------------------------------------------------------------------
//! EDIT THESE VALUES ACCORDING TO THE IMAGE SIZE AND SPECIFICATIONS
#define PIXELS 64 // PIXELS x PIXELS
#define PATCH_SIZE 3
#define FILTER_SIGMA 0.0185
#define PATCH_SIGMA 3.1550
//!------------------------------------------------------------------
// Device global variables
__device__ const int DEV_PIXELS = PIXELS;
__device__ const int DEV_PATCH_SIZE = PATCH_SIZE;
__device__ const float DEV_FILTER_SIGMA = (float)FILTER_SIGMA;
__device__ const int DEV_PADDING = PATCH_SIZE/2;
// Host global variables
const int HOST_PADDING = PATCH_SIZE/2;
// Functions
__global__ void denoise_image(float *filtered_image, float *image, int padded_size, float *G);
__device__ void compare_patches(float *comp_value, float *patch_i, int j, float *G, float *shared_memory);
__host__ float *nonLocalMeans(float *host_image);
__host__ float *gaussian_filter();
__host__ int main(){
//read image from txt
float *host_image = image_from_txt(PIXELS, HOST_PADDING);
float *filtered_image;
cudaMallocManaged(&filtered_image,0*sizeof(float));
struct timespec tic, toc;
clock_gettime(CLOCK_MONOTONIC, &tic);
filtered_image = nonLocalMeans(host_image);
clock_gettime(CLOCK_MONOTONIC, &toc);
FILE *f = fopen("filtered_image.txt", "w");
if(f == NULL){
printf("Cannot open filtered_image.txt\n");
exit(1);
}
int pixels_counter = 0;
int padded_size = PIXELS*PIXELS + 4*HOST_PADDING*PIXELS + 4*HOST_PADDING*HOST_PADDING;
int start = PIXELS*HOST_PADDING + 2*HOST_PADDING*HOST_PADDING + HOST_PADDING; //skip first padding rows
for(int i=start; i<(padded_size-start); i++){
fprintf(f, "%f ", filtered_image[i]);
pixels_counter++;
if(pixels_counter == PIXELS){
pixels_counter = 0;
i += 2*HOST_PADDING;
fprintf(f, "\n");
}
}
fclose(f);
free(host_image);
cudaFree(filtered_image);
printf("*NLM-CUDA-SHARED Duration = %f second(s)* || (Pixels, Patch) = (%d, %d)\n", elapsed_time(tic,toc), PIXELS, PATCH_SIZE);
return 0;
}
__host__ float *nonLocalMeans(float *host_image){
int padded_size = PIXELS*PIXELS + 4*HOST_PADDING*PIXELS + 4*HOST_PADDING*HOST_PADDING;
float *G;
cudaMallocManaged(&G, PATCH_SIZE*PATCH_SIZE*sizeof(float));
if(G == NULL){
exit(1);
}
float *temp = gaussian_filter();
memcpy(G, temp, PATCH_SIZE*PATCH_SIZE*sizeof(float));
//host_image is not know to both the host and device, hence the memcpy
float *image;
cudaMallocManaged(&image, padded_size*sizeof(float));
if(image == NULL){
exit(1);
}
memcpy(image, host_image, padded_size*sizeof(float));
float *filtered_image;
cudaMallocManaged(&filtered_image, padded_size*sizeof(float));
if(filtered_image == NULL){
exit(1);
}
// Fill array with -1, so after adding the image's values
// the padding will have -1 values
for(int i=0; i<padded_size; i++){
filtered_image[i]=(float)-1;
}
//! KERNEL
int shared_memory_size = PATCH_SIZE*(PIXELS + 2*HOST_PADDING);
denoise_image<<<PIXELS, PIXELS, shared_memory_size*sizeof(float)>>>(filtered_image, image, padded_size, G);
cudaDeviceSynchronize();
//! KERNEL
cudaFree(G);
cudaFree(image);
return filtered_image;
}
//! Compute the gaussian filter
__host__ float *gaussian_filter(){
float *G = (float *)malloc(PATCH_SIZE*PATCH_SIZE*sizeof(float));
if(G == NULL){
exit(1);
}
// bound for the 2D Gaussian filter
int bound = PATCH_SIZE/2;
for(int x=-bound; x<=bound; x++){
for(int y=-bound; y<=bound; y++){
int index = (x+bound)*PATCH_SIZE + (y+bound);
G[index] = exp( -(float)(x*x+y*y)/(float)(2*PATCH_SIGMA*PATCH_SIGMA) ) / (float)(2*M_PI*PATCH_SIGMA*PATCH_SIGMA);
}
}
return G;
}
__global__ void denoise_image(float *filtered_image, float *image, int padded_size, float *G){
int index = blockIdx.x*(blockDim.x+2*DEV_PADDING) + (threadIdx.x+DEV_PADDING) + DEV_PADDING*DEV_PIXELS + 2*DEV_PADDING*DEV_PADDING;
int row_size = DEV_PIXELS + 2*DEV_PADDING;
//safety-check if
if(index < padded_size){
//shared memory
extern __shared__ float shared_memory[];
//each thread handles its column
for(int i=0; i<DEV_PATCH_SIZE; i++){
shared_memory[(threadIdx.x + DEV_PADDING) + i*row_size] = image[(threadIdx.x+DEV_PADDING) + i*row_size];
}
//thread #0 also handles the side paddings
if(threadIdx.x == 0){
for(int row=0; row<DEV_PADDING; row++){
for(int col=0; col<DEV_PATCH_SIZE; col++){
shared_memory[row + col*row_size] = -1;
}
}
for(int row=(DEV_PADDING+DEV_PIXELS); row<row_size; row++){
for(int col=0; col<DEV_PATCH_SIZE; col++){
shared_memory[row + col*row_size] = -1;
}
}
}
__syncthreads();
//creating i's patch
float patch_i[DEV_PATCH_SIZE*DEV_PATCH_SIZE];
for(int it1=0; it1<DEV_PATCH_SIZE; it1++){
for(int it2=0; it2<DEV_PATCH_SIZE; it2++){
patch_i[it1*DEV_PATCH_SIZE + it2] = image[index + (it1-DEV_PADDING)*row_size + it2 - DEV_PADDING];
}
}
filtered_image[index] = 0;
float weight;
float Z = 0;
for(int it1=DEV_PADDING; it1<(DEV_PIXELS+DEV_PADDING); it1++){
for(int it2=DEV_PADDING; it2<(DEV_PIXELS+DEV_PADDING); it2++){
float comp_value = 0;
compare_patches(&comp_value, patch_i, it2, G, shared_memory);
weight = (float)(exp(-comp_value/(DEV_FILTER_SIGMA*DEV_FILTER_SIGMA)));
filtered_image[index] += weight * shared_memory[DEV_PADDING*row_size + it2];
Z += weight;
}
__syncthreads();
//alter the shared memory, slide everything one row up
for(int i=0; i<DEV_PATCH_SIZE-1; i++){
shared_memory[(threadIdx.x+DEV_PADDING) + i*row_size] = shared_memory[(threadIdx.x+DEV_PADDING) + (i+1)*row_size];
}
int row_offset = (it1+1-DEV_PADDING)*row_size;
//insert the new row in the shared_memory
shared_memory[(threadIdx.x+DEV_PADDING) + (DEV_PATCH_SIZE-1)*row_size] = image[row_offset + (threadIdx.x+DEV_PADDING) + (DEV_PATCH_SIZE-1)*row_size];
__syncthreads();
}
filtered_image[index] = filtered_image[index] / Z;
}
}
//! Compares patch_i with the patch of pixel j
__device__ void compare_patches(float *comp_value, float *patch_i, int j, float *G, float *shared_memory){
int offset = DEV_PADDING*(DEV_PIXELS + 2*DEV_PADDING);
j += offset;
for(int it1=0; it1<DEV_PATCH_SIZE; it1++){
for(int it2=0; it2<DEV_PATCH_SIZE; it2++){
int first_index = it1*DEV_PATCH_SIZE+it2;
int second_index = j+(it1-DEV_PADDING)*(DEV_PIXELS+2*DEV_PADDING) + it2 - DEV_PADDING;
// patch/shared_memory[x] == -1 means it's the added padding
if(patch_i[first_index] != (float)-1 && shared_memory[second_index] != (float)-1){
float diff = patch_i[first_index] - shared_memory[second_index];
*comp_value += G[first_index]*(diff*diff);
}
}
}
}