CUDA Threads and Kernels

Parallel Algorithms

David Mayerich

Scalable Tissue Imaging and Modeling (STIM) Laboratory

Department of Electrical and Computer Engineering

Cullen College of Engineering

University of Houston

David Mayerich

STIM Laboratory, University of Houston

Thread Organization

  • Programmer arranges threads into a grid

  • A grid composed of blocks of threads

  • The processor executes threads in SIMD warps

  • All threads in a block are executed on a single streaming multiprocessor

David Mayerich

STIM Laboratory, University of Houston

block 0

warp

warp

0

1

2

3

4

0

1

2

3

4

block 1

warp

warp

0

1

2

3

4

0

1

2

3

4

block 2

warp

warp

0

1

2

3

4

0

1

2

3

4

grid dimension

block dimension

a warp cannot span multiple blocks!

Launching Threads

  • User specifies grid parameters

  • Example:

    • a grid dimension of 100 blocks

    • a block dimension of 256 threads

    • GPU launches \(100\times 256=25600\) threads

  • All threads execute the same kernel

David Mayerich

STIM Laboratory, University of Houston

__global__ void kernelFunc(...) {
  // kernel code executed on the GPU
}

int main(...) {
  // load data and initialize
  const int blocks = 100;
  const int threads = 256;
  kernelFunc <<<blocks, threads>>>(...);
}
  • User specifies grid parameters

Defining Kernel Functions

David Mayerich

STIM Laboratory, University of Houston

// define a kernel
__global__ void kernelAbs(float* in, float* out, int n) {
  int i = blockIdx.x * blockDim.x + threadIdx.x;
  
  if(i > n) return;
  
  if(in[i] < 0) out[i] = in[i];
  else out[i] = in[i];
}
  • Parameters assigned per-thread:

gridDim   number of blocks in a grid

blockDim  number of threads in a block

blockIdx  id for the current block

threadIdx id for the current thread

  • Each parameter is a triple (\(x, y, z\))
        gridDim.x  is the number of blocks along x
        threadIdx.y is the \(y\)-axis identifier for the current thread within the current block

Data Parallelism in Kernels

David Mayerich

STIM Laboratory, University of Houston

block 0

warp

warp

0

1

2

3

4

5

6

7

8

9

block 1

warp

warp

0

1

2

3

4

5

6

7

8

9

block 2

warp

warp

0

1

2

3

4

5

6

7

8

9

11

12

13

14

15

16

17

18

19

1A

1B

1C

1D

1E

1F

20

21

22

23

24

25

blockIdx.x = 0
threadIdx.x = 2
gridDim.x = 3
blockDim.x = 10

blockIdx.x = 1
threadIdx.x = 7

__global__ void kernel(...) {
  int i;
  i = blockIdx.x * blockDim.x + threadIdx.x;
  ...
}

what is the value of \(i\)?

\(i=2 \times 10 + 6 = 26\)

Launching Kernel Functions

David Mayerich

STIM Laboratory, University of Houston

int main(int argc, char* argv[]) {
  size_t n;
  // get array size
  size_t bytes = sizeof(float) * n;  
  // allocate and load array data
  float* a = new float[bytes];
  
  // allocate GPU memory for input
  float* gpu_a;
  cudaMalloc(&gpu_a, bytes));							// allocate input on GPU
  cudaMemcpy(gpu_a, a, bytes, cudaMemcpyHostToDevice);	// copy input to the GPU
  
  // calculate output arrays
  float* gpu_b;
  cudaMalloc(&gpu_b, bytes); 							// allocate GPU output array
  float* b = new float[bytes];							// allocate host output array
  kernelAbs<<<n, 1>>>(gpu_b, gpu_a, n);					// launch kernel
  cudaMemcpy(b, gpu_b, bytes, cudaMemcpyDeviceToHost);  // copy data from the GPU
}

Software Architecture

  • Thread and block dimensions are limited

  • Sizes can be queried by the API:

David Mayerich

STIM Laboratory, University of Houston

cudaError_t cudaGetDeviceProperties(cudaDeviceProp* prop, int dev);

struct cudaDeviceProp {
  char name[256];
  size_t totalGlobalMem;
  int maxThreadsPerBlock;
  int maxThreadsDim[3];
  int maxGridSize[3];
  
  int major;
  int minor;
}

Scaling to Large Problems

  • transparent scalability - the ability of an algorithm to operate on data of different sizes without reprogramming and user tuning

  • Maximize the possible number of threads in case the data is large

    • both block and grid dimensions have maximum values

    • launch each block with as many threads as possible

David Mayerich

STIM Laboratory, University of Houston

// define a kernel
__global__ void kernelAbs(float* in, float* out, int n) {
  int i = blockIdx.x * blockDim.x + threadIdx.x;
  
  if(i > n) return;
  
  if(in[i] < 0) out[i] = in[i];
  else out[i] = in[i];
}

Process 101,500 elements

Max block dim: 1024

\frac{101500}{1024}\approx 99.12

Launch 100 blocks:

100 \times 1024 = 102400

Test in kernel

Vector Addition: Configuration Step

Solve \(\mathbf{c} = \mathbf{a} + \mathbf{b}\) where \(\mathbf{a},\mathbf{b}, \mathbf{c} \in \mathbb{R}^N\):

David Mayerich

STIM Laboratory, University of Houston

\begin{bmatrix} c_1\\ c_2\\ \vdots\\ c_N \end{bmatrix} = \begin{bmatrix} a_1\\ a_2\\ \vdots\\ a_N \end{bmatrix} + \begin{bmatrix} b_1\\ b_2\\ \vdots\\ b_N \end{bmatrix}
  • \(N\) is not known at compile time
    (but can be large)
  • Use the maximum number of threads per block
    • allows the largest possible vector
    • maximize compute (warps cannot span blocks)
// get the properties for device 0
cudaDeviceProp p;
cudaGetDeviceProperties(&p, 0);

// find the maximum number of threads in a block
int threads = prop.maxThreadsDim[0]

// calculate the required number of blocks
int blocks = N / threads + 1;
__global__ void kernelAdd(float* c, float* a, 
						  float* b, size_t n) {
  // calculate the index into the array
  size_t i = blockIdx.x * blockDim.x + threadIdx.x;
  
  // check for array over-shooting
  if(i >= n) return;
  
  // perform addition for a single element
  c[i] = a[i] + b[i];
}

Multidimensional Grids

  • 1D, 2D, and 3D grids are supported

  • Specify grid peroperties using the dim3 structure: dim3(x, y, z)

  • Common layouts for \(M\) blocks composed of \(N\) threads:

David Mayerich

STIM Laboratory, University of Houston

0

1

2

..

N-2

N-1

\(b_1\)

\(b_2\)

\(\vdots\)

\(b_{N-2}\)

\(b_{N-1}\)

0

1

2

..

N-2

N-1

0

N-1

0

...

0

0

...

0

...
...

...
N-1

N-10

N-1...

N-1N-1

\(b_1\)

\(b_2\)

\(\cdots\)

\(b_{N-2}\)

\(b_{N-1}\)

\(b_{1,0}\)

\(b_{0,1}\)

\(b_{1,1}\)

Matrix Multiplication: Configuration

David Mayerich

STIM Laboratory, University of Houston

\text{Calculate } \mathbf{C}=\mathbf{A}\mathbf{B} \text{ where } \mathbf{A}\in\mathbb{C}^{M\times N}, \mathbf{B}\in\mathbb{C}^{N\times K}, \mathbf{C}\in\mathbb{C}^{M\times K}
C_{ij}=\sum_{n=1}^{N} A_{in} B_{nj}
\begin{bmatrix} C_{11} & C_{12} & C_{13} & C_{14} & \cdots & C_{1K}\\ C_{21} & C_{22} & C_{23} & C_{24} & \cdots & C_{2K}\\ C_{31} & C_{32} & C_{33} & C_{34} & \cdots & C_{3K}\\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots\\ C_{M1} & C_{M2} & C_{M3} & C_{M4} & \cdots & C_{MK}\\ \end{bmatrix}
\begin{bmatrix} A_{11} & A_{12} & A_{13} & \cdots & A_{1N}\\ A_{21} & A_{22} & A_{23} & \cdots & A_{2N}\\ A_{31} & A_{32} & A_{33} & \cdots & A_{3N}\\ \vdots & \vdots & \vdots & \ddots & \vdots\\ A_{M1} & A_{M2} & A_{M3} & \cdots & A_{MN}\\ \end{bmatrix}
\begin{bmatrix} B_{11} & B_{12} & B_{13} & B_{14} & \cdots & B_{1K}\\ B_{21} & B_{22} & B_{23} & B_{24} & \cdots & B_{2K}\\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots\\ B_{M1} & B_{M2} & B_{M3} & B_{M4} & \cdots & B_{MK}\\ \end{bmatrix}
  • What should our grid cover?
  • What work will each thread do?

Matrix Multiplication: Configuration

David Mayerich

STIM Laboratory, University of Houston

\begin{bmatrix} C_{11} & C_{12} & C_{13} & C_{14} & \cdots & C_{1K}\\ C_{21} & C_{22} & C_{23} & C_{24} & \cdots & C_{2K}\\ C_{31} & C_{32} & C_{33} & C_{34} & \cdots & C_{3K}\\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots\\ C_{M1} & C_{M2} & C_{M3} & C_{M4} & \cdots & C_{MK}\\ \end{bmatrix}
\begin{bmatrix} A_{11} & A_{12} & A_{13} & \cdots & A_{1N}\\ A_{21} & A_{22} & A_{23} & \cdots & A_{2N}\\ A_{31} & A_{32} & A_{33} & \cdots & A_{3N}\\ \vdots & \vdots & \vdots & \ddots & \vdots\\ A_{M1} & A_{M2} & A_{M3} & \cdots & A_{MN}\\ \end{bmatrix}
\begin{bmatrix} B_{11} & B_{12} & B_{13} & B_{14} & \cdots & B_{1K}\\ B_{21} & B_{22} & B_{23} & B_{24} & \cdots & B_{2K}\\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots\\ B_{M1} & B_{M2} & B_{M3} & B_{M4} & \cdots & B_{MK}\\ \end{bmatrix}
int m, n, k;					// set matrix sizes
std::complex<float> *a, *b, *c;	// load matrices

// configure grid?

// launch the kernel
matmul<<<grid, block>>>(a, b, c, m, n, k);

Matrix Multiplication: Configuration

David Mayerich

STIM Laboratory, University of Houston

\begin{bmatrix} C_{11} & C_{12} & C_{13} & C_{14} & \cdots & C_{1K}\\ C_{21} & C_{22} & C_{23} & C_{24} & \cdots & C_{2K}\\ C_{31} & C_{32} & C_{33} & C_{34} & \cdots & C_{3K}\\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots\\ C_{M1} & C_{M2} & C_{M3} & C_{M4} & \cdots & C_{MK}\\ \end{bmatrix}
\begin{bmatrix} A_{11} & A_{12} & A_{13} & \cdots & A_{1N}\\ A_{21} & A_{22} & A_{23} & \cdots & A_{2N}\\ A_{31} & A_{32} & A_{33} & \cdots & A_{3N}\\ \vdots & \vdots & \vdots & \ddots & \vdots\\ A_{M1} & A_{M2} & A_{M3} & \cdots & A_{MN}\\ \end{bmatrix}
\begin{bmatrix} B_{11} & B_{12} & B_{13} & B_{14} & \cdots & B_{1K}\\ B_{21} & B_{22} & B_{23} & B_{24} & \cdots & B_{2K}\\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots\\ B_{M1} & B_{M2} & B_{M3} & B_{M4} & \cdots & B_{MK}\\ \end{bmatrix}
int m, n, k;					// set matrix sizes
std::complex<float> *a, *b, *c;	// load matrices, copy to GPU

// decide on a block size
dim3 block(bm, bk);
// assign blocks in the grid
dim3 grid(m / bm + 1, k / bk + 1);

// launch the kernel
matmul<<<grid, block>>>(a, b, c, m, n, k);

Matrix Multiplication: Configuration

David Mayerich

STIM Laboratory, University of Houston

\begin{bmatrix} C_{11} & C_{12} & C_{13} & C_{14} & \cdots & C_{1K}\\ C_{21} & C_{22} & C_{23} & C_{24} & \cdots & C_{2K}\\ C_{31} & C_{32} & C_{33} & C_{34} & \cdots & C_{3K}\\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots\\ C_{M1} & C_{M2} & C_{M3} & C_{M4} & \cdots & C_{MK}\\ \end{bmatrix}
int m, n, k;					// set matrix sizes
std::complex<float> *a, *b, *c;	// load matrices

// get hardware properties
cudaDeviceProp props;
cudaGetDeviceProperties(&props, 0);

// decide on a block size
dim3 block(bm, bk);
// assign blocks in the grid
dim3 grid(bm / m + 1, bk / k + 1);

// launch the kernel
matmul<<<grid, block>>>(a, b, c, m, n, k);

Matrix Multiplication: Configuration

David Mayerich

STIM Laboratory, University of Houston

\begin{bmatrix} C_{11} & C_{12} & C_{13} & C_{14} & \cdots & C_{1K}\\ C_{21} & C_{22} & C_{23} & C_{24} & \cdots & C_{2K}\\ C_{31} & C_{32} & C_{33} & C_{34} & \cdots & C_{3K}\\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots\\ C_{M1} & C_{M2} & C_{M3} & C_{M4} & \cdots & C_{MK}\\ \end{bmatrix}
int m, n, k;					// set matrix sizes
std::complex<float> *a, *b, *c;	// load matrices

// get hardware properties
cudaDeviceProp props;
cudaGetDeviceProperties(&props, 0);

// row blocks:
int bm = 1;
int bk = props.maxThreadsPerBlock;

// decide on a block size
dim3 block(bm, bk);
// assign blocks in the grid
dim3 grid(bm / m + 1, bk / k + 1);

// launch the kernel
matmul<<<grid, block>>>(a, b, c, m, n, k);

Matrix Multiplication: Configuration

David Mayerich

STIM Laboratory, University of Houston

\begin{bmatrix} C_{11} & C_{12} & C_{13} & C_{14} & \cdots & C_{1K}\\ C_{21} & C_{22} & C_{23} & C_{24} & \cdots & C_{2K}\\ C_{31} & C_{32} & C_{33} & C_{34} & \cdots & C_{3K}\\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots\\ C_{M1} & C_{M2} & C_{M3} & C_{M4} & \cdots & C_{MK}\\ \end{bmatrix}
int m, n, k;					// set matrix sizes
std::complex<float> *a, *b, *c;	// load matrices

// get hardware properties
cudaDeviceProp props;
cudaGetDeviceProperties(&props, 0);

// column blocks:
int bm = props.maxThreadsPerBlock;
int bk = 1;

// decide on a block size
dim3 block(bm, bk);
// assign blocks in the grid
dim3 grid(bm / m + 1, bk / k + 1);

// launch the kernel
matmul<<<grid, block>>>(a, b, c, m, n, k);

Matrix Multiplication: Configuration

David Mayerich

STIM Laboratory, University of Houston

\begin{bmatrix} C_{11} & C_{12} & C_{13} & C_{14} & \cdots & C_{1K}\\ C_{21} & C_{22} & C_{23} & C_{24} & \cdots & C_{2K}\\ C_{31} & C_{32} & C_{33} & C_{34} & \cdots & C_{3K}\\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots\\ C_{M1} & C_{M2} & C_{M3} & C_{M4} & \cdots & C_{MK}\\ \end{bmatrix}
int m, n, k;					// set matrix sizes
std::complex<float> *a, *b, *c;	// load matrices

// get hardware properties
cudaDeviceProp props;
cudaGetDeviceProperties(&props, 0);

// square blocks:
int bm = std::sqrt(props.maxThreadsPerBlock);
int bk = std::sqrt(props.maxThreadsPerBlock);

// decide on a block size
dim3 block(bm, bk);
// assign blocks in the grid
dim3 grid(bm / m + 1, bk / k + 1);

// launch the kernel
matmul<<<grid, block>>>(a, b, c, m, n, k);