-
Notifications
You must be signed in to change notification settings - Fork 3
/
kernel.cu
452 lines (370 loc) · 13.3 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
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
#include <cstdio>
#include <cuda_runtime.h>
#include "device_launch_parameters.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wb.h"
using namespace std;
#define BLOCK_SIZE 1024
#define HISTOGRAM_SIZE 256
/**
* File Operations
*/
struct Image {
int width;
int height;
int channels;
int colors;
float* data;
// Used 1 for grayscale images.
Image(int imageWidth = 0, int imageHeight = 0, int imageChannels = 1, int imageColors = wbInternal::kImageColorLimit) : width(imageWidth), height(imageHeight), channels(imageChannels), colors(imageColors), data(NULL) {
const int numElements = width * height * channels;
// Prevent zero-length memory allocation
if (numElements > 0)
data = new float[numElements];
}
};
Image importImage(const char* fName)
{
ifstream inFile(fName, ios::binary);
if (!inFile.is_open())
{
cerr << "Error opening image file " << fName << ". " << wbInternal::wbStrerror(errno) << endl;
exit(EXIT_FAILURE);
}
// Read PGM image header
string magic;
getline(inFile, magic);
// use P2 format
// if (magic != "P2")
// {
// cerr << "Error reading image file " << fName << ". " << "Expecting 'P2' image format but got '" << magic << "'" << endl;
// inFile.close();
// exit(EXIT_FAILURE);
// }
// Filter image comments
if (inFile.peek() == '#')
{
string commentStr;
getline(inFile, commentStr);
}
Image image;
inFile >> image.width;
if (inFile.fail() || 0 >= image.width)
{
cerr << "Error reading width of image in file " << fName << endl;
inFile.close();
exit(EXIT_FAILURE);
}
inFile >> image.height;
if (inFile.fail() || 0 >= image.height)
{
cerr << "Error reading height of image in file " << fName << endl;
inFile.close();
exit(EXIT_FAILURE);
}
inFile >> image.colors;
if (inFile.fail() || image.colors > wbInternal::kImageColorLimit)
{
cerr << "Error reading colors value of image in file " << fName << endl;
inFile.close();
exit(EXIT_FAILURE);
}
while (isspace(inFile.peek()))
{
inFile.get();
}
// not need raw data
const int numElements = image.width * image.height * image.channels;
float* data = new float[numElements];
for (int i = 0; i < numElements; i++)
{
inFile >> data[i];
}
inFile.close();
image.data = data;
return image;
}
void saveImage(const Image& image, const char* fName) {
ostringstream oss;
oss << "P2\n" << "# Created by applying histogram " << "\n" << image.width << " " << image.height << "\n" << image.colors << "\n";
string headerStr(oss.str());
ofstream outFile(fName, ios::binary);
outFile.write(headerStr.c_str(), headerStr.size());
const int numElements = image.width * image.height * image.channels;
for (int i = 0; i < numElements; ++i)
{
outFile << (int)image.data[i] << " ";
}
outFile.close();
}
int wbImage_getWidth(const Image& image)
{
return image.width;
}
int wbImage_getHeight(const Image& image)
{
return image.height;
}
int wbImage_getChannels(const Image& image)
{
return image.channels;
}
float* wbImage_getData(const Image& image)
{
return image.data;
}
Image createImage(const int imageWidth, const int imageHeight, const int imageChannels)
{
Image image(imageWidth, imageHeight, imageChannels);
return image;
}
void wbImage_delete(Image& image)
{
delete[] image.data;
}
/**
* Kernels
*/
__global__ void calculateHistogram(bool usingPrivatization, float *buffer, float *histogram, int size) {
if (usingPrivatization) {
// The idea of privatization is to replicate highly contended data structures into private copies
// so that each thread (or each subset of threads) can access a private copy
// The benefit is that the private copies can be accessed with much less contention and often at much lower latency.
__shared__ unsigned int cache[HISTOGRAM_SIZE];
if (threadIdx.x < HISTOGRAM_SIZE) {
cache[threadIdx.x] = 0;
}
__syncthreads();
// Used interleaved partitioning.
// All threads process a contiguous section of elements.
// They all move to the next section and repeat.
// The memory accesses are coalesced.
int tid = threadIdx.x + blockDim.x * blockIdx.x;
// Define stride. Stride is the total number of threads.
int stride = blockDim.x * gridDim.x;
while (tid < size) {
atomicAdd(&(cache[(int)buffer[tid]]), 1);
tid += stride;
}
__syncthreads();
if (threadIdx.x < HISTOGRAM_SIZE) {
atomicAdd(&(histogram[threadIdx.x]), cache[threadIdx.x]);
}
}
else {
// Used interleaved partitioning.
// All threads process a contiguous section of elements.
// They all move to the next section and repeat.
// The memory accesses are coalesced.
int tid = threadIdx.x + blockDim.x * blockIdx.x;
// Define stride. Stride is the total number of threads.
int stride = blockDim.x * gridDim.x;
while (tid < size) {
atomicAdd(&(histogram[(int)buffer[tid]]), 1);
tid += stride;
}
}
}
__global__ void createCumulativeDistributionFunction(int scanType, float *cdf, float *histogram, int size) {
// In probability theory and statistics, the cumulative distribution function (CDF) of a real-valued random variable X,
// or just distribution function of X, evaluated at x, is the probability that X will take a value less than or equal to x.
if (scanType == 0) {
// Used Kogge-Stone Scan to create CDF.
__shared__ float scan[HISTOGRAM_SIZE];
int tid = threadIdx.x + blockIdx.x * blockDim.x;
if (tid < size) {
scan[threadIdx.x] = histogram[tid];
}
for (unsigned int stride = 1; stride < blockDim.x; stride *= 2) {
__syncthreads();
if (threadIdx.x >= stride) {
scan[threadIdx.x] += scan[threadIdx.x - stride];
}
}
__syncthreads();
for(int i = 0 ; i< HISTOGRAM_SIZE ; i++ ) {
cdf[i] = scan[i];
}
}
else {
// Used Brent–Kung Scan to create CDF.
__shared__ float scan[2 * HISTOGRAM_SIZE];
int tid = threadIdx.x + blockDim.x * blockIdx.x;
if (tid < HISTOGRAM_SIZE) {
scan[threadIdx.x] = histogram[tid];
}
__syncthreads();
// Reduction
for (unsigned int stride = 1; stride <= blockDim.x; stride *= 2) {
__syncthreads();
int index = (threadIdx.x + 1) * stride * 2 - 1;
if (index < blockDim.x) {
scan[index] += scan[index - stride];
}
}
// Post Reduction
for (unsigned int stride = HISTOGRAM_SIZE / 4; stride > 0; stride /= 2) {
__syncthreads();
int index = (threadIdx.x + 1) * stride * 2 - 1;
if (index + stride < HISTOGRAM_SIZE) {
scan[index + stride] += scan[index];
}
}
__syncthreads();
for (int i = 0; i < HISTOGRAM_SIZE; i ++) {
cdf[i] = scan[i];
}
}
}
__global__ void calculateCumulativeDistributionFunction(float *cdf, float *cdfClone, int size) {
float minimum = cdf[0];
cdfClone[threadIdx.x] = ((cdf[threadIdx.x] - minimum) / (size - minimum) * (HISTOGRAM_SIZE - 1));
}
__global__ void equalizeHistogram(float *outputImageData, float *inputImageData, float *cdf, int size) {
int tid = threadIdx.x + blockIdx.x * blockDim.x;
if (tid < size){
outputImageData[tid] = cdf[(int)inputImageData[tid]];
}
__syncthreads();
}
int main(int argc, char ** argv) {
if (argc != 5) {
printf("Usage error. Program expects four argument. \n");
printf("Usage: ./histogram IMAGENAME PRIVATIZATION SCANTYPE OUTPUTIMAGENAME \n");
printf("Usage Example: ./histogram 1.pgm 1 1 output.pgm\n");
exit(1);
}
// System specifications
printf("-->\n");
printf("System Specifications:\n");
printf("\tAzure NC6\n");
printf("\tCores: 6\n");
printf("\tGPU: Tesla K80\n");
printf("\tMemory: 56 GB\n");
printf("\tDisk: 380 GB SSD\n");
printf("-->\n");
const char * inputImageFile;
const char * outputImageFile;
Image inputImage;
Image outputImage;
float * hostInputImageData;
float * hostOutputImageData;
float * hostHistogram;
float *deviceInputImageData;
float *deviceOutputImageData;
float *deviceHistogram;
float *cdfClone;
float *cdf;
bool usingPrivatization;
int scanType;
const char * strScanType;
const char * strPrivatization;
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
// Error code to check return values for CUDA calls
cudaError_t err = cudaSuccess;
inputImageFile = argv[1];
outputImageFile = argv[4];
if (string(argv[2]) == "0") {
usingPrivatization = false;
strPrivatization = "Privatization is not included.";
printf("Privatization was set to false.\n");
}
else {
usingPrivatization = true;
strPrivatization = "Privatization is included.";
printf("Privatization was set to true.\n");
}
if (string(argv[3]) == "0") {
scanType = 0;
strScanType = "Kogge-Stone is used.";
printf("Scan type was set to Kogge-Stone.\n");
}
else {
scanType = 1;
strScanType = "Brent-Kung is used.";
printf("Scan type was set to Brent-Kung.\n");
}
inputImage = importImage(inputImageFile);
hostHistogram = NULL;
hostInputImageData = wbImage_getData(inputImage);
int imageWidth = wbImage_getWidth(inputImage);
int imageHeight = wbImage_getHeight(inputImage);
int imageChannels = wbImage_getChannels(inputImage);
int imageSize = imageWidth * imageHeight * imageChannels;
printf("------\n");
printf("Image Info:\n");
printf("\tImage Size:%dx%d\n", imageWidth, imageHeight);
printf("\tImage Channel:%d\n", imageChannels);
printf("------\n");
outputImage = createImage(imageWidth, imageHeight, imageChannels);
hostOutputImageData = wbImage_getData(outputImage);
size_t sizeImage = imageWidth * imageHeight * imageChannels * sizeof(float);
size_t size = HISTOGRAM_SIZE * sizeof(float);
// Allocate GPU
err = cudaMalloc((void **)&deviceInputImageData, sizeImage);
if (err != cudaSuccess) {
fprintf(stderr, "Failed to allocate device input image (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMalloc((void **)&deviceOutputImageData, sizeImage);
if (err != cudaSuccess) {
fprintf(stderr, "Failed to allocate device output image (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMalloc((void **)&deviceHistogram, size);
if (err != cudaSuccess) {
fprintf(stderr, "Failed to allocate device histogram (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMalloc((void **)&cdfClone, size);
if (err != cudaSuccess) {
fprintf(stderr, "Failed to allocate clone cdf (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMalloc((void **)&cdf, size);
if (err != cudaSuccess) {
fprintf(stderr, "Failed to allocate cdf (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
// Copy image to device memory
printf("Copying image data from the host memory to the CUDA device...\n");
err = cudaMemcpy(deviceInputImageData, hostInputImageData, sizeImage, cudaMemcpyHostToDevice);
if (err != cudaSuccess) {
fprintf(stderr, "Failed to copy image from host to device (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
// Copy host histogram to device histogram
printf("Copying host histogram to device histogram...\n");
cudaMemcpy(deviceHistogram, hostHistogram, size, cudaMemcpyHostToDevice);
int gridDim = (imageSize + 1) / BLOCK_SIZE;
cudaEventRecord(start);
calculateHistogram <<< gridDim, BLOCK_SIZE >>>(usingPrivatization, deviceInputImageData, deviceHistogram, imageSize);
createCumulativeDistributionFunction <<< 1, HISTOGRAM_SIZE >>>(scanType, cdf, deviceHistogram, imageSize);
calculateCumulativeDistributionFunction <<< 1, HISTOGRAM_SIZE >>>(cdf, cdfClone, imageSize);
equalizeHistogram <<< gridDim, BLOCK_SIZE >>>(deviceOutputImageData, deviceInputImageData, cdfClone, imageSize);
cudaEventRecord(stop);
cudaEventSynchronize(stop);
printf("Copying device output image data to host output image data...\n");
err = cudaMemcpy(hostOutputImageData, deviceOutputImageData, sizeImage, cudaMemcpyDeviceToHost);
if (err != cudaSuccess) {
fprintf(stderr, "Failed to copy image from host to device (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
float ms = 0;
cudaEventElapsedTime(&ms, start, stop);
// Timings
printf("Execution time:\n");
printf("\tPrivatization: %s Scan type: %s \n\tElapsed time: %f milliseconds.\n", strPrivatization, strScanType, ms);
saveImage(outputImage, outputImageFile);
wbImage_delete(outputImage);
wbImage_delete(inputImage);
cudaFree(deviceInputImageData);
cudaFree(deviceOutputImageData);
cudaFree(deviceHistogram);
cudaFree(cdfClone);
return 0;
}