Skip to content

Segmented scan multisegment part 1 - #8639

Merged
oleksandr-pavlyk merged 155 commits into
NVIDIA:mainfrom
oleksandr-pavlyk:segmented-scan-multisegment-part-1
Apr 28, 2026
Merged

oleksandr-pavlyk merged 155 commits into
NVIDIA:mainfrom
oleksandr-pavlyk:segmented-scan-multisegment-part-1

Conversation

@oleksandr-pavlyk

Copy link
Copy Markdown
Contributor

Description

This PR builds on top of #6712 and adds two commits

  • First one removes support for thread-granularity agent and kernel
  • Second removes support for warp-granularity agent and kernel

This should reduce the number of lines changed significantly.


Closes #6783
Closes #8424

  • extend agent_segmented_scan with ability to to consume multiple segments per block

    • Introduce new agent template parameter MaxSegmentsPerBlock which controls the size of OffsetT array in shared memory dedicated to storing inclusive-scan of segment sizes. The actual number of segments to process is passed dynamically to consume_ranges method. It can not exceed MaxSegmentsPerBlock, however.
    • Multiple-segments are processed by reconstructing head-flags while loading values using multi_segment_iterator class, which is read- and write- transform iterator.
      • Read transformation reconstructs head-flag and forms value-flag pair, used as AccumT by BlockScan, and
      • write-transform unpacks value-flag pair and only writes the value to the output iterator.
    • Mapping from element_id to segment_id and relative_offset (or rank) is encapsulated into searcher types.
    • Three searcher types are implemented:
      • When segment sizes are all the same, segment_id = element_id / segment_size.
      • Generically, binary search in the array of inclusive-scan of segment sizes is performed. Two implementations (types) are provided:
        • When static bound on array size of known (as it is in our case), branch-less binary search provides best performance
        • When this bound is unknown, cuda::std::upper_bound can be used for larger number of sizes, and linear search using dynamic for-loop for smaller sizes
    • While computing inclusive-scan of segment sizes, we check if all sizes to be processed by block are the same, and use fixed-size searcher, otherwise use branch-less generic searcher.
    • When dynamic number of segments equals 1, simpler consume_range call is made, which does not reconstruct head-flags
  • Dispatch layer is modified to take 2 additional parameters (with default values), the number of segments to be processed by a worker (defaults to 1), and the type of worker requested (defaults to block).

…r_block

This agent allows processing several segments per block by using
Schwarz reformulation of segmented scan as originary scan over
head flag, value pairs.

Thsi agent provides overload method consume_ranges with two
signatures, one with 3 arrays of offsets, and another with
3 arrays of offsets and the actual number of segments populated
for the tail call.
All 3 kernels used agent_segmented_scan and communicate the number
of segments to use per block through AgentSegmentedScanPolicy
template parameter SegmentsPerBlock.

Tests exercise, single segment per block use case,
2 segments per block without tail case, and 3 segments per block
where one call to `consume_ranges` requires to specify number
of segments that is smaller that 3.
The default policy value for segments_per_block is one though.
…rror

Add benchmark, which also benchmarks algorithms with number of segments
per block > 1.

Kernels with > 3 segments per block suffer from local memory accesses.
Using linear search instead of binary search improved performance.

// with binary search everywhere
| T{ct} | OffsetT{ct} | SegmentsPerBlock{ct} |  Elements{io}   | SegmentSize{io} | Number of Segments |    Size     | Samples | CPU Time | Noise | GPU Time | Noise | Elem/s  | GlobalMem BW | BWUtil |
|-------|-------------|----------------------|-----------------|-----------------|--------------------|-------------|---------|----------|-------|----------|-------|---------|--------------|--------|
|   I32 |         I32 |                    4 | 2^26 = 67108864 |             123 |             545601 | 256.000 MiB |    304x | 4.789 ms | 6.40% | 4.777 ms | 6.42% | 14.049G | 112.391 GB/s | 14.63% |

// with linear search everywhere
|   I32 |         I32 |                    4 | 2^26 = 67108864 |             123 |             545601 | 256.000 MiB |    576x | 2.457 ms | 6.62% | 2.440 ms | 6.11% | 27.508G | 220.066 GB/s | 28.65% |

The linear search introduces much less divergence.
This lowers register pressure, reduces local memory traffic
from 28MB to 7MB per kernel invocation.

Also remove use flag_t type and replaced it with bool,
as per benchmarking results there was little difference between
using uint8_t, uint16_t, uint32_t, and bool.

Bool type for head-of-segment flag is the most appropriate type
semnatically.
1. Reduce register usage by passing begin offsets for input/output
   sequences as iterators, instead of local arrays.
2. Add segments_per_block to wrapper policy and use it in dispatching
   logic, thus reducing the grid size as number of segments per
   block increases.
3. Implemented benchmark to study performance of DeviceSegmentedScan.
4. Optimize operations in agent_segmented_scan guided by studying
   of NCU profiles collected on benchmark runs.
…tion

This improves performance for short segments by about 10%
Implementation shares some logic with agent_segmented_reduce at the moment.
…lti_segmented_output_iterator

As noted in PR review, the combined implementation imposes too strict conditions, and fails
to support transform_iterator/transform_output_iterator.
Implemented PR review feedback:
  1. Use mutable qualifier of input functor to support
     functors with non-const qualified call operator.
  2. Use POD struct instead of tuple to augmented-value pair to improve
     compile time and to return segment_id/logical_offset pair
     in bag of segments classes.

     This required use conversion structs to convert from initial
     value type to accumulator type where needed in kernels.
  3. Remove include of cuda/std/tuple in segmented_scan_helpers.cuh
  4. Each kernel for segmented_scan defined agent_policy which is
     obtained from policy getter.

     This should also improve compile timing by saving repeated
     evaluations of policy getter callable.
@oleksandr-pavlyk
oleksandr-pavlyk force-pushed the segmented-scan-multisegment-part-1 branch from c5b6b29 to 42abcc8 Compare April 27, 2026 19:29
@github-actions

This comment has been minimized.

@oleksandr-pavlyk
oleksandr-pavlyk force-pushed the segmented-scan-multisegment-part-1 branch from 42abcc8 to 8fc19f5 Compare April 27, 2026 22:08
@github-actions

This comment has been minimized.

@oleksandr-pavlyk
oleksandr-pavlyk force-pushed the segmented-scan-multisegment-part-1 branch from 8fc19f5 to 292064d Compare April 28, 2026 02:19
@github-actions

This comment has been minimized.

@oleksandr-pavlyk
oleksandr-pavlyk force-pushed the segmented-scan-multisegment-part-1 branch from 292064d to 53f063a Compare April 28, 2026 13:38
mutable ReadTransformT m_read_transform_fn;

using iterator_concept = ::cuda::std::random_access_iterator_tag;
using iterator_category = ::cuda::std::random_access_iterator_tag;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remark: multi_segmented_input_iterator is technically an input iterator, because it does not return value_type& for operator*. But that may degrade the iterator's performance and since this type is not public I can gloss over the non conformance.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: I don't see launch id parameterization (// %PARAM% TEST_LAUNCH lid 0:1:2). Does the new segmented implementation not support being called from device code or being graph captured?

@bernhardmgruber

Copy link
Copy Markdown
Contributor

I did a high level pass without trying to understand what the code is doing. Here are a few more comments. Since @NaderAlAwar already approved, there is no need to wait for my further approval.

@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 1h 15m: Pass: 100%/269 | Total: 2d 09h | Max: 46m 07s | Hits: 97%/179761

See results here.

@oleksandr-pavlyk
oleksandr-pavlyk merged commit 8dd2e30 into NVIDIA:main Apr 28, 2026
292 checks passed
@oleksandr-pavlyk
oleksandr-pavlyk deleted the segmented-scan-multisegment-part-1 branch April 28, 2026 16:25
@github-project-automation github-project-automation Bot moved this from In Review to Done in CCCL Apr 28, 2026
@bernhardmgruber bernhardmgruber mentioned this pull request Apr 28, 2026
2 tasks done
shwina pushed a commit to shwina/cccl that referenced this pull request Apr 30, 2026
Support for processing of multiple segments by block in segmented scan

  - extend `agent_segmented_scan` with ability to to consume multiple segments per block
     - Introduce new agent template parameter `MaxSegmentsPerBlock` which controls the size of `OffsetT` array in shared memory dedicated to storing inclusive-scan of segment sizes. The actual number of segments to process is passed dynamically to `consume_ranges` method. It can not exceed `MaxSegmentsPerBlock`, however.
     - Multiple-segments are processed by reconstructing head-flags while loading values using `multi_segment_iterator` class, which is read- and write- transform iterator.
        - Read transformation reconstructs head-flag and forms value-flag pair, used as `AccumT` by `BlockScan`, and
        - write-transform unpacks value-flag pair and only writes the value to the output iterator.
     - Mapping from `element_id` to `segment_id` and `relative_offset` (or rank) is encapsulated into searcher types.
     - Three searcher types are implemented:
        - When segment sizes are all the same, `segment_id = element_id / segment_size`.
        - Generically, binary search in the array of inclusive-scan of segment sizes is performed. Two implementations (types) are provided:
           - When static bound on array size of known (as it is in our case), branch-less binary search provides best performance
           - When this bound is unknown, `cuda::std::upper_bound` can be used for larger number of sizes, and linear search using dynamic for-loop for  smaller sizes
     - While computing inclusive-scan of segment sizes, we check if all sizes to be processed by block are the same, and use fixed-size searcher, otherwise use branch-less generic searcher.
     - When dynamic number of segments equals 1, simpler `consume_range` call is made, which does not reconstruct head-flags

   - Dispatch layer is modified to take 2 additional parameters (with default values), the number of segments to be processed by a worker (defaults to 1), and the type of worker requested (defaults to block).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

3 participants