Overview

4 The Map pattern

The chapter introduces the map pattern as one of the most important ideas in parallel programming: apply the same operation independently to many elements, with each thread handling one output element. This is the same pattern you have already seen in earlier kernels such as doubling and squaring, and it works because outputs do not depend on one another. The key mental model is “one thread, one element,” where each thread reads only the inputs for its own index and writes its own result, making the work embarrassingly parallel.

To help decide whether a problem fits map, the chapter offers a checklist: identify the inputs, identify the outputs, confirm that each output depends only on same-index inputs, and verify that no thread needs results from any other thread. Vector addition is the cleanest example, since each thread computes C[i] from A[i] and B[i]; scalar multiplication also fits, except one of the inputs is a shared read-only scalar passed by value instead of an array. The chapter also shows that element-wise activations like ReLU are maps too, because each output depends only on its own input value.

The chapter then explains how to implement map kernels in CUDA using the global thread index, a bounds check, and one simple per-thread operation. It emphasizes good habits such as writing to a separate output array, avoiding cross-thread reads, and treating read-only scalars as ordinary kernel parameters rather than GPU-allocated memory. Common mistakes include reading a neighbor that another thread may be updating, which creates race conditions, and overwriting an input array when the original data is still needed. By the end, the reader should be able to recognize map in many contexts and write correct CUDA kernels for it confidently.

The Map pattern: one thread, one output element, one independent transformation. Each thread reads from array A, applies function f, and writes the result to array B, with no thread depending on any other. The same pattern covers everything from simple vector addition (two inputs, C[i] = A[i] + B[i]) to neural network activations like ReLU. One caution: ReLU’s max(0, A[i]) is an element-wise maximum (a map), whereas finding the single largest value across the array is a reduction (chapter 5), not a map.
The Map pattern: one thread, one output element, one independent transformation. Each thread reads from array A, applies function f, and writes the result to array B, with no thread depending on any other. The same pattern covers everything from simple vector addition (two inputs, C[i] = A[i] + B[i]) to neural network activations like ReLU. One caution: ReLU’s max(0, A[i]) is an element-wise maximum (a map), whereas finding the single largest value across the array is a reduction (chapter 5), not a map.
The Parallel Checklist: four questions that confirm the Map pattern. Before writing a kernel, ask what your input and output are, what each output element needs, and what each thread needs from other threads. For the Map pattern, the answers are always the same: one or more input arrays in, one output array out, each output element reading only its same-index inputs, and no thread ever waiting on another.
The Parallel Checklist: four questions that confirm the Map pattern. Before writing a kernel, ask what your input and output are, what each output element needs, and what each thread needs from other threads. For the Map pattern, the answers are always the same: one or more input arrays in, one output array out, each output element reading only its same-index inputs, and no thread ever waiting on another.
Parallel Checklist for vector addition: all four boxes checked. Each thread reads A[i] and B[i], writes A[i]+B[i] to C[i], and needs nothing from any other thread: no dependency on any other output, no waiting. All four checklist answers confirm it: vector addition is a textbook Map pattern.
Parallel Checklist for vector addition: all four boxes checked. Each thread reads A[i] and B[i], writes A[i]+B[i] to C[i], and needs nothing from any other thread: no dependency on any other output, no waiting. All four checklist answers confirm it: vector addition is a textbook Map pattern.
Parallel Checklist for scalar vector multiplication: another clean Map. Each thread reads A[i] and the shared scalar k, writes k × A[i] to B[i], and needs nothing from any other thread: the scalar is read-only, so every thread can use it simultaneously without conflict.
Parallel Checklist for scalar vector multiplication: another clean Map. Each thread reads A[i] and the shared scalar k, writes k × A[i] to B[i], and needs nothing from any other thread: the scalar is read-only, so every thread can use it simultaneously without conflict.
Parallel Checklist for Vector ReLU. Each thread reads A[i], writes max(0, A[i]) to B[i], and needs nothing from any other thread: negative values get clamped to zero, while zero and positive values pass through unchanged, in parallel.
Parallel Checklist for Vector ReLU. Each thread reads A[i], writes max(0, A[i]) to B[i], and needs nothing from any other thread: negative values get clamped to zero, while zero and positive values pass through unchanged, in parallel.

Summary

  • The map pattern applies the same operation to every element independently. No output depends on any other output: one thread per output element.
  • The independence test: first, no output may depend on another output’s result (if one does, you need a different pattern); second, it’s a pure map only when every read lands at the thread’s own index i. A read at some other index is a gather, a close cousin that is still safe to run in parallel.
  • Parallel Checklist: four questions (inputs, outputs, what each output reads, and what each thread needs from others) identify any map.
  • Thread indexing: compute a unique global index with int idx = blockIdx.x * blockDim.x + threadIdx.x, then always check bounds with if (idx < n) to prevent out-of-bounds memory access.
  • Scalars by value: pass simple values (like n or k) directly to kernels. Anything a kernel reads through a pointer must live in memory the device can reach, which is why arrays go through cudaMalloc and cudaMemcpy.
  • Two mistakes to watch for: reading a neighbor while another thread overwrites it, and destroying an input by writing the result back into it.
  • Map is everywhere, from scaling a vector by a constant to neural network activation functions like ReLU. Once you see the pattern, you’ll recognize it in NumPy, PyTorch, SQL, and beyond.

FAQ

What is the map pattern in GPU programming?

The map pattern applies the same operation independently to many elements. Each thread handles one output element, and no output depends on any other output, so all threads can run in parallel.

How do I know if a problem can be written as a map?

Use the independence test: first ask whether output i depends on any other output. If not, ask whether output i reads only same-index inputs such as A[i] or B[i]. If both answers are yes, it is a pure map.

What does “one thread per element” mean?

It means each GPU thread computes exactly one output element. If you need n outputs, you launch at least n threads, and each thread uses its ID to decide which element it owns.

Why is vector addition considered a map?

Because each output element C[i] is computed independently from the same index of the inputs: C[i] = A[i] + B[i]. No thread needs another thread’s result, so every element can be computed simultaneously.

Can a map kernel use more than one input array?

Yes. A map can read multiple arrays as long as each thread reads only its own index, such as A[i] and B[i]. Vector addition is the classic example.

Can a scalar parameter be passed to a map kernel?

Yes. If the value is shared by every thread, it should usually be passed by value, not as a GPU array. For example, scalar multiplication uses int k because every thread needs the same constant.

Why is ReLU a map operation?

ReLU applies the same element-wise function to each input: B[i] = max(0, A[i]). Each output depends only on its own input value, so the work is independent across threads.

What is the role of the global index in a map kernel?

The global index tells each thread which element it should process. It is typically computed as blockIdx.x * blockDim.x + threadIdx.x, so the kernel works across many blocks and many threads.

What are the two common mistakes in map kernels?

First, reading or writing another thread’s element can create a race condition. Second, overwriting the input array in place can destroy data needed later, even if it is not a race. Using a separate output array is safer.

Why is the map pattern so common across languages and tools?

Because it matches many element-wise operations: NumPy array arithmetic, PyTorch tensor ops, SQL expressions over rows, JavaScript .map(), and CUDA kernels. The structure is the same even if the execution style differs.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking GPU Programming Grokking GPU Programming ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking GPU Programming Grokking GPU Programming ebook for free