2
votes

I'm new to cuda; I have a 2D image (width, height) with 3 channels (colors). What I want is to lunch a kernel that have 3D block and 2D grid like this

kernel_2D_3D<<<dim3(1,m,n), dim3(3,TILEy,TILEz)>>>(float *in, float *out)

I use x for colors, y for width and z for height. My question is: How can I calculate the row and column of the image:

  1. unsigned int Row = ?
  2. unsigned int Col = ?

and the I use this function to calculate global unique index

__device__ int getGlobalIdx_2D_3D()
{
    int blockId = blockIdx.x+ blockIdx.y * gridDim.x; 

    int Idx = blockId * (blockDim.x * blockDim.y * blockDim.z)
                 + (threadIdx.z * (blockDim.x * blockDim.y))
                 + (threadIdx.y * blockDim.x)
                 + threadIdx.x;

    return Idx;
}
1

1 Answers

1
votes

If you are using y for width and z for height, then the row and column of the image will be calculated like this inside the kernel:

unsigned int row = blockIdx.z * blockDim.z + threadIdx.z;
unsigned int col = blockIdx.y * blockDim.y + threadIdx.y;

The current channel would be equal to threadIdx.x.