liveBook · Manning

One thread per element

As explained in Grokking GPU Programming

One thread per element is a data-mapping approach that assigns one parallel thread to each element of an input collection.

One thread per element assigns each array position to its own GPU thread. Each thread performs the same operation independently; in a pure map, it reads only same-index inputs, so outputs do not depend on one another. section 4.0

For a one-dimensional array:

  • Compute a unique global index with blockIdx.x * blockDim.x + threadIdx.x. section 3.4
  • Check that the index is within the array’s bounds before accessing an element. section 4.16
__global__ void kernel(int* data) {    #A
    int idx = blockIdx.x * blockDim.x + threadIdx.x;    #B
    data[idx] = data[idx] * 2;    #C
}

This kernel doubles each element in place: each thread computes its index and updates that slot. section 4.3

Vector addition

Thread i reads A[i] and B[i] and writes C[i]. A grading-room analogy makes the independence intuitive: each worker handles one exam without needing another worker’s work. section 4.0

How Grokking GPU Programming covers it

How deep it goes
Traces a five-thread example and gives the global-index formula, while deferring alternative thread-to-data mappings and performance details.
Examples
Uses array doubling, vector addition, and a grading-room analogy to illustrate independent element work.
What you can do after
Build it from scratch by computing a global index, checking bounds, and processing that thread’s element.
How it connects
Connects the mapping to CUDA global indexing, SPMD, and the map pattern.

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.