Skip to content

Commit a0ba9ef

Browse files
Address maintainer feedback for is_sorted and is_partitioned
1. Reintroduced enum class for partition_status. 2. Restored module-style headers. 3. Reverted to std::begin/end for ranges. 4. Fixed execution context loss in local loops by using conditional unsequenced_policy. 5. Restored clear_container in results aggregation.
1 parent cdeb015 commit a0ba9ef

3 files changed

Lines changed: 84 additions & 37 deletions

File tree

libs/core/algorithms/include/hpx/parallel/algorithms/is_partitioned.hpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ namespace hpx {
112112

113113
#include <hpx/config.hpp>
114114
#include <hpx/algorithms/traits/projected.hpp>
115+
#include <hpx/modules/execution.hpp>
115116
#include <hpx/modules/executors.hpp>
116117
#include <hpx/modules/functional.hpp>
117118
#include <hpx/modules/iterator_support.hpp>
@@ -138,6 +139,13 @@ namespace hpx::parallel {
138139
////////////////////////////////////////////////////////////////////////////
139140
// is_partitioned
140141
namespace detail {
142+
enum class partition_status : std::uint8_t
143+
{
144+
false_ = 0,
145+
true_ = 1,
146+
mixed = 2,
147+
cancelled = 3
148+
};
141149

142150
/// \cond NOINTERNAL
143151

@@ -188,18 +196,25 @@ namespace hpx::parallel {
188196
util::invoke_projected<Pred, Proj> pred_projected(
189197
HPX_FORWARD(Pred, pred), HPX_FORWARD(Proj, proj));
190198
util::cancellation_token<> tok;
191-
using intermediate_result_t = std::uint8_t;
199+
using intermediate_result_t = partition_status;
192200

193201
auto f1 = [tok, pred_projected = HPX_MOVE(pred_projected)](
194202
Iter_ part_begin, std::size_t part_count) mutable
195203
-> intermediate_result_t {
196204
bool fst_bool = HPX_INVOKE(pred_projected, *part_begin);
197205
if (part_count == 1)
198-
return fst_bool ? 1 : 0;
206+
return fst_bool ? partition_status::true_ :
207+
partition_status::false_;
199208

200209
bool is_mixed = false;
201-
util::loop_n<hpx::execution::sequenced_policy>(++part_begin,
202-
--part_count, tok,
210+
211+
using local_policy = std::conditional_t<
212+
hpx::is_unsequenced_execution_policy_v<
213+
std::decay_t<ExPolicy>>,
214+
hpx::execution::unsequenced_policy,
215+
hpx::execution::sequenced_policy>;
216+
217+
util::loop_n<local_policy>(++part_begin, --part_count, tok,
203218
[&fst_bool, &is_mixed, &pred_projected, &tok](
204219
auto const& a) mutable -> void {
205220
if (fst_bool != hpx::invoke(pred_projected, *a))
@@ -217,27 +232,32 @@ namespace hpx::parallel {
217232
});
218233

219234
if (tok.was_cancelled())
220-
return 3;
235+
return partition_status::cancelled;
221236

222-
return is_mixed ? 2 : (fst_bool ? 1 : 0);
237+
return is_mixed ? partition_status::mixed :
238+
(fst_bool ? partition_status::true_ :
239+
partition_status::false_);
223240
};
224241

225242
auto f2 = [tok](auto&& results) -> bool {
226243
if (tok.was_cancelled())
227244
return false;
228245

229246
auto it = std::find_if(hpx::util::begin(results),
230-
hpx::util::end(results),
231-
[](intermediate_result_t x) { return x != 1; });
247+
hpx::util::end(results), [](partition_status x) {
248+
return x != partition_status::true_;
249+
});
232250

233251
if (it == hpx::util::end(results))
234252
return true;
235253

236-
if (*it == 2)
254+
if (*it == partition_status::mixed)
237255
++it;
238256

239257
return std::all_of(it, hpx::util::end(results),
240-
[](intermediate_result_t x) { return x == 0; });
258+
[](partition_status x) {
259+
return x == partition_status::false_;
260+
});
241261
};
242262

243263
return util::partitioner<ExPolicy, bool,

libs/core/algorithms/include/hpx/parallel/algorithms/is_sorted.hpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313

1414
#include <hpx/config.hpp>
1515

16-
#include <hpx/algorithms/traits/projected.hpp>
17-
#include <hpx/execution/algorithms/detail/is_negative.hpp>
18-
#include <hpx/execution/algorithms/detail/predicates.hpp>
19-
#include <hpx/executors/execution_policy.hpp>
20-
#include <hpx/functional/invoke.hpp>
21-
#include <hpx/iterator_support/traits/is_iterator.hpp>
16+
#include <hpx/modules/execution.hpp>
17+
#include <hpx/modules/executors.hpp>
18+
#include <hpx/modules/functional.hpp>
19+
#include <hpx/modules/iterator_support.hpp>
20+
#include <hpx/parallel/algorithms/detail/dispatch.hpp>
2221
#include <hpx/parallel/algorithms/detail/distance.hpp>
2322
#include <hpx/parallel/algorithms/detail/is_sorted.hpp>
2423
#include <hpx/parallel/util/detail/algorithm_result.hpp>
@@ -91,8 +90,13 @@ namespace hpx::parallel {
9190
FwdIter_ part_begin, std::size_t part_size) mutable
9291
-> intermediate_result_t {
9392
FwdIter_ trail = part_begin++;
94-
util::loop_n<hpx::execution::sequenced_policy>(part_begin,
95-
part_size - 1,
93+
using local_policy = std::conditional_t<
94+
hpx::is_unsequenced_execution_policy_v<
95+
std::decay_t<ExPolicy>>,
96+
hpx::execution::unsequenced_policy,
97+
hpx::execution::sequenced_policy>;
98+
99+
util::loop_n<local_policy>(part_begin, part_size - 1,
96100
[&trail, &tok, &pred_projected](
97101
auto const& it) mutable -> void {
98102
if (hpx::invoke(pred_projected, *it, *trail++))
@@ -116,7 +120,7 @@ namespace hpx::parallel {
116120

117121
auto f2 = [](auto&& results) {
118122
return std::all_of(hpx::util::begin(results),
119-
hpx::util::end(results), [](auto x) { return x; });
123+
hpx::util::end(results), hpx::unwrap());
120124
};
121125

122126
return util::partitioner<ExPolicy, bool,
@@ -192,8 +196,14 @@ namespace hpx::parallel {
192196
std::size_t const cross_idx = base_idx + part_size;
193197

194198
FwdIter_ trail = part_begin++;
195-
util::loop_idx_n<hpx::execution::sequenced_policy>(
196-
++base_idx, part_begin, part_size - 1, tok,
199+
200+
using local_policy = std::conditional_t<
201+
hpx::is_unsequenced_execution_policy_v<policy_type>,
202+
hpx::execution::unsequenced_policy,
203+
hpx::execution::sequenced_policy>;
204+
205+
util::loop_idx_n<local_policy>(++base_idx, part_begin,
206+
part_size - 1, tok,
197207
[&trail, &tok, &pred_projected](
198208
auto& v, std::size_t ind) -> void {
199209
if (hpx::invoke(pred_projected, v, *trail++))
@@ -219,12 +229,15 @@ namespace hpx::parallel {
219229
auto f2 = [first, tok](auto&&... data) mutable -> FwdIter_ {
220230
static_assert(sizeof...(data) < 2);
221231

232+
// make sure iterators embedded in function object that is
233+
// attached to futures are invalidated
234+
util::detail::clear_container(data...);
235+
222236
difference_type loc = tok.get_data();
223237
std::advance(first, loc);
224238
return first;
225239
};
226240

227-
using policy_type = std::decay_t<decltype(policy)>;
228241
return util::partitioner<policy_type, FwdIter_, void>::call_with_index(
229242
HPX_FORWARD(policy_type, policy), first, count, 1,
230243
HPX_MOVE(f1), HPX_MOVE(f2));

libs/core/algorithms/include/hpx/parallel/container_algorithms/is_sorted.hpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66

77
/// \page hpx::ranges::is_sorted, hpx::ranges::is_sorted_until
88
/// \headerfile hpx/algorithm.hpp
9+
#include <hpx/modules/execution.hpp>
10+
#include <hpx/modules/executors.hpp>
11+
#include <hpx/modules/functional.hpp>
12+
#include <hpx/modules/iterator_support.hpp>
13+
#include <hpx/parallel/algorithms/detail/dispatch.hpp>
14+
#include <hpx/parallel/algorithms/detail/distance.hpp>
15+
#include <hpx/parallel/algorithms/detail/is_sorted.hpp>
16+
#include <hpx/parallel/util/detail/algorithm_result.hpp>
17+
#include <hpx/parallel/util/detail/sender_util.hpp>
18+
#include <hpx/parallel/util/invoke_projected.hpp>
19+
20+
#include <hpx/parallel/util/loop.hpp>
21+
#include <hpx/parallel/util/partitioner.hpp>
22+
923

1024
#pragma once
1125

@@ -496,18 +510,18 @@ namespace hpx { namespace ranges {
496510

497511
#else
498512

499-
#include <hpx/config.hpp>
500-
#include <hpx/algorithms/traits/projected_range.hpp>
501513
#include <hpx/modules/executors.hpp>
514+
#include <hpx/modules/functional.hpp>
502515
#include <hpx/modules/iterator_support.hpp>
503-
#include <hpx/parallel/algorithms/is_sorted.hpp>
516+
#include <hpx/parallel/algorithms/detail/dispatch.hpp>
517+
#include <hpx/parallel/algorithms/detail/distance.hpp>
518+
#include <hpx/parallel/util/cancellation_token.hpp>
504519
#include <hpx/parallel/util/detail/algorithm_result.hpp>
505520
#include <hpx/parallel/util/detail/sender_util.hpp>
521+
#include <hpx/parallel/util/invoke_projected.hpp>
506522

507-
#include <cstddef>
508-
#include <iterator>
509-
#include <ranges>
510-
#include <type_traits>
523+
#include <hpx/parallel/util/loop.hpp>
524+
#include <hpx/parallel/util/partitioner.hpp>
511525
#include <utility>
512526

513527
namespace hpx::ranges {
@@ -582,8 +596,8 @@ namespace hpx::ranges {
582596
{
583597
return hpx::parallel::detail::is_sorted<
584598
std::ranges::iterator_t<Rng>, std::ranges::iterator_t<Rng>>()
585-
.call(hpx::execution::seq, hpx::util::begin(rng),
586-
hpx::util::end(rng), HPX_MOVE(pred), HPX_MOVE(proj));
599+
.call(hpx::execution::seq, std::begin(rng),
600+
std::end(rng), HPX_MOVE(pred), HPX_MOVE(proj));
587601
}
588602

589603
template <typename ExPolicy, typename Rng,
@@ -607,8 +621,8 @@ namespace hpx::ranges {
607621
{
608622
return hpx::parallel::detail::is_sorted<
609623
std::ranges::iterator_t<Rng>, std::ranges::iterator_t<Rng>>()
610-
.call(HPX_FORWARD(ExPolicy, policy), hpx::util::begin(rng),
611-
hpx::util::end(rng), HPX_MOVE(pred), HPX_MOVE(proj));
624+
.call(HPX_FORWARD(ExPolicy, policy), std::begin(rng),
625+
std::end(rng), HPX_MOVE(pred), HPX_MOVE(proj));
612626
}
613627
} is_sorted{};
614628

@@ -684,8 +698,8 @@ namespace hpx::ranges {
684698
{
685699
return hpx::parallel::detail::is_sorted_until<
686700
std::ranges::iterator_t<Rng>, std::ranges::iterator_t<Rng>>()
687-
.call(hpx::execution::seq, hpx::util::begin(rng),
688-
hpx::util::end(rng), HPX_MOVE(pred), HPX_MOVE(proj));
701+
.call(hpx::execution::seq, std::begin(rng),
702+
std::end(rng), HPX_MOVE(pred), HPX_MOVE(proj));
689703
}
690704

691705
template <typename ExPolicy, typename Rng,
@@ -710,8 +724,8 @@ namespace hpx::ranges {
710724
{
711725
return hpx::parallel::detail::is_sorted_until<
712726
std::ranges::iterator_t<Rng>, std::ranges::iterator_t<Rng>>()
713-
.call(HPX_FORWARD(ExPolicy, policy), hpx::util::begin(rng),
714-
hpx::util::end(rng), HPX_MOVE(pred), HPX_MOVE(proj));
727+
.call(HPX_FORWARD(ExPolicy, policy), std::begin(rng),
728+
std::end(rng), HPX_MOVE(pred), HPX_MOVE(proj));
715729
}
716730
} is_sorted_until{};
717731
} // namespace hpx::ranges

0 commit comments

Comments
 (0)