-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel_solution_v34.cu
375 lines (322 loc) · 15.3 KB
/
parallel_solution_v34.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
#include "parallel_solution_v34.cuh"
#include "timer.cuh"
namespace KernelFunction {
__device__ u_int32_t blockCountBackward;
__device__ u_int32_t blockCountForward;
__global__ void
updateSeamMapKernelPipeliningForward(int32_t *input, u_int32_t inputWidth,
bool volatile *isBlockFinished) {
// 1. Get block Index
__shared__ u_int32_t newBlockIdx;
if (threadIdx.x == 0) {
newBlockIdx = atomicAdd(&blockCountForward, 1);
}
__syncthreads();
u_int32_t numBlocksPerRow = ((inputWidth - 1) / blockDim.x + 1);
u_int32_t currentRow = (newBlockIdx / numBlocksPerRow) + 1;
u_int32_t currentRowBlock = newBlockIdx % numBlocksPerRow;
u_int32_t c = currentRowBlock * blockDim.x + threadIdx.x;
int32_t minVal = 0;
// 2. Waiting for before block newBlockIdx - numBlocksPerRow
if (threadIdx.x == 0) {
if (newBlockIdx >= numBlocksPerRow) {
while (!isBlockFinished[newBlockIdx - numBlocksPerRow]);
}
__threadfence();
}
__syncthreads();
if (c < inputWidth && threadIdx.x != blockDim.x - 1 && threadIdx.x != 0) {
minVal = input[convertIndex(currentRow - 1, c, inputWidth)];
if (c > 0)
minVal = min(minVal, input[convertIndex(currentRow - 1, c - 1, inputWidth)]);
if (c + 1 < inputWidth)
minVal = min(minVal, input[convertIndex(currentRow - 1, c + 1, inputWidth)]);
}
// 3. Waiting for before block newBlockIdx + 1 and newBlockIdx - 1
if (threadIdx.x == 0 || threadIdx.x == blockDim.x - 1) {
if (newBlockIdx > numBlocksPerRow) {
if (newBlockIdx % numBlocksPerRow != 0) {
while (!isBlockFinished[newBlockIdx - numBlocksPerRow - 1]);
}
if (newBlockIdx % numBlocksPerRow != numBlocksPerRow - 1) {
while (!isBlockFinished[newBlockIdx - numBlocksPerRow + 1]);
}
__threadfence();
}
if (c < inputWidth) {
minVal = input[convertIndex(currentRow - 1, c, inputWidth)];
if (c > 0)
minVal = min(minVal, input[convertIndex(currentRow - 1, c - 1, inputWidth)]);
if (c + 1 < inputWidth)
minVal = min(minVal, input[convertIndex(currentRow - 1, c + 1, inputWidth)]);
}
}
if (c < inputWidth)
input[convertIndex(currentRow, c, inputWidth)] += minVal;
__syncthreads();
// 4. Mark Threads as Done
__threadfence();
if (threadIdx.x == 0) {
isBlockFinished[newBlockIdx] = true;
}
}
__global__ void updateSeamMapKernelPipeliningBackward(int32_t *input, u_int32_t inputWidth, u_int32_t inputHeight,
volatile bool *isBlockFinished) {
// 1. Get block Index
__shared__ u_int32_t newBlockIdx;
if (threadIdx.x == 0) {
newBlockIdx = atomicAdd(&blockCountBackward, 1);
}
__syncthreads();
u_int32_t numBlocksPerRow = ((inputWidth - 1) / blockDim.x + 1);
u_int32_t currentRow = inputHeight - (newBlockIdx / numBlocksPerRow) - 2;
u_int32_t currentRowBlock = newBlockIdx % numBlocksPerRow;
u_int32_t c = currentRowBlock * blockDim.x + threadIdx.x;
int32_t minVal = 0;
// 2. Waiting for before block newBlockIdx - numBlocksPerRow
if (threadIdx.x == 0) {
if (newBlockIdx >= numBlocksPerRow) {
while (!isBlockFinished[newBlockIdx - numBlocksPerRow]);
}
__threadfence();
}
__syncthreads();
if (c < inputWidth && threadIdx.x != blockDim.x - 1 && threadIdx.x != 0) {
minVal = input[convertIndex(currentRow + 1, c, inputWidth)];
if (c > 0)
minVal = min(minVal, input[convertIndex(currentRow + 1, c - 1, inputWidth)]);
if (c + 1 < inputWidth)
minVal = min(minVal, input[convertIndex(currentRow + 1, c + 1, inputWidth)]);
}
// 3. Waiting for before block newBlockIdx + 1 and newBlockIdx - 1
if (threadIdx.x == 0 || threadIdx.x == blockDim.x - 1) {
if (newBlockIdx > numBlocksPerRow) {
if (newBlockIdx % numBlocksPerRow != 0) {
while (!isBlockFinished[newBlockIdx - numBlocksPerRow - 1]);
}
if (newBlockIdx % numBlocksPerRow != numBlocksPerRow - 1) {
while (!isBlockFinished[newBlockIdx - numBlocksPerRow + 1]);
}
__threadfence();
}
if (c < inputWidth) {
minVal = input[convertIndex(currentRow + 1, c, inputWidth)];
if (c > 0)
minVal = min(minVal, input[convertIndex(currentRow + 1, c - 1, inputWidth)]);
if (c + 1 < inputWidth)
minVal = min(minVal, input[convertIndex(currentRow + 1, c + 1, inputWidth)]);
}
}
if (c < inputWidth)
input[convertIndex(currentRow, c, inputWidth)] += minVal;
__syncthreads();
// 4. Mark Threads as Done
__threadfence();
if (threadIdx.x == 0) {
isBlockFinished[newBlockIdx] = true;
}
}
}
PnmImage ParallelSolutionV34::run(const PnmImage &inputImage, int argc, char **argv) {
// Extract arguments
int nDeletingSeams = 1;
dim3 blockSize(32, 32); // Default
if (argc > 0)
nDeletingSeams = int(strtol(argv[0], nullptr, 10));
if (argc > 1) {
blockSize.x = strtol(argv[1], nullptr, 10);
blockSize.y = strtol(argv[2], nullptr, 10);
}
// Start Timer
printf("Running Parallel Solution Version 3 + 4 with blockSize=(%d;%d).\n", blockSize.x, blockSize.y);
GpuTimer timer;
GpuTimer stepTimer;
float cal_energy_time = 0;
float cal_seam_time = 0;
float extract_seam_time = 0;
float delete_seam_time = 0;
timer.Start();
// Create Host Variable
PnmImage outputImage(inputImage.getWidth() - nDeletingSeams, inputImage.getHeight());
// Create Host Memory
auto *seam = (uint32_t *) malloc(inputImage.getHeight() * sizeof(uint32_t));
auto *energyMap = (int32_t *) malloc(inputImage.getHeight() * inputImage.getWidth() * sizeof(int32_t));
// Create Device Memory
uchar3 *d_inputImage;
CHECK(cudaMalloc(&d_inputImage, inputImage.getWidth() * inputImage.getHeight() * sizeof(uchar3)))
uchar3 *d_inputImageTemp;
CHECK(cudaMalloc(&d_inputImageTemp, inputImage.getWidth() * inputImage.getHeight() * sizeof(uchar3)))
int32_t *d_grayImage;
CHECK(cudaMalloc(&d_grayImage, inputImage.getWidth() * inputImage.getHeight() * sizeof(int32_t)))
int32_t *d_grayImageTemp;
CHECK(cudaMalloc(&d_grayImageTemp, inputImage.getWidth() * inputImage.getHeight() * sizeof(int32_t)))
int32_t *d_energyMap;
CHECK(cudaMalloc(&d_energyMap, inputImage.getWidth() * inputImage.getHeight() * sizeof(int32_t)))
int32_t *d_filterX;
CHECK(cudaMalloc(&d_filterX, FILTER_SIZE * FILTER_SIZE * sizeof(int32_t)))
int32_t *d_filterY;
CHECK(cudaMalloc(&d_filterY, FILTER_SIZE * FILTER_SIZE * sizeof(int32_t)))
// Copy Memory from Host to Device
CHECK(cudaMemcpy(d_inputImage, inputImage.getPixels(),
inputImage.getWidth() * inputImage.getHeight() * sizeof(uchar3), cudaMemcpyHostToDevice))
CHECK(cudaMemcpy(d_filterX, SOBEL_X, FILTER_SIZE * FILTER_SIZE * sizeof(int32_t), cudaMemcpyHostToDevice))
CHECK(cudaMemcpy(d_filterY, SOBEL_Y, FILTER_SIZE * FILTER_SIZE * sizeof(int32_t), cudaMemcpyHostToDevice))
// Run Kernel functions
convertToGrayScale(d_inputImage, inputImage.getWidth(), inputImage.getHeight(), blockSize, d_grayImage);
for (int i = 0; i < nDeletingSeams; ++i) {
// 1. Calculate the Energy Map
stepTimer.Start();
calculateEnergyMap(d_grayImage, inputImage.getWidth() - i, inputImage.getHeight(), d_filterX, d_filterY,
FILTER_SIZE, blockSize, d_energyMap);
stepTimer.Stop();
cal_energy_time += stepTimer.Elapsed();
// 2. Dynamic Programming
stepTimer.Start();
calculateSeamMap(d_energyMap, inputImage.getWidth() - i, inputImage.getHeight(), blockSize.x * blockSize.y);
stepTimer.Stop();
cal_seam_time += stepTimer.Elapsed();
// 3. Extract the seam
stepTimer.Start();
CHECK(cudaMemcpy(energyMap, d_energyMap,
(inputImage.getWidth() - i) * inputImage.getHeight() * sizeof(int32_t),
cudaMemcpyDeviceToHost))
extractSeam(energyMap, inputImage.getWidth() - i, inputImage.getHeight(), seam);
stepTimer.Stop();
extract_seam_time += stepTimer.Elapsed();
// 4. Delete the seam
stepTimer.Start();
deleteSeam(d_grayImage, inputImage.getWidth() - i, inputImage.getHeight(), seam, blockSize, d_grayImageTemp);
deleteSeam(d_inputImage, inputImage.getWidth() - i, inputImage.getHeight(), seam, blockSize, d_inputImageTemp);
stepTimer.Stop();
delete_seam_time += stepTimer.Elapsed();
swap(d_grayImage, d_grayImageTemp);
swap(d_inputImage, d_inputImageTemp);
}
// Copy memory from device to host
CHECK(cudaMemcpy(outputImage.getPixels(), d_inputImage,
outputImage.getWidth() * outputImage.getHeight() * sizeof(uchar3), cudaMemcpyDeviceToHost))
// Free Device Memory
CHECK(cudaFree(d_inputImage))
CHECK(cudaFree(d_inputImageTemp))
CHECK(cudaFree(d_grayImage))
CHECK(cudaFree(d_grayImageTemp))
CHECK(cudaFree(d_energyMap))
CHECK(cudaFree(d_filterX))
CHECK(cudaFree(d_filterY))
// Free Host Memory
free(seam);
free(energyMap);
// Stop Timer
timer.Stop();
printf("Time: %.3f ms\n", timer.Elapsed());
printf("Step time: 2) %.3f ms \t 3) %.3f ms \t 4) %.3f ms \t 5) %.3f ms\n", cal_energy_time, cal_seam_time, extract_seam_time, delete_seam_time);
printf("-------------------------------\n");
// Return
return outputImage;
}
void ParallelSolutionV34::calculateSeamMap(int32_t *d_inputImage, uint32_t inputWidth, uint32_t inputHeight,
uint32_t blockSize) {
// Create Host Memory
uint32_t gridSizeForward = ((inputWidth - 1) / blockSize + 1) * (inputHeight / 2 - 1);
uint32_t gridSizeBackward = ((inputWidth - 1) / blockSize + 1) * ((inputHeight - (inputHeight / 2)) - 1);
uint32_t zero = 0;
// Create Device Memory
bool *isBlockFinishedForward;
CHECK(cudaMalloc(&isBlockFinishedForward, gridSizeForward * sizeof(bool)))
bool *isBlockFinishedBackward;
CHECK(cudaMalloc(&isBlockFinishedBackward, gridSizeBackward * sizeof(bool)))
// Copy Memory from Host to Device
CHECK(cudaMemcpyToSymbol(KernelFunction::blockCountForward, &zero, sizeof(u_int32_t), 0, cudaMemcpyHostToDevice))
CHECK(cudaMemset(isBlockFinishedForward, 0, gridSizeForward * sizeof(bool)))
CHECK(cudaMemcpyToSymbol(KernelFunction::blockCountBackward, &zero, sizeof(u_int32_t), 0, cudaMemcpyHostToDevice))
CHECK(cudaMemset(isBlockFinishedBackward, 0, gridSizeBackward * sizeof(bool)))
// Run Device Methods
cudaStream_t streamForward, streamBackward;
cudaStreamCreate(&streamForward);
cudaStreamCreate(&streamBackward);
KernelFunction::updateSeamMapKernelPipeliningForward<<<gridSizeForward, blockSize, 0, streamForward>>>(d_inputImage, inputWidth, isBlockFinishedForward);
KernelFunction::updateSeamMapKernelPipeliningBackward<<<gridSizeBackward, blockSize, 0, streamBackward>>>(d_inputImage, inputWidth, inputHeight, isBlockFinishedBackward);
cudaStreamSynchronize(streamForward);
cudaStreamSynchronize(streamBackward);
CHECK(cudaGetLastError())
cudaStreamDestroy(streamForward);
cudaStreamDestroy(streamBackward);
// Copy Memory from Device to Host
// Free Device Memory
CHECK(cudaFree(isBlockFinishedForward))
CHECK(cudaFree(isBlockFinishedBackward))
// Free Host Memory
// Return result
}
void
ParallelSolutionV34::extractSeam(const int32_t *energyMap, uint32_t inputWidth, uint32_t inputHeight, uint32_t *seam) {
// Find minSeam
u_int32_t minValCol1 = 0;
u_int32_t minValCol2 = 0;
u_int32_t middleRow = inputHeight / 2 - 1;
int32_t bestVal = energyMap[KernelFunction::convertIndex(middleRow, 0, inputWidth)] +
energyMap[KernelFunction::convertIndex(middleRow + 1, 0, inputWidth)];
for (int c = 0; c < inputWidth; ++c) {
if (energyMap[KernelFunction::convertIndex(middleRow, c, inputWidth)] +
energyMap[KernelFunction::convertIndex(middleRow + 1, c, inputWidth)]
< bestVal) {
bestVal = energyMap[KernelFunction::convertIndex(middleRow, c, inputWidth)] +
energyMap[KernelFunction::convertIndex(middleRow + 1, c, inputWidth)];
minValCol1 = c;
minValCol2 = c;
}
if (c > 0 &&
energyMap[KernelFunction::convertIndex(middleRow, c - 1, inputWidth)] +
energyMap[KernelFunction::convertIndex(middleRow + 1, c, inputWidth)]
<= bestVal) {
bestVal = energyMap[KernelFunction::convertIndex(middleRow, c - 1, inputWidth)] +
energyMap[KernelFunction::convertIndex(middleRow + 1, c, inputWidth)];
minValCol1 = c - 1;
minValCol2 = c;
}
if (c + 1 < inputWidth &&
energyMap[KernelFunction::convertIndex(middleRow, c + 1, inputWidth)] +
energyMap[KernelFunction::convertIndex(middleRow + 1, c, inputWidth)]
< bestVal) {
bestVal = energyMap[KernelFunction::convertIndex(middleRow, c + 1, inputWidth)] +
energyMap[KernelFunction::convertIndex(middleRow + 1, c, inputWidth)];
minValCol1 = c + 1;
minValCol2 = c;
}
}
// Trace back
seam[inputHeight / 2 - 1] = minValCol1;
seam[inputHeight / 2] = minValCol2;
for (int r = int(inputHeight / 2) - 2; r >= 0; --r) {
auto c = minValCol1;
if (c > 0) {
if (energyMap[KernelFunction::convertIndex(r, c - 1, inputWidth)] <=
energyMap[KernelFunction::convertIndex(r, minValCol1, inputWidth)]) {
minValCol1 = c - 1;
}
}
if (c + 1 < inputWidth) {
if (energyMap[KernelFunction::convertIndex(r, c + 1, inputWidth)] <
energyMap[KernelFunction::convertIndex(r, minValCol1, inputWidth)]) {
minValCol1 = c + 1;
}
}
seam[r] = minValCol1;
}
for (int r = int(inputHeight / 2) + 1; r < inputHeight; ++r) {
auto c = minValCol2;
if (c > 0) {
if (energyMap[KernelFunction::convertIndex(r, c - 1, inputWidth)] <=
energyMap[KernelFunction::convertIndex(r, minValCol2, inputWidth)]) {
minValCol2 = c - 1;
}
}
if (c + 1 < inputWidth) {
if (energyMap[KernelFunction::convertIndex(r, c + 1, inputWidth)] <
energyMap[KernelFunction::convertIndex(r, minValCol2, inputWidth)]) {
minValCol2 = c + 1;
}
}
seam[r] = minValCol2;
}
}