Skip to content

Keep -0.0 and +0.0 in order in Thrust's CPU stable_sort - #11897

Open
UnknownHacker1 wants to merge 2 commits into
NVIDIA:mainfrom
UnknownHacker1:issue-750-stable-sort-zeros
Open

UnknownHacker1 wants to merge 2 commits into
NVIDIA:mainfrom
UnknownHacker1:issue-750-stable-sort-zeros

Conversation

@UnknownHacker1

Copy link
Copy Markdown
Contributor

Summary

Fixes #750.

Thrust's sequential radix sort, which stable_sort and stable_sort_by_key use for arithmetic keys on the CPP, OMP and TBB backends, encoded -0.0 as 0x7fffffff and +0.0 as 0x80000000. So every -0.0 was sorted before every +0.0, even though they compare equal and a stable sort has to keep them in input order. This maps -0.0 to +0.0 inside the radix encoder. That only affects which bucket a key goes into; the keys themselves are untouched. It's the same approach CUB took in cub#218.

While testing I found a second case: keys-only stable_sort with greater<T> sorted ascending and then reversed the result, which flips the order of equal keys. It now reverses the input first as well, the same way stable_sort_by_key already does.

Also adds one sentence to the stable_sort and stable_sort_by_key docs, plus regression tests for float and double in both directions.

Testing

  • An 8-element repro (+0 -0 -1 +0 -0 +1 -0 +0) gave the wrong order in all 8 cases on the CPP backend before the fix (float and double, ascending and descending, keys only and by key). After the fix all 8 are correct.
  • Randomized check against std::stable_sort with 100k keys (zeros, infinities and lots of ties, both directions), compared bit for bit: the CPP and OMP backends differed in 20 of 20 runs before the fix and 0 of 20 after.
  • The new tests fail 16 of 16 checks without the fix and pass with it, on the CPP and OMP backends, built with MSVC (VS 2022) /W4. I couldn't run TBB or CUDA locally, so I'm relying on CI for those.
  • Formatted with clang-format 22.1.5 from the pre-commit config.

Thrust's sequential radix sort encoded -0.0 and +0.0 differently, so
every -0.0 ended up before every +0.0 even though they compare equal.
That broke stable_sort and stable_sort_by_key on the CPP, OMP and TBB
backends, which all use it. Both zeros now share a bucket, the same
approach CUB uses since cub#218.

Keys-only stable_sort with greater<T> also reversed the sorted output
without reversing the input first, which flips the order of equal keys.
It now reverses the input too, like stable_sort_by_key already does.

Adds regression tests and documents the guarantee. Fixes NVIDIA#750.
@UnknownHacker1
UnknownHacker1 requested a review from a team as a code owner October 6, 2026 17:54
@copy-pr-bot

copy-pr-bot Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-project-automation github-project-automation Bot moved this to Todo in CCCL Oct 6, 2026
@coderabbitai

coderabbitai Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: NVIDIA/cccl/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: d9d09594-c6de-4b24-953b-13478502556a
📥 Commits

Reviewing files that changed from the base of the PR and between 2d5af88 and 47af6fb.

📒 Files selected for processing (1)
  • thrust/thrust/sort.h
🚧 Files skipped from review as they are similar to previous changes (1)
  • thrust/thrust/sort.h

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.


📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Stable sorting now preserves the original order of equivalent positive and negative zero values in floating-point data, for both ascending and descending order.
    • Corrected host-side descending stable sorting so equivalent values retain their relative order.
    • Stable sort-by-key now also preserves the original order of entries with equivalent positive and negative zero keys.
  • Documentation
    • Clarified when positive and negative zero compare as equivalent for stable sorting.

Walkthrough

Stable sorting now treats floating-point negative and positive zero as equivalent while preserving input order. Sequential sorting paths and radix encoders are updated. Tests cover host and device results, and documentation states the stability guarantee.

Changes

Signed-Zero Stability

Layer / File(s) Summary
Stable sorting behavior
thrust/thrust/system/detail/sequential/sort.h, thrust/thrust/system/detail/sequential/stable_radix_sort.h
The sequential greater<KeyType> path reverses input before primitive sorting. The float and double radix encoders assign negative zero the same encoding as positive zero.
Regression tests and stability documentation
thrust/testing/stable_sort.cu, thrust/testing/stable_sort_by_key.cu, thrust/thrust/sort.h
Tests compare host and device results against stable references for float and double with ascending and descending comparators. Documentation states the signed-zero equivalence for stable_sort and stable_sort_by_key.

Assessment against linked issues

Objective Addressed Explanation
Check whether Thrust backends handle signed zeros correctly [#750] ❓ The changes update sequential sorting and add host/device tests, but they do not identify coverage for every Thrust backend.
Document signed-zero stability for stable_sort [#750] ✅
Add regression tests that enforce signed-zero stability on all backends [#750] ❓ The tests cover host and device results, but backend-wide coverage is not confirmed.

Priority: ⬇️ Low

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 47af6

This change documents that Thrust's stable sorting treats negative and positive zero as equivalent while preserving input order. No actionable merge-blocking risk remains in the reviewed documentation change.

  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository: NVIDIA/cccl/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: 339d3132-aa03-4517-922e-131cc04dd90d
📥 Commits

Reviewing files that changed from the base of the PR and between e259c73 and 2d5af88.

📒 Files selected for processing (5)
  • thrust/testing/stable_sort.cu
  • thrust/testing/stable_sort_by_key.cu
  • thrust/thrust/sort.h
  • thrust/thrust/system/detail/sequential/sort.h
  • thrust/thrust/system/detail/sequential/stable_radix_sort.h

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.

Comment thread thrust/thrust/sort.h Resolved
@UnknownHacker1

Copy link
Copy Markdown
Contributor Author

Hi @gevtushenko, friendly ping on this one when you get a chance. Happy to change anything if needed. Thanks!

This branch has not been deployed

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

Labels

None yet

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

Check for issues with -0 / +0 stable_sort issues

1 participant