PROCESSING A PICTURE WITH A 2D GRID 16×16 blocks 62x76 picture 电子料线女学 University of Electreaie Science and Technolory of China O
PROCESSING A PICTURE WITH A 2D GRID
ROW-MAJOR LAYOUT IN C/C++ M Row*Vidth+Col=2*4+1≡9■ M Mi M2 M3 MM 5M6 M Ms Mo M10 Mi1 M12 Mi3 M Mi M Moo Mo1 Mo2 Mos Mio M11M12 Mis M20 M21 M22 M23 M3o M31 M32 M3s Moo Mor Mo2Mo3 Mio M12M13 M20M2,1M2.2M2.3 M30M3,M32M3,3 电子料做女学 Universityof ElectriScience and TachnolofChina O
M0,2 M1,1 M0,0 M0,1 M1,0 M0,3 M1,2 M1,3 M0,0 M0,1 M0,2 M0,3 M1,0 M1,1 M1,2 M1,3 M2,0 M2,1 M2,2 M2,3 M2,0 M2,1 M2,2 M2,3 M3,0 M3,1 M3,2 M3,3 M3,0 M3,1 M3,2 M3,3 M Row*Width+Col = 2*4+1 = 9 M0 M1 M2 M3 M4 M5 M6 M7 M8 M9 M10 M11 M12 M13 M14 M15 M ROW-MAJOR LAYOUT IN C/C++
SOURCE CODE OF A PICTUREKERNEL global void PictureKernel(float*d Pin,float*d Pout, int height,int width) { /Calculate the row of the d pin and d Pout element int Row blockIdx.y*blockDim.y threadIdx.y; /Calculate the column of the d pin and d Pout element int Col blockIdx.x*blockDim.x threadIdx.x; /each thread computes one element of d Pout if in range if ((Row height)&&(Col width)){ d Pout [Row*width+Col]2.0*d Pin[Row*width+Col]; 电子料发女学 University of Electreaie Science and Technolory of China O
SOURCE CODE OF A PICTUREKERNEL __global__ void PictureKernel(float* d_Pin, float* d_Pout, int height, int width) { // Calculate the row # of the d_Pin and d_Pout element int Row = blockIdx.y*blockDim.y + threadIdx.y; // Calculate the column # of the d_Pin and d_Pout element int Col = blockIdx.x*blockDim.x + threadIdx.x; // each thread computes one element of d_Pout if in range if ((Row < height) && (Col < width)) { d_Pout[Row*width+Col] = 2.0*d_Pin[Row*width+Col]; } } Scale every pixel value by 2.0
HOST CODE FOR LAUNCHING PICTUREKERNEL /assume that the picture is m x n, //m pixels in y dimension and n pixels in x dimension /input d_Pin has been allocated on and copied to device /output d Pout has been allocated on device dim3 DimGrid(n-1)/16+1,(m-1)/16+1,1); dim3 DimBlock(16,16,1); PictureKernel<<<DimGrid,DimBlock>>>(d_Pin, d_Pout,m,n); 电子料效女学 University of Electricience and TachnolopChina O
HOST CODE FOR LAUNCHING PICTUREKERNEL // assume that the picture is m × n, // m pixels in y dimension and n pixels in x dimension // input d_Pin has been allocated on and copied to device // output d_Pout has been allocated on device … dim3 DimGrid((n-1)/16 + 1, (m-1)/16+1, 1); dim3 DimBlock(16, 16, 1); PictureKernel<<<DimGrid,DimBlock>>>(d_Pin, d_Pout, m, n); …
COVERING A62×I6 PICTURE WITH16×16 BLOCKS 16×16 block Not all threads in a Block will follow the same control flow path. 电子料线女学 Universityof ElectriScience and TachnolopChina O
COVERING A 62×76 PICTURE WITH 16×16 BLOCKS Not all threads in a Block will follow the same control flow path