[parallel] Fix hpx::nth_element to correctly support C++20 projections and sentinels#7191
Open
aneek22112007-tech wants to merge 9 commits intoTheHPXProject:masterfrom
Conversation
…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.
Up to standards ✅🟢 Issues
|
|
Can one of the admins verify this patch? |
hkaiser
reviewed
Apr 16, 2026
hkaiser
reviewed
Apr 16, 2026
6391a4f to
e58f6aa
Compare
Contributor
|
@aneek22112007-tech Please pay attention to the CIs. The clang-format CI raised some issue. |
Contributor
Author
|
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. |
7e072fa to
5590bc1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a regression where
hpx::nth_elementignored 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
hpx::nth_element_tto natively accept C++20 sentinels (Sent) and projections (Proj) across both sequential and parallel execution overloads.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 withhpx::parallel::util::compare_projected, guaranteeing that custom projections are reliably evaluated before any structural comparisons are made.hpx::parallel::detail::partitionlambda andpivot9selection 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.nth_element_projection.cppto the unit test suite, aggressively testing both struct-member projections (&S::val) and complex type-transforming projections (e.g. mapping internal strings to sizes) acrossseq,par, andpar_unseqexecution policies.Any background context you want to provide?
While working through the parallel algorithm surfaces, I noticed that
hpx::nth_elementhad 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::stringdirectly against asize_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_elementagainst modern C++20 usage patterns to match the rest of the HPX algorithms ecosystem!Checklist
Not all points below apply to all pull requests.