-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_pixels.cu
159 lines (128 loc) · 4.47 KB
/
sort_pixels.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
#include <fstream>
#include <string>
#include <sstream>
#include <cuda.h>
#include "BmpImage.h"
#include "kernels.cu"
#define THRESHOLD 10010000
using namespace std;
RGBTRIPLE blackRgb = { 0, 0, 0};
RGBTRIPLE whiteRgb = { 255, 255, 255};
string IntToString (int a)
{
ostringstream temp;
temp<<a;
return temp.str();
}
int StringToInt(string str){
int i;
istringstream iss(str);
iss >> i;
return i;
}
void SavePixelsToIntArray(BmpImage *image, int *pixels, int imageHeight, int imageWidth)
{
for (int row = 0; row < imageHeight; row++)
{
for (int column = 0; column < imageWidth; column++)
{
pixels[row*imageWidth + column] = image->GetPixel24AsInt(column, row);
}
}
}
void SaveSortedPixelsToImage(BmpImage *image, int *pixels, int imageHeight, int imageWidth)
{
for (int row = 0; row < imageHeight; row++)
{
for (int column = 0; column < imageWidth; column++)
{
image->SetPixel24AsInt(column, row, pixels[row*imageWidth + column]);
}
}
}
void sortPixelsGpu(BmpImage *image, int colorMode, int blockSize, int chunkSize, bool optimized){
int *pixels_h, *pixels_d;
int imageWidth = image->GetWidth();
int imageHeight = image->GetHeight();
int imageSize = image->GetSize();
string timeInfo;
float timeElapsed = 0;
// eventes to get time
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
int totalSize = imageSize * sizeof(int);
pixels_h = (int *)malloc(totalSize);
SavePixelsToIntArray(image, pixels_h, imageHeight, imageWidth);
int rowsRemaining = imageHeight;
int iteration = 1;
cout << "Chunk size: " << chunkSize << endl;
while(rowsRemaining > 0)
{
int currentRows = (rowsRemaining > chunkSize) ? chunkSize : rowsRemaining;
int currentRowsSize = currentRows * imageWidth * sizeof(int);
int offset = imageHeight - rowsRemaining;
cout << "Iteration " << iteration << ":" << endl;
cout << "\tRows remaining: " << rowsRemaining << endl;
cudaMalloc((void **) &pixels_d, currentRowsSize);
cudaMemcpy(pixels_d, pixels_h + offset * imageWidth, currentRowsSize, cudaMemcpyHostToDevice);
int n_blocks = currentRows / blockSize + (imageHeight % blockSize == 0 ? 0:1);
cout << "\tBlocks: " << n_blocks << endl;
cout << "\tBlock size: " << blockSize << endl;
// invoking kernel and timing
cudaEventRecord(start);
if(optimized)
{
optimizedSortRows<<< n_blocks, blockSize >>> (pixels_d, imageHeight, imageWidth, colorMode);
timeInfo = "Optimized kernel execution time: ";
}
else
{
sortRows<<< n_blocks, blockSize >>> (pixels_d, imageHeight, imageWidth, colorMode);
timeInfo = "Kernel execution time: ";
}
cudaEventRecord(stop);
cudaMemcpy(pixels_h + offset * imageWidth, pixels_d, currentRowsSize, cudaMemcpyDeviceToHost);
cudaEventSynchronize(stop);
float tempTime;
cudaEventElapsedTime(&tempTime, start, stop);
timeElapsed += tempTime;
cudaFree(pixels_d);
rowsRemaining -= currentRows;
iteration++;
}
SaveSortedPixelsToImage(image, pixels_h, imageHeight, imageWidth);
cout << timeInfo << timeElapsed << "ms" << endl << endl;
free(pixels_h);
}
int main(int argc, char *argv[]){
if(argc < 5){
cout << "Not enough arguments - example call: apl_project <filename> <optimized> <blockSize> <chunkSize>" << endl;
return 1;
}
BmpImage image;
string filename = argv[1];
int optimized = StringToInt(argv[2]);
int blockSize = StringToInt(argv[3]);
int chunkSize = StringToInt(argv[4]);
cudaSetDevice(0);
cout << "----------------TEST RUN FOR BLOCKSIZE: " << blockSize << "----------------" << endl;
if(!image.Load(filename))
{
cout << "File didn't load correctly!" << endl;
return 1;
}
if(optimized)
{
sortPixelsGpu(&image, image.ParseRgbTripleToInt(whiteRgb), blockSize, chunkSize, true);
string optimizedSortedfile = IntToString(blockSize) + "_optimized_" + filename;
image.Save(optimizedSortedfile);
}
else
{
sortPixelsGpu(&image, image.ParseRgbTripleToInt(whiteRgb), blockSize, chunkSize, false);
string sortedfile = IntToString(blockSize) + "_" + filename;
image.Save(sortedfile);
}
return 0;
}