Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. __global__ void kernel_rotace( uchar4 *color_pic, uchar4* bw_pic, int sizex, int sizey )
  2. {
  3. // X,Y coordinates and check image dimensions
  4. int y = blockDim.y * blockIdx.y + threadIdx.y;
  5. if ( y >= sizey ) return;
  6. int x = blockDim.x * blockIdx.x + threadIdx.x;
  7. if ( x >= sizex ) return;
  8.  
  9. // Get point from color picture
  10.  
  11. uchar4 bgr = color_pic[ x * sizey - y ];
  12.  
  13.  
  14. // Store BW point to new image
  15. bw_pic[ y * sizex + x ] = bgr;
  16.  
  17. }
  18.  
  19. void run_rotace( uchar4 *color_pic, uchar4* bw_pic, int sizex, int sizey )
  20. {
  21. cudaError_t cerr;
  22. // Memory allocation in GPU device
  23. uchar4 *cudaColorPic;
  24. uchar4 *cudaBWPic;
  25. cerr = cudaMalloc( &cudaColorPic, sizex * sizey * sizeof( uchar4 ) );
  26. if ( cerr != cudaSuccess )
  27. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  28.  
  29. cerr = cudaMalloc( &cudaBWPic, sizex * sizey * sizeof( uchar4 ) );
  30. if ( cerr != cudaSuccess )
  31. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  32.  
  33. // Copy color image to GPU device
  34. cerr = cudaMemcpy( cudaColorPic, color_pic, sizex * sizey * sizeof( uchar4 ), cudaMemcpyHostToDevice );
  35. if ( cerr != cudaSuccess )
  36. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  37.  
  38. int block = 16;
  39. dim3 blocks( ( sizex + block - 1 ) / block, ( sizey + block - 1 ) / block );
  40. dim3 threads( block, block );
  41.  
  42. // Grid creation, size of grid must be greater than image
  43. kernel_rotace<<< blocks, threads >>>( cudaColorPic, cudaBWPic, sizex, sizey );
  44.  
  45. if ( ( cerr = cudaGetLastError() ) != cudaSuccess )
  46. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  47.  
  48. // Copy new image from GPU device
  49. cerr = cudaMemcpy( bw_pic, cudaBWPic, sizex * sizey * sizeof( uchar4 ), cudaMemcpyDeviceToHost );
  50. if ( cerr != cudaSuccess )
  51. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  52.  
  53. // Free memory
  54. cudaFree( cudaColorPic );
  55. cudaFree( cudaBWPic );
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement