Repository navigation
Use a native sender path for search_n #8257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |
|
|
||
| #include <cstddef> | ||
| #include <iterator> | ||
| #include <optional> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
|
|
@@ -295,8 +296,6 @@ namespace hpx::parallel::detail { | |
| HPX_CXX_CORE_EXPORT template <typename FwdIter, typename Sent> | ||
| struct search_n final : public algorithm<search_n<FwdIter, Sent>, FwdIter> | ||
| { | ||
| static constexpr bool uses_legacy_futures = true; | ||
|
|
||
| constexpr search_n() noexcept | ||
| : algorithm<search_n, FwdIter>("search_n") | ||
| { | ||
|
|
@@ -371,33 +370,89 @@ namespace hpx::parallel::detail { | |
| } | ||
| template <typename ExPolicy, typename Size, typename T, typename Pred, | ||
| typename Proj> | ||
| static util::detail::algorithm_result_t<ExPolicy, FwdIter> parallel( | ||
| ExPolicy&& orgpolicy, FwdIter first, Sent last, Size count, | ||
| T const& value, Pred&& pred, Proj&& proj) | ||
| static decltype(auto) parallel(ExPolicy&& orgpolicy, FwdIter first, | ||
| Sent last, Size count, T const& value, Pred&& pred, Proj&& proj) | ||
| { | ||
| if constexpr (hpx::execution_policy_has_scheduler_executor_v< | ||
| ExPolicy> && | ||
| hpx::is_async_execution_policy_v<ExPolicy>) | ||
| { | ||
| namespace ex = hpx::execution::experimental; | ||
| using base_type = algorithm<search_n<FwdIter, Sent>, FwdIter>; | ||
| auto deferred = ex::let_value(ex::just(), | ||
| [policy = HPX_FORWARD(ExPolicy, orgpolicy), first, last, | ||
| count, value = value, pred = HPX_FORWARD(Pred, pred), | ||
| proj = HPX_FORWARD(Proj, proj)]() mutable { | ||
| return parallel_impl(HPX_MOVE(policy), first, last, | ||
| count, value, HPX_MOVE(pred), HPX_MOVE(proj)); | ||
| }); | ||
| return ex::let_error(HPX_MOVE(deferred), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Retain the HPX completion domain on the returned sender. With one HPX worker, direct Restore the adapter's outer
|
||
| typename base_type::template sender_algorithm_error_handler< | ||
| ExPolicy>{}); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve termination for unsequenced-policy errors. For As per coding guidelines, “Preserve existing APIs and behavior unless the task explicitly asks otherwise.”
|
||
| } | ||
| else | ||
| { | ||
| return parallel_impl(HPX_FORWARD(ExPolicy, orgpolicy), first, | ||
| last, count, value, HPX_FORWARD(Pred, pred), | ||
| HPX_FORWARD(Proj, proj)); | ||
| } | ||
| } | ||
|
|
||
| template <typename ExPolicy, typename Size, typename T, typename Pred, | ||
| typename Proj> | ||
| static decltype(auto) parallel_impl(ExPolicy&& orgpolicy, FwdIter first, | ||
| Sent last, Size count, T const& value, Pred&& pred, Proj&& proj) | ||
| { | ||
| using result_type = | ||
| util::detail::algorithm_result<ExPolicy, FwdIter>; | ||
| using difference_type = | ||
| typename std::iterator_traits<FwdIter>::difference_type; | ||
| constexpr bool has_scheduler_executor = | ||
| hpx::execution_policy_has_scheduler_executor_v<ExPolicy>; | ||
|
|
||
| if (count <= 0) | ||
| return result_type::get(HPX_MOVE(first)); | ||
| if constexpr (!has_scheduler_executor) | ||
| { | ||
| if (count <= 0) | ||
| return result_type::get(HPX_MOVE(first)); | ||
|
|
||
| if (first == last) | ||
| return result_type::get(HPX_MOVE(first)); | ||
| if (first == last) | ||
| return result_type::get(HPX_MOVE(first)); | ||
| } | ||
|
|
||
| difference_type n = hpx::parallel::detail::distance(first, last); | ||
| if (static_cast<difference_type>(count) > n) | ||
| difference_type n = 0; | ||
| if (count > 0 && first != last) | ||
| n = hpx::parallel::detail::distance(first, last); | ||
|
|
||
| if constexpr (!has_scheduler_executor) | ||
| { | ||
| return result_type::get( | ||
| hpx::parallel::detail::advance_to_sentinel(first, last)); | ||
| if (static_cast<difference_type>(count) > n) | ||
| { | ||
| return result_type::get( | ||
| hpx::parallel::detail::advance_to_sentinel( | ||
| first, last)); | ||
| } | ||
| } | ||
|
|
||
| // Number of valid starting positions | ||
| difference_type max_start = | ||
| difference_type max_start = count <= 0 || first == last || | ||
| static_cast<difference_type>(count) > n ? | ||
| 0 : | ||
| n - static_cast<difference_type>(count) + 1; | ||
|
|
||
| auto value_proj = proj(value); | ||
| using projected_type = std::decay_t<decltype(proj(value))>; | ||
| auto value_proj = [&] { | ||
| if constexpr (has_scheduler_executor) | ||
| { | ||
| std::optional<projected_type> projected; | ||
| if (max_start != 0) | ||
| projected.emplace(proj(value)); | ||
| return projected; | ||
| } | ||
| else | ||
| { | ||
| return proj(value); | ||
| } | ||
| }(); | ||
|
|
||
| decltype(auto) policy = | ||
| hpx::execution::experimental::adapt_placement_mode( | ||
|
|
@@ -413,22 +468,34 @@ namespace hpx::parallel::detail { | |
| // an iteration compares up to count elements with the value | ||
| auto const size = hpx::threads::bulk_task_size( | ||
| task_size_of_iteration<task_size_argument_t<FwdIter, Proj>, | ||
| decltype(value_proj) const&>(pred, first), | ||
| static_cast<std::size_t>(count)); | ||
| projected_type const&>(pred, first), | ||
| max_start == 0 ? 0 : static_cast<std::size_t>(count)); | ||
| auto f1 = [max_start, count, value_proj, | ||
| pred = HPX_FORWARD(Pred, pred), | ||
| proj = HPX_FORWARD(Proj, proj), | ||
| tok](FwdIter it, std::size_t part_size, | ||
| std::size_t base_idx) mutable -> void { | ||
| sequential_search_n_t<policy_type>{}(it, base_idx, part_size, | ||
| static_cast<std::ptrdiff_t>(max_start), count, value_proj, | ||
| tok, pred, proj); | ||
| if constexpr (has_scheduler_executor) | ||
| { | ||
| sequential_search_n_t<policy_type>{}(it, base_idx, | ||
| part_size, static_cast<std::ptrdiff_t>(max_start), | ||
| count, *value_proj, tok, pred, proj); | ||
| } | ||
| else | ||
| { | ||
| sequential_search_n_t<policy_type>{}(it, base_idx, | ||
| part_size, static_cast<std::ptrdiff_t>(max_start), | ||
| count, value_proj, tok, pred, proj); | ||
| } | ||
| }; | ||
|
|
||
| auto f2 = [first, last, max_start, tok]( | ||
| auto f2 = [first, last, count, max_start, tok]( | ||
| auto&&... data) mutable -> FwdIter { | ||
| util::detail::clear_container(data...); | ||
|
|
||
| if (count <= 0) | ||
| return HPX_MOVE(first); | ||
|
|
||
| difference_type idx = tok.get_data(); | ||
| if (idx == max_start) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Route projection errors on synchronous scheduler policies through the policy's exception handler.
With
par.on(exec), a synchronous policy bypasses the deferred branch insearch_n::parallel, because the condition at search.hpp:377-378 requiresis_async_execution_policy_v. When valid starts exist,parallel_implcallsproj(value)in thevalue_projinitializer, before any sender or partitioner error handler applies. A projection that throwsstd::runtime_errorthen propagates through the parallel branch ofcall2in dispatch.hpp, which has notry. The caller receivesstd::runtime_error.The legacy-future adapter path that this PR removes routed such errors through
sender_algorithm_error_handler. Synchronous scheduler policies should keep that translation. The setup exception on this path must pass through the policy's exception handler, as the adapter did.The deferred
let_valuepath is one way to do this. Before reusing it, confirm thatparallel_implreturns a sender for synchronous scheduler policies. The callback must return a sender.