Guest User

zmenseniscuda

a guest
Dec 7th, 2017
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.25 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. //
  7. // Example of CUDA Technology Usage.
  8. // Image transformation from RGB to BW schema.
  9. //
  10. // ***********************************************************************
  11.  
  12. #include <cuda_runtime.h>
  13. #include <device_launch_parameters.h>
  14. #include <stdio.h>
  15.  
  16.  
  17. // RESIZE + MRIZKA
  18.  
  19. __global__ void kernel_resize(uchar4 *first, uchar4 *final, int size2x, int size2y)
  20. //( uchar4 *color_pic, uchar4* bw_pic, int sizex, int sizey )
  21.  
  22. {
  23. // souradnice vlakna - zjistujeme vlakna podobne jako pixely
  24. int y = blockDim.y * blockIdx.y + threadIdx.y;
  25. int x = blockDim.x * blockIdx.x + threadIdx.x;
  26. if (x >= size2x) return;
  27. if (y >= size2y) return;
  28.  
  29.  
  30. uchar4 bgr = first[(y * 4 * size2x) + x * 2] ;
  31.  
  32.  
  33. // vykreslí mřížku
  34. // sloupce
  35. for (int i = 0; i<x; i++)
  36. {
  37. //co 20pix nebo o 1 vedle
  38. // kdyby chtel co 40 tak to bude x%40 = 0 || x%40= 39
  39. if (x % 20 == 0 || x % 20 ==1)
  40. {
  41.  
  42. bgr.x = bgr.y = bgr.z = 250;
  43.  
  44. }
  45. }
  46.  
  47. // radky
  48. for (int j = 0; j<y; j++)
  49. {
  50. if (y % 20 == 0 || y % 20 ==1)
  51. {
  52.  
  53. bgr.x = bgr.y = bgr.z = 250;
  54.  
  55. }
  56. }
  57.  
  58.  
  59. // vsechny tri barevne slozky budou mit stejnou hodnotu
  60. //bgr.x = bgr.y = bgr.z = bgr.x * 0.11 + bgr.y * 0.59 + bgr.z * 0.30;
  61.  
  62. //uchar4 bgr = first[(y * 4 * size2x) + x * 2] ;
  63.  
  64. final [ y * size2x + x ] = bgr;
  65.  
  66. //final [y * size2x + x] = first[(y * 4 * size2x) + x * 2] = bgr;
  67. }
  68.  
  69. void joinImage(uchar4 *original, uchar4 *rotated, int width, int height, int width2, int height2)
  70. {
  71. cudaError_t cerr;
  72.  
  73. // alokace pameti ve videokarte
  74. uchar4 *cudaOriginal;
  75. uchar4 *cudaRotate;
  76. cerr = cudaMalloc(&cudaOriginal, width * height * sizeof(uchar4)); // alokuje, pozdeji zjistuje jestli je vse ok
  77. if (cerr != cudaSuccess)
  78. printf("CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString(cerr));
  79.  
  80. cerr = cudaMalloc(&cudaRotate, width2 * height2 * sizeof(uchar4));
  81. if (cerr != cudaSuccess)
  82. printf("CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString(cerr));
  83.  
  84. // prenos barevneho obrazku do videokarty
  85. cerr = cudaMemcpy(cudaOriginal, original, width * height * sizeof(uchar4), cudaMemcpyHostToDevice);
  86. if (cerr != cudaSuccess)
  87. printf("CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString(cerr));
  88.  
  89. int block = 16; // 16 pixelu
  90. dim3 blocks((width2 + block - 1) / block, (height2 + block - 1) / block); // ma 3 parametry, ale resime jen 2D - proto -1
  91. dim3 threads(block, block);
  92.  
  93. // vytvoreni bloku s vlakny, matice vlaken muze byt vetsi, nez samotny obrazek!
  94. kernel_resize << < blocks, threads >> >(cudaOriginal, cudaRotate, width2, height2);
  95.  
  96. if ((cerr = cudaGetLastError()) != cudaSuccess)
  97. printf("CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString(cerr));
  98.  
  99. // prenos dat z videokarty
  100. cerr = cudaMemcpy(rotated, cudaRotate, width2 * height2 * sizeof(uchar4), cudaMemcpyDeviceToHost);
  101. if (cerr != cudaSuccess)
  102. printf("CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString(cerr));
  103.  
  104. // uvolneni alokovane pameti ve videokarte
  105. cudaFree(cudaRotate);
  106. cudaFree(cudaOriginal);
  107.  
  108. }
  109.  
  110.  
  111.  
  112. /*
  113. // BLACK AND WHITE
  114.  
  115. // Demo kernel to tranfrom RGB color schema to BW schema
  116. __global__ void kernel_grayscale( uchar4 *color_pic, uchar4* bw_pic, int sizex, int sizey, char barva )
  117. {
  118. // X,Y coordinates and check image dimensions
  119. int y = blockDim.y * blockIdx.y + threadIdx.y;
  120. if ( y >= sizey ) return;
  121. int x = blockDim.x * blockIdx.x + threadIdx.x;
  122. if ( x >= sizex ) return;
  123.  
  124. // Get point from color picture
  125. uchar4 bgr = color_pic[ y * sizex + x ];
  126.  
  127. // All R-G-B channels will have the same value
  128. switch (barva){
  129. case'B': bgr.y = bgr.z = 0;
  130. break;
  131. case 'G': bgr.x = bgr.y = bgr.z = bgr.x * 0.11 + bgr.y * 0.59 + bgr.z * 0.30;
  132. break;
  133. }
  134.  
  135. // Store BW point to new image
  136. bw_pic[ y * sizex + x ] = bgr;
  137. }
  138.  
  139. void run_grayscale( uchar4 *color_pic, uchar4* bw_pic, int sizex, int sizey, char barva )
  140. {
  141. cudaError_t cerr;
  142. // Memory allocation in GPU device
  143. uchar4 *cudaColorPic;
  144. uchar4 *cudaBWPic;
  145. cerr = cudaMalloc( &cudaColorPic, sizex * sizey * sizeof( uchar4 ) );
  146. if ( cerr != cudaSuccess )
  147. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  148.  
  149. cerr = cudaMalloc( &cudaBWPic, sizex * sizey * sizeof( uchar4 ) );
  150. if ( cerr != cudaSuccess )
  151. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  152.  
  153. // Copy color image to GPU device
  154. cerr = cudaMemcpy( cudaColorPic, color_pic, sizex * sizey * sizeof( uchar4 ), cudaMemcpyHostToDevice );
  155. if ( cerr != cudaSuccess )
  156. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  157.  
  158. int block = 16;
  159. dim3 blocks( ( sizex + block - 1 ) / block, ( sizey + block - 1 ) / block );
  160. dim3 threads( block, block );
  161.  
  162. // Grid creation, size of grid must be greater than image
  163. kernel_grayscale<<< blocks, threads >>>( cudaColorPic, cudaBWPic, sizex, sizey, barva );
  164.  
  165. if ( ( cerr = cudaGetLastError() ) != cudaSuccess )
  166. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  167.  
  168. // Copy new image from GPU device
  169. cerr = cudaMemcpy( bw_pic, cudaBWPic, sizex * sizey * sizeof( uchar4 ), cudaMemcpyDeviceToHost );
  170. if ( cerr != cudaSuccess )
  171. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  172.  
  173. // Free memory
  174. cudaFree( cudaColorPic );
  175. cudaFree( cudaBWPic );
  176. }
  177. */
  178.  
  179.  
  180.  
  181. // ROTACE
  182.  
  183. __global__ void kernel_rotate(uchar4 *color_pic, uchar4 *rotated_pic, int sizex, int sizey)
  184. {
  185. //Souradnice vlaken, kontrola
  186. int y = blockDim.y * blockIdx.y + threadIdx.y;
  187. int x = blockDim.x * blockIdx.x + threadIdx.x;
  188. if (x >= sizex) return;
  189. if (y >= sizey) return;
  190.  
  191. // Upside down
  192. //rotated_pic[y * sizex + x] = color_pic[(sizey - y - 1) * sizex + x];
  193.  
  194.  
  195. rotated_pic[(y * sizex) + x] = color_pic[(y * sizex) + (sizex - x - 1)];
  196.  
  197.  
  198. }
  199.  
  200. void rotate_image(uchar4 *color_pic, uchar4 *rotated_pic, int sizex, int sizey)
  201. {
  202. cudaError_t cerr;
  203. // Memory allocation in GPU device
  204. uchar4 *cudaColorPic;
  205. uchar4 *cudaRotatedPic;
  206.  
  207. cerr = cudaMalloc(&cudaColorPic, sizex * sizey * sizeof(uchar4));
  208. if (cerr != cudaSuccess)
  209. printf("CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString(cerr));
  210.  
  211. cerr = cudaMalloc(&cudaRotatedPic, sizex * sizey * sizeof(uchar4));
  212. if (cerr != cudaSuccess)
  213. printf("CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString(cerr));
  214.  
  215. // Copy color image to GPU device
  216. cerr = cudaMemcpy(cudaColorPic, color_pic, sizex * sizey * sizeof(uchar4), cudaMemcpyHostToDevice);
  217. if (cerr != cudaSuccess)
  218. printf("CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString(cerr));
  219.  
  220. int block = 16; //VELIKOST BLOKU
  221. dim3 blocks((sizex + block - 1) / block, (sizey + block - 1) / block); //kolik blocku je v gridu
  222. dim3 threads(block, block); //kolik threadu je v blocku
  223.  
  224. // Grid creation, size of grid must be greater than image
  225. kernel_rotate<<< blocks, threads >>>(cudaColorPic, cudaRotatedPic, sizex, sizey);
  226.  
  227. if ((cerr = cudaGetLastError()) != cudaSuccess)
  228. printf("CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString(cerr));
  229.  
  230. // Copy new image from GPU device
  231. cerr = cudaMemcpy(rotated_pic, cudaRotatedPic, sizex * sizey * sizeof(uchar4), cudaMemcpyDeviceToHost);
  232. if (cerr != cudaSuccess)
  233. printf("CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString(cerr));
  234.  
  235. // Free memory
  236. cudaFree(cudaColorPic);
  237. cudaFree(cudaRotatedPic);
  238. }
  239.  
  240.  
  241. __global__ void kernel_flip( uchar4 *color_pic, uchar4* bw_pic, int sizex, int sizey )
  242. {
  243. // X,Y coordinates and check image dimensions
  244. int y = blockDim.y * blockIdx.y + threadIdx.y;
  245. if ( y >= sizey ) return;
  246. int x = blockDim.x * blockIdx.x + threadIdx.x;
  247. if ( x >= sizex ) return;
  248.  
  249. //180
  250. bw_pic[y * sizex + x] = color_pic[(sizey - y) * sizex + x];
  251.  
  252. //zrcadleni
  253. //bw_pic[ y * sizex + x ] = color_pic[ y * sizex + sizex - x ];
  254.  
  255. }
  256.  
  257.  
  258. void run_flip( uchar4 *color_pic, uchar4* bw_pic, int sizex, int sizey )
  259. {
  260. cudaError_t cerr;
  261. // Memory allocation in GPU device
  262. uchar4 *cudaColorPic;
  263. uchar4 *cudaBWPic;
  264. cerr = cudaMalloc( &cudaColorPic, sizex * sizey * sizeof( uchar4 ) );
  265. if ( cerr != cudaSuccess )
  266. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  267.  
  268. cerr = cudaMalloc( &cudaBWPic, sizex * sizey * sizeof( uchar4 ) );
  269. if ( cerr != cudaSuccess )
  270. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  271.  
  272. // Copy color image to GPU device
  273. cerr = cudaMemcpy( cudaColorPic, color_pic, sizex * sizey * sizeof( uchar4 ), cudaMemcpyHostToDevice );
  274. if ( cerr != cudaSuccess )
  275. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  276.  
  277. int block = 16;
  278. dim3 blocks( ( sizex + block - 1 ) / block, ( sizey + block - 1 ) / block );
  279. dim3 threads( block, block );
  280.  
  281. // Grid creation, size of grid must be greater than image
  282. kernel_flip<<< blocks, threads >>>( cudaColorPic, cudaBWPic, sizex, sizey );
  283.  
  284. if ( ( cerr = cudaGetLastError() ) != cudaSuccess )
  285. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  286.  
  287. // Copy new image from GPU device
  288. cerr = cudaMemcpy( bw_pic, cudaBWPic, sizex * sizey * sizeof( uchar4 ), cudaMemcpyDeviceToHost );
  289. if ( cerr != cudaSuccess )
  290. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  291.  
  292. // Free memory
  293. cudaFree( cudaColorPic );
  294. cudaFree( cudaBWPic );
  295. }
  296.  
  297.  
  298.  
  299. __global__ void kernel_mriz( uchar4 *color_pic, uchar4* bw_pic, int sizex, int sizey )
  300. {
  301. // X,Y coordinates and check image dimensions
  302. int y = blockDim.y * blockIdx.y + threadIdx.y;
  303. if ( y >= sizey ) return;
  304. int x = blockDim.x * blockIdx.x + threadIdx.x;
  305. if ( x >= sizex ) return;
  306.  
  307. uchar4 bgr = color_pic[ y * sizex + x ];
  308.  
  309. // vykresli se jen obsah krize
  310. /*
  311. if (y < 100)
  312. {
  313. if(x < 180 || x > 220)
  314. bgr.x = bgr.y = bgr.z = 100;
  315. }
  316. else if (y > 140)
  317. {
  318. if(x < 180 || x > 220)
  319. bgr.x = bgr.y = bgr.z = 100;
  320. }
  321.  
  322. bw_pic[ y * sizex + x ] = bgr;*/
  323.  
  324.  
  325. // nakresli kriz
  326. /*
  327. if (x > 100 && x < 280)
  328. {
  329. if (y > 148 && y < 150)
  330. {
  331. bgr.x = bgr.y = bgr.z =250;
  332. }
  333. }
  334.  
  335. if (y > 50 && y < 450)
  336. {
  337. if (x > 180 && x < 200)
  338. {
  339. bgr.x = bgr.y = bgr.z = 250;
  340. }
  341. }*/
  342.  
  343.  
  344. // vykreslí mřížku
  345. // sloupce
  346. for (int i = 0; i<x; i++)
  347. {
  348. //co 20pix nebo o 1 vedle
  349. // kdyby chtel co 40 tak to bude x%40 = 0 || x%40= 39
  350. if (x % 20 == 0 || x % 20 ==1)
  351. {
  352.  
  353. bgr.x = bgr.y = bgr.z =250;
  354.  
  355. }
  356. }
  357.  
  358. // radky
  359. for (int j = 0; j<y; j++)
  360. {
  361. if (y % 20 == 0 || y % 20 ==1)
  362. {
  363.  
  364. bgr.x = bgr.y = bgr.z =250;
  365.  
  366. }
  367. }
  368.  
  369. // vsechny tri barevne slozky budou mit stejnou hodnotu
  370. //bgr.x = bgr.y = bgr.z = bgr.x * 0.11 + bgr.y * 0.59 + bgr.z * 0.30;
  371.  
  372. bw_pic[ y * sizex + x ] = bgr;
  373. }
  374.  
  375. void run_mriz( uchar4 *color_pic, uchar4* bw_pic, int sizex, int sizey )
  376. {
  377. cudaError_t cerr;
  378. // Memory allocation in GPU device
  379. uchar4 *cudaColorPic;
  380. uchar4 *cudaBWPic;
  381. cerr = cudaMalloc( &cudaColorPic, sizex * sizey * sizeof( uchar4 ) );
  382. if ( cerr != cudaSuccess )
  383. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  384.  
  385. cerr = cudaMalloc( &cudaBWPic, sizex * sizey * sizeof( uchar4 ) );
  386. if ( cerr != cudaSuccess )
  387. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  388.  
  389. // Copy color image to GPU device
  390. cerr = cudaMemcpy( cudaColorPic, color_pic, sizex * sizey * sizeof( uchar4 ), cudaMemcpyHostToDevice );
  391. if ( cerr != cudaSuccess )
  392. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  393.  
  394. int block = 16;
  395. dim3 blocks( ( sizex + block - 1 ) / block, ( sizey + block - 1 ) / block );
  396. dim3 threads( block, block );
  397.  
  398. // Grid creation, size of grid must be greater than image
  399. kernel_mriz<<< blocks, threads >>>( cudaColorPic, cudaBWPic, sizex, sizey );
  400.  
  401. if ( ( cerr = cudaGetLastError() ) != cudaSuccess )
  402. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  403.  
  404. // Copy new image from GPU device
  405. cerr = cudaMemcpy( bw_pic, cudaBWPic, sizex * sizey * sizeof( uchar4 ), cudaMemcpyDeviceToHost );
  406. if ( cerr != cudaSuccess )
  407. printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  408.  
  409. // Free memory
  410. cudaFree( cudaColorPic );
  411. cudaFree( cudaBWPic );
  412. }
Advertisement
Add Comment
Please, Sign In to add comment