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.

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:

Load Example

ld.global.u32 %r1, [%rd1];

  • Load 32-bit unsigned integer from global memory address in %rd1 into 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:

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 and , the output is . We know that .

We can store the elements in row major () or column major () order. We assume row-major.

4.1 Naive Implementation

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 axis then the axis - this is called row-major thread mapping. This is not optimal for memory access, as threads in a warp will access non-contiguous memory locations.

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.

Back to Home