GPU
1. GPUs
GPUs have strong parallelism with high memory bandwidth designed for data-parallel tasks. GPUs exploit thread-level parallelism.
General Purpose GPUs (GPGPU) are used for general purpose computing, not just graphics. They are used in ML/DL, scientific computing, and more. Use SIMT (Single Instruction, Multiple Threads) model. Each thread executes the same instruction on different data.
GPUs contain specific cores for various tasks (e.g. Tensor Cores for matrix multiplications, RT Cores for ray tracing, FP Cores for floating point operations).
2. GPU Programming Model
Initially, the Host Code (CPU) launches kernels and transfers data to the GPU. The Device Code (GPU) executes the kernels. Then, the results are transferred back to the CPU.
- A thread is the basic execution unit of a kernel.
- A thread block is a group of threads with shared memory and thread synchronization. Up to 1024 threads per block.
- A grid is a group of thread blocks running the same kernel.
When we launch kernel function func<<<gridDim, blockDim>>>(M, N, K, A, B, beta, C), we specify the grid and block dimensions (how many blocks in a grid, how many threads ina block). We can then map the thread to the data using threadIdx, blockIdx, and blockDim to compute the global thread index.
3. GPU Architecture
The core execution unit of a GPU is the Streaming Multiprocessor (SM). This has:
- CUDA Cores: Each core has one Integer, FP32, F16, etc ALUs.
- Load Store Units: load data from and store to memory. They contain an Address Generation Unit (AGU). They also route memory requests to the appropriate memory (e.g. L1, L2, DRAM).
- Special Function Units (SFU): perform special operations like trigonometric functions, logarithms, etc.
- Tensor Cores are specialized for matrix-matrix multiplication.
Load Example
ld.global.u32 %r1, [%rd1];
- Load 32-bit unsigned integer from global memory address in
%rd1into register%r1.- The AGU computes the effective address, sends the request to memory, and waits for the data to be returned.
3.1 Memory Hierarchy
The memory hierarchy of a GPU consists of several levels:
- Registers: fastest, private to each thread. Split into banks, each bank can be accessed simultaneously. Each CUDA core has its own bank in the register file.
- Shared Memory / L1 Cache: SRAM shared among threads in a block, low latency.
- L2 Cache: shared among all SMs, higher latency. Managed by hardware.
- Global Memory (GDDR): accessible by all threads, high latency. Managed by user.
A bank conflict occurs when multiple threads in a block access different addresses in the same memory bank, causing serialization of memory accesses. To avoid, reorder memory layout in register files, and split reads per instruction.
3.2 Warp Scheduling
A warp is a group of threads executed in SIMT. All threads in a warp share the same instruction at any clock cycle. Warp size depends on vendor & GPU generation.
A thread block is divided into warps, and the SM schedules warps for execution. This reduces overhead from managing individual threads, and enables parallelism.
The warp scheduler controls warp execution from within the SM. It selects warps from the queue, issues instructions to compute units and manages control flow.
Warp divergence occurs when threads in a warp take different execution paths, leading to inefficiency as the warp must be executed sequentially.
4. Matrix Multiplication
Take two matrices
We can store the elements in row major (
4.1 Naive Implementation
- Partition
into sub-matrices. Each sub-matrix handled by 1 block (1024 threads). - Assign each thread to compute 1 element of
. - Obtain thread index for
using x=threadIdx.x+blockIdx.x*blockDim.x,y=threadIdx.y+blockIdx.y*blockDim.y. - Now inside the kernel, check that
and are within bounds, then do a dot product with a for-loop. - Then, accumulate the result into
(multiplying by , if needed). So:
1__global__ void gemm_naive(int M, int N, int K, float alpha,const float *A, const float *B, float beta, float *C) { 2 const uint x = threadIdx.x + blockIdx.x * blockDim.x; 3 const uint y = threadIdx.y + blockIdx.y * blockDim.y; 4 if (x < M && y < N) { 5 float tmp = 0.0f; 6 for (int k = 0; k < K; ++k) { 7 tmp += A[x * K + k] * B[k * N + y]; 8 } 9 C[x * N + y] = alpha * tmp + beta * C[x * N + y]; 10 } 11}
Threads increase along the
The solution is memory coalescing: ensuring that threads in a warp access contiguous memory locations. This works by changing the thread mapping to column-major thread mapping. This is done by swapping the x and y indices in the kernel. Now, threads in a warp will access contiguous memory locations, improving memory access efficiency.