﻿# rocPRIM scan: a wider output pointer does not guarantee wider accumulation

Choose rocPRIM scan operator and accumulator types explicitly, distinguishing stored input, intermediate arithmetic and final output conversion when testing overflow and precision.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-rocprim-scan-accumulator-output-type-distinction/
Language: en

## Separate stored input from intermediate arithmetic

A prefix calculation can accept narrow inputs and write a wide output array. That does not by itself prove that every intermediate sum used the wider type. Establish the operator's argument and return types and the accumulator selected by the routine. For an offset-building pipeline, an overflowed intermediate prefix cannot be repaired merely by storing the corrupted value in a larger destination. Test the actual arithmetic contract before relying on larger output allocations.

## Inspect the selected scan signature

The [rocPRIM scan reference](https://rocm.docs.amd.com/projects/rocPRIM/en/latest/device_ops/scan.html) exposes accumulator type parameters and describes their relationship with input and initial-value types. Read the exact overload and release rather than assuming that the output iterator drives accumulation. Inclusive and exclusive paths can derive types from different arguments. Make deliberate type choices in the implementation when the supported range or numerical precision is essential to the application.

## Include the operator in the type review

The [rocPRIM functional source](https://raw.githubusercontent.com/ROCm/rocm-libraries/develop/projects/rocprim/rocprim/include/rocprim/functional.hpp) defines typed arithmetic functors. A custom operator that narrows its arguments or return value can reintroduce loss even when the surrounding accumulator is wide. Review conversions inside the function, not only the template parameter at the call site. An implicit cast in a seemingly harmless helper may determine the effective range of every prefix result.

## Verify the execution path on a boundary case

The [scan implementation](https://raw.githubusercontent.com/ROCm/rocm-libraries/develop/projects/rocprim/rocprim/include/rocprim/device/device_scan.hpp) makes accumulation and output storage distinct stages of the primitive. Use a small input whose exact prefix exceeds the narrow representation while remaining valid in the intended accumulator. Compare every prefix with an independent reference. Avoid invoking signed-overflow behavior as a correctness strategy; choose representable intermediates and a declared range for the actual data population.

## Keep numerical stability separate from reproducibility

A wider accumulator can improve representable range or precision without making floating-point addition associative. Likewise, a deterministic execution path does not automatically supply a larger arithmetic type. The existing [HIP portability guide](/journal/learn-hip-launch-portability/) and [software reproducibility guide](/journal/software-stack-reproducibility/) support related records. Report input, operator, accumulator and output types together, plus any determinism requirement, so accuracy claims describe the implemented prefix calculation rather than the apparent width of its destination buffer.
