Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 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 without unified memory.
  9. //
  10. // Manipulation with prepared image.
  11. //
  12. // ***********************************************************************
  13.  
  14. #include <stdio.h>
  15. #include <cuda_device_runtime_api.h>
  16. #include <cuda_runtime.h>
  17. #include \"pic_type.h\"
  18. #include <cmath>
  19.  
  20. // Every threads identifies its position in grid and in block and modify image
  21. __global__ void kernel_animation( CUDA_Pic cuda_pic )
  22. {
  23. // X,Y coordinates
  24. int y = blockDim.y * blockIdx.y + threadIdx.y;
  25. int x = blockDim.x * blockIdx.x + threadIdx.x;
  26. if ( x >= cuda_pic.Size.x ) return;
  27. if ( y >= cuda_pic.Size.y ) return;
  28.  
  29. // Point [x,y] selection from image
  30. uchar3 bgr = cuda_pic.P_uchar3[ y * cuda_pic.Size.x + x ];
  31.  
  32. if ((((x - cuda_pic.Size.x / 2) * (x - cuda_pic.Size.x / 2) + (y - cuda_pic.Size.y / 2) * (y - cuda_pic.Size.y / 2)) < 10000) && (((x - cuda_pic.Size.x / 2) * (x - cuda_pic.Size.x / 2) + (y - cuda_pic.Size.y / 2) * (y - cuda_pic.Size.y / 2)) > 9000))
  33. {
  34. bgr.x = 0;
  35. bgr.y = 0;
  36. bgr.z = 255;
  37. cuda_pic.P_uchar3[ y * cuda_pic.Size.x + x ] = bgr;
  38. }
  39.  
  40. if (x > 125 && x < 130 && y > 90 && y < 210)
  41. {
  42. bgr.x = 0;
  43. bgr.y = 0;
  44. bgr.z = 0;
  45. cuda_pic.P_uchar3[ y * cuda_pic.Size.x + x ] = bgr;
  46. }
  47.  
  48. if (x > 170 && x < 175 && y > 90 && y < 210)
  49. {
  50. bgr.x = 0;
  51. bgr.y = 0;
  52. bgr.z = 255;
  53. cuda_pic.P_uchar3[ y * cuda_pic.Size.x + x ] = bgr;
  54. }
  55.  
  56. if (x + 79 <= y && x + 84 >= y && x > 99 && x < 129)
  57. {
  58. bgr.x = 0;
  59. bgr.y = 0;
  60. bgr.z = 0;
  61. cuda_pic.P_uchar3[ y * cuda_pic.Size.x + x ] = bgr;
  62. }
  63.  
  64. if (x - 32 >= abs(300 - y) && x - 37 <= abs(300 - y) && x > 129 && x < 159)
  65. {
  66. bgr.x = 0;
  67. bgr.y = 0;
  68. bgr.z = 0;
  69. cuda_pic.P_uchar3[ y * cuda_pic.Size.x + x ] = bgr;
  70. }
  71.  
  72. if (x - 80 >= y && x - 85 <= y && x > 174 && x < 199)
  73. {
  74. bgr.x = 0;
  75. bgr.y = 0;
  76. bgr.z = 255;
  77. cuda_pic.P_uchar3[ y * cuda_pic.Size.x + x ] = bgr;
  78. }
  79.  
  80. if (x + 33 <= abs(300 - y) && x + 38 >= abs(300 - y) && x > 146 && x < 174)
  81. {
  82. bgr.x = 0;
  83. bgr.y = 0;
  84. bgr.z = 255;
  85. cuda_pic.P_uchar3[ y * cuda_pic.Size.x + x ] = bgr;
  86. }
  87. }
  88.  
  89. void cu_run_animation( CUDA_Pic pic, uint2 block_size )
  90. {
  91. cudaError_t cerr;
  92.  
  93. CUDA_Pic cudaPic;
  94. cudaPic.Size = pic.Size;
  95.  
  96. // Memory allocation in GPU device
  97. cerr = cudaMalloc( &cudaPic.P_void, cudaPic.Size.x * cudaPic.Size.y * sizeof( uchar3 ) );
  98. if ( cerr != cudaSuccess )
  99. printf( \"CUDA Error [%d] - \'%s\'\\n\", __LINE__, cudaGetErrorString( cerr ) );
  100.  
  101. // Copy data to GPU device
  102. cerr = cudaMemcpy( cudaPic.P_void, pic.P_void, cudaPic.Size.x * cudaPic.Size.y * sizeof( uchar3 ), cudaMemcpyHostToDevice );
  103. if ( cerr != cudaSuccess )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement