Skip to content

[parallel] Fix hpx::nth_element to correctly support C++20 projections and sentinels#7191

Open
aneek22112007-tech wants to merge 9 commits intoTheHPXProject:masterfrom
aneek22112007-tech:fix/nth-element-projections-sentinels
Open

[parallel] Fix hpx::nth_element to correctly support C++20 projections and sentinels#7191
aneek22112007-tech wants to merge 9 commits intoTheHPXProject:masterfrom
aneek22112007-tech:fix/nth-element-projections-sentinels

Conversation

@aneek22112007-tech
Copy link
Copy Markdown
Contributor

This PR fixes a regression where hpx::nth_element ignored user-provided projections in its sequential and parallel paths. It also adds full support for sentinels and aligns the algorithm with C++20 Ranges standards.

Proposed Changes

  • CPO Enhancement & Sentinel Support: Upgraded hpx::nth_element_t to natively accept C++20 sentinels (Sent) and projections (Proj) across both sequential and parallel execution overloads.
  • Robust Internal Projection Handling: A major issue was that internal helper algorithms (min_element, sort, make_heap) were being called with raw, un-projected comparators. I have standardized all of these internal calls to cleanly wrap the base comparator with hpx::parallel::util::compare_projected, guaranteeing that custom projections are reliably evaluated before any structural comparisons are made.
  • Parallel Partition Correctness: Fixed a deeply hidden type-mismatch bug inside the parallel hpx::parallel::detail::partition lambda and pivot9 selection logic. The parallel lambda now correctly evaluates the projected value directly in the traversal loop. This resolves the compiler errors that would trigger whenever users passed in non-trivial type-transforming projections.
  • Comprehensive Regression Tests: Added nth_element_projection.cpp to the unit test suite, aggressively testing both struct-member projections (&S::val) and complex type-transforming projections (e.g. mapping internal strings to sizes) across seq, par, and par_unseq execution policies.

Any background context you want to provide?

While working through the parallel algorithm surfaces, I noticed that hpx::nth_element had an incomplete implementation of the C++20 Ranges specification. Specifically, while the CPO technically accepted custom projections, the projection targets were inadvertently dropped when the work was dispatched to internal loops.

This meant that if a user tried to partition a dataset by using a projection that fundamentally altered the type—for example, projecting a list of strings out to their integer lengths—the parallel partitioner would attempt to compare a raw std::string directly against a size_t, failing entirely at compile time.

This PR properly threads the projection directly into the innermost parallel computation lambdas, tightening the CPO layer and effectively hardening hpx::nth_element against modern C++20 usage patterns to match the rest of the HPX algorithms ecosystem!

Checklist

Not all points below apply to all pull requests.

  • I have added a new feature and have added tests to go along with it.
  • I have fixed a bug and have added a regression test.
  • I have added a test using random numbers; I have made sure it uses a seed, and that random numbers generated are valid inputs for the tests.

…sentinels

This PR fixes a regression where hpx::nth_element ignored user-provided projections in its sequential and parallel paths. It also adds full support for sentinels and aligns the algorithm with C++20 Ranges standards.
@codacy-production
Copy link
Copy Markdown

codacy-production bot commented Apr 16, 2026

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

TIP This summary will be updated as you push new changes. Give us feedback

@StellarBot
Copy link
Copy Markdown

Can one of the admins verify this patch?

Comment thread libs/core/algorithms/include/hpx/parallel/algorithms/nth_element.hpp Outdated
Comment thread libs/core/algorithms/include/hpx/parallel/algorithms/nth_element.hpp Outdated
Comment thread libs/core/algorithms/include/hpx/parallel/algorithms/nth_element.hpp Outdated
@aneek22112007-tech aneek22112007-tech force-pushed the fix/nth-element-projections-sentinels branch from 6391a4f to e58f6aa Compare April 16, 2026 21:50
@hkaiser
Copy link
Copy Markdown
Contributor

hkaiser commented Apr 16, 2026

@aneek22112007-tech Please pay attention to the CIs. The clang-format CI raised some issue.

@aneek22112007-tech
Copy link
Copy Markdown
Contributor Author

aneek22112007-tech commented Apr 16, 2026

Thanks for the review! I’ve gone ahead and consolidated that comparison wrapper at the top of the function to clean things up. I also switched to std::make_heap as suggested. Fixed the formatting issues too, so CI should be all clear now.

@aneek22112007-tech aneek22112007-tech force-pushed the fix/nth-element-projections-sentinels branch from 7e072fa to 5590bc1 Compare April 17, 2026 04:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants