Advertisement
Guest User

cuda

a guest
Nov 26th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. // ***********************************************************************
  2. //
  3. // Demo program for education in subject
  4. // Computer Architectures and Paralel Systems.
  5. // Petr Olivka, dep. of Computer Science, FEI, VSB-TU Ostrava
  6. // email:petr.olivka@vsb.cz
  7. //
  8. // Example of CUDA Technology Usage.
  9. // Image transformation from RGB to BW schema.
  10. //
  11. // ***********************************************************************
  12.  
  13. #include <cuda_runtime.h>
  14. #include <stdio.h>
  15.  
  16. // Demo kernel to tranfrom RGB color schema to BW schema
  17. __global__ void kernel_grayscale( uchar4 *color_pic, uchar4* bw_pic, int sizex, int sizey )
  18. {
  19.     // X,Y coordinates and check image dimensions
  20.     int y = blockDim.y * blockIdx.y + threadIdx.y;
  21.     if ( y >= sizey ) return;
  22.     int x = blockDim.x * blockIdx.x + threadIdx.x;
  23.     if ( x >= sizex ) return;
  24.  
  25.  
  26.     bw_pic[(sizex - x)* sizey + y] = color_pic[y * sizex + x ];
  27.  
  28. }
  29.  
  30. void run_grayscale( uchar4 *color_pic, uchar4* bw_pic, int sizex, int sizey )
  31. {
  32.     cudaError_t cerr;
  33.     // Memory allocation in GPU device
  34.     uchar4 *cudaColorPic;
  35.     uchar4 *cudaBWPic;
  36.     cerr = cudaMalloc( &cudaColorPic, sizex * sizey * sizeof( uchar4 ) );
  37.     if ( cerr != cudaSuccess )
  38.         printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  39.  
  40.     cerr = cudaMalloc( &cudaBWPic, sizex * sizey * sizeof( uchar4 ) );
  41.     if ( cerr != cudaSuccess )
  42.         printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  43.  
  44.     // Copy color image to GPU device
  45.     cerr = cudaMemcpy( cudaColorPic, color_pic, sizex * sizey * sizeof( uchar4 ), cudaMemcpyHostToDevice );
  46.     if ( cerr != cudaSuccess )
  47.         printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  48.  
  49.     int block = 16;
  50.     dim3 blocks( ( sizex + block - 1 ) / block, ( sizey + block - 1 ) / block );
  51.     dim3 threads( block, block );
  52.  
  53.     // Grid creation, size of grid must be greater than image
  54.     kernel_grayscale<<< blocks, threads >>>( cudaColorPic, cudaBWPic, sizex, sizey );
  55.  
  56.     if ( ( cerr = cudaGetLastError() ) != cudaSuccess )
  57.         printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  58.  
  59.     // Copy new image from GPU device
  60.     cerr = cudaMemcpy( bw_pic, cudaBWPic, sizex * sizey * sizeof( uchar4 ), cudaMemcpyDeviceToHost );
  61.     if ( cerr != cudaSuccess )
  62.         printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  63.  
  64.     // Free memory
  65.     cudaFree( cudaColorPic );
  66.     cudaFree( cudaBWPic );
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement