﻿# CUB DeviceScan permits exact in-place buffers, not arbitrary overlap

Distinguish exact in-place CUB scans from shifted overlapping ranges, and keep temporary-storage queries separate from actual prefix-scan execution.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-cub-device-scan-exact-inplace-overlap/
Language: en

## An offset buffer changes the alias relationship

A compaction pipeline may compute prefix offsets with `cub::DeviceScan` and try to save an allocation by moving output into the input buffer. There is a crucial distinction between exactly the same range and a shifted range that overlaps only part of the input.

The [current DeviceScan reference](https://nvidia.github.io/cccl/unstable/cub/api/structcub_1_1DeviceScan.html) explicitly permits equal input and output iterators for the documented in-place scans, while excluding other overlap. The linked development documentation should be matched to the installed CCCL headers.

## Exact identity is a specific algorithm contract

An implementation that supports in-place scanning can coordinate reads and writes for that relationship. It does not follow that writing each result into a neighboring input position is also supported. A shifted destination can overwrite data still needed elsewhere in the computation.

Draw the complete input and output ranges, including element size and offset. Pointer inequality alone does not prove separation, and membership in the same allocation does not prove supported in-place use. Treat range analysis as part of the adapter contract.

## Querying scratch does not validate values

The [device-wide API guide](https://nvidia.github.io/cccl/unstable/cub/device_wide.html) explains the traditional query/allocation/execution pattern. A null temporary-storage query determines requirements without performing the scan. Successful querying therefore cannot establish that the actual input values or application outputs are correct.

Keep the query and execution configuration consistent, including the current CUDA device. A scratch buffer and an output buffer have different ownership roles even if the application allocator supplies both.

## Preserve prefix semantics while changing storage

The official [exclusive-sum example](https://raw.githubusercontent.com/NVIDIA/cccl/main/cub/examples/device/example_device_scan.cu) separates a host reference from device execution. That comparison pattern is useful when introducing an in-place variant.

Check whether the application requires an inclusive or exclusive prefix before comparing buffers. The [tensor-axis guide](/journal/tensor-axes-batch-sequence-hidden/) offers a broader reminder that matching shapes do not establish matching meanings. An offset array can have the expected length while encoding the wrong prefix convention.

## Regression tests should target the alias boundary

Compare a disjoint-output reference with the documented exact in-place case across empty, short, and irregular-length inputs supported by the selected interface. Reject unsupported shifted overlap at the application boundary rather than relying on an accidental successful run.

Record the actual headers and overload in the [GPU evidence record](/journal/gpu-acceptance-evidence/). Newer execution-environment interfaces and traditional explicit-scratch interfaces should not be mixed through assumptions copied from a different release.
