Skip to content

Return the end of the whole range from the parallel partial_sort - #8238

Open
ArubikU wants to merge 1 commit into
TheHPXProject:masterfrom
ArubikU:partial-sort-return
Open

ArubikU wants to merge 1 commit into
TheHPXProject:masterfrom
ArubikU:partial-sort-return

Conversation

@ArubikU

@ArubikU ArubikU commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #8237

Proposed Changes

  • detail::parallel_partial_sort narrows the range to [first, c_last) after partitioning, and calls itself on that part. It returned what that call returned, the end of the narrowed range, so the overloads documented to return last returned an iterator inside the range once the sorted part had at least 4096 elements.
  • The call that narrows the range now waits for the recursive one and returns the original last, as the early returns and the branch that splits in two already do.
  • New test test_partial_sort_large_returns_last in partial_sort.cpp: 10007 and 100000 elements, par and par(task), checking the returned iterator and the sorted prefix.

Any background context you want to provide?

The existing tests check the returned iterator, but with sizes below the 4096-element threshold, so they never reach the narrowing path. The reproducer and the numbers (for 10007 elements it came back at 5612) are in #8237.

Checked with GCC 15.2 (C++20, local runtime): the new test fails without the change (both policies) and passes with it. partial_sort_test, partial_sort_copy_test and partial_sort_sender_test pass.

I found this with a differential test of hpx::ranges against std::ranges; happy to share it if it is of use.

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.

@ArubikU
ArubikU requested a review from hkaiser as a code owner November 25, 2026 21:55
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Fixed parallel partial_sort on large ranges so it returns the end of the full range, rather than an iterator to an internal portion.
    • The operation now waits for recursive sorting to complete before returning. This behavior is consistent across parallel and task-based execution, and the sorted prefix contains the expected values.

The recursive parallel partial_sort path now waits for its narrowed recursive call and returns the original range end. Regression tests check the returned iterator and sorted prefix for parallel and task-policy calls on large shuffled ranges.

Changes: Partial sort return iterator

Layer / File(s) Summary
Recursive return and regression coverage
libs/core/algorithms/include/hpx/parallel/algorithms/partial_sort.hpp, libs/core/algorithms/tests/unit/algorithms/partial_sort.cpp
The recursive call now completes before the algorithm returns the original range end. Tests check the returned iterator and sorted prefix for parallel and task-policy calls on large ranges.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested labels: type: defect, category: algorithms

Suggested reviewers: msimberg

Merge Risk: Minimal · up to fd58b

This fixes the parallel partial_sort return iterator for large ranges so it refers to the range end. No concrete merge risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title states the main change: parallel partial_sort now returns the end of the whole range instead of an iterator inside the range. It matches the raw summary, the PR objectives, and linked issue …
Description check Passed The description explains the defect in parallel_partial_sort, the fix that returns the original last, and the new regression test. It also lists the checks run. It matches the changeset and linked iss…
Linked Issues check Passed Issue #8237 requires that parallel hpx::partial_sort and hpx::ranges::partial_sort return an iterator to last once the sorted part reaches 4096 elements. The whole-PR diff (merge base to head fd…
Out of Scope Changes check Passed The whole-PR diff touches two files. partial_sort.hpp changes only the narrowing return in parallel_partial_sort, which is the defect named in #8237. The added comment explains that change. `parti…
Full details: Title check

Explanation

The title states the main change: parallel partial_sort now returns the end of the whole range instead of an iterator inside the range. It matches the raw summary, the PR objectives, and linked issue #8237, and it is specific enough for a teammate scanning history.

Full details: Description check

Explanation

The description explains the defect in parallel_partial_sort, the fix that returns the original last, and the new regression test. It also lists the checks run. It matches the changeset and linked issue #8237.

Full details: Linked Issues check

Explanation

Issue #8237 requires that parallel hpx::partial_sort and hpx::ranges::partial_sort return an iterator to last once the sorted part reaches 4096 elements. The whole-PR diff (merge base to head fd58b53) changes the narrowing return in detail::parallel_partial_sort in partial_sort.hpp. The recursive call on [first, c_last) now chains .then(hpx::launch::sync, ...). That continuation calls f.get() to propagate exceptions and returns the captured original last. This matches the early returns and the split-in-two branch, as the issue requires. The new test test_partial_sort_large_returns_last runs 10007 and 100000 elements with par and par(task). It checks that the returned iterator equals values.end() and that the first middle_index values equal 0..middle-1 for a permutation input. The test is invoked from partial_sort_test. The ranges variant of #8237 uses the same detail implementation, so it is covered by the same change. Sequential behavior is not changed. I did not compile or run the tests; the verdict rests on source inspection of the diff.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff touches two files. partial_sort.hpp changes only the narrowing return in parallel_partial_sort, which is the defect named in #8237. The added comment explains that change. partial_sort.cpp adds the regression test and one call to it in partial_sort_test. The test uses gen, std::iota, and std::shuffle, which the test file already uses. No unrelated code, refactoring, or documentation changes appear in the diff.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4fd1df47-dcab-41a3-84c9-2f7aa26c80c5
Commits

Reviewing files that changed from the base of the PR and between ca45111 and fd58b53.

Files selected for processing: 1

  • libs/core/algorithms/include/hpx/parallel/algorithms/partial_sort.hpp

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

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

The parallel partial_sort narrows the range to [first, c_last) after
partitioning around a pivot, and calls itself on that part. When it did,
it returned what the recursive call returned, which is the end of the
narrowed range. The overloads documented to return last therefore
returned an iterator somewhere inside the range whenever the sorted
part had at least 4096 elements, which depends on the data: for 10007
elements it came back at 5612.

The early returns and the case that splits in two already return last.
Do the same for the call that narrows the range.

The existing tests only used ranges too small to reach that path. Add
one that does, for the synchronous and the task policy.

Signed-off-by: Arubik <102335860+ArubikU@users.noreply.github.com>
@ArubikU
ArubikU force-pushed the partial-sort-return branch from ca45111 to fd58b53 Compare November 25, 2026 22:27

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

None yet

Development

Successfully merging this pull request may close these issues.

Parallel partial_sort returns an iterator inside the range for large inputs

1 participant