Repository navigation
Segmented scan multisegment part 1 - #8639
Merged
oleksandr-pavlyk merged 155 commits intoApr 28, 2026
Merged
oleksandr-pavlyk merged 155 commits into
oleksandr-pavlyk merged 155 commits into
Conversation
…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.
…ith non-const call operator
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
force-pushed
the
segmented-scan-multisegment-part-1
branch
from
April 27, 2026 19:29
c5b6b29 to
42abcc8
Compare
This comment has been minimized.
This comment has been minimized.
oleksandr-pavlyk
force-pushed
the
segmented-scan-multisegment-part-1
branch
from
April 27, 2026 22:08
42abcc8 to
8fc19f5
Compare
This comment has been minimized.
This comment has been minimized.
oleksandr-pavlyk
force-pushed
the
segmented-scan-multisegment-part-1
branch
from
April 28, 2026 02:19
8fc19f5 to
292064d
Compare
This comment has been minimized.
This comment has been minimized.
oleksandr-pavlyk
force-pushed
the
segmented-scan-multisegment-part-1
branch
from
April 28, 2026 13:38
292064d to
53f063a
Compare
| mutable ReadTransformT m_read_transform_fn; | ||
|
|
||
| using iterator_concept = ::cuda::std::random_access_iterator_tag; | ||
| using iterator_category = ::cuda::std::random_access_iterator_tag; |
Contributor
There was a problem hiding this comment.
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.
Contributor
There was a problem hiding this comment.
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?
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. |
Contributor
🥳 CI Workflow Results🟩 Finished in 1h 15m: Pass: 100%/269 | Total: 2d 09h | Max: 46m 07s | Hits: 97%/179761See results here. |
This was referenced 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).
2 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR builds on top of #6712 and adds two commits
This should reduce the number of lines changed significantly.
Closes #6783
Closes #8424
extend
agent_segmented_scanwith ability to to consume multiple segments per blockMaxSegmentsPerBlockwhich controls the size ofOffsetTarray in shared memory dedicated to storing inclusive-scan of segment sizes. The actual number of segments to process is passed dynamically toconsume_rangesmethod. It can not exceedMaxSegmentsPerBlock, however.multi_segment_iteratorclass, which is read- and write- transform iterator.AccumTbyBlockScan, andelement_idtosegment_idandrelative_offset(or rank) is encapsulated into searcher types.segment_id = element_id / segment_size.cuda::std::upper_boundcan be used for larger number of sizes, and linear search using dynamic for-loop for smaller sizesconsume_rangecall is made, which does not reconstruct head-flagsDispatch 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).