Understanding indexing and how many thread there are in a block

Viewed 103

I'm studying cuda programming and I've found that there are more than one way to indexing a grid.

What I don't understan is how those indexing tecnique are different between each other.

Those are my indexing:

1D grid of 1D blocks

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

1D grid of 2D blocks

th=blockIdx.x * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x;

1D grid of 3D blocks

th =blockIdx.x * blockDim.x * blockDim.y * blockDim.z + threadIdx.z * blockDim.y * blockDim.x + threadIdx.y * blockDim.x + threadIdx.x;

What is the advantage of using the 2nd type of indexing respect to the first one?

I've also problem in reading this information: "the maximum number of thread per block is 1024 and the max dimension size of thread block (x,y,z) is (1024,1024,64)" what does it mean that the blockdim.z is equal to 64? there are only 64 threads or 1024*64 ? what if I use all the direction? does the number of thread that I can use in a grid increase?

1 Answers

The indexing reflect how you want to logically partition your data among your threads. If you are dealing with a 1D problem (imagine calculating the sum of two vectors), then you agree with me that it is much more easy to use a 1D decomposition so you can easily map one thread to a pair of elements from the two input arrays.

If you are working on a 2D and 3D structure like a matrix, the same argument applies.

Regarding the second question about the maximum number of threads. You can calculate the number of threads in a block with dimension (x,y,z) as x*y*z. The max number of allowed thread per block is 1024. This means that you are allowed to use all the values you want for x,y,z provided that their product does not get higher than 1024 and that x,y<=1024 and z<=64.

Related