chick3n0x07CC
chick3n0x07CC

Reputation: 798

Why do I need block dimension in order to calculate the thread ID in CUDA?

I am new to CUDA and I have read about it and how to calculate the ID of the thread. I have a question about thread ID, as can be seen, for instance, in this image.

What I don´t understand is why thread and block coordinates aren´t enough to calculate the threadID and instead I need to use the block dimensions.

Upvotes: 0

Views: 824

Answers (2)

Hu Go
Hu Go

Reputation: 1

We must really2 understand and can imagine, physical visualization in our head when kernel was launched and threads was created, and about what blockDim, blockIdx, threadIdx really mean....

Block dimension(BlockDim) is "NUMBER (count) of THREADS" in each BLOCK in X or Y or Z dimension (dependent of .X, .Y, or .Z).

BlockIdx.x represent index number which thread is locate in what block. Index number of block is counted from zero, ordered from left to right (x), up down (y), front to back (z).

threadIdx.x is index locally start from zero in each block (blockIdx.x). But, we should know the "Global Index of thread" when it running/life. So it need:

    uint globalIdx_x = blockIdx.X * blockDim.X + threadIdx.x;

Try to read and learn from this book: Professional CUDA C Programming -- Wrox Programming.

Upvotes: 0

ApoorvaJ
ApoorvaJ

Reputation: 830

As seen in the CUDA documentation, multiple threads together form a block, and multiple such blocks form a grid.

Now, when each thread in the grid needs to be uniquely identified, you must factor in the block dimensions.

enter image description here

In the following example, i and j together uniquely identify each thread in the grid.

int i = blockIdx.x * blockDim.x + threadIdx.x;
int j = blockIdx.y * blockDim.y + threadIdx.y;

threadIdx is the index of a thread within a block, and blockIdx is the index of a block within a grid. So you must have blockDim to get the unique indices i, j of a thread within the grid.

Upvotes: 3

Related Questions