| 1 |
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. |
| 2 |
* |
| 3 |
* Redistribution and use in source and binary forms, with or without |
| 4 |
* modification, are permitted provided that the following conditions |
| 5 |
* are met: |
| 6 |
* * Redistributions of source code must retain the above copyright |
| 7 |
* notice, this list of conditions and the following disclaimer. |
| 8 |
* * Redistributions in binary form must reproduce the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer in the |
| 10 |
* documentation and/or other materials provided with the distribution. |
| 11 |
* * Neither the name of NVIDIA CORPORATION nor the names of its |
| 12 |
* contributors may be used to endorse or promote products derived |
| 13 |
* from this software without specific prior written permission. |
| 14 |
* |
| 15 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 16 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 19 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
/** |
| 29 |
************************************************************************** |
| 30 |
* \file dct8x8.cu |
| 31 |
* \brief Contains entry point, wrappers to host and device code and benchmark. |
| 32 |
* |
| 33 |
* This sample implements forward and inverse Discrete Cosine Transform to blocks |
| 34 |
* of image pixels (of 8x8 size), as in JPEG standard. The typical work flow is |
| 35 |
*as |
| 36 |
* follows: |
| 37 |
* 1. Run CPU version (Host code) and measure execution time; |
| 38 |
* 2. Run CUDA version (Device code) and measure execution time; |
| 39 |
* 3. Output execution timings and calculate CUDA speedup. |
| 40 |
*/ |
| 41 |
|
| 42 |
#include "Common.h" |
| 43 |
#include "DCT8x8_Gold.h" |
| 44 |
#include "BmpUtil.h" |
| 45 |
|
| 46 |
/** |
| 47 |
* The number of DCT kernel calls |
| 48 |
*/ |
| 49 |
#define BENCHMARK_SIZE 10 |
| 50 |
|
| 51 |
/** |
| 52 |
* The PSNR values over this threshold indicate images equality |
| 53 |
*/ |
| 54 |
#define PSNR_THRESHOLD_EQUAL 40 |
| 55 |
|
| 56 |
// includes kernels |
| 57 |
#include "dct8x8_kernel1.cuh" |
| 58 |
#include "dct8x8_kernel2.cuh" |
| 59 |
#include "dct8x8_kernel_short.cuh" |
| 60 |
#include "dct8x8_kernel_quantization.cuh" |
| 61 |
|
| 62 |
/** |
| 63 |
************************************************************************** |
| 64 |
* Wrapper function for 1st gold version of DCT, quantization and IDCT |
| 65 |
*implementations |
| 66 |
* |
| 67 |
* \param ImgSrc [IN] - Source byte image plane |
| 68 |
* \param ImgDst [IN] - Quantized result byte image plane |
| 69 |
* \param Stride [IN] - Stride for both source and result planes |
| 70 |
* \param Size [IN] - Size of both planes |
| 71 |
* |
| 72 |
* \return Execution time in milliseconds |
| 73 |
*/ |
| 74 |
float WrapperGold1(byte *ImgSrc, byte *ImgDst, int Stride, ROI Size) { |
| 75 |
// allocate float buffers for DCT and other data |
| 76 |
int StrideF; |
| 77 |
float *ImgF1 = MallocPlaneFloat(Size.width, Size.height, &StrideF); |
| 78 |
float *ImgF2 = MallocPlaneFloat(Size.width, Size.height, &StrideF); |
| 79 |
|
| 80 |
// convert source image to float representation |
| 81 |
CopyByte2Float(ImgSrc, Stride, ImgF1, StrideF, Size); |
| 82 |
AddFloatPlane(-128.0f, ImgF1, StrideF, Size); |
| 83 |
|
| 84 |
// create and start CUDA timer |
| 85 |
StopWatchInterface *timerGold = 0; |
| 86 |
sdkCreateTimer(&timerGold); |
| 87 |
sdkResetTimer(&timerGold); |
| 88 |
|
| 89 |
// perform block-wise DCT processing and benchmarking |
| 90 |
for (int i = 0; i < BENCHMARK_SIZE; i++) { |
| 91 |
sdkStartTimer(&timerGold); |
| 92 |
computeDCT8x8Gold1(ImgF1, ImgF2, StrideF, Size); |
| 93 |
sdkStopTimer(&timerGold); |
| 94 |
} |
| 95 |
|
| 96 |
// stop and destroy CUDA timer |
| 97 |
float TimerGoldSpan = sdkGetAverageTimerValue(&timerGold); |
| 98 |
sdkDeleteTimer(&timerGold); |
| 99 |
|
| 100 |
// perform quantization |
| 101 |
quantizeGoldFloat(ImgF2, StrideF, Size); |
| 102 |
|
| 103 |
// perform block-wise IDCT processing |
| 104 |
computeIDCT8x8Gold1(ImgF2, ImgF1, StrideF, Size); |
| 105 |
|
| 106 |
// convert image back to byte representation |
| 107 |
AddFloatPlane(128.0f, ImgF1, StrideF, Size); |
| 108 |
CopyFloat2Byte(ImgF1, StrideF, ImgDst, Stride, Size); |
| 109 |
|
| 110 |
// free float buffers |
| 111 |
FreePlane(ImgF1); |
| 112 |
FreePlane(ImgF2); |
| 113 |
|
| 114 |
// return time taken by the operation |
| 115 |
return TimerGoldSpan; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
************************************************************************** |
| 120 |
* Wrapper function for 2nd gold version of DCT, quantization and IDCT |
| 121 |
*implementations |
| 122 |
* |
| 123 |
* \param ImgSrc [IN] - Source byte image plane |
| 124 |
* \param ImgDst [IN] - Quantized result byte image plane |
| 125 |
* \param Stride [IN] - Stride for both source and result planes |
| 126 |
* \param Size [IN] - Size of both planes |
| 127 |
* |
| 128 |
* \return Execution time in milliseconds |
| 129 |
*/ |
| 130 |
float WrapperGold2(byte *ImgSrc, byte *ImgDst, int Stride, ROI Size) { |
| 131 |
// allocate float buffers for DCT and other data |
| 132 |
int StrideF; |
| 133 |
float *ImgF1 = MallocPlaneFloat(Size.width, Size.height, &StrideF); |
| 134 |
float *ImgF2 = MallocPlaneFloat(Size.width, Size.height, &StrideF); |
| 135 |
|
| 136 |
// convert source image to float representation |
| 137 |
CopyByte2Float(ImgSrc, Stride, ImgF1, StrideF, Size); |
| 138 |
AddFloatPlane(-128.0f, ImgF1, StrideF, Size); |
| 139 |
|
| 140 |
// create and start CUDA timer |
| 141 |
StopWatchInterface *timerGold = 0; |
| 142 |
sdkCreateTimer(&timerGold); |
| 143 |
sdkResetTimer(&timerGold); |
| 144 |
|
| 145 |
// perform block-wise DCT processing and benchmarking |
| 146 |
for (int i = 0; i < BENCHMARK_SIZE; i++) { |
| 147 |
sdkStartTimer(&timerGold); |
| 148 |
computeDCT8x8Gold2(ImgF1, ImgF2, StrideF, Size); |
| 149 |
sdkStopTimer(&timerGold); |
| 150 |
} |
| 151 |
|
| 152 |
// stop and destroy CUDA timer |
| 153 |
float TimerGoldSpan = sdkGetAverageTimerValue(&timerGold); |
| 154 |
sdkDeleteTimer(&timerGold); |
| 155 |
|
| 156 |
// perform quantization |
| 157 |
quantizeGoldFloat(ImgF2, StrideF, Size); |
| 158 |
|
| 159 |
// perform block-wise IDCT processing |
| 160 |
computeIDCT8x8Gold2(ImgF2, ImgF1, StrideF, Size); |
| 161 |
|
| 162 |
// convert image back to byte representation |
| 163 |
AddFloatPlane(128.0f, ImgF1, StrideF, Size); |
| 164 |
CopyFloat2Byte(ImgF1, StrideF, ImgDst, Stride, Size); |
| 165 |
|
| 166 |
// free float buffers |
| 167 |
FreePlane(ImgF1); |
| 168 |
FreePlane(ImgF2); |
| 169 |
|
| 170 |
// return time taken by the operation |
| 171 |
return TimerGoldSpan; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
************************************************************************** |
| 176 |
* Wrapper function for 1st CUDA version of DCT, quantization and IDCT |
| 177 |
*implementations |
| 178 |
* |
| 179 |
* \param ImgSrc [IN] - Source byte image plane |
| 180 |
* \param ImgDst [IN] - Quantized result byte image plane |
| 181 |
* \param Stride [IN] - Stride for both source and result planes |
| 182 |
* \param Size [IN] - Size of both planes |
| 183 |
* |
| 184 |
* \return Execution time in milliseconds |
| 185 |
*/ |
| 186 |
float WrapperCUDA1(byte *ImgSrc, byte *ImgDst, int Stride, ROI Size) { |
| 187 |
// prepare channel format descriptor for passing texture into kernels |
| 188 |
cudaChannelFormatDesc floattex = cudaCreateChannelDesc<float>(); |
| 189 |
|
| 190 |
// allocate device memory |
| 191 |
cudaArray *Src; |
| 192 |
float *Dst; |
| 193 |
size_t DstStride; |
| 194 |
checkCudaErrors(cudaMallocArray(&Src, &floattex, Size.width, Size.height)); |
| 195 |
checkCudaErrors(cudaMallocPitch((void **)(&Dst), &DstStride, |
| 196 |
Size.width * sizeof(float), Size.height)); |
| 197 |
DstStride /= sizeof(float); |
| 198 |
|
| 199 |
// convert source image to float representation |
| 200 |
int ImgSrcFStride; |
| 201 |
float *ImgSrcF = MallocPlaneFloat(Size.width, Size.height, &ImgSrcFStride); |
| 202 |
CopyByte2Float(ImgSrc, Stride, ImgSrcF, ImgSrcFStride, Size); |
| 203 |
AddFloatPlane(-128.0f, ImgSrcF, ImgSrcFStride, Size); |
| 204 |
|
| 205 |
// copy from host memory to device |
| 206 |
checkCudaErrors(cudaMemcpy2DToArray( |
| 207 |
Src, 0, 0, ImgSrcF, ImgSrcFStride * sizeof(float), |
| 208 |
Size.width * sizeof(float), Size.height, cudaMemcpyHostToDevice)); |
| 209 |
|
| 210 |
// setup execution parameters |
| 211 |
dim3 threads(BLOCK_SIZE, BLOCK_SIZE); |
| 212 |
dim3 grid(Size.width / BLOCK_SIZE, Size.height / BLOCK_SIZE); |
| 213 |
|
| 214 |
// create and start CUDA timer |
| 215 |
StopWatchInterface *timerCUDA = 0; |
| 216 |
sdkCreateTimer(&timerCUDA); |
| 217 |
sdkResetTimer(&timerCUDA); |
| 218 |
|
| 219 |
// execute DCT kernel and benchmark |
| 220 |
cudaTextureObject_t TexSrc; |
| 221 |
cudaResourceDesc texRes; |
| 222 |
memset(&texRes, 0, sizeof(cudaResourceDesc)); |
| 223 |
|
| 224 |
texRes.resType = cudaResourceTypeArray; |
| 225 |
texRes.res.array.array = Src; |
| 226 |
|
| 227 |
cudaTextureDesc texDescr; |
| 228 |
memset(&texDescr, 0, sizeof(cudaTextureDesc)); |
| 229 |
|
| 230 |
texDescr.normalizedCoords = false; |
| 231 |
texDescr.filterMode = cudaFilterModeLinear; |
| 232 |
texDescr.addressMode[0] = cudaAddressModeWrap; |
| 233 |
texDescr.addressMode[1] = cudaAddressModeWrap; |
| 234 |
texDescr.readMode = cudaReadModeElementType; |
| 235 |
|
| 236 |
checkCudaErrors(cudaCreateTextureObject(&TexSrc, &texRes, &texDescr, NULL)); |
| 237 |
|
| 238 |
for (int i = 0; i < BENCHMARK_SIZE; i++) { |
| 239 |
sdkStartTimer(&timerCUDA); |
| 240 |
CUDAkernel1DCT<<<grid, threads>>>(Dst, (int)DstStride, 0, 0, TexSrc); |
| 241 |
checkCudaErrors(cudaDeviceSynchronize()); |
| 242 |
sdkStopTimer(&timerCUDA); |
| 243 |
} |
| 244 |
|
| 245 |
getLastCudaError("Kernel execution failed"); |
| 246 |
|
| 247 |
// finalize CUDA timer |
| 248 |
float TimerCUDASpan = sdkGetAverageTimerValue(&timerCUDA); |
| 249 |
sdkDeleteTimer(&timerCUDA); |
| 250 |
|
| 251 |
// execute Quantization kernel |
| 252 |
CUDAkernelQuantizationFloat<<<grid, threads>>>(Dst, (int)DstStride); |
| 253 |
getLastCudaError("Kernel execution failed"); |
| 254 |
|
| 255 |
// copy quantized coefficients from host memory to device array |
| 256 |
checkCudaErrors(cudaMemcpy2DToArray(Src, 0, 0, Dst, DstStride * sizeof(float), |
| 257 |
Size.width * sizeof(float), Size.height, |
| 258 |
cudaMemcpyDeviceToDevice)); |
| 259 |
|
| 260 |
// execute IDCT kernel |
| 261 |
CUDAkernel1IDCT<<<grid, threads>>>(Dst, (int)DstStride, 0, 0, TexSrc); |
| 262 |
getLastCudaError("Kernel execution failed"); |
| 263 |
|
| 264 |
// copy quantized image block to host |
| 265 |
checkCudaErrors(cudaMemcpy2D( |
| 266 |
ImgSrcF, ImgSrcFStride * sizeof(float), Dst, DstStride * sizeof(float), |
| 267 |
Size.width * sizeof(float), Size.height, cudaMemcpyDeviceToHost)); |
| 268 |
|
| 269 |
// convert image back to byte representation |
| 270 |
AddFloatPlane(128.0f, ImgSrcF, ImgSrcFStride, Size); |
| 271 |
CopyFloat2Byte(ImgSrcF, ImgSrcFStride, ImgDst, Stride, Size); |
| 272 |
|
| 273 |
// clean up memory |
| 274 |
checkCudaErrors(cudaDestroyTextureObject(TexSrc)); |
| 275 |
checkCudaErrors(cudaFreeArray(Src)); |
| 276 |
checkCudaErrors(cudaFree(Dst)); |
| 277 |
FreePlane(ImgSrcF); |
| 278 |
|
| 279 |
// return time taken by the operation |
| 280 |
return TimerCUDASpan; |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
************************************************************************** |
| 285 |
* Wrapper function for 2nd CUDA version of DCT, quantization and IDCT |
| 286 |
*implementations |
| 287 |
* |
| 288 |
* \param ImgSrc [IN] - Source byte image plane |
| 289 |
* \param ImgDst [IN] - Quantized result byte image plane |
| 290 |
* \param Stride [IN] - Stride for both source and result planes |
| 291 |
* \param Size [IN] - Size of both planes |
| 292 |
* |
| 293 |
* \return Execution time in milliseconds |
| 294 |
*/ |
| 295 |
|
| 296 |
float WrapperCUDA2(byte *ImgSrc, byte *ImgDst, int Stride, ROI Size) { |
| 297 |
// allocate host buffers for DCT and other data |
| 298 |
int StrideF; |
| 299 |
float *ImgF1 = MallocPlaneFloat(Size.width, Size.height, &StrideF); |
| 300 |
|
| 301 |
// convert source image to float representation |
| 302 |
CopyByte2Float(ImgSrc, Stride, ImgF1, StrideF, Size); |
| 303 |
AddFloatPlane(-128.0f, ImgF1, StrideF, Size); |
| 304 |
|
| 305 |
// allocate device memory |
| 306 |
float *src, *dst; |
| 307 |
size_t DeviceStride; |
| 308 |
checkCudaErrors(cudaMallocPitch((void **)&src, &DeviceStride, |
| 309 |
Size.width * sizeof(float), Size.height)); |
| 310 |
checkCudaErrors(cudaMallocPitch((void **)&dst, &DeviceStride, |
| 311 |
Size.width * sizeof(float), Size.height)); |
| 312 |
DeviceStride /= sizeof(float); |
| 313 |
|
| 314 |
// copy from host memory to device |
| 315 |
checkCudaErrors(cudaMemcpy2D( |
| 316 |
src, DeviceStride * sizeof(float), ImgF1, StrideF * sizeof(float), |
| 317 |
Size.width * sizeof(float), Size.height, cudaMemcpyHostToDevice)); |
| 318 |
|
| 319 |
// create and start CUDA timer |
| 320 |
StopWatchInterface *timerCUDA = 0; |
| 321 |
sdkCreateTimer(&timerCUDA); |
| 322 |
|
| 323 |
// setup execution parameters |
| 324 |
dim3 GridFullWarps(Size.width / KER2_BLOCK_WIDTH, |
| 325 |
Size.height / KER2_BLOCK_HEIGHT, 1); |
| 326 |
dim3 ThreadsFullWarps(8, KER2_BLOCK_WIDTH / 8, KER2_BLOCK_HEIGHT / 8); |
| 327 |
|
| 328 |
// perform block-wise DCT processing and benchmarking |
| 329 |
const int numIterations = 100; |
| 330 |
|
| 331 |
for (int i = -1; i < numIterations; i++) { |
| 332 |
if (i == 0) { |
| 333 |
checkCudaErrors(cudaDeviceSynchronize()); |
| 334 |
sdkResetTimer(&timerCUDA); |
| 335 |
sdkStartTimer(&timerCUDA); |
| 336 |
} |
| 337 |
|
| 338 |
CUDAkernel2DCT<<<GridFullWarps, ThreadsFullWarps>>>(dst, src, |
| 339 |
(int)DeviceStride); |
| 340 |
getLastCudaError("Kernel execution failed"); |
| 341 |
} |
| 342 |
|
| 343 |
checkCudaErrors(cudaDeviceSynchronize()); |
| 344 |
sdkStopTimer(&timerCUDA); |
| 345 |
|
| 346 |
// finalize timing of CUDA Kernels |
| 347 |
float avgTime = (float)sdkGetTimerValue(&timerCUDA) / (float)numIterations; |
| 348 |
sdkDeleteTimer(&timerCUDA); |
| 349 |
printf("%f MPix/s //%f ms\n", |
| 350 |
(1E-6 * (float)Size.width * (float)Size.height) / (1E-3 * avgTime), |
| 351 |
avgTime); |
| 352 |
|
| 353 |
// setup execution parameters for quantization |
| 354 |
dim3 ThreadsSmallBlocks(BLOCK_SIZE, BLOCK_SIZE); |
| 355 |
dim3 GridSmallBlocks(Size.width / BLOCK_SIZE, Size.height / BLOCK_SIZE); |
| 356 |
|
| 357 |
// execute Quantization kernel |
| 358 |
CUDAkernelQuantizationFloat<<<GridSmallBlocks, ThreadsSmallBlocks>>>( |
| 359 |
dst, (int)DeviceStride); |
| 360 |
getLastCudaError("Kernel execution failed"); |
| 361 |
|
| 362 |
// perform block-wise IDCT processing |
| 363 |
CUDAkernel2IDCT<<<GridFullWarps, ThreadsFullWarps>>>(src, dst, |
| 364 |
(int)DeviceStride); |
| 365 |
checkCudaErrors(cudaDeviceSynchronize()); |
| 366 |
getLastCudaError("Kernel execution failed"); |
| 367 |
|
| 368 |
// copy quantized image block to host |
| 369 |
checkCudaErrors(cudaMemcpy2D( |
| 370 |
ImgF1, StrideF * sizeof(float), src, DeviceStride * sizeof(float), |
| 371 |
Size.width * sizeof(float), Size.height, cudaMemcpyDeviceToHost)); |
| 372 |
|
| 373 |
// convert image back to byte representation |
| 374 |
AddFloatPlane(128.0f, ImgF1, StrideF, Size); |
| 375 |
CopyFloat2Byte(ImgF1, StrideF, ImgDst, Stride, Size); |
| 376 |
|
| 377 |
// clean up memory |
| 378 |
checkCudaErrors(cudaFree(dst)); |
| 379 |
checkCudaErrors(cudaFree(src)); |
| 380 |
FreePlane(ImgF1); |
| 381 |
|
| 382 |
// return time taken by the operation |
| 383 |
return avgTime; |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
************************************************************************** |
| 388 |
* Wrapper function for short CUDA version of DCT, quantization and IDCT |
| 389 |
*implementations |
| 390 |
* |
| 391 |
* \param ImgSrc [IN] - Source byte image plane |
| 392 |
* \param ImgDst [IN] - Quantized result byte image plane |
| 393 |
* \param Stride [IN] - Stride for both source and result planes |
| 394 |
* \param Size [IN] - Size of both planes |
| 395 |
* |
| 396 |
* \return Execution time in milliseconds |
| 397 |
*/ |
| 398 |
float WrapperCUDAshort(byte *ImgSrc, byte *ImgDst, int Stride, ROI Size) { |
| 399 |
// allocate host buffers for DCT and other data |
| 400 |
int StrideS; |
| 401 |
short *ImgS1 = MallocPlaneShort(Size.width, Size.height, &StrideS); |
| 402 |
|
| 403 |
// convert source image to short representation centered at 128 |
| 404 |
for (int i = 0; i < Size.height; i++) { |
| 405 |
for (int j = 0; j < Size.width; j++) { |
| 406 |
ImgS1[i * StrideS + j] = (short)ImgSrc[i * Stride + j] - 128; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
// allocate device memory |
| 411 |
short *SrcDst; |
| 412 |
size_t DeviceStride; |
| 413 |
checkCudaErrors(cudaMallocPitch((void **)(&SrcDst), &DeviceStride, |
| 414 |
Size.width * sizeof(short), Size.height)); |
| 415 |
DeviceStride /= sizeof(short); |
| 416 |
|
| 417 |
// copy from host memory to device |
| 418 |
checkCudaErrors(cudaMemcpy2D( |
| 419 |
SrcDst, DeviceStride * sizeof(short), ImgS1, StrideS * sizeof(short), |
| 420 |
Size.width * sizeof(short), Size.height, cudaMemcpyHostToDevice)); |
| 421 |
|
| 422 |
// create and start CUDA timer |
| 423 |
StopWatchInterface *timerLibJpeg = 0; |
| 424 |
sdkCreateTimer(&timerLibJpeg); |
| 425 |
sdkResetTimer(&timerLibJpeg); |
| 426 |
|
| 427 |
// setup execution parameters |
| 428 |
dim3 GridShort(Size.width / KERS_BLOCK_WIDTH, Size.height / KERS_BLOCK_HEIGHT, |
| 429 |
1); |
| 430 |
dim3 ThreadsShort(8, KERS_BLOCK_WIDTH / 8, KERS_BLOCK_HEIGHT / 8); |
| 431 |
|
| 432 |
// perform block-wise DCT processing and benchmarking |
| 433 |
sdkStartTimer(&timerLibJpeg); |
| 434 |
CUDAkernelShortDCT<<<GridShort, ThreadsShort>>>(SrcDst, (int)DeviceStride); |
| 435 |
checkCudaErrors(cudaDeviceSynchronize()); |
| 436 |
sdkStopTimer(&timerLibJpeg); |
| 437 |
getLastCudaError("Kernel execution failed"); |
| 438 |
|
| 439 |
// stop and destroy CUDA timer |
| 440 |
float TimerLibJpegSpan16b = sdkGetAverageTimerValue(&timerLibJpeg); |
| 441 |
sdkDeleteTimer(&timerLibJpeg); |
| 442 |
|
| 443 |
// setup execution parameters for quantization |
| 444 |
dim3 ThreadsSmallBlocks(BLOCK_SIZE, BLOCK_SIZE); |
| 445 |
dim3 GridSmallBlocks(Size.width / BLOCK_SIZE, Size.height / BLOCK_SIZE); |
| 446 |
|
| 447 |
// execute Quantization kernel |
| 448 |
CUDAkernelQuantizationShort<<<GridSmallBlocks, ThreadsSmallBlocks>>>( |
| 449 |
SrcDst, (int)DeviceStride); |
| 450 |
getLastCudaError("Kernel execution failed"); |
| 451 |
|
| 452 |
// perform block-wise IDCT processing |
| 453 |
CUDAkernelShortIDCT<<<GridShort, ThreadsShort>>>(SrcDst, (int)DeviceStride); |
| 454 |
checkCudaErrors(cudaDeviceSynchronize()); |
| 455 |
getLastCudaError("Kernel execution failed"); |
| 456 |
|
| 457 |
// copy quantized image block to host |
| 458 |
checkCudaErrors(cudaMemcpy2D( |
| 459 |
ImgS1, StrideS * sizeof(short), SrcDst, DeviceStride * sizeof(short), |
| 460 |
Size.width * sizeof(short), Size.height, cudaMemcpyDeviceToHost)); |
| 461 |
|
| 462 |
// convert image back to byte representation |
| 463 |
for (int i = 0; i < Size.height; i++) { |
| 464 |
for (int j = 0; j < Size.width; j++) { |
| 465 |
ImgDst[i * Stride + j] = clamp_0_255(ImgS1[i * StrideS + j] + 128); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
// free float buffers |
| 470 |
checkCudaErrors(cudaFree(SrcDst)); |
| 471 |
FreePlane(ImgS1); |
| 472 |
|
| 473 |
// return time taken by the operation |
| 474 |
return TimerLibJpegSpan16b; |
| 475 |
} |
| 476 |
|
| 477 |
/** |
| 478 |
************************************************************************** |
| 479 |
* Program entry point |
| 480 |
* |
| 481 |
* \param argc [IN] - Number of command-line arguments |
| 482 |
* \param argv [IN] - Array of command-line arguments |
| 483 |
* |
| 484 |
* \return Status code |
| 485 |
*/ |
| 486 |
|
| 487 |
int main(int argc, char **argv) { |
| 488 |
// |
| 489 |
// Sample initialization |
| 490 |
// |
| 491 |
printf("%s Starting...\n\n", argv[0]); |
| 492 |
|
| 493 |
// initialize CUDA |
| 494 |
findCudaDevice(argc, (const char **)argv); |
| 495 |
|
| 496 |
// source and results image filenames |
| 497 |
char SampleImageFname[] = "teapot512.bmp"; |
| 498 |
char SampleImageFnameResGold1[] = "teapot512_gold1.bmp"; |
| 499 |
char SampleImageFnameResGold2[] = "teapot512_gold2.bmp"; |
| 500 |
char SampleImageFnameResCUDA1[] = "teapot512_cuda1.bmp"; |
| 501 |
char SampleImageFnameResCUDA2[] = "teapot512_cuda2.bmp"; |
| 502 |
char SampleImageFnameResCUDAshort[] = "teapot512_cuda_short.bmp"; |
| 503 |
|
| 504 |
char *pSampleImageFpath = sdkFindFilePath(SampleImageFname, argv[0]); |
| 505 |
|
| 506 |
if (pSampleImageFpath == NULL) { |
| 507 |
printf("dct8x8 could not locate Sample Image <%s>\nExiting...\n", |
| 508 |
pSampleImageFpath); |
| 509 |
exit(EXIT_FAILURE); |
| 510 |
} |
| 511 |
|
| 512 |
// preload image (acquire dimensions) |
| 513 |
int ImgWidth, ImgHeight; |
| 514 |
ROI ImgSize; |
| 515 |
int res = PreLoadBmp(pSampleImageFpath, &ImgWidth, &ImgHeight); |
| 516 |
ImgSize.width = ImgWidth; |
| 517 |
ImgSize.height = ImgHeight; |
| 518 |
|
| 519 |
// CONSOLE INFORMATION: saying hello to user |
| 520 |
printf("CUDA sample DCT/IDCT implementation\n"); |
| 521 |
printf("===================================\n"); |
| 522 |
printf("Loading test image: %s... ", SampleImageFname); |
| 523 |
|
| 524 |
if (res) { |
| 525 |
printf("\nError: Image file not found or invalid!\n"); |
| 526 |
exit(EXIT_FAILURE); |
| 527 |
return 1; |
| 528 |
} |
| 529 |
|
| 530 |
// check image dimensions are multiples of BLOCK_SIZE |
| 531 |
if (ImgWidth % BLOCK_SIZE != 0 || ImgHeight % BLOCK_SIZE != 0) { |
| 532 |
printf("\nError: Input image dimensions must be multiples of 8!\n"); |
| 533 |
exit(EXIT_FAILURE); |
| 534 |
return 1; |
| 535 |
} |
| 536 |
|
| 537 |
printf("[%d x %d]... ", ImgWidth, ImgHeight); |
| 538 |
|
| 539 |
// allocate image buffers |
| 540 |
int ImgStride; |
| 541 |
byte *ImgSrc = MallocPlaneByte(ImgWidth, ImgHeight, &ImgStride); |
| 542 |
byte *ImgDstGold1 = MallocPlaneByte(ImgWidth, ImgHeight, &ImgStride); |
| 543 |
byte *ImgDstGold2 = MallocPlaneByte(ImgWidth, ImgHeight, &ImgStride); |
| 544 |
byte *ImgDstCUDA1 = MallocPlaneByte(ImgWidth, ImgHeight, &ImgStride); |
| 545 |
byte *ImgDstCUDA2 = MallocPlaneByte(ImgWidth, ImgHeight, &ImgStride); |
| 546 |
byte *ImgDstCUDAshort = MallocPlaneByte(ImgWidth, ImgHeight, &ImgStride); |
| 547 |
|
| 548 |
// load sample image |
| 549 |
LoadBmpAsGray(pSampleImageFpath, ImgStride, ImgSize, ImgSrc); |
| 550 |
|
| 551 |
// |
| 552 |
// RUNNING WRAPPERS |
| 553 |
// |
| 554 |
|
| 555 |
// compute Gold 1 version of DCT/quantization/IDCT |
| 556 |
printf("Success\nRunning Gold 1 (CPU) version... "); |
| 557 |
float TimeGold1 = WrapperGold1(ImgSrc, ImgDstGold1, ImgStride, ImgSize); |
| 558 |
|
| 559 |
// compute Gold 2 version of DCT/quantization/IDCT |
| 560 |
printf("Success\nRunning Gold 2 (CPU) version... "); |
| 561 |
float TimeGold2 = WrapperGold2(ImgSrc, ImgDstGold2, ImgStride, ImgSize); |
| 562 |
|
| 563 |
// compute CUDA 1 version of DCT/quantization/IDCT |
| 564 |
printf("Success\nRunning CUDA 1 (GPU) version... "); |
| 565 |
float TimeCUDA1 = WrapperCUDA1(ImgSrc, ImgDstCUDA1, ImgStride, ImgSize); |
| 566 |
|
| 567 |
// compute CUDA 2 version of DCT/quantization/IDCT |
| 568 |
printf("Success\nRunning CUDA 2 (GPU) version... "); |
| 569 |
float TimeCUDA2 = WrapperCUDA2(ImgSrc, ImgDstCUDA2, ImgStride, ImgSize); |
| 570 |
|
| 571 |
// compute CUDA short version of DCT/quantization/IDCT |
| 572 |
printf("Success\nRunning CUDA short (GPU) version... "); |
| 573 |
float TimeCUDAshort = |
| 574 |
WrapperCUDAshort(ImgSrc, ImgDstCUDAshort, ImgStride, ImgSize); |
| 575 |
// |
| 576 |
// Execution statistics, result saving and validation |
| 577 |
// |
| 578 |
|
| 579 |
// dump result of Gold 1 processing |
| 580 |
printf("Success\nDumping result to %s... ", SampleImageFnameResGold1); |
| 581 |
DumpBmpAsGray(SampleImageFnameResGold1, ImgDstGold1, ImgStride, ImgSize); |
| 582 |
|
| 583 |
// dump result of Gold 2 processing |
| 584 |
printf("Success\nDumping result to %s... ", SampleImageFnameResGold2); |
| 585 |
DumpBmpAsGray(SampleImageFnameResGold2, ImgDstGold2, ImgStride, ImgSize); |
| 586 |
|
| 587 |
// dump result of CUDA 1 processing |
| 588 |
printf("Success\nDumping result to %s... ", SampleImageFnameResCUDA1); |
| 589 |
DumpBmpAsGray(SampleImageFnameResCUDA1, ImgDstCUDA1, ImgStride, ImgSize); |
| 590 |
|
| 591 |
// dump result of CUDA 2 processing |
| 592 |
printf("Success\nDumping result to %s... ", SampleImageFnameResCUDA2); |
| 593 |
DumpBmpAsGray(SampleImageFnameResCUDA2, ImgDstCUDA2, ImgStride, ImgSize); |
| 594 |
|
| 595 |
// dump result of CUDA short processing |
| 596 |
printf("Success\nDumping result to %s... ", SampleImageFnameResCUDAshort); |
| 597 |
DumpBmpAsGray(SampleImageFnameResCUDAshort, ImgDstCUDAshort, ImgStride, |
| 598 |
ImgSize); |
| 599 |
// print speed info |
| 600 |
printf("Success\n"); |
| 601 |
|
| 602 |
printf("Processing time (CUDA 1) : %f ms \n", TimeCUDA1); |
| 603 |
printf("Processing time (CUDA 2) : %f ms \n", TimeCUDA2); |
| 604 |
printf("Processing time (CUDA short): %f ms \n", TimeCUDAshort); |
| 605 |
|
| 606 |
// calculate PSNR between each pair of images |
| 607 |
float PSNR_Src_DstGold1 = |
| 608 |
CalculatePSNR(ImgSrc, ImgDstGold1, ImgStride, ImgSize); |
| 609 |
float PSNR_Src_DstGold2 = |
| 610 |
CalculatePSNR(ImgSrc, ImgDstGold2, ImgStride, ImgSize); |
| 611 |
float PSNR_Src_DstCUDA1 = |
| 612 |
CalculatePSNR(ImgSrc, ImgDstCUDA1, ImgStride, ImgSize); |
| 613 |
float PSNR_Src_DstCUDA2 = |
| 614 |
CalculatePSNR(ImgSrc, ImgDstCUDA2, ImgStride, ImgSize); |
| 615 |
float PSNR_Src_DstCUDAshort = |
| 616 |
CalculatePSNR(ImgSrc, ImgDstCUDAshort, ImgStride, ImgSize); |
| 617 |
float PSNR_DstGold1_DstCUDA1 = |
| 618 |
CalculatePSNR(ImgDstGold1, ImgDstCUDA1, ImgStride, ImgSize); |
| 619 |
float PSNR_DstGold2_DstCUDA2 = |
| 620 |
CalculatePSNR(ImgDstGold2, ImgDstCUDA2, ImgStride, ImgSize); |
| 621 |
float PSNR_DstGold2_DstCUDA16b = |
| 622 |
CalculatePSNR(ImgDstGold2, ImgDstCUDAshort, ImgStride, ImgSize); |
| 623 |
|
| 624 |
printf("PSNR Original <---> CPU(Gold 1) : %f\n", PSNR_Src_DstGold1); |
| 625 |
printf("PSNR Original <---> CPU(Gold 2) : %f\n", PSNR_Src_DstGold2); |
| 626 |
printf("PSNR Original <---> GPU(CUDA 1) : %f\n", PSNR_Src_DstCUDA1); |
| 627 |
printf("PSNR Original <---> GPU(CUDA 2) : %f\n", PSNR_Src_DstCUDA2); |
| 628 |
printf("PSNR Original <---> GPU(CUDA short): %f\n", PSNR_Src_DstCUDAshort); |
| 629 |
printf("PSNR CPU(Gold 1) <---> GPU(CUDA 1) : %f\n", |
| 630 |
PSNR_DstGold1_DstCUDA1); |
| 631 |
printf("PSNR CPU(Gold 2) <---> GPU(CUDA 2) : %f\n", |
| 632 |
PSNR_DstGold2_DstCUDA2); |
| 633 |
printf("PSNR CPU(Gold 2) <---> GPU(CUDA short): %f\n", |
| 634 |
PSNR_DstGold2_DstCUDA16b); |
| 635 |
|
| 636 |
bool bTestResult = (PSNR_DstGold1_DstCUDA1 > PSNR_THRESHOLD_EQUAL && |
| 637 |
PSNR_DstGold2_DstCUDA2 > PSNR_THRESHOLD_EQUAL && |
| 638 |
PSNR_DstGold2_DstCUDA16b > PSNR_THRESHOLD_EQUAL); |
| 639 |
|
| 640 |
// |
| 641 |
// Finalization |
| 642 |
// |
| 643 |
|
| 644 |
// release byte planes |
| 645 |
FreePlane(ImgSrc); |
| 646 |
FreePlane(ImgDstGold1); |
| 647 |
FreePlane(ImgDstGold2); |
| 648 |
FreePlane(ImgDstCUDA1); |
| 649 |
FreePlane(ImgDstCUDA2); |
| 650 |
FreePlane(ImgDstCUDAshort); |
| 651 |
|
| 652 |
// finalize |
| 653 |
printf("\nTest Summary...\n"); |
| 654 |
|
| 655 |
if (!bTestResult) { |
| 656 |
printf("Test failed!\n"); |
| 657 |
exit(EXIT_FAILURE); |
| 658 |
} |
| 659 |
|
| 660 |
printf("Test passed\n"); |
| 661 |
exit(EXIT_SUCCESS); |
| 662 |
} |