Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 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. // Manipulation with prepared image.
  10. //
  11. // ***********************************************************************
  12.  
  13. #include <cuda_runtime.h>
  14. #include <stdio.h>
  15.  
  16. // Every threads identifies its position in grid and in block and modify image
  17. __global__ void kernel_animation( uchar4 *pic, int sizex, int sizey )
  18. {
  19.  
  20. // X,Y coordinates
  21. int y = blockDim.y * blockIdx.y + threadIdx.y;
  22. int x = blockDim.x * blockIdx.x + threadIdx.x;
  23. if ( x >= sizex ) return;
  24. if ( y >= sizey ) return;
  25.  
  26. // Point [x,y] selection from image
  27. uchar4 bgr = pic[ y * sizex + x ];
  28.  
  29. // Color rotation inside block
  30. int x2 = blockDim.x / 2;
  31. int y2 = blockDim.y / 2;
  32. int px = __sad( x2, threadIdx.x, 0 ); // abs function
  33. int py = __sad( y2, threadIdx.y, 0 );
  34.  
  35. if ( px < x2 * ( y2 - py ) / y2 )
  36. {
  37. uchar4 tmp = bgr;
  38. bgr.x = tmp.y;
  39. bgr.y = tmp.z;
  40. bgr.z = tmp.x;
  41. }
  42.  
  43. // Store point [x,y] back to image
  44. pic[ y * sizex + x ] = bgr;
  45.  
  46. uchar4 bgr = { 255, 255, 0 }; // zluta
  47.  
  48. //Trojuhelnik dolu
  49. if ((x+y) <= sizex+sizex/2 && (x+(sizex/2)>=y) && y > sizey/2)
  50. {
  51. bgr.x = bgr.z = 0; //zelena
  52. }
  53.  
  54. //Trojuhelnik nahoru
  55. if ((x+y>=sizex/2) && (x-(sizex/2)<=y) && y < sizey/2)
  56. {
  57. bgr.x = bgr.z = 0; //zelena
  58. }
  59.  
  60. //Trojuhelnik do prava
  61. if ( (x-(sizex/2)<=y) && (x+y) <= sizex+sizex/2 && x> sizex/2)
  62. {
  63. bgr.x = bgr.y = bgr.z = 0; //cerna
  64. }
  65.  
  66. //Trojuhelnik do leva
  67. if ( (x+y>=sizex/2) && (x+(sizex/2)>=y ) && x <= sizex/2)
  68. {
  69. bgr.x = bgr.y = bgr.z = 0; //cerna
  70. }
  71.  
  72. }
  73.  
  74. void run_animation( uchar4 *pic, int sizex, int sizey, int blockx, int blocky )
  75. {
  76. cudaError_t cerr;
  77.  
  78. // Memory allocation in GPU device
  79. uchar4 *cudaPic;
  80. cerr = cudaMalloc( &cudaPic, sizex * sizey * sizeof( uchar4 ) );
  81. if ( cerr != cudaSuccess )
  82. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  83.  
  84. // Copy data to GPU device
  85. cerr = cudaMemcpy( cudaPic, pic, sizex * sizey * sizeof( uchar4 ), cudaMemcpyHostToDevice );
  86. if ( cerr != cudaSuccess )
  87. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  88.  
  89. // Grid creation with computed organization
  90. dim3 mrizka( ( sizex + blockx - 1 ) / blockx, ( sizey + blocky - 1 ) / blocky );
  91. kernel_animation<<< mrizka, dim3( blockx, blocky ) >>>( cudaPic, sizex, sizey );
  92.  
  93. if ( ( cerr = cudaGetLastError() ) != cudaSuccess )
  94. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  95.  
  96. // Copy data from GPU device to PC
  97. cerr = cudaMemcpy( pic, cudaPic, sizex * sizey * sizeof( uchar4 ), cudaMemcpyDeviceToHost );
  98. if ( cerr != cudaSuccess )
  99. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  100.  
  101. // Free memory
  102. cudaFree( cudaPic );
  103.  
  104. // For printf
  105. //cudaDeviceSynchronize();
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement