﻿# rocPRIM segmented reduction: output count follows segments, not input elements

Define rocPRIM segmented reductions with linked begin/end offsets, one output per segment and an operation-appropriate initial value; distinguish them from segmented scans.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-rocprim-segmented-reduce-offset-output-cardinality/
Language: en

## Define the group represented by each result

A ragged batch stores values for groups with different lengths in one flat array. A segmented reduction must preserve which input interval belongs to each output. For example, aggregating neighbors of graph nodes is different from reducing the entire edge-feature array into a single total. Keep the stable group identity beside each segment definition, especially when upstream filtering changes group sizes while preserving the total number of surviving elements.

## Supply linked beginning and ending boundaries

The [rocPRIM reduction reference](https://rocm.docs.amd.com/projects/rocPRIM/en/latest/device_ops/reduce.html) defines segmented input through begin and end offset iterators. A shared boundary sequence can supply adjacent beginnings and endings, but the terminal boundary must exist too. Validate monotonicity, range and the mapping from segment index to application group. A large enough input allocation cannot repair an incorrect offset that silently assigns part of one group's data to another.

## Allocate for segment results rather than scanned elements

The reduction API writes a result per segment. The [scan reference](https://rocm.docs.amd.com/projects/rocPRIM/en/latest/device_ops/scan.html) describes a different operation that retains prefix results across input sequences. Do not choose between them only because both accept segmented boundaries. A consumer expecting group totals cannot interpret every prefix value as a separate group result. State output cardinality explicitly in the adapter and verify it with unequal group lengths.

## Choose the initial value for the operator

An initial value participates in the selected reduction contract. A convenient default scalar can be inappropriate for a minimum, maximum or custom operation. Check the mathematical identity and the selected routine's treatment of empty intervals, then include an empty-group diagnostic when that case can occur. Avoid changing missing-group semantics merely to make every output slot contain a familiar finite number. Keep the group-presence decision separate from its numerical aggregation.

## Respect the operation's execution scope

The [rocPRIM scope guide](https://rocm.docs.amd.com/projects/rocPRIM/en/latest/conceptual/rocPRIM-scope.html) distinguishes device-level primitives from operations used within smaller cooperative scopes. Select the appropriate interface rather than placing a device-wide host API where a local kernel primitive is expected. The existing [HIP portability guide](/journal/learn-hip-launch-portability/) and [software reproducibility guide](/journal/software-stack-reproducibility/) provide related context. A correct ragged aggregation records segment ownership, boundaries, initial value and result count before tuning launch behavior.
