Skip to content

Implement the new tuning API for DispatchSegmentedSort - #7874

Merged
bernhardmgruber merged 5 commits into
NVIDIA:mainfrom
bernhardmgruber:tuning_seg_sort
Mar 9, 2026
Merged

bernhardmgruber merged 5 commits into
NVIDIA:mainfrom
bernhardmgruber:tuning_seg_sort

Conversation

@bernhardmgruber

@bernhardmgruber bernhardmgruber commented Mar 3, 2026 •

Copy link
Copy Markdown
Contributor
  • No SASS changes for cub.bench.segmented_sort.keys.base on SM 75;80;86. The large and small kernels are no longer emitted since their code paths are not taken. If you look into the git history, I forced emitting them to verify their SASS did not change, but the PR ultimately removes the kernels from the SASS entirely. (reverted)

Note: the implementation of DispatchSegmentedSort also uses DeviceThreeWayPartition, which will be rewritten separately in #7900

Fixes: #7643
Fixes: #7477

@bernhardmgruber
bernhardmgruber requested review from a team as code owners March 3, 2026 23:57
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Mar 3, 2026
@bernhardmgruber bernhardmgruber changed the title Implement the new tuning API for DispatchSegmentedSort Implement the new tuning API for DispatchSegmentedSort Mar 3, 2026
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Mar 3, 2026
@bernhardmgruber
bernhardmgruber marked this pull request as draft March 3, 2026 23:57
@copy-pr-bot

copy-pr-bot Bot commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@cccl-authenticator-app cccl-authenticator-app Bot moved this from In Review to In Progress in CCCL Mar 3, 2026
@github-actions

This comment has been minimized.

@bernhardmgruber
bernhardmgruber marked this pull request as ready for review March 4, 2026 14:41
@bernhardmgruber
bernhardmgruber requested a review from a team as a code owner March 4, 2026 14:41
@cccl-authenticator-app cccl-authenticator-app Bot moved this from In Progress to In Review in CCCL Mar 4, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

*static_cast<indirect_arg_t**>(&val_arg_in), *static_cast<indirect_arg_t**>(&val_arg_out));

auto exec_status = cub::DispatchSegmentedSort<
// TODO(bgruber): remove all template arguments except the first two (the others can be deduced)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in #7900

auto medium_segment_policy = RuntimeSubWarpMergeSortAgentPolicy::from_json(runtime_policy, "MediumSegmentPolicy");

int partitioning_threshold = runtime_policy["PartitioningThreshold"].get<int>();
// TODO(bgruber): convert to the new tuning API

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in #7900

@bernhardmgruber
bernhardmgruber force-pushed the tuning_seg_sort branch 2 times, most recently from 29c8a14 to 0e08988 Compare March 5, 2026 16:05
Comment on lines +979 to +1014
template <typename PartitionPolicyHub,
typename LargeKernelT,
typename SmallKernelT,
typename KeyT,
typename ValueT,
typename BeginOffsetIteratorT,
typename EndOffsetIteratorT,
typename KernelSource,
typename PartitionKernelSource,
typename KernelLauncherFactory,
typename GetFinalOutputOp>
CUB_RUNTIME_FUNCTION _CCCL_VISIBILITY_HIDDEN _CCCL_FORCEINLINE cudaError_t sort_with_partitioning(
LargeKernelT large_kernel,
SmallKernelT small_kernel,
global_segment_offset_t num_segments,
::cuda::std::int64_t num_items,
BeginOffsetIteratorT d_begin_offsets,
EndOffsetIteratorT d_end_offsets,
cudaStream_t stream,
size_t three_way_partition_temp_storage_bytes,
DoubleBuffer<KeyT>& d_keys,
DoubleBuffer<ValueT>& d_values,
device_double_buffer<KeyT>& d_keys_double_buffer,
device_double_buffer<ValueT>& d_values_double_buffer,
typename KernelSource::LargeSegmentsSelectorT& large_segments_selector,
typename KernelSource::SmallSegmentsSelectorT& small_segments_selector,
temporary_storage::alias<uint8_t>& device_partition_temp_storage,
temporary_storage::alias<local_segment_index_t>& large_and_medium_segments_indices,
temporary_storage::alias<local_segment_index_t>& small_segments_indices,
temporary_storage::alias<local_segment_index_t>& group_sizes,
KernelSource& kernel_source,
PartitionKernelSource& partition_kernel_source,
KernelLauncherFactory& launcher_factory,
typename PartitionPolicyHub::MaxPolicy partition_max_policy,
const segmented_sort_policy& active_policy,
GetFinalOutputOp&& get_final_output)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This was of course generated by Cursor, because no sane person would type this out. I also tried using a lambda inside the dispatch function below, but then it gets really long and is also unreadable. I will gladly take any suggestions on how to make this more readable. I even considered moving back to a struct with member functions, so we can share all the template parameters between the functions.

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.

