From e9d0be6bae2dfedc770694e1194f26418c4bb5ee Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Tue, 23 May 2023 09:45:36 -0500 Subject: [PATCH 01/32] fold_left, need to add concepts Signed-off-by: Hari Hara Naveen S --- libs/core/algorithms/CMakeLists.txt | 1 + .../include/hpx/parallel/algorithm.hpp | 1 + .../parallel/algorithms/detail/generate.hpp | 19 ++++- .../include/hpx/parallel/algorithms/fold.hpp | 78 +++++++++++++++++++ 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp diff --git a/libs/core/algorithms/CMakeLists.txt b/libs/core/algorithms/CMakeLists.txt index 8f37b9277a31..b14aceb6c63e 100644 --- a/libs/core/algorithms/CMakeLists.txt +++ b/libs/core/algorithms/CMakeLists.txt @@ -50,6 +50,7 @@ set(algorithms_headers hpx/parallel/algorithms/exclusive_scan.hpp hpx/parallel/algorithms/fill.hpp hpx/parallel/algorithms/find.hpp + hpx/parallel/algorithms/fold.hpp hpx/parallel/algorithms/for_each.hpp hpx/parallel/algorithms/for_loop.hpp hpx/parallel/algorithms/for_loop_induction.hpp diff --git a/libs/core/algorithms/include/hpx/parallel/algorithm.hpp b/libs/core/algorithms/include/hpx/parallel/algorithm.hpp index 127c3f8993b8..c249f1c3316e 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithm.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithm.hpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/detail/generate.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/detail/generate.hpp index c5cdba12f57a..6a591bd39866 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/detail/generate.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/detail/generate.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -53,7 +54,20 @@ namespace hpx::parallel::detail { /////////////////////////////////////////////////////////////////////////// template constexpr Iter sequential_generate_n_helper( - Iter first, std::size_t count, F&& f) + Iter first, std::size_t count, F&& f, /*is unsequenced*/ std::true_type) + { +#ifdef HPX_WITH_CXX20_STD_EXECUTION_POLICES // unseq execution was added in CXX20 + return std::generate_n(std::execution::unseq, first, count, f); +#else + auto f2 = [&](Iter it) { *it = f(); }; + return ::hpx::parallel::util::detail::unseq_loop_n::call( + first, count, f2); +#endif + } + + template + constexpr Iter sequential_generate_n_helper(Iter first, std::size_t count, + F&& f, /*is unsequenced*/ std::false_type) { return std::generate_n(first, count, f); } @@ -66,8 +80,9 @@ namespace hpx::parallel::detail { friend constexpr Iter tag_fallback_invoke(sequential_generate_n_t, ExPolicy&&, Iter first, std::size_t count, F&& f) { + using is_unseq = hpx::is_unsequenced_execution_policy; return sequential_generate_n_helper( - first, count, HPX_FORWARD(F, f)); + first, count, HPX_FORWARD(F, f), is_unseq()); } }; diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp new file mode 100644 index 000000000000..5d1a34072df3 --- /dev/null +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -0,0 +1,78 @@ +#pragma once + +#include +#include + +namespace hpx::parallel { namespace detail { + + template + struct fold_left : public algorithm, _T> + { + constexpr fold_left() noexcept + : algorithm("fold_left") + { + } + + template + HPX_HOST_DEVICE static constexpr T sequential( + ExPolicy&& policy, FwdIter first, Sent last, T&& init, F&& f) + { + if (first == last) + return HPX_MOVE(init); + T acc = HPX_MOVE(init); + while (first != last) + acc = HPX_MOVE(f(HPX_MOVE(acc), *first++)); + return HPX_MOVE(acc); + } + + template + static util::detail::algorithm_result_t parallel( + ExPolicy&& policy, FwdIter first, Sent last, T const& val) + { + return first; + } + }; + +}} // namespace hpx::parallel::detail + +namespace hpx { +inline constexpr struct fold_left_t final + : hpx::detail::tag_parallel_algorithm +{ +private: + template // TODO : add concept + friend + typename hpx::parallel::util::detail::algorithm_result::type + tag_fallback_invoke(fold_left_t, ExPolicy&& policy, FwdIter first, + FwdIter last, T&& init, F&& f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + using result_type = + typename hpx::parallel::util::detail::algorithm_result< + ExPolicy>::type; + + return hpx::util::void_guard(), + hpx::parallel::detail::fold_left().call( + HPX_FORWARD(ExPolicy, policy), first, last, + HPX_FORWARD(T, init), HPX_FORWARD(F, f)); + } + + template // TODO : add concept + // clang-format on + friend T tag_fallback_invoke( + fold_left_t, FwdIter first, FwdIter last, T&& init, F&& f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return hpx::parallel::detail::fold_left().call(hpx::execution::seq, + first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f)); + } +} fold_left{}; +} // namespace hpx From 6bcc03f75dd08879de00189b1a60940ee56a5dde Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Tue, 23 May 2023 09:58:04 -0500 Subject: [PATCH 02/32] added fold_left Signed-off-by: Hari Hara Naveen S --- .../parallel/algorithms/detail/generate.hpp | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/detail/generate.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/detail/generate.hpp index 6a591bd39866..c5cdba12f57a 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/detail/generate.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/detail/generate.hpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include @@ -54,20 +53,7 @@ namespace hpx::parallel::detail { /////////////////////////////////////////////////////////////////////////// template constexpr Iter sequential_generate_n_helper( - Iter first, std::size_t count, F&& f, /*is unsequenced*/ std::true_type) - { -#ifdef HPX_WITH_CXX20_STD_EXECUTION_POLICES // unseq execution was added in CXX20 - return std::generate_n(std::execution::unseq, first, count, f); -#else - auto f2 = [&](Iter it) { *it = f(); }; - return ::hpx::parallel::util::detail::unseq_loop_n::call( - first, count, f2); -#endif - } - - template - constexpr Iter sequential_generate_n_helper(Iter first, std::size_t count, - F&& f, /*is unsequenced*/ std::false_type) + Iter first, std::size_t count, F&& f) { return std::generate_n(first, count, f); } @@ -80,9 +66,8 @@ namespace hpx::parallel::detail { friend constexpr Iter tag_fallback_invoke(sequential_generate_n_t, ExPolicy&&, Iter first, std::size_t count, F&& f) { - using is_unseq = hpx::is_unsequenced_execution_policy; return sequential_generate_n_helper( - first, count, HPX_FORWARD(F, f), is_unseq()); + first, count, HPX_FORWARD(F, f)); } }; From 7bbd46510cebac73b91e1cf568d787802c1542a5 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Tue, 23 May 2023 17:57:40 -0500 Subject: [PATCH 03/32] fixed unused error, added parallel overload Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index 5d1a34072df3..122c3e478e03 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -16,14 +16,9 @@ namespace hpx::parallel { namespace detail { template HPX_HOST_DEVICE static constexpr T sequential( - ExPolicy&& policy, FwdIter first, Sent last, T&& init, F&& f) + ExPolicy&&, FwdIter first, Sent last, T&& init, F&& f) { - if (first == last) - return HPX_MOVE(init); - T acc = HPX_MOVE(init); - while (first != last) - acc = HPX_MOVE(f(HPX_MOVE(acc), *first++)); - return HPX_MOVE(acc); + hpx::reduce(first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f)); } template parallel( ExPolicy&& policy, FwdIter first, Sent last, T const& val) { - return first; + hpx::reduce(HPX_FORWARD(policy), first, last, HPX_FORWARD(T, init), + HPX_FORWARD(F, f)); } }; From 0011e7c234e9f72330624934c56a6199e8d289a2 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Tue, 23 May 2023 18:08:03 -0500 Subject: [PATCH 04/32] fixed var names Signed-off-by: Hari Hara Naveen S --- libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index 122c3e478e03..1c4190d4dc5e 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -24,9 +24,9 @@ namespace hpx::parallel { namespace detail { template static util::detail::algorithm_result_t parallel( - ExPolicy&& policy, FwdIter first, Sent last, T const& val) + ExPolicy&& policy, FwdIter first, Sent last, T&& init, F&& f) { - hpx::reduce(HPX_FORWARD(policy), first, last, HPX_FORWARD(T, init), + hpx::reduce(HPX_FORWARD(ExPolicy, policy), first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f)); } }; From 68ddc70c1fa74ae1d19e677039d3b52d78bcfce9 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Tue, 23 May 2023 18:10:42 -0500 Subject: [PATCH 05/32] forgot template name Signed-off-by: Hari Hara Naveen S --- libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index 1c4190d4dc5e..df135c0ff133 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -22,7 +22,7 @@ namespace hpx::parallel { namespace detail { } template + typename T, typename F> static util::detail::algorithm_result_t parallel( ExPolicy&& policy, FwdIter first, Sent last, T&& init, F&& f) { From eb4cd7e5af2ef94f67c37f8baf301e9db5653beb Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Wed, 24 May 2023 00:14:28 -0500 Subject: [PATCH 06/32] changed var name to adhere to convention, clang-format Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index df135c0ff133..5aeab14c1f8c 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -5,11 +5,11 @@ namespace hpx::parallel { namespace detail { - template - struct fold_left : public algorithm, _T> + template + struct fold_left : public algorithm, T_> { constexpr fold_left() noexcept - : algorithm("fold_left") + : algorithm("fold_left") { } @@ -26,8 +26,8 @@ namespace hpx::parallel { namespace detail { static util::detail::algorithm_result_t parallel( ExPolicy&& policy, FwdIter first, Sent last, T&& init, F&& f) { - hpx::reduce(HPX_FORWARD(ExPolicy, policy), first, last, HPX_FORWARD(T, init), - HPX_FORWARD(F, f)); + hpx::reduce(HPX_FORWARD(ExPolicy, policy), first, last, + HPX_FORWARD(T, init), HPX_FORWARD(F, f)); } }; @@ -43,7 +43,7 @@ inline constexpr struct fold_left_t final friend typename hpx::parallel::util::detail::algorithm_result::type tag_fallback_invoke(fold_left_t, ExPolicy&& policy, FwdIter first, - FwdIter last, T&& init, F&& f) + FwdIter last, T init, F f) { static_assert(hpx::traits::is_forward_iterator_v, "Requires at least forward iterator."); @@ -62,7 +62,7 @@ inline constexpr struct fold_left_t final typename F> // TODO : add concept // clang-format on friend T tag_fallback_invoke( - fold_left_t, FwdIter first, FwdIter last, T&& init, F&& f) + fold_left_t, FwdIter first, FwdIter last, T init, F f) { static_assert(hpx::traits::is_forward_iterator_v, "Requires at least forward iterator."); From 6ec4f4c24c4e5ac8370663861b87073871bc5b03 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Wed, 24 May 2023 15:26:36 -0500 Subject: [PATCH 07/32] fold_leftfirst added Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 92 ++++++++++++++++++- 1 file changed, 90 insertions(+), 2 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index 5aeab14c1f8c..944112590e97 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -3,6 +3,10 @@ #include #include + +#include +#include + namespace hpx::parallel { namespace detail { template @@ -18,7 +22,7 @@ namespace hpx::parallel { namespace detail { HPX_HOST_DEVICE static constexpr T sequential( ExPolicy&&, FwdIter first, Sent last, T&& init, F&& f) { - hpx::reduce(first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f)); + hpx::reduce(first, last, init, f); } template // TODO : add concept - // clang-format on friend T tag_fallback_invoke( fold_left_t, FwdIter first, FwdIter last, T init, F f) { @@ -72,3 +75,88 @@ inline constexpr struct fold_left_t final } } fold_left{}; } // namespace hpx + +namespace hpx::parallel { namespace detail { + + template + struct fold_left_first : public algorithm, T_> + { + constexpr fold_left_first() noexcept + : algorithm("fold_left_first") + { + } + + template + HPX_HOST_DEVICE static constexpr auto sequential( + ExPolicy&&, FwdIter first, Sent last, F&& f) + { + using U = decltype(hpx::fold_left( + std::move(first), last, ::hpx::traits::iter_value_t(*first), f)); + if (first == last) + return std::optional(); + + ::hpx::traits::iter_value_t init = *first; + std::advance(first, 1); + return hpx::fold_left(first, last, init, HPX_FORWARD(F, f)); + } + + template + static util::detail::algorithm_result_t parallel( + ExPolicy&& policy, FwdIter first, Sent last, F&& f) + { + using U = decltype(hpx::fold_left( + std::move(first), last, ::hpx::traits::iter_value_t(*first), f)); + if (first == last) + return std::optional(); + + ::hpx::traits::iter_value_t init = *first; + std::advance(first, 1); + return hpx::fold_left(HPX_FORWARD(ExPolicy, policy), first, last, + init, HPX_FORWARD(F, f)); + } + }; + +}} // namespace hpx::parallel::detail + +namespace hpx { +inline constexpr struct fold_left_first_t final + : hpx::detail::tag_parallel_algorithm +{ +private: + template // TODO : add concept + friend + typename hpx::parallel::util::detail::algorithm_result::type + tag_fallback_invoke(fold_left_first_t, ExPolicy&& policy, FwdIter first, + FwdIter last, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + using result_type = + typename hpx::parallel::util::detail::algorithm_result< + ExPolicy>::type; + + return hpx::util::void_guard(), + hpx::parallel::detail::fold_left_first().call( + HPX_FORWARD(ExPolicy, policy), first, last, + HPX_FORWARD(F, f)); + } + + template // TODO : add concept + friend auto tag_fallback_invoke( + fold_left_first_t, FwdIter first, FwdIter last, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + using U = decltype(hpx::fold_left( + std::move(first), last, ::hpx::traits::iter_value_t(*first), f)); + + return hpx::parallel::detail::fold_left_first>().call( + hpx::execution::seq, first, last, HPX_FORWARD(F, f)); + } +} fold_left_first{}; +} // namespace hpx From f0ebc37b2dbbf366df9cec1ba3e24c144da33c2d Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Fri, 26 May 2023 06:06:51 -0500 Subject: [PATCH 08/32] fold_left_first implemented Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 187 ++++++++++-------- libs/core/datastructures/CMakeLists.txt | 1 + .../hpx/datastructures/in_value_result.hpp | 22 +++ 3 files changed, 124 insertions(+), 86 deletions(-) create mode 100644 libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index 944112590e97..31ec8538692f 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -2,10 +2,10 @@ #include #include - +#include #include -#include +#include namespace hpx::parallel { namespace detail { @@ -22,58 +22,62 @@ namespace hpx::parallel { namespace detail { HPX_HOST_DEVICE static constexpr T sequential( ExPolicy&&, FwdIter first, Sent last, T&& init, F&& f) { - hpx::reduce(first, last, init, f); + if (first == last) + return HPX_MOVE(init); + T acc = HPX_MOVE(init); + while (first != last) + acc = HPX_MOVE(f(HPX_MOVE(acc), *first++)); + return HPX_MOVE(acc); } template - static util::detail::algorithm_result_t parallel( + static constexpr auto parallel( ExPolicy&& policy, FwdIter first, Sent last, T&& init, F&& f) { - hpx::reduce(HPX_FORWARD(ExPolicy, policy), first, last, +#ifdef HPX_WITH_CXX17_STD_EXECUTION_POLICES + return std::reduce(HPX_FORWARD(ExPolicy, policy), first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f)); +#else + return std::reduce( + first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f)); +#endif } }; }} // namespace hpx::parallel::detail namespace hpx { -inline constexpr struct fold_left_t final - : hpx::detail::tag_parallel_algorithm -{ -private: - template // TODO : add concept - friend - typename hpx::parallel::util::detail::algorithm_result::type - tag_fallback_invoke(fold_left_t, ExPolicy&& policy, FwdIter first, - FwdIter last, T init, F f) + inline constexpr struct fold_left_t final + : hpx::detail::tag_parallel_algorithm { - static_assert(hpx::traits::is_forward_iterator_v, - "Requires at least forward iterator."); - - using result_type = - typename hpx::parallel::util::detail::algorithm_result< - ExPolicy>::type; - - return hpx::util::void_guard(), - hpx::parallel::detail::fold_left().call( - HPX_FORWARD(ExPolicy, policy), first, last, - HPX_FORWARD(T, init), HPX_FORWARD(F, f)); - } - - template // TODO : add concept - friend T tag_fallback_invoke( - fold_left_t, FwdIter first, FwdIter last, T init, F f) - { - static_assert(hpx::traits::is_forward_iterator_v, - "Requires at least forward iterator."); + private: + template // TODO : add concept + friend T tag_fallback_invoke(fold_left_t, ExPolicy&& policy, + FwdIter first, FwdIter last, T init, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return hpx::parallel::detail::fold_left().call( + HPX_FORWARD(ExPolicy, policy), first, last, + HPX_FORWARD(T, init), HPX_FORWARD(F, f)); + } - return hpx::parallel::detail::fold_left().call(hpx::execution::seq, - first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f)); - } -} fold_left{}; + template // TODO : add concept + friend T tag_fallback_invoke( + fold_left_t, FwdIter first, FwdIter last, T init, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return hpx::parallel::detail::fold_left().call( + hpx::execution::seq, first, last, HPX_FORWARD(T, init), + HPX_FORWARD(F, f)); + } + } fold_left{}; } // namespace hpx namespace hpx::parallel { namespace detail { @@ -91,72 +95,83 @@ namespace hpx::parallel { namespace detail { HPX_HOST_DEVICE static constexpr auto sequential( ExPolicy&&, FwdIter first, Sent last, F&& f) { - using U = decltype(hpx::fold_left( - std::move(first), last, ::hpx::traits::iter_value_t(*first), f)); + using T = ::hpx::traits::iter_value_t; + using U = + decltype(hpx::fold_left(std::move(first), last, T(*first), f)); + if (first == last) - return std::optional(); + return hpx::optional(); + + T init = *first; - ::hpx::traits::iter_value_t init = *first; std::advance(first, 1); - return hpx::fold_left(first, last, init, HPX_FORWARD(F, f)); + + return hpx::optional( + hpx::parallel::detail::fold_left().call(hpx::execution::seq, + first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f))); } template - static util::detail::algorithm_result_t parallel( + static constexpr auto parallel( ExPolicy&& policy, FwdIter first, Sent last, F&& f) { - using U = decltype(hpx::fold_left( - std::move(first), last, ::hpx::traits::iter_value_t(*first), f)); + using T = ::hpx::traits::iter_value_t; + using U = + decltype(hpx::fold_left(std::move(first), last, T(*first), f)); + if (first == last) - return std::optional(); + return hpx::optional(); + + T init = *first; - ::hpx::traits::iter_value_t init = *first; std::advance(first, 1); - return hpx::fold_left(HPX_FORWARD(ExPolicy, policy), first, last, - init, HPX_FORWARD(F, f)); + + return hpx::optional(hpx::parallel::detail::fold_left().call( + HPX_FORWARD(ExPolicy, policy), first, last, + HPX_FORWARD(T, init), HPX_FORWARD(F, f))); } }; }} // namespace hpx::parallel::detail namespace hpx { -inline constexpr struct fold_left_first_t final - : hpx::detail::tag_parallel_algorithm -{ -private: - template // TODO : add concept - friend - typename hpx::parallel::util::detail::algorithm_result::type - tag_fallback_invoke(fold_left_first_t, ExPolicy&& policy, FwdIter first, - FwdIter last, F f) + inline constexpr struct fold_left_first_t final + : hpx::detail::tag_parallel_algorithm { - static_assert(hpx::traits::is_forward_iterator_v, - "Requires at least forward iterator."); - - using result_type = - typename hpx::parallel::util::detail::algorithm_result< - ExPolicy>::type; - - return hpx::util::void_guard(), - hpx::parallel::detail::fold_left_first().call( - HPX_FORWARD(ExPolicy, policy), first, last, - HPX_FORWARD(F, f)); - } - - template // TODO : add concept - friend auto tag_fallback_invoke( - fold_left_first_t, FwdIter first, FwdIter last, F f) - { - static_assert(hpx::traits::is_forward_iterator_v, - "Requires at least forward iterator."); + private: + template // TODO : add concept + friend auto tag_fallback_invoke(fold_left_first_t, ExPolicy&& policy, + FwdIter first, FwdIter last, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + using result_type = + typename hpx::parallel::util::detail::algorithm_result< + ExPolicy>::type; - using U = decltype(hpx::fold_left( - std::move(first), last, ::hpx::traits::iter_value_t(*first), f)); + using U = decltype(hpx::fold_left(std::move(first), last, + ::hpx::traits::iter_value_t(*first), f)); - return hpx::parallel::detail::fold_left_first>().call( - hpx::execution::seq, first, last, HPX_FORWARD(F, f)); - } -} fold_left_first{}; + return hpx::parallel::detail::fold_left_first>() + .call(HPX_FORWARD(ExPolicy, policy), first, last, + HPX_FORWARD(F, f)); + } + + template // TODO : add concept + friend auto tag_fallback_invoke( + fold_left_first_t, FwdIter first, FwdIter last, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + using U = decltype(hpx::fold_left(std::move(first), last, + ::hpx::traits::iter_value_t(*first), f)); + + return hpx::parallel::detail::fold_left_first>() + .call(hpx::execution::seq, first, last, HPX_FORWARD(F, f)); + } + } fold_left_first{}; } // namespace hpx diff --git a/libs/core/datastructures/CMakeLists.txt b/libs/core/datastructures/CMakeLists.txt index f593e4153027..6f92700fdc06 100644 --- a/libs/core/datastructures/CMakeLists.txt +++ b/libs/core/datastructures/CMakeLists.txt @@ -55,6 +55,7 @@ set(datastructures_headers hpx/datastructures/member_pack.hpp hpx/datastructures/optional.hpp hpx/datastructures/tuple.hpp + hpx/datastructures/in_value_result.hpp hpx/datastructures/traits/supports_streaming_with_any.hpp hpx/datastructures/traits/is_tuple_like.hpp hpx/datastructures/variant.hpp diff --git a/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp b/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp new file mode 100644 index 000000000000..1e2bffbee26e --- /dev/null +++ b/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp @@ -0,0 +1,22 @@ +namespace hpx::experimental { namespace ranges { + template + struct in_value_result + { + [[no_unique_address]] Iter in; + [[no_unique_address]] T value; + + template + // hpx::convertible? + constexpr operator in_value_result() const& + { + return {in, value}; + } + + template + // hpx::convertible? + constexpr operator in_value_result() && + { + return {std::move(in), std::move(value)}; + } + }; +}} // namespace hpx::experimental::ranges \ No newline at end of file From 5d289f414819662ce0127d9bddaf2e32cfca2356 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sat, 27 May 2023 11:23:15 -0500 Subject: [PATCH 09/32] fold_right seq implemented, par remaining Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 185 ++++++++++++------ .../hpx/datastructures/in_value_result.hpp | 2 + 2 files changed, 129 insertions(+), 58 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index 31ec8538692f..7a7b1a703aa1 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -1,8 +1,8 @@ #pragma once #include -#include #include +#include #include #include @@ -48,36 +48,35 @@ namespace hpx::parallel { namespace detail { }} // namespace hpx::parallel::detail namespace hpx { - inline constexpr struct fold_left_t final - : hpx::detail::tag_parallel_algorithm +inline constexpr struct fold_left_t final + : hpx::detail::tag_parallel_algorithm +{ +private: + template // TODO : add concept + friend T tag_fallback_invoke(fold_left_t, ExPolicy&& policy, FwdIter first, + FwdIter last, T init, F f) { - private: - template // TODO : add concept - friend T tag_fallback_invoke(fold_left_t, ExPolicy&& policy, - FwdIter first, FwdIter last, T init, F f) - { - static_assert(hpx::traits::is_forward_iterator_v, - "Requires at least forward iterator."); - - return hpx::parallel::detail::fold_left().call( - HPX_FORWARD(ExPolicy, policy), first, last, - HPX_FORWARD(T, init), HPX_FORWARD(F, f)); - } - - template // TODO : add concept - friend T tag_fallback_invoke( - fold_left_t, FwdIter first, FwdIter last, T init, F f) - { - static_assert(hpx::traits::is_forward_iterator_v, - "Requires at least forward iterator."); + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return hpx::parallel::detail::fold_left().call( + HPX_FORWARD(ExPolicy, policy), first, last, HPX_FORWARD(T, init), + HPX_FORWARD(F, f)); + } + + template // TODO : add concept + friend T tag_fallback_invoke( + fold_left_t, FwdIter first, FwdIter last, T init, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); - return hpx::parallel::detail::fold_left().call( - hpx::execution::seq, first, last, HPX_FORWARD(T, init), - HPX_FORWARD(F, f)); - } - } fold_left{}; + return hpx::parallel::detail::fold_left().call(hpx::execution::seq, + first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f)); + } +} fold_left{}; } // namespace hpx namespace hpx::parallel { namespace detail { @@ -136,42 +135,112 @@ namespace hpx::parallel { namespace detail { }} // namespace hpx::parallel::detail namespace hpx { - inline constexpr struct fold_left_first_t final - : hpx::detail::tag_parallel_algorithm +inline constexpr struct fold_left_first_t final + : hpx::detail::tag_parallel_algorithm +{ +private: + template // TODO : add concept + friend auto tag_fallback_invoke( + fold_left_first_t, ExPolicy&& policy, FwdIter first, FwdIter last, F f) { - private: - template // TODO : add concept - friend auto tag_fallback_invoke(fold_left_first_t, ExPolicy&& policy, - FwdIter first, FwdIter last, F f) - { - static_assert(hpx::traits::is_forward_iterator_v, - "Requires at least forward iterator."); + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); - using result_type = - typename hpx::parallel::util::detail::algorithm_result< - ExPolicy>::type; + using result_type = + typename hpx::parallel::util::detail::algorithm_result< + ExPolicy>::type; - using U = decltype(hpx::fold_left(std::move(first), last, - ::hpx::traits::iter_value_t(*first), f)); + using U = decltype(hpx::fold_left(std::move(first), last, + ::hpx::traits::iter_value_t(*first), f)); - return hpx::parallel::detail::fold_left_first>() - .call(HPX_FORWARD(ExPolicy, policy), first, last, - HPX_FORWARD(F, f)); - } + return hpx::parallel::detail::fold_left_first>().call( + HPX_FORWARD(ExPolicy, policy), first, last, HPX_FORWARD(F, f)); + } + + template // TODO : add concept + friend auto tag_fallback_invoke( + fold_left_first_t, FwdIter first, FwdIter last, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + using U = decltype(hpx::fold_left(std::move(first), last, + ::hpx::traits::iter_value_t(*first), f)); - template // TODO : add concept - friend auto tag_fallback_invoke( - fold_left_first_t, FwdIter first, FwdIter last, F f) + return hpx::parallel::detail::fold_left_first>().call( + hpx::execution::seq, first, last, HPX_FORWARD(F, f)); + } +} fold_left_first{}; +} // namespace hpx + +namespace hpx::parallel { namespace detail { + + template + struct fold_right : public algorithm, T_> + { + constexpr fold_right() noexcept + : algorithm("fold_right") { - static_assert(hpx::traits::is_forward_iterator_v, - "Requires at least forward iterator."); + } - using U = decltype(hpx::fold_left(std::move(first), last, - ::hpx::traits::iter_value_t(*first), f)); + template + HPX_HOST_DEVICE static constexpr auto sequential( + ExPolicy&&, FwdIter first, Sent last, T&& init, F&& f) + { + using U = std::decay_t< + std::invoke_result_t, T>>; + if (first == last) + return U(std::move(init)); + + U accum = f(*--last, std::move(init)); + while (first != last) + accum = f(*--last, std::move(accum)); + return accum; + } - return hpx::parallel::detail::fold_left_first>() - .call(hpx::execution::seq, first, last, HPX_FORWARD(F, f)); + template + static constexpr auto parallel( + ExPolicy&& policy, FwdIter first, Sent last, T&& init, F&& f) + { + exit( + 1); // parallel version of fold_right has not been implemented + return f(first, init); } - } fold_left_first{}; + }; + +}} // namespace hpx::parallel::detail + +namespace hpx { +inline constexpr struct fold_right_t final + : hpx::detail::tag_parallel_algorithm +{ +private: + template // TODO : add concept + friend T tag_fallback_invoke(fold_right_t, ExPolicy&& policy, FwdIter first, + FwdIter last, T init, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return hpx::parallel::detail::fold_right().call( + HPX_FORWARD(ExPolicy, policy), first, last, HPX_FORWARD(T, init), + HPX_FORWARD(F, f)); + } + + template // TODO : add concept + friend T tag_fallback_invoke( + fold_right_t, FwdIter first, FwdIter last, T init, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return hpx::parallel::detail::fold_right().call(hpx::execution::seq, + first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f)); + } +} fold_right{}; } // namespace hpx diff --git a/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp b/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp index 1e2bffbee26e..9d0c52d60418 100644 --- a/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp +++ b/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp @@ -1,3 +1,5 @@ +#pragma once + namespace hpx::experimental { namespace ranges { template struct in_value_result From 9a93cd162057971aaa254198a3072ad90033b9c9 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sat, 27 May 2023 11:24:53 -0500 Subject: [PATCH 10/32] changed std::move to hpHPX_MOVE Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index 7a7b1a703aa1..51231316df9f 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -96,7 +96,7 @@ namespace hpx::parallel { namespace detail { { using T = ::hpx::traits::iter_value_t; using U = - decltype(hpx::fold_left(std::move(first), last, T(*first), f)); + decltype(hpx::fold_left(HPX_MOVE(first), last, T(*first), f)); if (first == last) return hpx::optional(); @@ -117,7 +117,7 @@ namespace hpx::parallel { namespace detail { { using T = ::hpx::traits::iter_value_t; using U = - decltype(hpx::fold_left(std::move(first), last, T(*first), f)); + decltype(hpx::fold_left(HPX_MOVE(first), last, T(*first), f)); if (first == last) return hpx::optional(); @@ -151,7 +151,7 @@ inline constexpr struct fold_left_first_t final typename hpx::parallel::util::detail::algorithm_result< ExPolicy>::type; - using U = decltype(hpx::fold_left(std::move(first), last, + using U = decltype(hpx::fold_left(HPX_MOVE(first), last, ::hpx::traits::iter_value_t(*first), f)); return hpx::parallel::detail::fold_left_first>().call( @@ -165,7 +165,7 @@ inline constexpr struct fold_left_first_t final static_assert(hpx::traits::is_forward_iterator_v, "Requires at least forward iterator."); - using U = decltype(hpx::fold_left(std::move(first), last, + using U = decltype(hpx::fold_left(HPX_MOVE(first), last, ::hpx::traits::iter_value_t(*first), f)); return hpx::parallel::detail::fold_left_first>().call( @@ -192,11 +192,11 @@ namespace hpx::parallel { namespace detail { using U = std::decay_t< std::invoke_result_t, T>>; if (first == last) - return U(std::move(init)); + return U(HPX_MOVE(init)); - U accum = f(*--last, std::move(init)); + U accum = f(*--last, HPX_MOVE(init)); while (first != last) - accum = f(*--last, std::move(accum)); + accum = f(*--last, HPX_MOVE(accum)); return accum; } From 2f9efe862bc12c9105650d48363b56f7bbb43114 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sat, 27 May 2023 11:26:27 -0500 Subject: [PATCH 11/32] clang-format Signed-off-by: Hari Hara Naveen S --- .../algorithms/include/hpx/parallel/algorithms/fold.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index 51231316df9f..ec133a437b24 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -189,11 +189,11 @@ namespace hpx::parallel { namespace detail { HPX_HOST_DEVICE static constexpr auto sequential( ExPolicy&&, FwdIter first, Sent last, T&& init, F&& f) { - using U = std::decay_t< - std::invoke_result_t, T>>; + using U = std::decay_t, T>>; if (first == last) return U(HPX_MOVE(init)); - + U accum = f(*--last, HPX_MOVE(init)); while (first != last) accum = f(*--last, HPX_MOVE(accum)); From f08d599f44037391751d13c847a550db2fcfe9de Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sun, 28 May 2023 04:55:39 -0500 Subject: [PATCH 12/32] fixed unused errors Signed-off-by: Hari Hara Naveen S --- .../algorithms/include/hpx/parallel/algorithms/fold.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index ec133a437b24..267dde5481c1 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -39,6 +39,7 @@ namespace hpx::parallel { namespace detail { return std::reduce(HPX_FORWARD(ExPolicy, policy), first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f)); #else + HPX_UNUSED(policy); // TODO : par support return std::reduce( first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f)); #endif @@ -205,6 +206,12 @@ namespace hpx::parallel { namespace detail { static constexpr auto parallel( ExPolicy&& policy, FwdIter first, Sent last, T&& init, F&& f) { + HPX_UNUSED(policy); + HPX_UNUSED(first); + HPX_UNUSED(last); + HPX_UNUSED(init); + HPX_UNUSED(f); + exit( 1); // parallel version of fold_right has not been implemented return f(first, init); From 3928743cbdcdbcd254798008a98f7f1e890d1921 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Wed, 31 May 2023 03:27:54 -0500 Subject: [PATCH 13/32] fixed few inspect errors Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 10 +++------- .../include/hpx/datastructures/in_value_result.hpp | 4 +++- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index 267dde5481c1..eb0e9947d887 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -26,7 +26,7 @@ namespace hpx::parallel { namespace detail { return HPX_MOVE(init); T acc = HPX_MOVE(init); while (first != last) - acc = HPX_MOVE(f(HPX_MOVE(acc), *first++)); + acc = f(HPX_MOVE(acc), *first++); return HPX_MOVE(acc); } @@ -148,10 +148,6 @@ inline constexpr struct fold_left_first_t final static_assert(hpx::traits::is_forward_iterator_v, "Requires at least forward iterator."); - using result_type = - typename hpx::parallel::util::detail::algorithm_result< - ExPolicy>::type; - using U = decltype(hpx::fold_left(HPX_MOVE(first), last, ::hpx::traits::iter_value_t(*first), f)); @@ -212,8 +208,8 @@ namespace hpx::parallel { namespace detail { HPX_UNUSED(init); HPX_UNUSED(f); - exit( - 1); // parallel version of fold_right has not been implemented + // parallel version of fold_right has not been implemented + exit(1); return f(first, init); } }; diff --git a/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp b/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp index 9d0c52d60418..f1a561a1445e 100644 --- a/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp +++ b/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp @@ -1,4 +1,6 @@ #pragma once +#include + namespace hpx::experimental { namespace ranges { template @@ -18,7 +20,7 @@ namespace hpx::experimental { namespace ranges { // hpx::convertible? constexpr operator in_value_result() && { - return {std::move(in), std::move(value)}; + return {HPX_MOVE(in), HPX_MOVE(value)}; } }; }} // namespace hpx::experimental::ranges \ No newline at end of file From f2a7f4a28dd948cccaa974f0a6c62cae28b0051a Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sun, 4 Jun 2023 19:47:21 -0500 Subject: [PATCH 14/32] fixed licenses, clang-format Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 6 ++++++ .../include/hpx/datastructures/in_value_result.hpp | 12 ++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index eb0e9947d887..46edf8c24bcc 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -1,3 +1,9 @@ +// Copyright (c) 2020-2022 STE||AR Group +// +// SPDX-License-Identifier: BSL-1.0 +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + #pragma once #include diff --git a/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp b/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp index f1a561a1445e..2e6c9a313fb7 100644 --- a/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp +++ b/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp @@ -1,6 +1,12 @@ +// Copyright (c) 2020-2022 STE||AR Group +// +// SPDX-License-Identifier: BSL-1.0 +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + #pragma once -#include +#include namespace hpx::experimental { namespace ranges { template @@ -10,17 +16,15 @@ namespace hpx::experimental { namespace ranges { [[no_unique_address]] T value; template - // hpx::convertible? constexpr operator in_value_result() const& { return {in, value}; } template - // hpx::convertible? constexpr operator in_value_result() && { return {HPX_MOVE(in), HPX_MOVE(value)}; } }; -}} // namespace hpx::experimental::ranges \ No newline at end of file +}} // namespace hpx::experimental::ranges From e3a89cae31fff45c6d3e88fda5c5d35f57ed308f Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Tue, 6 Jun 2023 17:45:30 -0500 Subject: [PATCH 15/32] moved in_value_result to parallel/algorithms/util Signed-off-by: Hari Hara Naveen S --- .../hpx/parallel/util/result_types.hpp | 20 +++++++++++++ libs/core/datastructures/CMakeLists.txt | 1 - .../hpx/datastructures/in_value_result.hpp | 30 ------------------- 3 files changed, 20 insertions(+), 31 deletions(-) delete mode 100644 libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp diff --git a/libs/core/algorithms/include/hpx/parallel/util/result_types.hpp b/libs/core/algorithms/include/hpx/parallel/util/result_types.hpp index c4a8c0e1374a..ae2577e69394 100644 --- a/libs/core/algorithms/include/hpx/parallel/util/result_types.hpp +++ b/libs/core/algorithms/include/hpx/parallel/util/result_types.hpp @@ -101,6 +101,25 @@ namespace hpx::parallel::util { } }; + template + struct in_value_result + { + HPX_NO_UNIQUE_ADDRESS Iter in; + HPX_NO_UNIQUE_ADDRESS T value; + + template + constexpr operator in_value_result() const& + { + return {in, value}; + } + + template + constexpr operator in_value_result() && + { + return {HPX_MOVE(in), HPX_MOVE(value)}; + } + }; + /////////////////////////////////////////////////////////////////////// template std::pair get_pair(util::in_out_result&& p) @@ -496,6 +515,7 @@ namespace hpx::ranges { using hpx::parallel::util::in_in_result; using hpx::parallel::util::in_out_out_result; using hpx::parallel::util::in_out_result; + using hpx::parallel::util::in_value_result; using hpx::parallel::util::min_max_result; } // namespace hpx::ranges // namespace hpx::ranges diff --git a/libs/core/datastructures/CMakeLists.txt b/libs/core/datastructures/CMakeLists.txt index 6f92700fdc06..f593e4153027 100644 --- a/libs/core/datastructures/CMakeLists.txt +++ b/libs/core/datastructures/CMakeLists.txt @@ -55,7 +55,6 @@ set(datastructures_headers hpx/datastructures/member_pack.hpp hpx/datastructures/optional.hpp hpx/datastructures/tuple.hpp - hpx/datastructures/in_value_result.hpp hpx/datastructures/traits/supports_streaming_with_any.hpp hpx/datastructures/traits/is_tuple_like.hpp hpx/datastructures/variant.hpp diff --git a/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp b/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp deleted file mode 100644 index 2e6c9a313fb7..000000000000 --- a/libs/core/datastructures/include/hpx/datastructures/in_value_result.hpp +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) 2020-2022 STE||AR Group -// -// SPDX-License-Identifier: BSL-1.0 -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#pragma once - -#include - -namespace hpx::experimental { namespace ranges { - template - struct in_value_result - { - [[no_unique_address]] Iter in; - [[no_unique_address]] T value; - - template - constexpr operator in_value_result() const& - { - return {in, value}; - } - - template - constexpr operator in_value_result() && - { - return {HPX_MOVE(in), HPX_MOVE(value)}; - } - }; -}} // namespace hpx::experimental::ranges From c6a229de4da401ca87f55010d51e7c79f067ae26 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sat, 10 Jun 2023 04:27:18 -0500 Subject: [PATCH 16/32] using util loo instead of std::reduce Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index 46edf8c24bcc..c0615deb9e75 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include @@ -28,12 +28,10 @@ namespace hpx::parallel { namespace detail { HPX_HOST_DEVICE static constexpr T sequential( ExPolicy&&, FwdIter first, Sent last, T&& init, F&& f) { - if (first == last) - return HPX_MOVE(init); - T acc = HPX_MOVE(init); - while (first != last) - acc = f(HPX_MOVE(acc), *first++); - return HPX_MOVE(acc); + util::loop_ind( + first, last, [&init, &f](auto const& it) mutable { + init = HPX_INVOKE(f, HPX_MOVE(init), it); + }); } template ( + first, last, [&init, &f](auto const& it) mutable { + init = HPX_INVOKE(f, HPX_MOVE(init), it); + }); } }; From f6e22c7c6830c369055735c537a270485e469e60 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sat, 10 Jun 2023 05:19:50 -0500 Subject: [PATCH 17/32] fixed unused error Signed-off-by: Hari Hara Naveen S --- libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index c0615deb9e75..b93d10e5816c 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -37,7 +37,7 @@ namespace hpx::parallel { namespace detail { template static constexpr auto parallel( - ExPolicy&& policy, FwdIter first, Sent last, T&& init, F&& f) + ExPolicy&&, FwdIter first, Sent last, T&& init, F&& f) { util::loop_ind( first, last, [&init, &f](auto const& it) mutable { From 9b0c65ac6f630e8fb3346517b962e0b8cd0288df Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sat, 10 Jun 2023 15:50:24 -0500 Subject: [PATCH 18/32] fixed clang-format buug, avoid namespace foo { namespace bar Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 177 +++++++++--------- 1 file changed, 90 insertions(+), 87 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index b93d10e5816c..e1818e269f63 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -13,7 +13,7 @@ #include #include -namespace hpx::parallel { namespace detail { +namespace hpx::parallel::detail { template struct fold_left : public algorithm, T_> @@ -46,41 +46,42 @@ namespace hpx::parallel { namespace detail { } }; -}} // namespace hpx::parallel::detail +} // namespace hpx::parallel::detail namespace hpx { -inline constexpr struct fold_left_t final - : hpx::detail::tag_parallel_algorithm -{ -private: - template // TODO : add concept - friend T tag_fallback_invoke(fold_left_t, ExPolicy&& policy, FwdIter first, - FwdIter last, T init, F f) + inline constexpr struct fold_left_t final + : hpx::detail::tag_parallel_algorithm { - static_assert(hpx::traits::is_forward_iterator_v, - "Requires at least forward iterator."); - - return hpx::parallel::detail::fold_left().call( - HPX_FORWARD(ExPolicy, policy), first, last, HPX_FORWARD(T, init), - HPX_FORWARD(F, f)); - } - - template // TODO : add concept - friend T tag_fallback_invoke( - fold_left_t, FwdIter first, FwdIter last, T init, F f) - { - static_assert(hpx::traits::is_forward_iterator_v, - "Requires at least forward iterator."); + private: + template // TODO : add concept + friend T tag_fallback_invoke(fold_left_t, ExPolicy&& policy, + FwdIter first, FwdIter last, T init, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return hpx::parallel::detail::fold_left().call( + HPX_FORWARD(ExPolicy, policy), first, last, + HPX_FORWARD(T, init), HPX_FORWARD(F, f)); + } - return hpx::parallel::detail::fold_left().call(hpx::execution::seq, - first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f)); - } -} fold_left{}; + template // TODO : add concept + friend T tag_fallback_invoke( + fold_left_t, FwdIter first, FwdIter last, T init, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return hpx::parallel::detail::fold_left().call( + hpx::execution::seq, first, last, HPX_FORWARD(T, init), + HPX_FORWARD(F, f)); + } + } fold_left{}; } // namespace hpx -namespace hpx::parallel { namespace detail { +namespace hpx::parallel::detail { template struct fold_left_first : public algorithm, T_> @@ -133,45 +134,46 @@ namespace hpx::parallel { namespace detail { } }; -}} // namespace hpx::parallel::detail +} // namespace hpx::parallel::detail namespace hpx { -inline constexpr struct fold_left_first_t final - : hpx::detail::tag_parallel_algorithm -{ -private: - template // TODO : add concept - friend auto tag_fallback_invoke( - fold_left_first_t, ExPolicy&& policy, FwdIter first, FwdIter last, F f) + inline constexpr struct fold_left_first_t final + : hpx::detail::tag_parallel_algorithm { - static_assert(hpx::traits::is_forward_iterator_v, - "Requires at least forward iterator."); + private: + template // TODO : add concept + friend auto tag_fallback_invoke(fold_left_first_t, ExPolicy&& policy, + FwdIter first, FwdIter last, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); - using U = decltype(hpx::fold_left(HPX_MOVE(first), last, - ::hpx::traits::iter_value_t(*first), f)); + using U = decltype(hpx::fold_left(HPX_MOVE(first), last, + ::hpx::traits::iter_value_t(*first), f)); - return hpx::parallel::detail::fold_left_first>().call( - HPX_FORWARD(ExPolicy, policy), first, last, HPX_FORWARD(F, f)); - } + return hpx::parallel::detail::fold_left_first>() + .call(HPX_FORWARD(ExPolicy, policy), first, last, + HPX_FORWARD(F, f)); + } - template // TODO : add concept - friend auto tag_fallback_invoke( - fold_left_first_t, FwdIter first, FwdIter last, F f) - { - static_assert(hpx::traits::is_forward_iterator_v, - "Requires at least forward iterator."); + template // TODO : add concept + friend auto tag_fallback_invoke( + fold_left_first_t, FwdIter first, FwdIter last, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); - using U = decltype(hpx::fold_left(HPX_MOVE(first), last, - ::hpx::traits::iter_value_t(*first), f)); + using U = decltype(hpx::fold_left(HPX_MOVE(first), last, + ::hpx::traits::iter_value_t(*first), f)); - return hpx::parallel::detail::fold_left_first>().call( - hpx::execution::seq, first, last, HPX_FORWARD(F, f)); - } -} fold_left_first{}; + return hpx::parallel::detail::fold_left_first>() + .call(hpx::execution::seq, first, last, HPX_FORWARD(F, f)); + } + } fold_left_first{}; } // namespace hpx -namespace hpx::parallel { namespace detail { +namespace hpx::parallel::detail { template struct fold_right : public algorithm, T_> @@ -214,36 +216,37 @@ namespace hpx::parallel { namespace detail { } }; -}} // namespace hpx::parallel::detail +} // namespace hpx::parallel::detail namespace hpx { -inline constexpr struct fold_right_t final - : hpx::detail::tag_parallel_algorithm -{ -private: - template // TODO : add concept - friend T tag_fallback_invoke(fold_right_t, ExPolicy&& policy, FwdIter first, - FwdIter last, T init, F f) - { - static_assert(hpx::traits::is_forward_iterator_v, - "Requires at least forward iterator."); - - return hpx::parallel::detail::fold_right().call( - HPX_FORWARD(ExPolicy, policy), first, last, HPX_FORWARD(T, init), - HPX_FORWARD(F, f)); - } - - template // TODO : add concept - friend T tag_fallback_invoke( - fold_right_t, FwdIter first, FwdIter last, T init, F f) + inline constexpr struct fold_right_t final + : hpx::detail::tag_parallel_algorithm { - static_assert(hpx::traits::is_forward_iterator_v, - "Requires at least forward iterator."); + private: + template // TODO : add concept + friend T tag_fallback_invoke(fold_right_t, ExPolicy&& policy, + FwdIter first, FwdIter last, T init, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return hpx::parallel::detail::fold_right().call( + HPX_FORWARD(ExPolicy, policy), first, last, + HPX_FORWARD(T, init), HPX_FORWARD(F, f)); + } - return hpx::parallel::detail::fold_right().call(hpx::execution::seq, - first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f)); - } -} fold_right{}; + template // TODO : add concept + friend T tag_fallback_invoke( + fold_right_t, FwdIter first, FwdIter last, T init, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return hpx::parallel::detail::fold_right().call( + hpx::execution::seq, first, last, HPX_FORWARD(T, init), + HPX_FORWARD(F, f)); + } + } fold_right{}; } // namespace hpx From c7b7fa2aba3c60c3f3acfef770fb5304f2016cfd Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sun, 11 Jun 2023 06:31:37 -0500 Subject: [PATCH 19/32] adding headers Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index e1818e269f63..e6780df000af 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -8,6 +8,8 @@ #include #include +#include +#include #include #include @@ -75,7 +77,7 @@ namespace hpx { "Requires at least forward iterator."); return hpx::parallel::detail::fold_left().call( - hpx::execution::seq, first, last, HPX_FORWARD(T, init), + ::hpx::execution::seq, first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f)); } } fold_left{}; @@ -107,9 +109,9 @@ namespace hpx::parallel::detail { std::advance(first, 1); - return hpx::optional( - hpx::parallel::detail::fold_left().call(hpx::execution::seq, - first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f))); + return hpx::optional(hpx::parallel::detail::fold_left().call( + ::hpx::execution::seq, first, last, HPX_FORWARD(T, init), + HPX_FORWARD(F, f))); } template (*first), f)); return hpx::parallel::detail::fold_left_first>() - .call(hpx::execution::seq, first, last, HPX_FORWARD(F, f)); + .call(::hpx::execution::seq, first, last, HPX_FORWARD(F, f)); } } fold_left_first{}; } // namespace hpx @@ -245,7 +247,7 @@ namespace hpx { "Requires at least forward iterator."); return hpx::parallel::detail::fold_right().call( - hpx::execution::seq, first, last, HPX_FORWARD(T, init), + ::hpx::execution::seq, first, last, HPX_FORWARD(T, init), HPX_FORWARD(F, f)); } } fold_right{}; From d833bff998f7dcee34af8a30d4b0db4fa06d8885 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Wed, 14 Jun 2023 07:27:56 -0500 Subject: [PATCH 20/32] inncluded dispatch_header Signed-off-by: Hari Hara Naveen S --- libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index e6780df000af..f0778075a804 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include From 9b2ba13b3c17e3441d7e2628a7d1659a0eddd07a Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Thu, 15 Jun 2023 12:32:12 -0500 Subject: [PATCH 21/32] added return Signed-off-by: Hari Hara Naveen S --- libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index f0778075a804..a4181a9bd792 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -31,7 +31,7 @@ namespace hpx::parallel::detail { HPX_HOST_DEVICE static constexpr T sequential( ExPolicy&&, FwdIter first, Sent last, T&& init, F&& f) { - util::loop_ind( + return util::loop_ind( first, last, [&init, &f](auto const& it) mutable { init = HPX_INVOKE(f, HPX_MOVE(init), it); }); @@ -42,7 +42,7 @@ namespace hpx::parallel::detail { static constexpr auto parallel( ExPolicy&&, FwdIter first, Sent last, T&& init, F&& f) { - util::loop_ind( + return util::loop_ind( first, last, [&init, &f](auto const& it) mutable { init = HPX_INVOKE(f, HPX_MOVE(init), it); }); From 3a7ef64d46d51d60b293744c7c0a5367b3903691 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sat, 17 Jun 2023 10:09:24 -0500 Subject: [PATCH 22/32] tests +parallel policy Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 199 ++++++------------ 1 file changed, 64 insertions(+), 135 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index a4181a9bd792..cbfc2e29ed6b 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -10,133 +10,60 @@ #include #include #include +#include #include #include #include #include -namespace hpx::parallel::detail { - - template - struct fold_left : public algorithm, T_> - { - constexpr fold_left() noexcept - : algorithm("fold_left") - { - } - - template - HPX_HOST_DEVICE static constexpr T sequential( - ExPolicy&&, FwdIter first, Sent last, T&& init, F&& f) - { - return util::loop_ind( - first, last, [&init, &f](auto const& it) mutable { - init = HPX_INVOKE(f, HPX_MOVE(init), it); - }); - } - - template - static constexpr auto parallel( - ExPolicy&&, FwdIter first, Sent last, T&& init, F&& f) - { - return util::loop_ind( - first, last, [&init, &f](auto const& it) mutable { - init = HPX_INVOKE(f, HPX_MOVE(init), it); - }); - } - }; - -} // namespace hpx::parallel::detail - namespace hpx { inline constexpr struct fold_left_t final : hpx::detail::tag_parallel_algorithm { private: - template // TODO : add concept + template friend T tag_fallback_invoke(fold_left_t, ExPolicy&& policy, FwdIter first, FwdIter last, T init, F f) { static_assert(hpx::traits::is_forward_iterator_v, "Requires at least forward iterator."); - return hpx::parallel::detail::fold_left().call( - HPX_FORWARD(ExPolicy, policy), first, last, - HPX_FORWARD(T, init), HPX_FORWARD(F, f)); + return hpx::reduce(HPX_FORWARD(ExPolicy, policy), first, last, init, + HPX_FORWARD(F, f)); } - template // TODO : add concept + template friend T tag_fallback_invoke( fold_left_t, FwdIter first, FwdIter last, T init, F f) { static_assert(hpx::traits::is_forward_iterator_v, "Requires at least forward iterator."); - return hpx::parallel::detail::fold_left().call( - ::hpx::execution::seq, first, last, HPX_FORWARD(T, init), - HPX_FORWARD(F, f)); + return hpx::reduce( + hpx::execution::seq, first, last, init, HPX_FORWARD(F, f)); } } fold_left{}; } // namespace hpx namespace hpx::parallel::detail { - - template - struct fold_left_first : public algorithm, T_> + template + auto fold_left_first_helper( + ExPolicy&& policy, FwdIter first, FwdIter last, F&& f) { - constexpr fold_left_first() noexcept - : algorithm("fold_left_first") - { - } + using T = ::hpx::traits::iter_value_t; + using U = decltype(hpx::fold_left(HPX_MOVE(first), last, T(*first), f)); - template - HPX_HOST_DEVICE static constexpr auto sequential( - ExPolicy&&, FwdIter first, Sent last, F&& f) - { - using T = ::hpx::traits::iter_value_t; - using U = - decltype(hpx::fold_left(HPX_MOVE(first), last, T(*first), f)); - - if (first == last) - return hpx::optional(); - - T init = *first; - - std::advance(first, 1); - - return hpx::optional(hpx::parallel::detail::fold_left().call( - ::hpx::execution::seq, first, last, HPX_FORWARD(T, init), - HPX_FORWARD(F, f))); - } - - template - static constexpr auto parallel( - ExPolicy&& policy, FwdIter first, Sent last, F&& f) - { - using T = ::hpx::traits::iter_value_t; - using U = - decltype(hpx::fold_left(HPX_MOVE(first), last, T(*first), f)); - - if (first == last) - return hpx::optional(); + if (first == last) + return hpx::optional(); - T init = *first; + T init = *first; - std::advance(first, 1); - - return hpx::optional(hpx::parallel::detail::fold_left().call( - HPX_FORWARD(ExPolicy, policy), first, last, - HPX_FORWARD(T, init), HPX_FORWARD(F, f))); - } - }; + std::advance(first, 1); + return hpx::optional(hpx::fold_left(HPX_FORWARD(ExPolicy, policy), + first, last, HPX_MOVE(init), HPX_MOVE(f))); + } } // namespace hpx::parallel::detail namespace hpx { @@ -144,34 +71,26 @@ namespace hpx { : hpx::detail::tag_parallel_algorithm { private: - template // TODO : add concept + template friend auto tag_fallback_invoke(fold_left_first_t, ExPolicy&& policy, FwdIter first, FwdIter last, F f) { static_assert(hpx::traits::is_forward_iterator_v, "Requires at least forward iterator."); - using U = decltype(hpx::fold_left(HPX_MOVE(first), last, - ::hpx::traits::iter_value_t(*first), f)); - - return hpx::parallel::detail::fold_left_first>() - .call(HPX_FORWARD(ExPolicy, policy), first, last, - HPX_FORWARD(F, f)); + return hpx::parallel::detail::fold_left_first_helper( + HPX_FORWARD(ExPolicy, policy), first, last, HPX_MOVE(f)); } - template // TODO : add concept + template friend auto tag_fallback_invoke( fold_left_first_t, FwdIter first, FwdIter last, F f) { static_assert(hpx::traits::is_forward_iterator_v, "Requires at least forward iterator."); - using U = decltype(hpx::fold_left(HPX_MOVE(first), last, - ::hpx::traits::iter_value_t(*first), f)); - - return hpx::parallel::detail::fold_left_first>() - .call(::hpx::execution::seq, first, last, HPX_FORWARD(F, f)); + return hpx::parallel::detail::fold_left_first_helper( + hpx::execution::seq, first, last, HPX_MOVE(f)); } } fold_left_first{}; } // namespace hpx @@ -189,17 +108,13 @@ namespace hpx::parallel::detail { template HPX_HOST_DEVICE static constexpr auto sequential( - ExPolicy&&, FwdIter first, Sent last, T&& init, F&& f) + ExPolicy&& policy, FwdIter first, Sent last, T&& init, F&& f) { - using U = std::decay_t, T>>; - if (first == last) - return U(HPX_MOVE(init)); - - U accum = f(*--last, HPX_MOVE(init)); - while (first != last) - accum = f(*--last, HPX_MOVE(accum)); - return accum; + // last++ moves backward when its reverse iterator + return hpx::fold_left(HPX_FORWARD(ExPolicy, policy), + std::make_reverse_iterator(last), + std::make_reverse_iterator(first), HPX_FORWARD(T, init), + HPX_FORWARD(F, f)); } template ::call( + HPX_FORWARD(ExPolicy, policy), first, + std::distance(first, last), HPX_MOVE(ChunkReduce), + hpx::unwrapping(HPX_MOVE(RecursiveReduce))); } }; - -} // namespace hpx::parallel::detail +}; // namespace hpx::parallel::detail namespace hpx { inline constexpr struct fold_right_t final : hpx::detail::tag_parallel_algorithm { private: - template // TODO : add concept + template friend T tag_fallback_invoke(fold_right_t, ExPolicy&& policy, FwdIter first, FwdIter last, T init, F f) { @@ -235,12 +165,11 @@ namespace hpx { "Requires at least forward iterator."); return hpx::parallel::detail::fold_right().call( - HPX_FORWARD(ExPolicy, policy), first, last, - HPX_FORWARD(T, init), HPX_FORWARD(F, f)); + HPX_FORWARD(ExPolicy, policy), first, last, HPX_MOVE(init), + HPX_MOVE(f)); } - template // TODO : add concept + template friend T tag_fallback_invoke( fold_right_t, FwdIter first, FwdIter last, T init, F f) { @@ -248,8 +177,8 @@ namespace hpx { "Requires at least forward iterator."); return hpx::parallel::detail::fold_right().call( - ::hpx::execution::seq, first, last, HPX_FORWARD(T, init), - HPX_FORWARD(F, f)); + ::hpx::execution::seq, first, last, HPX_MOVE(init), + HPX_MOVE(f)); } } fold_right{}; } // namespace hpx From 29304b6c37945f39ab821922567393c4954bf970 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sat, 17 Jun 2023 18:53:48 -0500 Subject: [PATCH 23/32] fold tests Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 14 ++++++++------ .../tests/unit/algorithms/CMakeLists.txt | 1 + 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index cbfc2e29ed6b..df38970f4f04 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -127,21 +127,23 @@ namespace hpx::parallel::detail { return init; } - auto ChunkReduce = [f, policy](FwdIter it, std::size_t chunkSize) { + auto ChunkReduce = [f = HPX_FORWARD(F, f), + policy = HPX_FORWARD(ExPolicy, policy)]( + FwdIter it, std::size_t chunkSize) { FwdIter endIter = it; std::advance(endIter, --chunkSize); T init = *endIter; - return sequential( - HPX_FORWARD(ExPolicy, policy), it, endIter, init, f); + return sequential(HPX_FORWARD(ExPolicy, policy), it, endIter, + init, HPX_FORWARD(F, f)); }; - auto RecursiveReduce = [f, policy, init](auto&& results) { + auto RecursiveReduce = [f, policy, init](auto&& results) mutable { auto begin = hpx::util::begin(results); auto end = hpx::util::end(results); - return sequential( - HPX_FORWARD(ExPolicy, policy), begin, end, init, f); + return sequential(HPX_FORWARD(ExPolicy, policy), begin, end, + init, HPX_FORWARD(F, f)); }; return util::partitioner::call( diff --git a/libs/core/algorithms/tests/unit/algorithms/CMakeLists.txt b/libs/core/algorithms/tests/unit/algorithms/CMakeLists.txt index cbdca61a4bdd..c011bc2bf854 100644 --- a/libs/core/algorithms/tests/unit/algorithms/CMakeLists.txt +++ b/libs/core/algorithms/tests/unit/algorithms/CMakeLists.txt @@ -50,6 +50,7 @@ set(tests findfirstof_binary findif findifnot + fold_ foreach foreach_executors foreach_prefetching From 22e045f8dec15fac1d46783d9c0414ea9a269476 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sat, 17 Jun 2023 18:55:23 -0500 Subject: [PATCH 24/32] fold tests Signed-off-by: Hari Hara Naveen S --- .../tests/unit/algorithms/fold_.cpp | 91 ++++++++++++++++ .../tests/unit/algorithms/fold_tests.hpp | 103 ++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 libs/core/algorithms/tests/unit/algorithms/fold_.cpp create mode 100644 libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp diff --git a/libs/core/algorithms/tests/unit/algorithms/fold_.cpp b/libs/core/algorithms/tests/unit/algorithms/fold_.cpp new file mode 100644 index 000000000000..70c2bf3cbd33 --- /dev/null +++ b/libs/core/algorithms/tests/unit/algorithms/fold_.cpp @@ -0,0 +1,91 @@ +// Copyright (c) 2014-2020 Hartmut Kaiser +// +// SPDX-License-Identifier: BSL-1.0 +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include + +#include +#include +#include +#include + +#include "fold_tests.hpp" + +template +void fold_left_test_dispatch_2(IteratorTag) +{ + fold_left_test1(IteratorTag()); + fold_left_test1(hpx::execution::seq, IteratorTag()); + fold_left_test1(hpx::execution::par, IteratorTag()); +} + +void fold_left_test_dispatch_1() +{ + fold_left_test_dispatch_2(std::random_access_iterator_tag()); + fold_left_test_dispatch_2(std::forward_iterator_tag()); +} + +void fold_left_test_dispatch() +{ + fold_left_test_dispatch_1(); +} + +template +void fold_right_test_dispatch_2(IteratorTag) +{ + fold_right_test1(IteratorTag()); + fold_right_test1(hpx::execution::seq, IteratorTag()); + fold_right_test1(hpx::execution::par, IteratorTag()); +} + +void fold_right_test_dispatch_1() +{ + fold_right_test_dispatch_2(std::random_access_iterator_tag()); + // fold_right_test_dispatch_2(std::forward_iterator_tag()); +} + +void fold_right_test_dispatch() +{ + fold_right_test_dispatch_1(); +} + +/////////////////////////////////////////////////////////////////////////////// +int hpx_main(hpx::program_options::variables_map& vm) +{ + unsigned int seed = (unsigned int) std::time(nullptr); + if (vm.count("seed")) + seed = vm["seed"].as(); + + std::cout << "using seed: " << seed << std::endl; + gen.seed(seed); + + fold_left_test_dispatch(); + fold_right_test_dispatch(); + + return hpx::local::finalize(); +} + +int main(int argc, char* argv[]) +{ + // add command line option which controls the random number generator seed + using namespace hpx::program_options; + options_description desc_commandline( + "Usage: " HPX_APPLICATION_STRING " [options]"); + + desc_commandline.add_options()("seed,s", value(), + "the random number generator seed to use for this run"); + // By default this test should run on all available cores + std::vector const cfg = {"hpx.os_threads=all"}; + + // Initialize and run HPX + hpx::local::init_params init_args; + init_args.desc_cmdline = desc_commandline; + init_args.cfg = cfg; + + HPX_TEST_EQ_MSG(hpx::local::init(hpx_main, argc, argv, init_args), 0, + "HPX main exited with non-zero status"); + + return hpx::util::report_errors(); +} diff --git a/libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp b/libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp new file mode 100644 index 000000000000..b77aea0b69cd --- /dev/null +++ b/libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp @@ -0,0 +1,103 @@ +// Copyright (c) 2014-2020 Hartmut Kaiser +// +// SPDX-License-Identifier: BSL-1.0 +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#pragma once + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "test_utils.hpp" + +int seed = std::random_device{}(); +std::mt19937 gen(seed); + +// fold_left(begin, end, init, op) +template +void fold_left_test1(IteratorTag) +{ + using base_iterator = std::vector::iterator; + using iterator = test::test_iterator; + + std::vector c = {1, 2, 3, 4, 5}; + + int val(1); + auto op = [](auto v1, auto v2) { return v1 * v2; }; + + int r1 = + hpx::fold_left(iterator(std::begin(c)), iterator(std::end(c)), val, op); + + // verify values + int r2 = 120; + HPX_TEST_EQ(r1, r2); +} + +// fold_left(policy, begin, end, init, op) +template +void fold_left_test1(ExPolicy policy, IteratorTag) +{ + using base_iterator = std::vector::iterator; + using iterator = test::test_iterator; + + std::vector c = {1, 2, 3, 4, 5}; + + int val(1); + auto op = [](auto v1, auto v2) { return v1 * v2; }; + + int r1 = hpx::fold_left( + policy, iterator(std::begin(c)), iterator(std::end(c)), val, op); + + // verify values + int r2 = 120; + HPX_TEST_EQ(r1, r2); +} + +// fold_right(begin, end, init, op) +template +void fold_right_test1(IteratorTag) +{ + using base_iterator = std::vector::iterator; + using iterator = test::test_iterator; + + std::vector c = {1, 2, 3, 4, 5}; + + int val(1); + auto op = [](auto v1, auto v2) { return v1 * v2; }; + + int r1 = + hpx::fold_right(iterator(std::begin(c)), iterator(std::end(c)), val, op); + + // verify values + int r2 = 120; + HPX_TEST_EQ(r1, r2); +} + +// fold_right(policy, begin, end, init, op) +template +void fold_right_test1(ExPolicy policy, IteratorTag) +{ + using base_iterator = std::vector::iterator; + using iterator = test::test_iterator; + + std::vector c = {1, 2, 3, 4, 5}; + + int val(1); + auto op = [](auto v1, auto v2) { return v1 * v2; }; + + int r1 = hpx::fold_right( + policy, iterator(std::begin(c)), iterator(std::end(c)), val, op); + + // verify values + int r2 = 120; + HPX_TEST_EQ(r1, r2); +} From b257f401baa14c0f0d6632f048e79bd52e1b44ef Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sat, 17 Jun 2023 19:07:40 -0500 Subject: [PATCH 25/32] fold_right_first Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 52 +++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index df38970f4f04..b7e3b2813db0 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -57,9 +57,7 @@ namespace hpx::parallel::detail { if (first == last) return hpx::optional(); - T init = *first; - - std::advance(first, 1); + T init = *first++; return hpx::optional(hpx::fold_left(HPX_FORWARD(ExPolicy, policy), first, last, HPX_MOVE(init), HPX_MOVE(f))); @@ -184,3 +182,51 @@ namespace hpx { } } fold_right{}; } // namespace hpx + +namespace hpx::parallel::detail { + template + auto fold_right_first_helper( + ExPolicy&& policy, FwdIter first, FwdIter last, F&& f) + { + using T = ::hpx::traits::iter_value_t; + using U = + decltype(hpx::fold_right(HPX_MOVE(first), last, T(*first), f)); + + if (first == last) + return hpx::optional(); + + T init = *--last; + + return hpx::optional(hpx::fold_right(HPX_FORWARD(ExPolicy, policy), + first, last, HPX_MOVE(init), HPX_MOVE(f))); + } +} // namespace hpx::parallel::detail + +namespace hpx { + inline constexpr struct fold_right_first_t final + : hpx::detail::tag_parallel_algorithm + { + private: + template + friend auto tag_fallback_invoke(fold_right_first_t, ExPolicy&& policy, + FwdIter first, FwdIter last, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return hpx::parallel::detail::fold_right_first_helper( + HPX_FORWARD(ExPolicy, policy), first, last, HPX_MOVE(f)); + } + + template + friend auto tag_fallback_invoke( + fold_right_first_t, FwdIter first, FwdIter last, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return hpx::parallel::detail::fold_right_first_helper( + hpx::execution::seq, first, last, HPX_MOVE(f)); + } + } fold_right_first{}; +} // namespace hpx From 177ec74903aef7e60edee84fcfe215fb9c4c8c73 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sun, 18 Jun 2023 05:32:54 -0500 Subject: [PATCH 26/32] fixed forwarding error, added header, cf Signed-off-by: Hari Hara Naveen S --- .../algorithms/include/hpx/parallel/algorithms/fold.hpp | 7 ++++--- libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index b7e3b2813db0..74214a71daf9 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -133,15 +134,15 @@ namespace hpx::parallel::detail { T init = *endIter; - return sequential(HPX_FORWARD(ExPolicy, policy), it, endIter, - init, HPX_FORWARD(F, f)); + return sequential(policy, it, endIter, + init, f); }; auto RecursiveReduce = [f, policy, init](auto&& results) mutable { auto begin = hpx::util::begin(results); auto end = hpx::util::end(results); return sequential(HPX_FORWARD(ExPolicy, policy), begin, end, - init, HPX_FORWARD(F, f)); + init, f); }; return util::partitioner::call( diff --git a/libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp b/libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp index b77aea0b69cd..0a0ceb08135b 100644 --- a/libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp +++ b/libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp @@ -74,8 +74,8 @@ void fold_right_test1(IteratorTag) int val(1); auto op = [](auto v1, auto v2) { return v1 * v2; }; - int r1 = - hpx::fold_right(iterator(std::begin(c)), iterator(std::end(c)), val, op); + int r1 = hpx::fold_right( + iterator(std::begin(c)), iterator(std::end(c)), val, op); // verify values int r2 = 120; From 80ce83025af89cf982ec9556126fb9f780aa2bb6 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sun, 18 Jun 2023 05:34:09 -0500 Subject: [PATCH 27/32] fixed forwarding error Signed-off-by: Hari Hara Naveen S --- libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index 74214a71daf9..b8d342f110ca 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -141,7 +141,7 @@ namespace hpx::parallel::detail { auto RecursiveReduce = [f, policy, init](auto&& results) mutable { auto begin = hpx::util::begin(results); auto end = hpx::util::end(results); - return sequential(HPX_FORWARD(ExPolicy, policy), begin, end, + return sequential(policy, begin, end, init, f); }; From cd8bfa46a7e7beac0be8d5333bd3d39416ed65e0 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sun, 18 Jun 2023 06:45:54 -0500 Subject: [PATCH 28/32] fold with iter implemented Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 95 ++++++++++++++++++- 1 file changed, 91 insertions(+), 4 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index b8d342f110ca..2181d804b082 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -134,15 +135,13 @@ namespace hpx::parallel::detail { T init = *endIter; - return sequential(policy, it, endIter, - init, f); + return sequential(policy, it, endIter, init, f); }; auto RecursiveReduce = [f, policy, init](auto&& results) mutable { auto begin = hpx::util::begin(results); auto end = hpx::util::end(results); - return sequential(policy, begin, end, - init, f); + return sequential(policy, begin, end, init, f); }; return util::partitioner::call( @@ -231,3 +230,91 @@ namespace hpx { } } fold_right_first{}; } // namespace hpx + +namespace hpx { + inline constexpr struct fold_left_with_iter_t final + : hpx::detail::tag_parallel_algorithm + { + private: + template + using fold_left_with_iter_ty = hpx::ranges::in_value_result; + + template + friend T tag_fallback_invoke(fold_left_with_iter_t, ExPolicy&& policy, + FwdIter first, FwdIter last, T init, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return fold_left_with_iter_ty{ + hpx::reduce(HPX_FORWARD(ExPolicy, policy), first, last, init, + HPX_FORWARD(F, f)), + last}; + } + + template + friend T tag_fallback_invoke( + fold_left_with_iter_t, FwdIter first, FwdIter last, T init, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return fold_left_with_iter_ty{ + hpx::reduce( + hpx::execution::seq, first, last, init, HPX_FORWARD(F, f)), + last}; + } + } fold_left_with_iter{}; +} // namespace hpx + +namespace hpx::parallel::detail { + + template + auto fold_left_first_with_iter_helper( + ExPolicy&& policy, FwdIter first, Sent last, F&& f) + { + using T = ::hpx::traits::iter_value_t; + using fold_left_first_with_iter_ty = + hpx::optional>; + + if (first == last) + return fold_left_first_with_iter_ty(); + + T init = *first++; + + return fold_left_first_with_iter_ty( + {hpx::fold_left_with_iter(HPX_FORWARD(ExPolicy, policy), first, + last, HPX_MOVE(init), HPX_MOVE(f)), + last}); + } +} // namespace hpx::parallel::detail + +namespace hpx { + inline constexpr struct fold_left_first_with_iter_t final + : hpx::detail::tag_parallel_algorithm + { + private: + template + friend auto tag_fallback_invoke(fold_left_first_with_iter_t, + ExPolicy&& policy, FwdIter first, Sent last, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return fold_left_first_with_iter_helper( + HPX_FORWARD(ExPolicy, policy), first, last, f); + } + + template + friend auto tag_fallback_invoke( + fold_left_first_with_iter_t, FwdIter first, Sent last, F f) + { + static_assert(hpx::traits::is_forward_iterator_v, + "Requires at least forward iterator."); + + return fold_left_first_with_iter_helper( + hpx::execution::seq, first, last, f); + } + } fold_left_first_with_iter{}; +} // namespace hpx From 301630c53c7e5a3b38fb069f1a501432ac548038 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sun, 18 Jun 2023 13:41:58 -0500 Subject: [PATCH 29/32] removed unnecessery semi colon Signed-off-by: Hari Hara Naveen S --- libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index 2181d804b082..8263aab1577a 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -150,7 +150,7 @@ namespace hpx::parallel::detail { hpx::unwrapping(HPX_MOVE(RecursiveReduce))); } }; -}; // namespace hpx::parallel::detail +} // namespace hpx::parallel::detail namespace hpx { inline constexpr struct fold_right_t final From dbde51b8186cc4cb26baecddeeda74f46ac820b1 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sun, 16 Jul 2023 11:41:24 -0500 Subject: [PATCH 30/32] fold first tests Signed-off-by: Hari Hara Naveen S --- .../tests/unit/algorithms/fold_tests.hpp | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp b/libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp index 0a0ceb08135b..7e1ecd803fb5 100644 --- a/libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp +++ b/libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp @@ -101,3 +101,79 @@ void fold_right_test1(ExPolicy policy, IteratorTag) int r2 = 120; HPX_TEST_EQ(r1, r2); } + +// fold_left(begin, end, init, op) +template +void fold_left_first_test1(IteratorTag) +{ + using base_iterator = std::vector::iterator; + using iterator = test::test_iterator; + + std::vector c = {1, 2, 3, 4, 5}; + + auto op = [](auto v1, auto v2) { return v1 * v2; }; + + hpx::optional r1 = hpx::fold_left_first( + iterator(std::begin(c)), iterator(std::end(c)), op); + + // verify values + int r2 = 120; + HPX_TEST_EQ(r1.value(), r2); +} + +// fold_left_first(policy, begin, end, op) +template +void fold_left_first_test1(ExPolicy policy, IteratorTag) +{ + using base_iterator = std::vector::iterator; + using iterator = test::test_iterator; + + std::vector c = {1, 2, 3, 4, 5}; + + auto op = [](auto v1, auto v2) { return v1 * v2; }; + + hpx::optional r1 = hpx::fold_left_first( + policy, iterator(std::begin(c)), iterator(std::end(c)), op); + + // verify values + int r2 = 120; + HPX_TEST_EQ(r1.value(), r2); +} + +// fold_right_first(begin, end, op) +template +void fold_right_first_test1(IteratorTag) +{ + using base_iterator = std::vector::iterator; + using iterator = test::test_iterator; + + std::vector c = {1, 2, 3, 4, 5}; + + auto op = [](auto v1, auto v2) { return v1 * v2; }; + + hpx::optional r1 = hpx::fold_right_first( + iterator(std::begin(c)), iterator(std::end(c)), op); + + // verify values + int r2 = 120; + HPX_TEST_EQ(r1.value(), r2); +} + +// fold_right_first(policy, begin, end, op) +template +void fold_right_first_test1(ExPolicy policy, IteratorTag) +{ + using base_iterator = std::vector::iterator; + using iterator = test::test_iterator; + + std::vector c = {1, 2, 3, 4, 5}; + + auto op = [](auto v1, auto v2) { return v1 * v2; }; + + hpx::optional r1 = hpx::fold_right_first( + policy, iterator(std::begin(c)), iterator(std::end(c)), op); + + // verify values + int r2 = 120; + HPX_TEST_EQ(r1.value(), r2); +} From 8820151e242d6106c231d6d043e7430dd9a1cd2f Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Mon, 17 Jul 2023 07:01:14 -0500 Subject: [PATCH 31/32] all tests implemented Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/algorithms/fold.hpp | 42 +++++---- .../tests/unit/algorithms/fold_.cpp | 82 +++++++++++++++++- .../tests/unit/algorithms/fold_tests.hpp | 85 +++++++++++++++++++ 3 files changed, 185 insertions(+), 24 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp index 8263aab1577a..f27412d31b09 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/fold.hpp @@ -78,7 +78,7 @@ namespace hpx { static_assert(hpx::traits::is_forward_iterator_v, "Requires at least forward iterator."); - return hpx::parallel::detail::fold_left_first_helper( + return ::hpx::parallel::detail::fold_left_first_helper( HPX_FORWARD(ExPolicy, policy), first, last, HPX_MOVE(f)); } @@ -89,7 +89,7 @@ namespace hpx { static_assert(hpx::traits::is_forward_iterator_v, "Requires at least forward iterator."); - return hpx::parallel::detail::fold_left_first_helper( + return ::hpx::parallel::detail::fold_left_first_helper( hpx::execution::seq, first, last, HPX_MOVE(f)); } } fold_left_first{}; @@ -240,29 +240,31 @@ namespace hpx { using fold_left_with_iter_ty = hpx::ranges::in_value_result; template - friend T tag_fallback_invoke(fold_left_with_iter_t, ExPolicy&& policy, - FwdIter first, FwdIter last, T init, F f) + friend auto tag_fallback_invoke(fold_left_with_iter_t, + ExPolicy&& policy, FwdIter first, FwdIter last, T init, F f) { static_assert(hpx::traits::is_forward_iterator_v, "Requires at least forward iterator."); + T fold_left_value = hpx::fold_left(HPX_FORWARD(ExPolicy, policy), + first, last, init, HPX_FORWARD(F, f)); + return fold_left_with_iter_ty{ - hpx::reduce(HPX_FORWARD(ExPolicy, policy), first, last, init, - HPX_FORWARD(F, f)), - last}; + last, HPX_MOVE(fold_left_value)}; } template - friend T tag_fallback_invoke( + friend auto tag_fallback_invoke( fold_left_with_iter_t, FwdIter first, FwdIter last, T init, F f) { static_assert(hpx::traits::is_forward_iterator_v, "Requires at least forward iterator."); + T fold_left_value = hpx::fold_left( + hpx::execution::seq, first, last, init, HPX_FORWARD(F, f)); + return fold_left_with_iter_ty{ - hpx::reduce( - hpx::execution::seq, first, last, init, HPX_FORWARD(F, f)), - last}; + last, HPX_MOVE(fold_left_value)}; } } fold_left_with_iter{}; } // namespace hpx @@ -275,17 +277,11 @@ namespace hpx::parallel::detail { { using T = ::hpx::traits::iter_value_t; using fold_left_first_with_iter_ty = - hpx::optional>; - - if (first == last) - return fold_left_first_with_iter_ty(); - - T init = *first++; + hpx::ranges::in_value_result>; - return fold_left_first_with_iter_ty( - {hpx::fold_left_with_iter(HPX_FORWARD(ExPolicy, policy), first, - last, HPX_MOVE(init), HPX_MOVE(f)), - last}); + return fold_left_first_with_iter_ty{last, + hpx::fold_left_first( + HPX_FORWARD(ExPolicy, policy), first, last, HPX_MOVE(f))}; } } // namespace hpx::parallel::detail @@ -302,7 +298,7 @@ namespace hpx { static_assert(hpx::traits::is_forward_iterator_v, "Requires at least forward iterator."); - return fold_left_first_with_iter_helper( + return ::hpx::parallel::detail::fold_left_first_with_iter_helper( HPX_FORWARD(ExPolicy, policy), first, last, f); } @@ -313,7 +309,7 @@ namespace hpx { static_assert(hpx::traits::is_forward_iterator_v, "Requires at least forward iterator."); - return fold_left_first_with_iter_helper( + return ::hpx::parallel::detail::fold_left_first_with_iter_helper( hpx::execution::seq, first, last, f); } } fold_left_first_with_iter{}; diff --git a/libs/core/algorithms/tests/unit/algorithms/fold_.cpp b/libs/core/algorithms/tests/unit/algorithms/fold_.cpp index 70c2bf3cbd33..080b681ebb89 100644 --- a/libs/core/algorithms/tests/unit/algorithms/fold_.cpp +++ b/libs/core/algorithms/tests/unit/algorithms/fold_.cpp @@ -43,7 +43,6 @@ void fold_right_test_dispatch_2(IteratorTag) void fold_right_test_dispatch_1() { fold_right_test_dispatch_2(std::random_access_iterator_tag()); - // fold_right_test_dispatch_2(std::forward_iterator_tag()); } void fold_right_test_dispatch() @@ -51,6 +50,81 @@ void fold_right_test_dispatch() fold_right_test_dispatch_1(); } +template +void fold_left_first_test_dispatch_2(IteratorTag) +{ + fold_left_first_test1(IteratorTag()); + fold_left_first_test1(hpx::execution::seq, IteratorTag()); + fold_left_first_test1(hpx::execution::par, IteratorTag()); +} + +void fold_left_first_test_dispatch_1() +{ + fold_left_first_test_dispatch_2(std::random_access_iterator_tag()); + fold_left_first_test_dispatch_2(std::forward_iterator_tag()); +} + +void fold_left_first_test_dispatch() +{ + fold_left_first_test_dispatch_1(); +} + +template +void fold_right_first_test_dispatch_2(IteratorTag) +{ + fold_right_first_test1(IteratorTag()); + fold_right_first_test1(hpx::execution::seq, IteratorTag()); + fold_right_first_test1(hpx::execution::par, IteratorTag()); +} + +void fold_right_first_test_dispatch_1() +{ + fold_right_first_test_dispatch_2(std::random_access_iterator_tag()); +} + +void fold_right_first_test_dispatch() +{ + fold_right_first_test_dispatch_1(); +} + +template +void fold_left_with_iter_test_dispatch_2(IteratorTag) +{ + fold_left_with_iter_test1(IteratorTag()); + fold_left_with_iter_test1(hpx::execution::seq, IteratorTag()); + fold_left_with_iter_test1(hpx::execution::par, IteratorTag()); +} + +void fold_left_with_iter_test_dispatch_1() +{ + fold_left_with_iter_test_dispatch_2(std::random_access_iterator_tag()); + fold_left_with_iter_test_dispatch_2(std::forward_iterator_tag()); +} + +void fold_left_with_iter_test_dispatch() +{ + fold_left_with_iter_test_dispatch_1(); +} + +template +void fold_left_first_with_iter_test_dispatch_2(IteratorTag) +{ + fold_left_first_with_iter_test1(IteratorTag()); + fold_left_first_with_iter_test1(hpx::execution::seq, IteratorTag()); + fold_left_first_with_iter_test1(hpx::execution::par, IteratorTag()); +} + +void fold_left_first_with_iter_test_dispatch_1() +{ + fold_left_first_with_iter_test_dispatch_2(std::random_access_iterator_tag()); + fold_left_first_with_iter_test_dispatch_2(std::forward_iterator_tag()); +} + +void fold_left_first_with_iter_test_dispatch() +{ + fold_left_first_with_iter_test_dispatch_1(); +} + /////////////////////////////////////////////////////////////////////////////// int hpx_main(hpx::program_options::variables_map& vm) { @@ -64,6 +138,12 @@ int hpx_main(hpx::program_options::variables_map& vm) fold_left_test_dispatch(); fold_right_test_dispatch(); + fold_left_first_test_dispatch(); + fold_right_first_test_dispatch(); + + fold_left_with_iter_test_dispatch(); + fold_left_first_with_iter_test_dispatch(); + return hpx::local::finalize(); } diff --git a/libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp b/libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp index 7e1ecd803fb5..30768211f5dc 100644 --- a/libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp +++ b/libs/core/algorithms/tests/unit/algorithms/fold_tests.hpp @@ -177,3 +177,88 @@ void fold_right_first_test1(ExPolicy policy, IteratorTag) int r2 = 120; HPX_TEST_EQ(r1.value(), r2); } + +// fold_left(begin, end, init, op) +template +void fold_left_with_iter_test1(IteratorTag) +{ + using base_iterator = std::vector::iterator; + using iterator = test::test_iterator; + + std::vector c = {1, 2, 3, 4, 5}; + + int val(1); + auto op = [](auto v1, auto v2) { return v1 * v2; }; + + hpx::ranges::in_value_result r1 = hpx::fold_left_with_iter( + iterator(std::begin(c)), iterator(std::end(c)), val, op); + + // verify values + int r2 = 120; + HPX_TEST_EQ(r1.value, r2); + HPX_TEST(r1.in == iterator(std::end(c))); +} + +// fold_left_first_with_iter(policy, begin, end, init, op) +template +void fold_left_with_iter_test1(ExPolicy policy, IteratorTag) +{ + using base_iterator = std::vector::iterator; + using iterator = test::test_iterator; + + std::vector c = {1, 2, 3, 4, 5}; + + int val(1); + auto op = [](auto v1, auto v2) { return v1 * v2; }; + + hpx::ranges::in_value_result> r1 = + hpx::fold_left_with_iter( + policy, iterator(std::begin(c)), iterator(std::end(c)), val, op); + + // verify values + int r2 = 120; + HPX_TEST_EQ(r1.value.value(), r2); + HPX_TEST(r1.in == iterator(std::end(c))); +} + +// fold_left(begin, end, init, op) +template +void fold_left_first_with_iter_test1(IteratorTag) +{ + using base_iterator = std::vector::iterator; + using iterator = test::test_iterator; + + std::vector c = {1, 2, 3, 4, 5}; + + auto op = [](auto v1, auto v2) { return v1 * v2; }; + + hpx::ranges::in_value_result> r1 = + hpx::fold_left_first_with_iter( + iterator(std::begin(c)), iterator(std::end(c)), op); + + // verify values + int r2 = 120; + HPX_TEST_EQ(r1.value.value(), r2); + HPX_TEST(r1.in == iterator(std::end(c))); +} + +// fold_left_first_with_iter(policy, begin, end, init, op) +template +void fold_left_first_with_iter_test1(ExPolicy policy, IteratorTag) +{ + using base_iterator = std::vector::iterator; + using iterator = test::test_iterator; + + std::vector c = {1, 2, 3, 4, 5}; + + auto op = [](auto v1, auto v2) { return v1 * v2; }; + + hpx::ranges::in_value_result> r1 = + hpx::fold_left_first_with_iter( + policy, iterator(std::begin(c)), iterator(std::end(c)), op); + + // verify values + int r2 = 120; + HPX_TEST_EQ(r1.value.value(), r2); + HPX_TEST(r1.in == iterator(std::end(c))); +} From be6f45a6af53e4a4a83beae6aca7ea49c17396f0 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Mon, 17 Jul 2023 07:05:16 -0500 Subject: [PATCH 32/32] cf Signed-off-by: Hari Hara Naveen S --- libs/core/algorithms/tests/unit/algorithms/fold_.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libs/core/algorithms/tests/unit/algorithms/fold_.cpp b/libs/core/algorithms/tests/unit/algorithms/fold_.cpp index 080b681ebb89..443e9b3825e2 100644 --- a/libs/core/algorithms/tests/unit/algorithms/fold_.cpp +++ b/libs/core/algorithms/tests/unit/algorithms/fold_.cpp @@ -116,7 +116,8 @@ void fold_left_first_with_iter_test_dispatch_2(IteratorTag) void fold_left_first_with_iter_test_dispatch_1() { - fold_left_first_with_iter_test_dispatch_2(std::random_access_iterator_tag()); + fold_left_first_with_iter_test_dispatch_2( + std::random_access_iterator_tag()); fold_left_first_with_iter_test_dispatch_2(std::forward_iterator_tag()); }