← Questions

Deterministic-order parallel reduction in CUDA

0
Applied IntuitionCodingLast asked Apr 2026

A coding interview question asked in Applied Intuition's technical phone screen, last asked Apr 2026. It tests cuda, gpu and parallel algorithms.

Floating-point addition is not associative, so a GPU reduction that combines values in whatever order threads finish (atomics, or block results added as they arrive) can return slightly different sums for the same input from run to run. Design and write a CUDA reduction (sum of a large float array) with a fixed, well-defined combination order, so the same input on the same configuration always gives a bit-identical result. The idea is close to a parallel prefix sum.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • float deterministic_sum(const float *d_in, size_t n); — d_in is in device memory; returns the sum.
  • Block size and grid size are fixed constants of your implementation, so the reduction tree depends only on n.

Example

in = [1e8, 1.0, -1e8, 1.0]   (float32)
left-to-right:  ((1e8 + 1) - 1e8) + 1 = 1.0
pairwise:       (1e8 + 1) + (-1e8 + 1) = 0.0

Both are valid sums of the same numbers; a deterministic reduction always returns the one its fixed tree defines.

Constraints

  • No atomicAdd on floats in the combining path.
  • n can be far larger than one block, and need not be a power of two.

Clarifying questions

  • Must the result match across different GPUs or block sizes, or only across runs on the same configuration?
  • Is matching a sequential CPU sum required, or only run-to-run reproducibility?
  • How large is n, and is the reduction called once or many times?
  • Is extra memory for per-block partials acceptable?

What a strong answer covers

  • Inside a block: each thread sums a fixed, strided slice of the input in order, then a shared-memory (or warp-shuffle) tree with a fixed pairing.
  • Across blocks: each block writes its partial to partials[blockIdx.x], then a second pass (or a last-block-done pattern that still reduces partials in index order) combines them in a fixed tree.
  • How this mirrors the up-sweep of a scan: the same balanced tree, so order is defined by index, not by timing.
  • What is and isn't guaranteed: bit reproducibility for a fixed configuration, not equality with a sequential sum, and not across different grid sizes unless the tree is fixed by n. Options like Kahan or higher-precision accumulators for accuracy.
  • Cost: an extra kernel or pass and O(blocks) memory, versus the atomic version; both are bandwidth bound at O(n).

Follow-ups

  • Make the result identical even when the grid size changes.
  • Extend it to an inclusive prefix sum across blocks.
  • Reduce many small arrays (a segmented reduction) deterministically.

Where it's asked

What it tests

cuda · gpu · parallel algorithms · prefix sum

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding