Short answer: CUDA vector addition is the first program every GPU course teaches, and this page lets you watch it run. The kernel computes C[i] = A[i] + B[i]. A grid of thread blocks does the work, one thread per element. Each thread finds its element with one formula: i = blockIdx.x * blockDim.x + threadIdx.x. Move the two sliders, N and blockDim.x, and the grid regroups in front of you. Every block has its own colour, every row of a block is one warp of 32 threads. Hover a thread and the formula fills in with real numbers. Switch the if (i < N) guard off and the out-of-bounds writes turn red. Then run the kernel step by step: cudaMalloc, cudaMemcpy, the launch, waves of blocks on the streaming multiprocessors, warps in lockstep, the copy back. A CPU loop races the GPU grid tick by tick, so the speed-up is seen rather than told. The code panel shows the real CUDA C and a Numba version with your values. A third tab takes the next step of every CUDA course, the tiled matrix multiply: tiles of A and B staged in shared memory with the load, synchronize, compute pattern, next to the naive kernel, with the global-memory loads counted for both.
Prefer a full window? Open the playground full screen →
What the CUDA vector addition playground shows
- Grid, blocks, threads. The three-level hierarchy of CUDA. A kernel launch creates a grid. The grid holds blocks. A block holds up to 1024 threads. The grid size follows from N and the block size:
gridDim.x = ceil(N / blockDim.x). The page shows the formula with your numbers. - The index formula. Every thread runs the same code. The only difference is its index.
blockIdx.xis the block number,blockDim.xthe threads per block,threadIdx.xthe thread number inside the block. Hover any cell and read, for example, 2 × 128 + 5 = 261. Type an element number into “Which thread computes C[…]” to find the thread the other way round. - The bounds guard. A grid is always whole blocks, so 1000 elements with 256 threads per block launch 1024 threads. The 24 extra threads have i ≥ N. With the guard they do nothing and show in grey. Without it they write past the end of C and show in red, with the explanation of what breaks.
- Kernel launch, step by step. Phase 0 host arrays, phase 1 cudaMalloc and cudaMemcpy host to device, phase 2 the launch
vecAdd<<<gridDim, blockDim>>>with blocks scheduled onto SMs in waves and warps of 32 threads executing in lockstep, phase 3 cudaMemcpy device to host, phase 4 cudaFree. Play, pause, step, replay and speed, as in the other vDan simulators. - CPU versus GPU. The CPU loop does one element per tick. The GPU does one warp per scheduler per tick on every SM. Two strips fill side by side, a counter shows the ticks, and a chart plots ticks against N for both. Pick a GPU profile: a tiny demo GPU with 4 SMs, a Jetson Nano, a GTX 1080, an RTX 3080, an RTX 4090, an A100 or an H100.
- Warps and occupancy. Threads per block, warps per block, idle lanes of a partial warp (blockDim 100 means 4 warps and 28 idle lanes), resident blocks per SM, occupancy and the number of waves. One sentence per number.
- Tiled matrix multiply. C = A × B for an 8, 16 or 32 square matrix with 2 × 2, 4 × 4 or 8 × 8 tiles. Click a tile of C to pick its block and watch the k-loop: load one element of A and one of B per thread into
__shared__tiles,__syncthreads(), multiply from shared memory, write C. The naive kernel runs the same block from global memory only. Counters show global loads, shared reads and the reuse factor, which equals the tile size; the whole-matrix totals are 2 × N³ naive and 2 × N³ / TILE tiled. Below the counters, NVIDIA’s measured effective bandwidth for the same three kernels on a Tesla V100 (119.9, 144.4 and 195.5 GB/s, Table 2 of the CUDA C++ Best Practices Guide) shows what the saving is worth on real hardware. - Code and quiz. The CUDA C kernel and host code with the slider values substituted and the current line highlighted during the animation, a 2D matrix-add variant with blockIdx.y and threadIdx.y, a Python Numba version with a CuPy note, the tiled and naive matrix-multiply kernels, and six generated self-check questions that can show their answer in the grid.
How to use the CUDA vector addition playground
- Start with the defaults. N = 1000 and blockDim.x = 256 give 4 blocks and 24 idle threads. Hover the last block to see the guard at work. Then set blockDim.x to 100 and look at the dashed idle lanes at the end of every block.
- Run the kernel on the demo GPU first. With N = 1024 and blockDim.x = 32 the 32 blocks need two waves on 4 SMs. Switch to an H100 and the same grid is one wave and one tick. Switch to the Jetson Nano and every block queues on a single SM.
- Read the code while it runs. The code panel on the right highlights the host line of each phase and the three kernel lines during the ticks. Copy it, paste it into a .cu file and compile it with nvcc.
- Keep the main idea in view. GPU performance is two things at once: spreading the work over many threads, and organizing how those threads reach their data. Vector addition shows the first, one thread per element. Matrix multiplication shows the second: neighbouring threads keep reloading the same inputs, so a block stages tiles of A and B in shared memory and reuses them, in the rhythm load, synchronize, compute, synchronize. NVIDIA’s worked example of exactly this kernel is Shared Memory in Matrix Multiplication (C = AB) in the CUDA C++ Best Practices Guide.
- Links. “Copy link” gives a URL such as
?n=1000&bd=256&tab=run&step=6or?tab=gemm&mat=16&t=4&blk=1,2that opens this page with the same settings, the selected thread included (&i=261). Keys: Space plays and pauses, the full stop and the comma step, R replays, the arrow keys move the selected thread in the grid.
Frequently asked questions
What is the CUDA index formula?
int i = blockIdx.x * blockDim.x + threadIdx.x;. Blocks before this one contribute blockIdx.x × blockDim.x threads, and threadIdx.x is the position inside this block. With 128 threads per block, thread 5 of block 2 computes element 2 × 128 + 5 = 261. In two dimensions the same formula is applied once for x and once for y.
Why does the kernel need if (i < N)?
Because a grid consists of whole blocks. For 1000 elements and 256 threads per block, ceil(1000 / 256) = 4 blocks launch 1024 threads. The last 24 have i from 1000 to 1023. Without the guard they read and write past the end of the arrays, which is undefined behaviour: silent corruption or a crash. The guard costs nothing worth measuring. The playground shows the guarded threads in grey and the unguarded writes in red.
How many threads per block should I use?
A multiple of 32, because the hardware executes threads in warps of 32. 128 or 256 is the usual starting point. 100 threads per block means 4 warps with 28 idle lanes in every block, 22 % of the hardware wasted. The upper limit is 1024 threads per block. The occupancy panel shows how many blocks of your size fit on one SM of the chosen GPU and how many waves the grid needs.
What is a warp and what is a wave?
A warp is a group of 32 threads of one block that execute the same instruction at the same time. A block of 256 threads is 8 warps. A wave is the set of blocks that are resident on all SMs at once. An SM holds a limited number of blocks and warps, so a grid larger than SMs × resident blocks runs in several waves, which the animation shows one after the other.
Why does a tiled matrix multiply use shared memory?
Because the naive kernel reads every element of A and B from global memory again and again: N times each, 2 × N³ loads for an N × N product, and global memory costs hundreds of cycles per access. A tiled kernel lets each block load a TILE × TILE piece of A and of B once into shared memory, on-chip and tens of cycles away, then all TILE threads in a row or column reuse it. The loads drop by the factor TILE. The pattern is load, __syncthreads(), compute, __syncthreads(), repeated for every tile along k, then one write of C.
Is the CPU versus GPU race a benchmark?
No. It is a counting model: the CPU does one element per tick, each SM issues one warp per scheduler per tick, and there is no memory latency, launch overhead or copy time. It shows why the GPU count barely grows with N while the CPU count grows linearly. A real vector addition is limited by memory bandwidth, and the two cudaMemcpy calls cost more than the kernel. That is why real programs keep data on the GPU and chain many kernels before copying back.
Related vDan tools for AI and GPU computing
More tools from the AI corner of vDan, and one more animated simulator.
Sources and further reading
- NVIDIA CUDA C++ Programming Guide for the terms grid, block, thread, warp, streaming multiprocessor, cudaMalloc, cudaMemcpy and the execution configuration syntax; NVIDIA Training for the Deep Learning Institute courses on accelerated computing with CUDA C/C++ and CUDA Python
- Inspiration, nothing copied: the CUDA Visualizer and the CUDA Thread Visualizer (both MIT) and the tiled GEMM simulator by Joel Fernandes; every scene on this page is drawn by the page itself
- NVIDIA CUDA C++ Best Practices Guide, Shared Memory in Matrix Multiplication (C = AB) for the tiled kernel and its measured bandwidth; An Even Easier Introduction to CUDA and Using Shared Memory in CUDA C/C++ on the NVIDIA Technical Blog; the kitchen picture in the captions (pantry, workbench, hands) follows Zachary Huang’s CUDA video
- On vDan: Kubernetes Deployment Strategies (the same play, step and replay simulator pattern), all tools