Deterministic-order parallel reduction in CUDA
I was asked this
No reports yet. Sign in to say where you were asked it.
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_inis 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
atomicAddon floats in the combining path. -
ncan 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 atO(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
- Applied Intuition · Technical phone screen · Last asked Apr 202645 min · Live coding in CoderPad; the language can be fixed by the role (Python, C++, or C for embedded)
What it tests
cuda · gpu · parallel algorithms · prefix sum
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
