From fd58b53ed499ed53971f9923479344f096a5e8c7 Mon Sep 17 00:00:00 2001 From: Arubik <102335860+ArubikU@users.noreply.github.com> Date: Wed, 25 Nov 2026 16:55:34 -0500 Subject: [PATCH] Return the end of the whole range from the parallel partial_sort 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> --- .../hpx/parallel/algorithms/partial_sort.hpp | 9 ++++- .../tests/unit/algorithms/partial_sort.cpp | 38 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/partial_sort.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/partial_sort.hpp index cb79b8b7cd91..3b2f4e58b285 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/partial_sort.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/partial_sort.hpp @@ -420,8 +420,15 @@ namespace hpx::parallel { HPX_MOVE(left), HPX_MOVE(right)); } + // The range was narrowed to [first, c_last). The caller expects + // the end of the whole range, as in the cases above, not the end + // of the part that was sorted. return parallel_partial_sort(HPX_FORWARD(ExPolicy, policy), first, - middle, c_last, level - 1, HPX_FORWARD(Comp, comp)); + middle, c_last, level - 1, HPX_FORWARD(Comp, comp)) + .then(hpx::launch::sync, [last](hpx::future&& f) -> Iter { + f.get(); + return last; + }); } /// \endcond NOINTERNAL } // end namespace detail diff --git a/libs/core/algorithms/tests/unit/algorithms/partial_sort.cpp b/libs/core/algorithms/tests/unit/algorithms/partial_sort.cpp index e7ad03857160..0656ca9969ea 100644 --- a/libs/core/algorithms/tests/unit/algorithms/partial_sort.cpp +++ b/libs/core/algorithms/tests/unit/algorithms/partial_sort.cpp @@ -230,6 +230,43 @@ void test_partial_sort_edge_cases() } } +// Only ranges with a sorted part of at least 4096 elements are split further +// in parallel. The iterator returned must still be the end of the whole range. +void test_partial_sort_large_returns_last() +{ + using namespace hpx::execution; + using compare_t = std::less; + + for (std::size_t size : {10007u, 100000u}) + { + std::vector input(size); + std::iota(input.begin(), input.end(), std::uint64_t{0}); + std::shuffle(input.begin(), input.end(), gen); + + std::size_t const middle_index = size / 2; + + { + auto values = input; + auto result = hpx::partial_sort(par, values.begin(), + values.begin() + static_cast(middle_index), + values.end(), compare_t()); + HPX_TEST(result == values.end()); + for (std::uint64_t j = 0; j < middle_index; ++j) + HPX_TEST_EQ(values[j], j); + } + + { + auto values = input; + auto f = hpx::partial_sort(par(task), values.begin(), + values.begin() + static_cast(middle_index), + values.end(), compare_t()); + HPX_TEST(f.get() == values.end()); + for (std::uint64_t j = 0; j < middle_index; ++j) + HPX_TEST_EQ(values[j], j); + } + } +} + void test_partial_sort_sorted_prefix_with_smaller_tail() { using namespace hpx::execution; @@ -280,6 +317,7 @@ void partial_sort_test() test_partial_sort(); test_partial_sort(); test_partial_sort_edge_cases(); + test_partial_sort_large_returns_last(); test_partial_sort_sorted_prefix_with_smaller_tail(); }