The quiz 1 of Intro to Parallel Program

来源:互联网 发布:手机删除软件残留 编辑:程序博客网 时间:2024/05/16 17:50

Homework 1

Color to Greyscale Conversion

 

A common way to represent color images isknown as RGBA - the color is specified by how much Red, Green, and Blue is init. The 'A' stands for Alpha and is used for transparency; it will be ignoredin this homework.

 

Each channel Red, Blue, Green, and Alpha isrepresented by one byte. Since we are using one byte for each color there are256 different possible values for each color. This means we use 4 bytes per pixel.

 

Greyscale images are represented by asingle intensity value per pixel which is one byte in size.

 

To convert an image from color to grayscaleone simple method is to set the intensity to the average of the RGBchannels.  But we will use a moresophisticated method that takes into account how the eye perceives color andweights the channels unequally.

 

The eye responds most strongly to greenfollowed by red and then blue. The NTSC (National Television System Committee)recommends the following formula for color to greyscale conversion:

 

I = .299f * R + .587f * G + .114f * B

 

Notice the trailing f's on the numberswhich indicate that they are single precision floating point constants and notdouble precision constants.

 

You should fill in the kernel as well asset the block and grid sizes so that the entire image is processed.

 

At first, we need to figure out thetransform from 2D matrix to 1D vector. For example, x represents the row ofmatrix, y represents the column of matrix. Then (x, y) is x * total row +y = y* total cols + x.

 

Then calculate the new pixels.

 

 

int r = threadIdx.x +blockIdx.x*blockDim.x;

int c = threadIdx.y +blockIdx.y*blockDim.y;

int index = r*numCols + c; (c*numRows+r)

greyImage [index] =rgbaImage[index].x*.299f + rgbaImage[index].y*.587f + rgbaImage[index].z*.114f;

 

About the size of block and grid.

We could defind the blockSideLen at first.

 

int blockSideLen = 32;

const dim3 blockSize(blockSideLen,blockSideLen, 1); //TODO

const dim3 gridSize( numRows/blockSideLen+1, numCols/blockSideLen +1, 1); //TODO

threadIdx: thread within block

threadIdx.x, threadIdx.y, threadIdx.z

BlockDim: the size of block

How many threads are there in this blockalong x,y,z

BlockIdx: Similar to thread.

GridDim: Similar to block

 

rgba_to_greyscale<<<gridSize,blockSize>>>(d_rgbaImage, d_greyImage, numRows, numCols);

cudaDeviceSynchronize();checkCudaErrors(cudaGetLastError());


Thanks to the people who are discussing in forums.


Note:

记录一下threadIdx之类供忘记后回忆:

threadIdx: thread within block: threadIdx.x, threadIdx.y, threadIdx,z

blockDim:the size of block. how many threads are there in one block (along x, y , z). it is actually like threadIdx.

blockIdxL block within grid. blockIdx.x, blockIdx.y, blockIdx.z

gridDim. Size of grid (how many blocks are there in one grid (along x, y , z). it is actually like blockIdx.)

原创粉丝点击