开发者

How to write CUDA global function for this?

I want to convert the following function into CUDA.

void fun()
{
    for(i = 0; i < terrainGridLength; i++)
    {
       for(j = 0; j < terrainGridWidth; j++) 
       {
             //CODE of f开发者_JS百科unction
       }
    }
}

I wrote the function like this :

__global__ void fun()
{
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    int j = blockIdx.y * blockDim.y + threadIdx.y;

    if((i < terrainGridLength)&&(j<terrainGridWidth))
    {
           //CODE of function
    }
}

I declared both terrainGridLength and terrainGridWidth as constants and assigned value 120 to both. And I am calling function like

fun<<<30,500>>>()

But i am not getting correct output.

Is the code which i wrote is correct?.I didn't understood much about the parellel execution of the code.Please explain me how the code will work and correct me if i done any mistakes.


You use y dimension which means you are using 2D array threads, so you cannot invoke the kernel with only:

int numBlock = 30;
int numThreadsPerBlock = 500;
fun<<<numBlock,numThreadsPerBlock>>>()

The invocation should be: (Note that now Blocks have 2D Threads)

dim3 dimGrid(GRID_SIZE, GRID_SIZE); // 2D Grids with size = GRID_SIZE*GRID_SIZE
dim3 dimBlocks(BLOCK_SIZE, BLOCK_SIZE); //2D Blocks with size = BLOCK_SIZE*BLOCK_SIZE  
fun<<<dimGrid, dimBlocks>>>()

Refer to CUDA Programming Guide for further info, and also if you want to do 2D array or 3D, you better use cudaMalloc3D or cudaMallocPitch

As of your code, I think this would work (but I haven't tried though, hope you can grab the idea with this):

//main
dim3 dimGrid(1, 1); // 2D Grids with size = 1
dim3 dimBlocks(Width, Height); //2D Blocks with size = Height*Width 
fun<<<dimGrid, dimBlocks>>>(Width, Height)

//kernel
__global__ void fun(int Width, int Height)
{
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    int j = blockIdx.y * blockDim.y + threadIdx.y;

    if((i < Width)&&(j<Height))
    {
           //CODE of function
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