Unique Block Index in 3D Grid

What is the equation for the unique Block Index of a three dimensional grid.

All literature only needs two dimensions, but most of my kernels use a unique block index for accessing data and so they are scalable to the maximum the hardware allows. (theoretically, if video ram would be enough)

Maybe someone already has it?

int bidx = blockIdx.x + blockIdx.y * blockDim.x + blockIdx.z * blockDim.x * blockDim.y

On compute capability 2.0 you have 6 indices you can play with. On less you have 5 indice to play. There are many ways.

For example define the grid(lz,ly,1) and the threads(lx,1,1) and in the kernel you will have




You can also submit like

threads(16,32,1) grid((lx+16)/16,(ly+32)/32,lz)

and in the kernel you will have

ix=blockIdx.x * blockDim.x + threadIdx.x;

iy=blockIdx.y * blockDim.y + threadIdx.y;


I depends a little on you card (because of the max number of threads per block) and the size of the matrix. SO please tell us if you card has compute 2 capability or not and the maximum/ typical size of the matrix you will use,

@MarkusM a unique thread index is dependent on the block dimension so a unique block index must be dependent on the grid dimension

@pasoleatis I was asking for a unique block index of a three dimensional grid!, i dont know what you are doing

Here is the equation for the unique linear index of one block in the three dimensional grid.

const unsigned long long int blockIdx3D = blockIdx.x //1D

	+ blockIdx.y * gridDim.x //2D

	+ gridDim.x * gridDim.y * blockIdx.z; //3D