How are global thread IDs numbered?

I thought, global block IDs with lower decimal values will contain global thread-IDs with lower decimal values.

I have written the following listing to see the order or pattern in which global-thread-IDs are formed.

As it stands, global block-IDs have patterns. However, global thread-IDs don’t have any pattern.

Am I missing something?

#include <iostream>
#define GRID_DIM_X 2
#define GRID_DIM_Y 2
#define GRID_DIM_Z 2

#define BLOCK_DIM_X 2
#define BLOCK_DIM_Y 2
#define BLOCK_DIM_Z 2

#define GLOBAL_BLOCK_ID(bidx, bidy, bidz) ((bidx) + (bidy) * (GRID_DIM_X) + (bidz) * ((GRID_DIM_X) * (GRID_DIM_Y)))

#define G_TID_X(tid_x, bid_x) ((tid_x) + (bid_x) * (BLOCK_DIM_X))
#define G_TID_Y(tid_y, bid_y) ((tid_y) + (bid_y) * (BLOCK_DIM_Y))
#define G_TID_Z(tid_z, bid_z) ((tid_z) + (bid_z) * (BLOCK_DIM_Z))

#define GLOBAL_THREAD_ID(tidx, tidy, tidz, bidx, bidy, bidz) ((G_TID_X(tidx, bidx)) + \
                            ((G_TID_Y(tidy, bidy)) * ((BLOCK_DIM_X) * (GRID_DIM_X))) + \
                            ((G_TID_Z(tidz, bidz)) * ((BLOCK_DIM_X) * (GRID_DIM_X) * (BLOCK_DIM_Y) * (GRID_DIM_Y))))

int main()
{
    printf("z\ty\tx\tbid\tz\ty\tx\ttid\n");
    for(int l_bid_z=0 ; l_bid_z<GRID_DIM_Z ; l_bid_z++)
        for(int l_bid_y=0 ; l_bid_y<GRID_DIM_Y ; l_bid_y++)
            for(int l_bid_x=0 ; l_bid_x<GRID_DIM_X ; l_bid_x++)
                for(int l_tid_z=0 ; l_tid_z<BLOCK_DIM_Z ; l_tid_z++)
                for(int l_tid_y=0 ; l_tid_y<BLOCK_DIM_Y ; l_tid_y++)
                for(int l_tid_x=0 ; l_tid_x<BLOCK_DIM_X ; l_tid_x++)
            {
                int gbid = GLOBAL_BLOCK_ID(l_bid_x, l_bid_y, l_bid_z);
                int gtid = GLOBAL_THREAD_ID(l_tid_x, l_tid_y, l_tid_z, l_bid_x, l_bid_y, l_bid_z);
                printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
                  l_bid_z, l_bid_y, l_bid_x, gbid,
                  l_tid_z, l_tid_y, l_tid_x, gtid);
            }
}

Output

z       y       x       bid     z       y       x       tid
0       0       0       0       0       0       0       0
0       0       0       0       0       0       1       1
0       0       0       0       0       1       0       4
0       0       0       0       0       1       1       5
0       0       0       0       1       0       0       16
0       0       0       0       1       0       1       17
0       0       0       0       1       1       0       20
0       0       0       0       1       1       1       21
0       0       1       1       0       0       0       2
0       0       1       1       0       0       1       3
0       0       1       1       0       1       0       6
0       0       1       1       0       1       1       7
0       0       1       1       1       0       0       18
0       0       1       1       1       0       1       19
0       0       1       1       1       1       0       22
0       0       1       1       1       1       1       23
0       1       0       2       0       0       0       8
0       1       0       2       0       0       1       9
0       1       0       2       0       1       0       12
0       1       0       2       0       1       1       13
0       1       0       2       1       0       0       24
0       1       0       2       1       0       1       25
0       1       0       2       1       1       0       28
0       1       0       2       1       1       1       29
0       1       1       3       0       0       0       10
0       1       1       3       0       0       1       11
0       1       1       3       0       1       0       14
0       1       1       3       0       1       1       15
0       1       1       3       1       0       0       26
0       1       1       3       1       0       1       27
0       1       1       3       1       1       0       30
0       1       1       3       1       1       1       31
1       0       0       4       0       0       0       32
1       0       0       4       0       0       1       33
1       0       0       4       0       1       0       36
1       0       0       4       0       1       1       37
1       0       0       4       1       0       0       48
1       0       0       4       1       0       1       49
1       0       0       4       1       1       0       52
1       0       0       4       1       1       1       53
1       0       1       5       0       0       0       34
1       0       1       5       0       0       1       35
1       0       1       5       0       1       0       38
1       0       1       5       0       1       1       39
1       0       1       5       1       0       0       50
1       0       1       5       1       0       1       51
1       0       1       5       1       1       0       54
1       0       1       5       1       1       1       55
1       1       0       6       0       0       0       40
1       1       0       6       0       0       1       41
1       1       0       6       0       1       0       44
1       1       0       6       0       1       1       45
1       1       0       6       1       0       0       56
1       1       0       6       1       0       1       57
1       1       0       6       1       1       0       60
1       1       0       6       1       1       1       61
1       1       1       7       0       0       0       42
1       1       1       7       0       0       1       43
1       1       1       7       0       1       0       46
1       1       1       7       0       1       1       47
1       1       1       7       1       0       0       58
1       1       1       7       1       0       1       59
1       1       1       7       1       1       0       62
1       1       1       7       1       1       1       63

The pattern is correct. Here is the visualization of the first layer (z=0), with global thread ID’s depicted:

       bx0      bx1
 by0   0   1  | 2  3
       4   5  | 6  7
     -----------------
 by1   8   9  | 10 11
       12  13 | 14 15

Comparing against your table, we see that the block at coordinate (x,y,z) = (0,0,0) includes threads with global ID 0,1,4,5 both in your table and in my diagram. The block at coordinate (x,y,z) = (0, 1, 0) includes threads 2,3,6,7 both in my diagram and in your table.

That is the expected numbering. The global thread ID should increase (linearly) as we move from left to right in my diagram. In order for that to happen across blocks, it’s not rational in your configuration to expect block at (0,0,0) to contain threads with global ID’s of 0,1,2,3

The pattern is this way so that a thread array will map nicely onto a similarly sized multidimensional data set.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.