liveBook · Manning

Vector addition

As explained in Grokking GPU Programming

Vector addition combines two vectors by adding their corresponding components.

Vector addition adds matching elements from two vectors to produce a third: if A = [1, 2, 3] and B = [4, 5, 6], then C = [5, 7, 9]. section 4.4

As a parallel map, each thread handles one index: it reads A[i] and B[i], then writes their sum to C[i]. Threads work independently because no output depends on another index. section 4.6 section 4.0

C[idx] = A[idx] + B[idx];

The kernel’s essential operation adds the two inputs at the thread’s index and stores the result at that same index. section 4.6

A CUDA kernel first finds its global index and checks that it is within the vector bounds; then it performs the addition. Launch enough blocks to cover every element. section 4.6

How Grokking GPU Programming covers it

How deep it goes
Works through the element-wise operation, per-thread indexing, bounds checks, and CUDA launch sizing.
Examples
Shows adding [1, 2, 3] and [4, 5, 6], then assigns each indexed addition to one thread.
What it assumes
Assumes familiarity with CUDA global indexing and bounds checks, while reusing the established kernel launch pattern.
What you can do after
Builds a CUDA vector-add kernel by assigning each output index to a thread that adds matching inputs.
How it connects
Connects vector addition to the map pattern, global indexing, bounds checking, and CUDA kernels.

Also explained in

Compare how 2 books explain vector addition →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking GPU Programming. The section links point to the parts of the book it draws on.