OIP

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.

Why do we pass the individual aliases of the temporary storage individually?

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.

Also why is there

DoubleBuffer<KeyT>& d_keys,
DoubleBuffer<ValueT>& d_values,
device_double_buffer<KeyT>& d_keys_double_buffer,
device_double_buffer<ValueT>& d_values_double_buffer,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Why do we pass the individual aliases of the temporary storage individually?

Because they have references internally to the main temporary_storage_variable outside the function.

Also why is there ...

The first two are the user inputs, the last two are either pointing into those or into some auxiliary buffers depending on some settings.

@github-actions

This comment has been minimized.

@bernhardmgruber

Copy link
Copy Markdown
Contributor Author

I tried to simplify the code for about half an hour now, but couldn't beat the current line count or reduce the complexity :S

@github-actions

This comment has been minimized.

Comment thread c/parallel/src/radix_sort.cu Outdated
Comment on lines +231 to +233
// FIXME(bgruber): input_values_it.value_type.size is 4 when it represents cub::NullType, which is very odd
keys_only ? 0 : static_cast<int>(input_values_it.value_type.size),
keys_only ? 0 : static_cast<int>(input_values_it.value_type.size), // TODO(bgruber): instead of 0 we should probably
// use int{sizeof(cub::NullType)}

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.

Nitpick:

Suggested change
// FIXME(bgruber): input_values_it.value_type.size is 4 when it represents cub::NullType, which is very odd
keys_only ? 0 : static_cast<int>(input_values_it.value_type.size),
keys_only ? 0 : static_cast<int>(input_values_it.value_type.size), // TODO(bgruber): instead of 0 we should probably
// use int{sizeof(cub::NullType)}
// FIXME(bgruber): input_values_it.value_type.size is 4 when it represents cub::NullType, which is very odd
// TODO(bgruber): instead of 0 we should probably use int{sizeof(cub::NullType)}
keys_only ? 0 : static_cast<int>(input_values_it.value_type.size),


namespace detail::segmented_sort
{
struct segmented_radix_sort_policy

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.

Nit: for reduce, we named the equivalent policy agent_reduce_policy. I think it would be more consistent to name this agent_segmented_radix_sort_policy.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's actually the other way around. The name agent_ should completely disappear. This will also happen before the tuning API goes public.

#endif // !_CCCL_COMPILER(NVRTC)
};

struct sub_warp_merge_sort_policy

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.

Nit: same as above, name this agent_sub_warp_merge_sort_policy

@github-actions

This comment has been minimized.

@github-actions

github-actions Bot commented Mar 9, 2026

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 2h 09m: Pass: 100%/255 | Total: 1d 21h | Max: 44m 15s | Hits: 98%/156304

See results here.

@bernhardmgruber
bernhardmgruber merged commit 09b7323 into NVIDIA:main Mar 9, 2026
539 of 543 checks passed
@bernhardmgruber
bernhardmgruber deleted the tuning_seg_sort branch March 9, 2026 17:04
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

Development

Successfully merging this pull request may close these issues.

Implement the new tuning API for DispatchSegmentedSort Refactor cccl.c segmented_sort to use new tuning API

3 participants