Advertisement
matejnevlud

cuda_4

Nov 20th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.67 KB | None | 0 0
  1. // ***********************************************************************
  2. //
  3. // Demo program for education in subject
  4. // Computer Architectures and Parallel 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 with unified memory.
  9. //
  10. // Image transformation from RGB to BW schema.
  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.  
  19. // Demo kernel to transform RGB color schema to BW schema
  20. __global__ void kernel_grayscale( CUDA_Pic colorPic, CUDA_Pic bwPic ) {
  21.     // X,Y coordinates and check image dimensions
  22.     int y = blockDim.y * blockIdx.y + threadIdx.y;
  23.     int x = blockDim.x * blockIdx.x + threadIdx.x;
  24.     if ( y >= colorPic.Size.y ) return;
  25.     if ( x >= colorPic.Size.x ) return;
  26.  
  27.  
  28.  
  29.     // Get point from color picture
  30.     uchar3 bgr = colorPic.P_uchar3[ y * colorPic.Size.x + x ];
  31.  
  32.     // Store BW point to new image
  33.     bwPic.P_uchar1[ y * bwPic.Size.x + x ].x = bgr.x * 0.11 + bgr.y * 0.59 + bgr.z * 0.30;
  34. }
  35.  
  36. __global__ void kernel_resize( CUDA_Pic pic_orig, CUDA_Pic pic_small ) {
  37.     // X,Y coordinates and check image dimensions
  38.     int y = blockDim.y * blockIdx.y + threadIdx.y;
  39.     int x = blockDim.x * blockIdx.x + threadIdx.x;
  40.     if ( y >= pic_orig.Size.y ) return;
  41.     if ( x >= pic_orig.Size.x ) return;
  42.  
  43.     if(x % 2 == 0 && y % 2 == 0){
  44.         //pic_orig.P_uchar3[ y * pic_orig.Size.x + x ] = ( uchar3 ) { 0, 255, 0 };
  45.  
  46.         uchar3 lh = pic_orig.P_uchar3[ y * pic_orig.Size.x + x ];
  47.         uchar3 ph = pic_orig.P_uchar3[ y * pic_orig.Size.x + x + 1 ];
  48.         uchar3 ld = pic_orig.P_uchar3[ (y + 1) * pic_orig.Size.x + x ];
  49.         uchar3 pd = pic_orig.P_uchar3[ (y + 1) * pic_orig.Size.x + x + 1 ];
  50.  
  51.         uchar3 novy_pixel;
  52.         novy_pixel.x = (lh.x + ph.x + ld.x + pd.x) / 4;
  53.         novy_pixel.y = (lh.y + ph.y + ld.y + pd.y) / 4;
  54.         novy_pixel.z = (lh.z + ph.z + ld.z + pd.z) / 4;
  55.  
  56.  
  57.         int newPosition = (y / 2) * pic_small.Size.x + (x / 2);
  58.  
  59.         pic_small.P_uchar3[ newPosition ] = novy_pixel;
  60.     }
  61.  
  62.     // Get point from color picture
  63.     //uchar3 bgr = colorPic.P_uchar3[ y * colorPic.Size.x + x ];
  64.  
  65.     // Store BW point to new image
  66.     //bwPic.P_uchar1[ y * bwPic.Size.x + x ].x = bgr.x * 0.11 + bgr.y * 0.59 + bgr.z * 0.30;
  67.     //pic_small.P_uchar3[ newPosition ] = ( uchar3 ) { 0, 255, 0 };
  68.     //pic_small.P_uchar3[ newPosition ] = pic_orig.P_uchar3[ y * pic_orig.Size.x + x ];
  69. }
  70.  
  71. __global__ void kernel_blur( CUDA_Pic pic_orig, CUDA_Pic pic_blurred ) {
  72.     // X,Y coordinates and check image dimensions
  73.     int y = blockDim.y * blockIdx.y + threadIdx.y;
  74.     int x = blockDim.x * blockIdx.x + threadIdx.x;
  75.     if ( y >= pic_orig.Size.y ) return;
  76.     if ( x >= pic_orig.Size.x ) return;
  77.  
  78.     if(true){
  79.         //pic_orig.P_uchar3[ y * pic_orig.Size.x + x ] = ( uchar3 ) { 0, 255, 0 };
  80.  
  81.         uchar3 lh = pic_orig.P_uchar3[ y * pic_orig.Size.x + x ];
  82.         uchar3 ph = pic_orig.P_uchar3[ y * pic_orig.Size.x + x + 1 ];
  83.         uchar3 ld = pic_orig.P_uchar3[ (y + 1) * pic_orig.Size.x + x ];
  84.         uchar3 pd = pic_orig.P_uchar3[ (y + 1) * pic_orig.Size.x + x + 1 ];
  85.  
  86.         uchar3 novy_pixel;
  87.         novy_pixel.x = (lh.x + ph.x + ld.x + pd.x) / 4;
  88.         novy_pixel.y = (lh.y + ph.y + ld.y + pd.y) / 4;
  89.         novy_pixel.z = (lh.z + ph.z + ld.z + pd.z) / 4;
  90.  
  91.  
  92.         //int newPosition = (y / 2) * pic_small.Size.x + (x / 2);
  93.         //pic_blurred.P_uchar3[ newPosition ] = novy_pixel;
  94.         pic_blurred.P_uchar3[ y * pic_orig.Size.x + x ] = novy_pixel;
  95.         //pic_blurred.P_uchar3[ y * pic_orig.Size.x + x + 1 ] = novy_pixel;
  96.         //pic_blurred.P_uchar3[ (y + 1) * pic_orig.Size.x + x ] = novy_pixel;
  97.         //pic_blurred.P_uchar3[ (y + 1) * pic_orig.Size.x + x + 1 ] = novy_pixel;
  98.  
  99.  
  100.     }
  101.  
  102.     // Get point from color picture
  103.     //uchar3 bgr = colorPic.P_uchar3[ y * colorPic.Size.x + x ];
  104.  
  105.     // Store BW point to new image
  106.     //bwPic.P_uchar1[ y * bwPic.Size.x + x ].x = bgr.x * 0.11 + bgr.y * 0.59 + bgr.z * 0.30;
  107.     //pic_small.P_uchar3[ newPosition ] = ( uchar3 ) { 0, 255, 0 };
  108.     //pic_small.P_uchar3[ newPosition ] = pic_orig.P_uchar3[ y * pic_orig.Size.x + x ];
  109. }
  110.  
  111. __global__ void kernel_blur2(CUDA_Pic org_pic, CUDA_Pic new_pic)
  112. {
  113.     // X,Y coordinates and check image dimensions
  114.     int y = blockDim.y * blockIdx.y + threadIdx.y;
  115.     int x = blockDim.x * blockIdx.x + threadIdx.x;
  116.     if (y == 0) return;
  117.     if (x == 0) return;
  118.     if (y+1 >= org_pic.Size.y) return;
  119.     if (x+1 >= org_pic.Size.x) return;
  120.  
  121.     uchar3 bgr[9];
  122.     bgr[0] = org_pic.P_uchar3[(y - 1) * org_pic.Size.x + x - 1];
  123.     bgr[1] = org_pic.P_uchar3[(y - 1) * org_pic.Size.x + x];
  124.     bgr[2] = org_pic.P_uchar3[(y - 1) * org_pic.Size.x + x + 1];
  125.  
  126.     bgr[3] = org_pic.P_uchar3[y * org_pic.Size.x + x - 1];
  127.     bgr[4] = org_pic.P_uchar3[y * org_pic.Size.x + x];
  128.     bgr[5] = org_pic.P_uchar3[y * org_pic.Size.x + x + 1];
  129.  
  130.     bgr[6] = org_pic.P_uchar3[(y + 1) * org_pic.Size.x + x - 1];
  131.     bgr[7] = org_pic.P_uchar3[(y + 1) * org_pic.Size.x + x];
  132.     bgr[8] = org_pic.P_uchar3[(y + 1) * org_pic.Size.x + x + 1];
  133.  
  134.     uint3 avg;
  135.     avg.x = 0;
  136.     avg.y = 0;
  137.     avg.z = 0;
  138.  
  139.     for (int i = 0; i < 9; i++) {
  140.         avg.x += bgr[i].x;
  141.         avg.y += bgr[i].y;
  142.         avg.z += bgr[i].z;
  143.     }
  144.  
  145.     avg.x = avg.x / 9;
  146.     avg.y = avg.y / 9;
  147.     avg.z = avg.z / 9;
  148.  
  149.     uchar3 avgChar;
  150.     avgChar.x = avg.x;
  151.     avgChar.y = avg.y;
  152.     avgChar.z = avg.z;
  153.  
  154.     new_pic.P_uchar3[(y)* new_pic.Size.x + (x)] = avgChar;
  155. }
  156.  
  157.  
  158.  
  159. __global__ void kernel_rotate( CUDA_Pic pic_orig, CUDA_Pic pic_rotated, float angle ) {
  160.     // X,Y coordinates and check image dimensions
  161.     int y = blockDim.y * blockIdx.y + threadIdx.y;
  162.     int x = blockDim.x * blockIdx.x + threadIdx.x;
  163.     if ( y >= pic_orig.Size.y ) return;
  164.     if ( x >= pic_orig.Size.x ) return;
  165.  
  166.     int newx = x * cos(angle) + y * sin(angle);
  167.     int newy = y * cos(angle) - x * sin(angle);
  168.     newy = pic_rotated.Size.y + newy;
  169.  
  170.     int newPosition = (newy) * pic_rotated.Size.x + (newx);
  171.  
  172.     if(0 <= newPosition && newPosition < pic_rotated.Size.x * pic_rotated.Size.y )
  173.         pic_rotated.P_uchar3[ newPosition ] =  pic_orig.P_uchar3[ y * pic_orig.Size.x + x ];
  174.     //pic_rotated.P_uchar3[ newPosition ] = pic_orig.P_uchar3[ y * pic_orig.Size.x + x ];
  175.  
  176.     // Get point from color picture
  177.     //uchar3 bgr = colorPic.P_uchar3[ y * colorPic.Size.x + x ];
  178.  
  179.     // Store BW point to new image
  180.     //bwPic.P_uchar1[ y * bwPic.Size.x + x ].x = bgr.x * 0.11 + bgr.y * 0.59 + bgr.z * 0.30;
  181.     //pic_small.P_uchar3[ newPosition ] = ( uchar3 ) { 0, 255, 0 };
  182.     //pic_small.P_uchar3[ newPosition ] = pic_orig.P_uchar3[ y * pic_orig.Size.x + x ];
  183. }
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190. void cu_run_grayscale( CUDA_Pic colorPic, CUDA_Pic bwPic ) {
  191.     cudaError_t cerr;
  192.  
  193.     // Grid creation, size of grid must be equal or greater than images
  194.     int block_size = 16;
  195.     dim3 blocks( ( colorPic.Size.x + block_size - 1 ) / block_size, ( colorPic.Size.y + block_size - 1 ) / block_size );
  196.     dim3 threads( block_size, block_size );
  197.  
  198.  
  199.     kernel_grayscale<<< blocks, threads >>>( colorPic, bwPic );
  200.  
  201.     if ( ( cerr = cudaGetLastError() ) != cudaSuccess )
  202.         printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  203.  
  204.     cudaDeviceSynchronize();
  205. }
  206.  
  207. void cu_run_resize( CUDA_Pic pic_orig, CUDA_Pic pic_small ) {
  208.     cudaError_t cerr;
  209.  
  210.     // Grid creation, size of grid must be equal or greater than images
  211.     int block_size = 16;
  212.     dim3 blocks( ( pic_orig.Size.x + block_size - 1 ) / block_size, ( pic_orig.Size.y + block_size - 1 ) / block_size );
  213.     dim3 threads( block_size, block_size );
  214.  
  215.  
  216.     kernel_resize<<< blocks, threads >>>( pic_orig, pic_small );
  217.  
  218.     if ( ( cerr = cudaGetLastError() ) != cudaSuccess )
  219.         printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  220.  
  221.     cudaDeviceSynchronize();
  222. }
  223.  
  224. void cu_run_blur( CUDA_Pic pic_orig, CUDA_Pic pic_blurred ) {
  225.     cudaError_t cerr;
  226.  
  227.     // Grid creation, size of grid must be equal or greater than images
  228.     int block_size = 16;
  229.     dim3 blocks( ( pic_orig.Size.x + block_size - 1 ) / block_size, ( pic_orig.Size.y + block_size - 1 ) / block_size );
  230.     dim3 threads( block_size, block_size );
  231.  
  232.  
  233.     kernel_blur<<< blocks, threads >>>( pic_orig, pic_blurred );
  234.  
  235.     if ( ( cerr = cudaGetLastError() ) != cudaSuccess )
  236.         printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  237.  
  238.     cudaDeviceSynchronize();
  239. }
  240.  
  241. void cu_run_rotate( CUDA_Pic pic_orig, CUDA_Pic pic_rotated, float angle ) {
  242.     cudaError_t cerr;
  243.  
  244.     // Grid creation, size of grid must be equal or greater than images
  245.     int block_size = 16;
  246.     dim3 blocks( ( pic_orig.Size.x + block_size - 1 ) / block_size, ( pic_orig.Size.y + block_size - 1 ) / block_size );
  247.     dim3 threads( block_size, block_size );
  248.  
  249.  
  250.     kernel_rotate<<< blocks, threads >>>( pic_orig, pic_rotated, angle );
  251.  
  252.     if ( ( cerr = cudaGetLastError() ) != cudaSuccess )
  253.         printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
  254.  
  255.     cudaDeviceSynchronize();
  256.  
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement