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.xis the number of blocks along x
threadIdx.yis 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;
}CUDA samples:
https://github.com/NVIDIA/cuda-samples
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
Launch 100 blocks:
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
- \(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
dim3structure: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
- What should our grid cover?
- What work will each thread do?
Matrix Multiplication: Configuration
David Mayerich
STIM Laboratory, University of Houston
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
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
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
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
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
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);E.4 CUDA Programming
By STIM Laboratory
E.4 CUDA Programming
- 225