9
1 ITCS 4/5010 CUDA Programming, UNC-Charlotte, B. Wilkinson, Jan 28, 2013, 3-DBlocks.ppt Addressing 2-D grids with 3-D blocks Class Discussion Notes

1 ITCS 4/5010 CUDA Programming, UNC-Charlotte, B. Wilkinson, Jan 28, 2013, 3-DBlocks.ppt Addressing 2-D grids with 3-D blocks Class Discussion Notes

Embed Size (px)

Citation preview

Page 1: 1 ITCS 4/5010 CUDA Programming, UNC-Charlotte, B. Wilkinson, Jan 28, 2013, 3-DBlocks.ppt Addressing 2-D grids with 3-D blocks Class Discussion Notes

1ITCS 4/5010 CUDA Programming, UNC-Charlotte, B. Wilkinson, Jan 28, 2013, 3-DBlocks.ppt

Addressing 2-D grids with 3-D blocks

Class Discussion Notes

Page 2: 1 ITCS 4/5010 CUDA Programming, UNC-Charlotte, B. Wilkinson, Jan 28, 2013, 3-DBlocks.ppt Addressing 2-D grids with 3-D blocks Class Discussion Notes

2

Given two-dimensional addressing, row, column

Use the general 2-D to 1-D flattening equation:

index = col + row * Ncol

where Ncol is the total number of columns in a row to get a unique ID.

Equation can be used multiple times in 2D/3D grids/blocks to get unique thread ID.

General Approach

Page 3: 1 ITCS 4/5010 CUDA Programming, UNC-Charlotte, B. Wilkinson, Jan 28, 2013, 3-DBlocks.ppt Addressing 2-D grids with 3-D blocks Class Discussion Notes

3Thread

blockIdx.x

blockIdx.y

Block

Grid

threadID.x

threadID.y

x

y

We have already considered 2-D grids and 2-D blocks

Page 4: 1 ITCS 4/5010 CUDA Programming, UNC-Charlotte, B. Wilkinson, Jan 28, 2013, 3-DBlocks.ppt Addressing 2-D grids with 3-D blocks Class Discussion Notes

4

Applicable when mapping 2-D data array onto grid.Determine number of threads to the chosen thread, row and column:

col = blockIdx.x*blockDim.x+threadIdx.xrow = blockIdx.y*blockDim.y+threadIdx.y

Then use:

ThreadID = col + row * N

where N = no of columns of threads in grid. N = blockDim.x * gridDim.x

Substituting we would get:

ThreadID = (blockIdx.x*blockDim.x+threadIdx.x) + (blockIdx.y*blockDim.y+threadIdx.y)* (blockDim.x * gridDim.x)= blockIdx.x*blockDim.x+threadIdx.x+ blockIdx.y*blockDim.y* blockDim.x * gridDim.x + threadIdx.y*blockDim.x *gridDim.x

Global thread ID – one approach

Page 5: 1 ITCS 4/5010 CUDA Programming, UNC-Charlotte, B. Wilkinson, Jan 28, 2013, 3-DBlocks.ppt Addressing 2-D grids with 3-D blocks Class Discussion Notes

5

Using the general 2-D to 1-D flattening equation:index = column + row * Ncolumn

Block ID within grid:

blockID = blockIdx.x + blockIdx.y * gridDim.x

Thread ID within block:

BlockthreadID = threadIdx.x + threadIdx.y * blockDim.x

Then substitute BlockthreadID and blockID into flattening equation again to get threadID

Global thread ID - Another approach

Page 6: 1 ITCS 4/5010 CUDA Programming, UNC-Charlotte, B. Wilkinson, Jan 28, 2013, 3-DBlocks.ppt Addressing 2-D grids with 3-D blocks Class Discussion Notes

6

2-D Grids and 3-D blocks

threadID.z

Thread

blockIdx.y

Block

Grid

threadID.y

threadID.x

blockIdx.x

Page 7: 1 ITCS 4/5010 CUDA Programming, UNC-Charlotte, B. Wilkinson, Jan 28, 2013, 3-DBlocks.ppt Addressing 2-D grids with 3-D blocks Class Discussion Notes

7

We have global thread ID not considering z direction.

Call it threadIDxy

Using the general 2-D to 1-D flattening equation:

index = col + row * Ncol

threadID = threadID.z + threadIDxy * blockDim.z

3D Global thread ID One approach

Page 8: 1 ITCS 4/5010 CUDA Programming, UNC-Charlotte, B. Wilkinson, Jan 28, 2013, 3-DBlocks.ppt Addressing 2-D grids with 3-D blocks Class Discussion Notes

8

2-D Address, (x, y) and block sizes Dx and Dy

Unique global thread ID = x + y Dx

3-D Address, (x, y, z) and block sizes Dx, Dy, and Dz.

Unique global thread ID = x + y Dx + zDxDy

3D Global thread ID Another approach

Page 9: 1 ITCS 4/5010 CUDA Programming, UNC-Charlotte, B. Wilkinson, Jan 28, 2013, 3-DBlocks.ppt Addressing 2-D grids with 3-D blocks Class Discussion Notes

Questions