Repository navigation
Keep -0.0 and +0.0 in order in Thrust's CPU stable_sort - #11897
UnknownHacker1 wants to merge 2 commits into
Conversation
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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review. 📝 SummarySummary by CodeRabbit
WalkthroughStable 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. ChangesSigned-Zero Stability
Assessment against linked issues
Priority: ⬇️ Low Change: Bug fix Merge Risk: ⚪ Minimal · up to 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.
Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (5)
thrust/testing/stable_sort.cuthrust/testing/stable_sort_by_key.cuthrust/thrust/sort.hthrust/thrust/system/detail/sequential/sort.hthrust/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.
|
Hi @gevtushenko, friendly ping on this one when you get a chance. Happy to change anything if needed. Thanks! |
Summary
Fixes #750.
Thrust's sequential radix sort, which
stable_sortandstable_sort_by_keyuse for arithmetic keys on the CPP, OMP and TBB backends, encoded -0.0 as0x7fffffffand +0.0 as0x80000000. 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_sortwithgreater<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 waystable_sort_by_keyalready does.Also adds one sentence to the
stable_sortandstable_sort_by_keydocs, plus regression tests for float and double in both directions.Testing
+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.std::stable_sortwith 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./W4. I couldn't run TBB or CUDA locally, so I'm relying on CI for those.