-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.cu
284 lines (241 loc) · 7.55 KB
/
kernel.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
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "device_functions.h"
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <iostream>
#include <string>
#include <ctime>
#define TILE_SIZE 16
using namespace cv;
using namespace std;
cudaError_t eqHist(unsigned char *input, unsigned int width, unsigned int height, unsigned char *output);
// Kernel
// Add GPU kernel and functions
__global__ void kernel(unsigned char *input, unsigned char *output)
{
int x = blockIdx.x * TILE_SIZE + threadIdx.x;
int y = blockIdx.y * TILE_SIZE + threadIdx.y;
int location = y * TILE_SIZE * gridDim.x + x;
output[location] = x % 255;
}
int main(int argc, const char **argv)
{
// rudimentory timer
clock_t start, stop;
clock_t duration, durationCV, durationCUDA;
// read in image
if (argc != 2)
{
cout << "Usage: " << argv[0] << " IMAGE_LOCATION" << endl;
return -1;
}
Mat src = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
if (src.empty())
{
cout << "������ͼƬ��" << endl;
return -1;
}
// Mat -> vector
int imgWidth = src.cols;
int imgHeight = src.rows;
int imgSize = imgWidth * imgHeight;
unsigned char *img = new unsigned char[imgSize];
memcpy(img, src.data, imgSize * sizeof(unsigned char));
start = clock();
// get histogram
unsigned int Hist[256] = {0};
for (int i = 0; i < imgSize; ++i)
++Hist[img[i]];
int mincdf;
for (int i = 0; i < 256; ++i)
{
if (Hist[i] > 0)
{
mincdf = i;
break;
}
}
// histogram addition
unsigned int cdfHist[256] = {0};
cdfHist[0] = Hist[0];
for (int i = 1; i < 256; ++i)
{
cdfHist[i] = cdfHist[i - 1] + Hist[i];
}
// generate look-up table
double lutTemp[256] = {0};
for (int i = mincdf + 1; i < 256; ++i)
{
lutTemp[i] = 255.0 * (cdfHist[i] - cdfHist[mincdf]) / (imgSize - cdfHist[mincdf]);
}
unsigned char lut[256] = {0};
for (int i = 0; i < 256; ++i)
{
lut[i] = round(lutTemp[i]);
}
// transform LUT to output
unsigned char *img2 = new unsigned char[imgSize];
for (int i = 0; i < imgSize; ++i)
{
img2[i] = lut[img[i]];
}
stop = clock();
duration = stop - start;
// using OpenCV
Mat imgCV;
start = clock();
equalizeHist(src, imgCV);
stop = clock();
durationCV = stop - start;
// vector -> Mat
Mat output(imgHeight, imgWidth, CV_8U);
memcpy(output.data, img2, imgSize * sizeof(unsigned char));
// comparing OpenCV & CPU version
bool testOK = true;
for (int i = 0; i < imgSize; ++i)
{
int temp = abs(int(imgCV.data[i]) - int(img2[i]));
if (temp != 0)
{
testOK = false;
break;
}
}
if (testOK)
cout << "User defined function PASSED!" << endl;
else
cout << "User defined function FAILED!" << endl;
imshow("CPU-Output", output);
imshow("OpenCV-Output", imgCV);
imshow("Origin", src);
waitKey(0);
// GPU calculation
unsigned char *imgGPU = new unsigned char[imgSize];
start = clock();
cudaError_t cudaStatus = eqHist(img, imgWidth, imgHeight, imgGPU);
stop = clock();
durationCUDA = stop - start;
if (cudaStatus != cudaSuccess)
{
fprintf(stderr, "eqHist on CUDA failed!");
return 1;
}
cout << "Calculation Time:\nUser: " << duration << "\tOpenCV: " << durationCV
<< "\tCalculation Time:\nCUDA: " << durationCUDA << endl;
// comparing OpenCV & GPU
bool testGPU = true;
int count = 0;
for (int i = 0; i < imgSize; ++i)
{
int temp = int(imgCV.data[i]) - int(imgGPU[i]);
if (abs(temp) > 20)
++count;
}
//GPU Error shouldn't be more than 1%
if (count > imgSize / 100)
testGPU = false;
if (testGPU)
cout << "CUDA PASSED! DIFFERENCE COUNT: " << count << endl;
else
cout << "CUDA FAILED! DIFFERENCE COUNT: " << count << endl;
// vector -> Mat
Mat outputGPU(imgHeight, imgWidth, CV_8U);
memcpy(outputGPU.data, imgGPU, imgSize * sizeof(unsigned char));
imshow("CUDA�����ͼƬ", outputGPU);
waitKey(0);
// cudaDeviceReset must be called before exiting in order for profiling and
// tracing tools such as Nsight and Visual Profiler to show complete traces.
cudaStatus = cudaDeviceReset();
if (cudaStatus != cudaSuccess)
{
fprintf(stderr, "cudaDeviceReset failed!");
return 1;
}
system("pause");
return 0;
}
//histogram equalization������
cudaError_t eqHist(unsigned char *input, unsigned int width, unsigned int height, unsigned char *output)
{
cudaError_t cudaStatus;
int gridXSize = 1 + ((width - 1) / TILE_SIZE);
int gridYSize = 1 + ((height - 1) / TILE_SIZE);
int XSize = gridXSize * TILE_SIZE;
int YSize = gridYSize * TILE_SIZE;
// Both are the same size (CPU/GPU).
int size = XSize * YSize;
unsigned char *input_gpu;
unsigned char *output_gpu;
// Choose which GPU to run on, change this on a multi-GPU system.
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess)
{
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
goto Error;
}
// Allocate GPU buffers for image
cudaStatus = cudaMalloc((void **)&input_gpu, size * sizeof(unsigned char));
if (cudaStatus != cudaSuccess)
{
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}
cudaStatus = cudaMalloc((void **)&output_gpu, size * sizeof(unsigned char));
if (cudaStatus != cudaSuccess)
{
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}
// Set tho output image to 0
cudaStatus = cudaMemset(output_gpu, 0, 256 * sizeof(unsigned char));
if (cudaStatus != cudaSuccess)
{
fprintf(stderr, "cudaMemset failed!");
goto Error;
}
// Copy input vectors from host memory to GPU buffers.
cudaStatus = cudaMemcpy(input_gpu, input, size * sizeof(unsigned char), cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess)
{
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess)
{
fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
goto Error;
}
// Execute algorithm
dim3 dimGrid(gridXSize, gridYSize);
dim3 dimBlock(TILE_SIZE, TILE_SIZE);
// Kernel Call
kernel<<<dimGrid, dimBlock>>>(input_gpu, output_gpu);
// Check for any errors launching the kernel
cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess)
{
fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
goto Error;
}
// cudaDeviceSynchronize waits for the kernel to finish, and returns
// any errors encountered during the launch.
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess)
{
fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
goto Error;
}
// Copy output vector from GPU buffer to host memory.
cudaStatus = cudaMemcpy(output, output_gpu, size * sizeof(unsigned char), cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess)
{
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
Error:
cudaFree(input_gpu);
cudaFree(output_gpu);
return cudaStatus;
}