Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use primitive lexicographic comparison for numeric sorting - #24138

Open
PointKernel wants to merge 3 commits into
NVIDIA:mainfrom
PointKernel:primitive-lexicographic-sort
Open

Use primitive lexicographic comparison for numeric sorting#24138
PointKernel wants to merge 3 commits into
NVIDIA:mainfrom
PointKernel:primitive-lexicographic-sort

Conversation

@PointKernel

Copy link
Copy Markdown
Member

Description

This PR adds a primitive lexicographic row comparator and uses it for multi-column numeric sorting, including stable sort. It reuses the reduced type map from primitive row equality to keep nonnumeric comparison instantiations out of numeric sort kernels. The comparator uses device views and ordering spans directly, avoiding general table preprocessing, with shared comparison helpers in lexicographic_common.cuh.

On RTX PRO 6000, the representative eight-column INT32/FLOAT32 sort and sorted_order benchmarks show a 1.79x geometric-mean speedup over main. Block-sort and merge register usage drops from 64 to 48 registers per thread.

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@copy-pr-bot

copy-pr-bot Bot commented Sep 11, 2026

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the libcudf Affects libcudf (C++/CUDA) code. label Sep 11, 2026
@PointKernel PointKernel added improvement Improvement / enhancement to an existing function Performance Performance related issue non-breaking Non-breaking change labels Sep 11, 2026
@PointKernel

Copy link
Copy Markdown
Member Author

/ok to test

@PointKernel

Copy link
Copy Markdown
Member Author

I32/F32 only — 32 cases. Before = main; after = this PR, first paired round. Negative change means faster.

Type Rows Columns Nulls Operation Change
I32 262,144 1 0% sort -0.35%
I32 262,144 1 10% sort +0.19%
I32 2,097,152 1 0% sort -0.13%
I32 2,097,152 1 10% sort -0.28%
I32 262,144 8 0% sort -48.89%
I32 262,144 8 10% sort -41.72%
I32 2,097,152 8 0% sort -45.28%
I32 2,097,152 8 10% sort -36.50%
I32 262,144 1 0% sorted_order -0.56%
I32 262,144 1 10% sorted_order -0.25%
I32 2,097,152 1 0% sorted_order -0.27%
I32 2,097,152 1 10% sorted_order -0.25%
I32 262,144 8 0% sorted_order -49.28%
I32 262,144 8 10% sorted_order -42.72%
I32 2,097,152 8 0% sorted_order -45.97%
I32 2,097,152 8 10% sorted_order -37.67%
F32 262,144 1 0% sort +1.36%
F32 262,144 1 10% sort +0.02%
F32 2,097,152 1 0% sort -0.13%
F32 2,097,152 1 10% sort -0.40%
F32 262,144 8 0% sort -48.91%
F32 262,144 8 10% sort -40.36%
F32 2,097,152 8 0% sort -46.28%
F32 2,097,152 8 10% sort -37.79%
F32 262,144 1 0% sorted_order -0.25%
F32 262,144 1 10% sorted_order -0.17%
F32 2,097,152 1 0% sorted_order -0.28%
F32 2,097,152 1 10% sorted_order -0.28%
F32 262,144 8 0% sorted_order -49.33%
F32 262,144 8 10% sorted_order -41.63%
F32 2,097,152 8 0% sorted_order -46.98%
F32 2,097,152 8 10% sorted_order -39.06%

The primitive comparator delivers a 1.79x geometric-mean speedup for eight-column INT32/FLOAT32 sorting across three rounds on RTX PRO 6000. Block-sort and merge register usage drops from 64 to 48 registers per thread.

@coderabbitai

coderabbitai Bot commented Sep 12, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: bb04c834-880f-4cab-8281-0ab4bfd2e9d8

📥 Commits

Reviewing files that changed from the base of the PR and between 11a901b and 9aa85a4.

📒 Files selected for processing (8)
  • cpp/include/cudf/detail/row_operator/lexicographic.cuh
  • cpp/include/cudf/detail/row_operator/lexicographic_common.cuh
  • cpp/include/cudf/detail/row_operator/primitive_lexicographic.cuh
  • cpp/include/cudf/detail/row_operator/primitive_row_operators.cuh
  • cpp/src/sort/sort_impl.cuh
  • cpp/tests/sort/sort_test.cpp
  • cpp/tests/sort/stable_sort_tests.cpp
  • cpp/tests/streams/sorting_test.cpp

Included review availability: Your plan provides up to 12 included reviews per hour; 10 remain after this review.


📝 Summary

Summary by CodeRabbit

  • New Features

    • Added optimized lexicographic sorting for tables containing multiple numeric columns.
    • Supports per-column ascending or descending order and configurable null precedence.
    • Provides consistent ordering for nulls, NaNs, signed zeros, and numeric boundary values.
    • Applies to regular, stable, and key-based sorting workflows.
  • Documentation

    • Clarified that primitive row operations support tables composed entirely of numeric columns.
  • Tests

    • Added coverage for mixed-column sorting, sliced tables, nulls, NaNs, ties, and varied ordering options.

Walkthrough

Changes

Primitive lexicographic sorting

Layer / File(s) Summary
Shared comparison utilities
cpp/include/cudf/detail/row_operator/lexicographic*.cuh
Moves physical element, NaN, weak-ordering, and convenience comparator definitions into lexicographic_common.cuh.
Primitive row comparison and sort integration
cpp/include/cudf/detail/row_operator/primitive_lexicographic.cuh, cpp/include/cudf/detail/row_operator/primitive_row_operators.cuh, cpp/src/sort/sort_impl.cuh
Adds numeric multi-column row comparison with null precedence and sort direction, then uses it for compatible tables.
Sorting regression coverage
cpp/tests/sort/sort_test.cpp, cpp/tests/sort/stable_sort_tests.cpp, cpp/tests/streams/sorting_test.cpp
Adds coverage for extrema, nulls, NaNs, signed zeros, sliced columns, ties, and opposing column policies across sort APIs.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~30 minutes

Change: Feature

Suggested reviewers: davidwendt

Merge Risk: ⚪ Minimal · up to 9aa85

This change adds a faster comparison path for sorting tables whose columns are all numeric, with the previous general implementation retained for every other input. New tests cover nulls, NaN and signed-zero values, extreme values, sliced inputs, and tie ordering, and no unresolved correctness concerns remain, so the change looks ready to merge.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 3 files. (5 skipped: 5 … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: using primitive lexicographic comparison for numeric sorting.
Description check ✅ Passed The description directly explains the primitive row comparator, numeric sorting support, performance results, shared helpers, and test coverage.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 3 files. (5 skipped: 5 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

make_device_uvector_async(null_precedence, stream, cudf::get_current_device_resource_ref());
if (not column_order.empty() or not null_precedence.empty()) {
// Finish copying the host policies before the caller can release them after this call.
cudf::detail::sync_stream(stream);

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.

I think we can defer this sync further, until after do_sort.

Also why should the stream sync responsibility be handled in different parts of the call stack for primitive and non-primitive? The other comparator below must be handling this internally, because there’s no corresponding stream sync for it.

Can we structure this as an if/else (rather than an early exit) and lift the stream syncs accordingly? Perhaps that means shifting the cudf::detail::row::lexicographic::self_comparator to take device views of the column order and null precedence, and doing the H->D copy and stream sync in this function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Improvement / enhancement to an existing function libcudf Affects libcudf (C++/CUDA) code. non-breaking Non-breaking change Performance Performance related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants