Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ***********************************************************************
- //
- // Demo program pro vyuku predmetu APPS (10/2021)
- // Petr Olivka, katedra informatiky, FEI, VSB-TU Ostrava
- // email:petr.olivka@vsb.cz
- //
- // Priklad pouziti CUDA technologie.
- // Prevod barevneho obrazku na odstiny sede barvy
- //
- // ***********************************************************************
- #include <cuda_runtime.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- __global__ void resizebig(uchar4 * input ,uchar4 * output ,int sizex, int sizey)
- {
- // souradnice vlakna
- int y = blockDim.y * blockIdx.y + threadIdx.y;
- if ( y >= sizey ) return;
- int x = blockDim.x * blockIdx.x + threadIdx.x;
- if ( x >= sizex ) return;
- for (int j = 1; j <= 1; j++)
- {
- for (int i = -1; i <= 1; i++)
- {
- output[(y * 2 + j) * sizex * 2 + (x * 2 + i)] = input[y * sizex + x];
- }
- }
- }
- void run_resize_big(uchar4 *input ,uchar4* output ,int sizex ,int sizey){
- cudaError_t erorlog;
- uchar4 * CUDAinput;
- uchar4 * CUDAoutput;
- erorlog = cudaMalloc( &CUDAinput, sizex * sizey * sizeof( uchar4 ) );
- if ( erorlog != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( erorlog ) );
- /// sice by mohlo být paměti víc ... ale vyhrál jsme si :D
- erorlog = cudaMalloc( &CUDAoutput, (sizex*2) * (sizey*2) * sizeof( uchar4 ) );
- if ( erorlog != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( erorlog ) );
- // prenos barevneho obrazku do videokarty
- erorlog = cudaMemcpy( CUDAinput, input, sizex * sizey* sizeof( uchar4 ), cudaMemcpyHostToDevice );
- if ( erorlog != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( erorlog) );
- int block = 16;
- dim3 blocks( ( sizex + block - 1 ) / block, ( sizey + block - 1 ) / block );
- dim3 threads( block, block );
- /// proste hodiny ney jsem na to přišel
- resizebig<<< blocks,threads >>> (CUDAinput ,CUDAoutput,sizex ,sizey );
- if ( ( erorlog = cudaGetLastError() ) != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( erorlog ) );
- // prenos dat z videokarty
- erorlog = cudaMemcpy( output, CUDAoutput, (sizex*2) * (sizey *2)* sizeof( uchar4 ), cudaMemcpyDeviceToHost );
- if (erorlog != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( erorlog ) );
- cudaThreadSynchronize();
- cudaFree( CUDAinput );
- cudaFree( CUDAoutput );
- }
- __global__ void resizelow(uchar4 * input ,uchar4 * output ,int sizex, int sizey)
- {
- // souradnice vlakna
- int y = blockDim.y * blockIdx.y + threadIdx.y;
- if ( y >= sizey ) return;
- int x = blockDim.x * blockIdx.x + threadIdx.x;
- if ( x >= sizex ) return;
- //vyorec na zmenseni
- output[y * sizex + x] = input[(y * 4 * sizex) + x*2];
- }
- /// upravou vzorce otocis doleva nebo doprava
- __global__ void kernel_rotace(uchar4 * input ,uchar4 * output ,int sizex, int sizey)
- {
- int x = blockDim.x * blockIdx.x + threadIdx.x;
- if ( x >= sizex ) return;
- int y = blockDim.y * blockIdx.y + threadIdx.y;
- if ( y >= sizey ) return;
- /// otoceni doleva
- output[(sizex - x) * sizey + y] = input[y * sizex + x];
- //otoceni doprava
- //output[x * sizey + (sizey - y)] =input[y * sizex + x];
- /*
- //vyplneni barvou
- /*
- int position = y * sizex + x;
- output[position].x = 255;
- output[position].y = 0;
- output[position].z = 0;
- */
- }
- void run_rotace(uchar4 *input ,uchar4* output ,int sizex ,int sizey){
- cudaError_t erorlog;
- uchar4 * CUDAinput;
- uchar4 * CUDAoutput;
- erorlog = cudaMalloc( &CUDAinput, sizex * sizey * sizeof( uchar4 ) );
- if ( erorlog != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( erorlog ) );
- erorlog = cudaMalloc( &CUDAoutput, sizex * sizey * sizeof( uchar4 ) );
- if ( erorlog != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( erorlog ) );
- // prenos barevneho obrazku do videokarty
- erorlog = cudaMemcpy( CUDAinput, input, sizex * sizey * sizeof( uchar4 ), cudaMemcpyHostToDevice );
- if ( erorlog != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( erorlog) );
- int block = 16;
- dim3 blocks( ( sizex + block - 1 ) / block, ( sizey + block - 1 ) / block );
- dim3 threads( block, block );
- kernel_rotace <<< blocks,threads >>> (CUDAinput ,CUDAoutput,sizex ,sizey );
- if ( ( erorlog = cudaGetLastError() ) != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( erorlog ) );
- // prenos dat z videokarty
- erorlog = cudaMemcpy( output, CUDAoutput, sizex * sizey * sizeof( uchar4 ), cudaMemcpyDeviceToHost );
- if (erorlog != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( erorlog ) );
- cudaThreadSynchronize();
- cudaFree( CUDAinput );
- cudaFree( CUDAoutput );
- }
- void run_resize_low(uchar4 *input ,uchar4* output ,int sizex ,int sizey){
- cudaError_t erorlog;
- uchar4 * CUDAinput;
- uchar4 * CUDAoutput;
- erorlog = cudaMalloc( &CUDAinput, sizex * sizey * sizeof( uchar4 ) );
- if ( erorlog != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( erorlog ) );
- /// sice by mohlo být paměti víc ... ale vyhrál jsme si :D
- erorlog = cudaMalloc( &CUDAoutput, (sizex/2) * (sizey/2) * sizeof( uchar4 ) );
- if ( erorlog != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( erorlog ) );
- // prenos barevneho obrazku do videokarty
- erorlog = cudaMemcpy( CUDAinput, input, sizex * sizey* sizeof( uchar4 ), cudaMemcpyHostToDevice );
- if ( erorlog != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( erorlog) );
- int block = 16;
- dim3 blocks( ( sizex + block - 1 ) / block, ( sizey + block - 1 ) / block );
- dim3 threads( block, block );
- /// proste hodiny ney jsem na to přišel
- resizelow <<< blocks,threads >>> (CUDAinput ,CUDAoutput,sizex/2 ,sizey/2 );
- if ( ( erorlog = cudaGetLastError() ) != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( erorlog ) );
- // prenos dat z videokarty
- erorlog = cudaMemcpy( output, CUDAoutput, (sizex/2) * (sizey /2)* sizeof( uchar4 ), cudaMemcpyDeviceToHost );
- if (erorlog != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( erorlog ) );
- cudaThreadSynchronize();
- cudaFree( CUDAinput );
- cudaFree( CUDAoutput );
- }
- // Demo kernel pro prevod barevneho bodu na odstin sede.
- __global__ void kernel_grayscale( uchar4 *color_pic, uchar4* bw_pic, uchar4* MyO ,int sizex, int sizey )
- {
- // souradnice vlakna, kontrola rozmeru obrazku
- int y = blockDim.y * blockIdx.y + threadIdx.y;
- if ( y >= sizey ) return;
- int x = blockDim.x * blockIdx.x + threadIdx.x;
- if ( x >= sizex ) return;
- uchar4 bgr = color_pic[ y * sizex + x ];
- uchar4 bgr1 = color_pic[ y * sizex + x ];
- // vsechny tri barevne slozky budou mit stejnou hodnotu
- bgr.x = bgr.y = bgr.z = bgr.x * 0.11 + bgr.y * 0.59 + bgr.z * 0.30;
- // ulozeni bodu do obrazku
- bw_pic[ y * sizex + x ] = bgr;
- bgr1.x = bgr1.x * 0.75 ;
- bgr1.y = bgr1.y * 0.1 ;
- bgr1.z = bgr1.z * 0.75;
- MyO [y * sizex + x ] = bgr1;
- }
- void run_grayscale( uchar4 *color_pic, uchar4* bw_pic, uchar4* MyO, int sizex, int sizey )
- {
- cudaError_t cerr;
- // alokace pameti ve videokarte
- uchar4 *cudaColorPic;
- uchar4 *cudaBWPic;
- //////////////////////////////
- uchar4* CudaMyO;
- /////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
- cerr = cudaMalloc( &CudaMyO, sizex * sizey * sizeof( uchar4 ) );
- if ( cerr != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
- /////////////////////////////////////////////////////////////////////////////////////
- cerr = cudaMalloc( &cudaColorPic, sizex * sizey * sizeof( uchar4 ) );
- if ( cerr != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
- cerr = cudaMalloc( &cudaBWPic, sizex * sizey * sizeof( uchar4 ) );
- if ( cerr != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
- // prenos barevneho obrazku do videokarty
- cerr = cudaMemcpy( cudaColorPic, color_pic, sizex * sizey * sizeof( uchar4 ), cudaMemcpyHostToDevice );
- if ( cerr != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
- int block = 16;
- dim3 blocks( ( sizex + block - 1 ) / block, ( sizey + block - 1 ) / block );
- dim3 threads( block, block );
- // vytvoreni bloku s vlakny, matice vlaken muze byt vetsi, nez samotny obrazek!
- kernel_grayscale<<< blocks, threads >>>( cudaColorPic, cudaBWPic,CudaMyO, sizex, sizey );
- ////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////
- if ( ( cerr = cudaGetLastError() ) != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
- // prenos dat z videokarty
- cerr = cudaMemcpy( bw_pic, cudaBWPic, sizex * sizey * sizeof( uchar4 ), cudaMemcpyDeviceToHost );
- if ( cerr != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
- //////////////////////////////////////////////////////////////////////////////////////
- cerr = cudaMemcpy( MyO, CudaMyO, sizex * sizey * sizeof( uchar4 ), cudaMemcpyDeviceToHost );
- if ( cerr != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
- //////////////////////////////////////////////////////////////////////////////////////
- // uvolneni alokovane pameti ve videokarte
- cudaFree( cudaColorPic );
- cudaFree( cudaBWPic );
- cudaFree( CudaMyO);
- }
- __global__ void prolnuti( uchar4 *first,uchar4 *second,uchar4 *final, int sizex, int sizey )
- {
- // souradnice vlakna
- int y = blockDim.y * blockIdx.y + threadIdx.y;
- int x = blockDim.x * blockIdx.x + threadIdx.x;
- if ( x >= sizex ) return;
- if ( y >= sizey ) return;
- float perc2 = x / (float)sizex;
- final[y * sizex + x].x = first[y * sizex + x].x * perc2 + second[y * sizex + x].x * (1 - perc2);
- final[y * sizex + x].y = first[y * sizex + x].y * perc2 + second[y * sizex + x].y * (1 - perc2);
- final[y * sizex + x].z = first[y * sizex + x].z * perc2 + second[y * sizex + x].z * (1 - perc2);
- }
- void joinImage( uchar4 *original, uchar4 *original2, uchar4 *joined, int width, int height )
- {
- cudaError_t cerr;
- // alokace pameti ve videokarte
- uchar4 *cudaOriginal;
- uchar4 *cudaOriginal2;
- uchar4 *cudajoined;
- cerr = cudaMalloc( &cudaOriginal, width * height * sizeof( uchar4 ) );
- if ( cerr != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
- cerr = cudaMalloc( &cudaOriginal2, width * height * sizeof( uchar4 ) );
- if ( cerr != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
- cerr = cudaMalloc( &cudajoined, width * height * sizeof( uchar4 ) );
- if ( cerr != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
- // prenos barevneho obrazku do videokarty
- cerr = cudaMemcpy( cudaOriginal, original, width * height * sizeof( uchar4 ), cudaMemcpyHostToDevice );
- if ( cerr != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
- cerr = cudaMemcpy( cudaOriginal2, original2, width * height * sizeof( uchar4 ), cudaMemcpyHostToDevice );
- if ( cerr != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
- int block = 16;
- dim3 blocks( ( width + block - 1 ) / block, ( height + block - 1 ) / block );
- dim3 threads( block, block );
- // vytvoreni bloku s vlakny, matice vlaken muze byt vetsi, nez samotny obrazek!
- prolnuti<<< blocks, threads >>>( cudaOriginal,cudaOriginal2, cudajoined, width, height );
- if ( ( cerr = cudaGetLastError() ) != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
- // prenos dat z videokarty
- cerr = cudaMemcpy( joined, cudajoined, width * height * sizeof( uchar4 ), cudaMemcpyDeviceToHost );
- if ( cerr != cudaSuccess )
- printf( "CUDA Error [%d] - '%s'\n", __LINE__, cudaGetErrorString( cerr ) );
- // uvolneni alokovane pameti ve videokarte
- cudaFree( cudajoined );
- cudaFree( cudaOriginal );
- cudaFree( cudaOriginal2 );
- }
Add Comment
Please, Sign In to add comment