From 7289d0b4545935655bf4695f6599a88d0baaaf69 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sun, 16 Jul 2023 07:21:14 -0500 Subject: [PATCH 1/6] unseq find Signed-off-by: Hari Hara Naveen S --- .../hpx/parallel/algorithms/detail/find.hpp | 3 +- .../include/hpx/parallel/util/loop.hpp | 41 +++++++++++++++++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/detail/find.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/detail/find.hpp index 649e386386e9..30db28d9ac45 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/detail/find.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/detail/find.hpp @@ -34,8 +34,7 @@ namespace hpx::parallel::detail { sequential_find_t, Iterator first, Sentinel last, T const& value, Proj proj = Proj()) { - return util::loop_pred< - std::decay_t>( + return util::loop_pred( first, last, [&value, &proj](auto const& curr) { return HPX_INVOKE(proj, *curr) == value; }); diff --git a/libs/core/algorithms/include/hpx/parallel/util/loop.hpp b/libs/core/algorithms/include/hpx/parallel/util/loop.hpp index b3207bc980e7..579cc0eafb81 100644 --- a/libs/core/algorithms/include/hpx/parallel/util/loop.hpp +++ b/libs/core/algorithms/include/hpx/parallel/util/loop.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -149,11 +150,12 @@ namespace hpx::parallel::util { /////////////////////////////////////////////////////////////////////////// namespace detail { - + template + struct loop_pred; // Helper class to repeatedly call a function starting from a given // iterator position till the predicate returns true. template - struct loop_pred + struct loop_pred { /////////////////////////////////////////////////////////////////// template @@ -170,6 +172,39 @@ namespace hpx::parallel::util { }; } // namespace detail + /////////////////////////////////////////////////////////////////////////// + namespace detail { + + // Helper class to repeatedly call a function starting from a given + // iterator position till the predicate returns true. + template + struct loop_pred + { + /////////////////////////////////////////////////////////////////// + template + HPX_HOST_DEVICE HPX_FORCEINLINE static constexpr Begin call( + Begin it, End end, Pred&& pred) + { + if constexpr (hpx::is_unsequenced_execution_policy_v< + ExPolicy> && + hpx::traits::is_random_access_iterator_v) + { + return unseq_first_n( + it, std::distance(it, end), HPX_FORWARD(Pred, pred)); + } + else + { + for (/**/; it != end; ++it) + { + if (HPX_INVOKE(pred, it)) + return it; + } + return it; + } + } + }; + } // namespace detail + template struct loop_pred_t final : hpx::functional::detail::tag_fallback> @@ -180,7 +215,7 @@ namespace hpx::parallel::util { tag_fallback_invoke(hpx::parallel::util::loop_pred_t, Begin begin, End end, Pred&& pred) { - return detail::loop_pred::call( + return detail::loop_pred::call( begin, end, HPX_FORWARD(Pred, pred)); } }; From 09b39e6c99cff3c70b6a6d498594f32c0f448b68 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Sun, 16 Jul 2023 08:55:40 -0500 Subject: [PATCH 2/6] predicate parameter in simd-helpers first take iterators Signed-off-by: Hari Hara Naveen S changing test according to change made to helper Signed-off-by: Hari Hara Naveen S --- .../include/hpx/parallel/unseq/simd_helpers.hpp | 14 +++++++------- .../unit/algorithms/util/test_simd_helpers.cpp | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/unseq/simd_helpers.hpp b/libs/core/algorithms/include/hpx/parallel/unseq/simd_helpers.hpp index 4ecc66cfdcfc..4e2dd5131810 100644 --- a/libs/core/algorithms/include/hpx/parallel/unseq/simd_helpers.hpp +++ b/libs/core/algorithms/include/hpx/parallel/unseq/simd_helpers.hpp @@ -42,7 +42,7 @@ namespace hpx::parallel::util { HPX_PRAGMA_VECTOR_UNALIGNED HPX_PRAGMA_SIMD_EARLYEXIT for (; i < n; ++i) { - if (f(*(first + i))) + if (f(first + i)) { break; } @@ -64,7 +64,7 @@ namespace hpx::parallel::util { HPX_PRAGMA_VECTOR_UNALIGNED HPX_VECTOR_REDUCTION(| : found_flag) for (IterDiff j = i; j < i + num_blocks; ++j) { - std::int32_t const t = f(*(first + j)); + std::int32_t const t = f(first + j); simd_lane[j - i] = t; found_flag |= t; } @@ -88,7 +88,7 @@ namespace hpx::parallel::util { //Keep remainder scalar while (i != n) { - if (f(*(first + i))) + if (f(first + i)) { break; } @@ -108,7 +108,7 @@ namespace hpx::parallel::util { // clang-format off HPX_PRAGMA_VECTOR_UNALIGNED HPX_PRAGMA_SIMD_EARLYEXIT for (; i < n; ++i) - if (f(*(first1 + i), *(first2 + i))) + if (f(first1 + i, first2 + i)) break; // clang-format on @@ -129,8 +129,8 @@ namespace hpx::parallel::util { HPX_PRAGMA_VECTOR_UNALIGNED HPX_VECTOR_REDUCTION(| : found_flag) for (i = 0; i < num_blocks; ++i) { - IterDiff const t = f(*(first1 + outer_loop_ind + i), - *(first2 + outer_loop_ind + i)); + IterDiff const t = f(first1 + outer_loop_ind + i, + first2 + outer_loop_ind + i); simd_lane[i] = t; found_flag |= t; } @@ -152,7 +152,7 @@ namespace hpx::parallel::util { //Keep remainder scalar for (; outer_loop_ind != n; ++outer_loop_ind) - if (f(*(first1 + outer_loop_ind), *(first2 + outer_loop_ind))) + if (f(first1 + outer_loop_ind, first2 + outer_loop_ind)) break; return std::make_pair(first1 + outer_loop_ind, first2 + outer_loop_ind); diff --git a/libs/core/algorithms/tests/unit/algorithms/util/test_simd_helpers.cpp b/libs/core/algorithms/tests/unit/algorithms/util/test_simd_helpers.cpp index 9fc98de2af61..932867e03a9d 100644 --- a/libs/core/algorithms/tests/unit/algorithms/util/test_simd_helpers.cpp +++ b/libs/core/algorithms/tests/unit/algorithms/util/test_simd_helpers.cpp @@ -36,7 +36,7 @@ void test_unseq_first_n1_dispatch2(std::size_t length, std::size_t first_index) i++; }); - auto f = [](T t) { return t; }; + auto f = [](auto t) { return *t; }; auto iter_test = hpx::parallel::util::unseq_first_n( v.begin(), static_cast(length), f); @@ -80,7 +80,7 @@ void test_unseq_first_n2_dispatch2(std::size_t length, std::size_t first_index) idx++; } - auto f = [](T t1, T t2) { return t1 && t2; }; + auto f = [](auto t1, auto t2) { return *t1 && *t2; }; auto iter_pair_test = hpx::parallel::util::unseq2_first_n( v1.begin(), v2.begin(), static_cast(length), f); From c6a69325cb0e77582886100ae4af90ff53e8ed8a Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Tue, 18 Jul 2023 04:36:17 -0500 Subject: [PATCH 3/6] adding par_unseq to find Signed-off-by: Hari Hara Naveen S unseq Signed-off-by: Hari Hara Naveen S --- .../hpx/parallel/algorithms/detail/find.hpp | 3 ++ .../include/hpx/parallel/util/loop.hpp | 49 ++++++++++++------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/detail/find.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/detail/find.hpp index 30db28d9ac45..8d80fe055f3c 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/detail/find.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/detail/find.hpp @@ -45,6 +45,7 @@ namespace hpx::parallel::detail { std::size_t base_idx, FwdIter part_begin, std::size_t part_count, Token& tok, T const& val, Proj&& proj) { + // TODO util::loop_idx_n(base_idx, part_begin, part_count, tok, [&val, &proj, &tok](auto& v, std::size_t i) -> void { if (HPX_INVOKE(proj, v) == val) @@ -101,6 +102,7 @@ namespace hpx::parallel::detail { sequential_find_if_t, FwdIter part_begin, std::size_t part_count, Token& tok, F&& op, Proj&& proj) { + // TODO util::loop_n>(part_begin, part_count, tok, [&op, &tok, &proj](auto const& curr) { if (HPX_INVOKE(op, HPX_INVOKE(proj, *curr))) @@ -116,6 +118,7 @@ namespace hpx::parallel::detail { FwdIter part_begin, std::size_t part_count, Token& tok, F&& f, Proj&& proj) { + // TODO util::loop_idx_n(base_idx, part_begin, part_count, tok, [&f, &proj, &tok](auto& v, std::size_t i) -> void { if (HPX_INVOKE(f, HPX_INVOKE(proj, v))) diff --git a/libs/core/algorithms/include/hpx/parallel/util/loop.hpp b/libs/core/algorithms/include/hpx/parallel/util/loop.hpp index 579cc0eafb81..4018c05ea12c 100644 --- a/libs/core/algorithms/include/hpx/parallel/util/loop.hpp +++ b/libs/core/algorithms/include/hpx/parallel/util/loop.hpp @@ -185,22 +185,12 @@ namespace hpx::parallel::util { HPX_HOST_DEVICE HPX_FORCEINLINE static constexpr Begin call( Begin it, End end, Pred&& pred) { - if constexpr (hpx::is_unsequenced_execution_policy_v< - ExPolicy> && - hpx::traits::is_random_access_iterator_v) - { - return unseq_first_n( - it, std::distance(it, end), HPX_FORWARD(Pred, pred)); - } - else + for (/**/; it != end; ++it) { - for (/**/; it != end; ++it) - { - if (HPX_INVOKE(pred, it)) - return it; - } - return it; + if (HPX_INVOKE(pred, it)) + return it; } + return it; } }; } // namespace detail @@ -220,6 +210,27 @@ namespace hpx::parallel::util { } }; + template )> + HPX_HOST_DEVICE HPX_FORCEINLINE Begin tag_invoke( + hpx::parallel::util::loop_pred_t, + Begin HPX_RESTRICT begin, End HPX_RESTRICT end, Pred&& pred) + { + return unseq_first_n( + begin, std::distance(begin, end), HPX_FORWARD(Pred, pred)); + } + + template )> + HPX_HOST_DEVICE HPX_FORCEINLINE Begin tag_invoke( + hpx::parallel::util::loop_pred_t< + hpx::execution::unsequenced_task_policy>, + Begin HPX_RESTRICT begin, End HPX_RESTRICT end, Pred&& pred) + { + return unseq_first_n( + begin, std::distance(begin, end), HPX_FORWARD(Pred, pred)); + } + #if !defined(HPX_COMPUTE_DEVICE_CODE) template inline constexpr loop_pred_t loop_pred = loop_pred_t{}; @@ -1161,7 +1172,7 @@ namespace hpx::parallel::util { // Helper class to repeatedly call a function a given number of times // starting from a given iterator position. - template + template struct loop_idx_n { /////////////////////////////////////////////////////////////////// @@ -1199,8 +1210,8 @@ namespace hpx::parallel::util { } }; - template <> - struct loop_idx_n + template + struct loop_idx_n { /////////////////////////////////////////////////////////////////// // handle sequences of non-futures @@ -1268,7 +1279,7 @@ namespace hpx::parallel::util { std::size_t base_idx, Iter it, std::size_t count, F&& f) { using cat = typename std::iterator_traits::iterator_category; - return detail::loop_idx_n::call( + return detail::loop_idx_n::call( base_idx, it, count, HPX_FORWARD(F, f)); } @@ -1279,7 +1290,7 @@ namespace hpx::parallel::util { F&& f) { using cat = typename std::iterator_traits::iterator_category; - return detail::loop_idx_n::call( + return detail::loop_idx_n::call( base_idx, it, count, tok, HPX_FORWARD(F, f)); } }; From cbc7cf68067cba254274c00e3b5300f24ac0a8f2 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Tue, 8 Aug 2023 16:20:45 -0500 Subject: [PATCH 4/6] removed ExPolicy as template for loop_pred (tag_invoke is being used for function call deduction) Signed-off-by: Hari Hara Naveen S --- .../hpx/parallel/unseq/simd_helpers.hpp | 2 +- .../include/hpx/parallel/util/loop.hpp | 27 +------------------ 2 files changed, 2 insertions(+), 27 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/unseq/simd_helpers.hpp b/libs/core/algorithms/include/hpx/parallel/unseq/simd_helpers.hpp index 4e2dd5131810..62f3caa7658a 100644 --- a/libs/core/algorithms/include/hpx/parallel/unseq/simd_helpers.hpp +++ b/libs/core/algorithms/include/hpx/parallel/unseq/simd_helpers.hpp @@ -13,6 +13,7 @@ #include #include + #include #include #include @@ -20,7 +21,6 @@ #include #include -// Please use static assert and enforce Iter to be Random Access Iterator namespace hpx::parallel::util { /* Compiler and Hardware should also support vector operations for IterDiff, diff --git a/libs/core/algorithms/include/hpx/parallel/util/loop.hpp b/libs/core/algorithms/include/hpx/parallel/util/loop.hpp index 4018c05ea12c..e4af118ab774 100644 --- a/libs/core/algorithms/include/hpx/parallel/util/loop.hpp +++ b/libs/core/algorithms/include/hpx/parallel/util/loop.hpp @@ -150,34 +150,9 @@ namespace hpx::parallel::util { /////////////////////////////////////////////////////////////////////////// namespace detail { - template - struct loop_pred; // Helper class to repeatedly call a function starting from a given // iterator position till the predicate returns true. template - struct loop_pred - { - /////////////////////////////////////////////////////////////////// - template - HPX_HOST_DEVICE HPX_FORCEINLINE static constexpr Begin call( - Begin it, End end, Pred&& pred) - { - for (/**/; it != end; ++it) - { - if (HPX_INVOKE(pred, it)) - return it; - } - return it; - } - }; - } // namespace detail - - /////////////////////////////////////////////////////////////////////////// - namespace detail { - - // Helper class to repeatedly call a function starting from a given - // iterator position till the predicate returns true. - template struct loop_pred { /////////////////////////////////////////////////////////////////// @@ -205,7 +180,7 @@ namespace hpx::parallel::util { tag_fallback_invoke(hpx::parallel::util::loop_pred_t, Begin begin, End end, Pred&& pred) { - return detail::loop_pred::call( + return detail::loop_pred::call( begin, end, HPX_FORWARD(Pred, pred)); } }; From 7f69717774fa2bb256fee6445e1f79a0a87e15f7 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Tue, 8 Aug 2023 16:29:20 -0500 Subject: [PATCH 5/6] false changed to 0 Signed-off-by: Hari Hara Naveen S --- .../algorithms/tests/unit/algorithms/util/test_simd_helpers.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/core/algorithms/tests/unit/algorithms/util/test_simd_helpers.cpp b/libs/core/algorithms/tests/unit/algorithms/util/test_simd_helpers.cpp index 932867e03a9d..e2927525257c 100644 --- a/libs/core/algorithms/tests/unit/algorithms/util/test_simd_helpers.cpp +++ b/libs/core/algorithms/tests/unit/algorithms/util/test_simd_helpers.cpp @@ -23,7 +23,7 @@ void test_unseq_first_n1_dispatch2(std::size_t length, std::size_t first_index) { first_index = first_index % length; - std::vector v(length, static_cast(false)); + std::vector v(length, static_cast(0)); std::size_t i = 0; std::for_each(v.begin(), v.end(), [&](T& t) { From d8096f0a3173a594c21cf6df1a993f39c5302ad1 Mon Sep 17 00:00:00 2001 From: Hari Hara Naveen S Date: Tue, 8 Aug 2023 16:33:44 -0500 Subject: [PATCH 6/6] fixed cf Signed-off-by: Hari Hara Naveen S fixed expolicy not accepting par_unseq Signed-off-by: Hari Hara Naveen S cleanup changes Signed-off-by: Hari Hara Naveen S fixing missing concept header Signed-off-by: Hari Hara Naveen S --- .../hpx/parallel/algorithms/detail/find.hpp | 3 -- .../hpx/parallel/unseq/simd_helpers.hpp | 1 - .../include/hpx/parallel/util/loop.hpp | 32 +++++++------------ .../algorithms/util/test_simd_helpers.cpp | 2 +- 4 files changed, 12 insertions(+), 26 deletions(-) diff --git a/libs/core/algorithms/include/hpx/parallel/algorithms/detail/find.hpp b/libs/core/algorithms/include/hpx/parallel/algorithms/detail/find.hpp index 8d80fe055f3c..30db28d9ac45 100644 --- a/libs/core/algorithms/include/hpx/parallel/algorithms/detail/find.hpp +++ b/libs/core/algorithms/include/hpx/parallel/algorithms/detail/find.hpp @@ -45,7 +45,6 @@ namespace hpx::parallel::detail { std::size_t base_idx, FwdIter part_begin, std::size_t part_count, Token& tok, T const& val, Proj&& proj) { - // TODO util::loop_idx_n(base_idx, part_begin, part_count, tok, [&val, &proj, &tok](auto& v, std::size_t i) -> void { if (HPX_INVOKE(proj, v) == val) @@ -102,7 +101,6 @@ namespace hpx::parallel::detail { sequential_find_if_t, FwdIter part_begin, std::size_t part_count, Token& tok, F&& op, Proj&& proj) { - // TODO util::loop_n>(part_begin, part_count, tok, [&op, &tok, &proj](auto const& curr) { if (HPX_INVOKE(op, HPX_INVOKE(proj, *curr))) @@ -118,7 +116,6 @@ namespace hpx::parallel::detail { FwdIter part_begin, std::size_t part_count, Token& tok, F&& f, Proj&& proj) { - // TODO util::loop_idx_n(base_idx, part_begin, part_count, tok, [&f, &proj, &tok](auto& v, std::size_t i) -> void { if (HPX_INVOKE(f, HPX_INVOKE(proj, v))) diff --git a/libs/core/algorithms/include/hpx/parallel/unseq/simd_helpers.hpp b/libs/core/algorithms/include/hpx/parallel/unseq/simd_helpers.hpp index 62f3caa7658a..cf7bf8380e86 100644 --- a/libs/core/algorithms/include/hpx/parallel/unseq/simd_helpers.hpp +++ b/libs/core/algorithms/include/hpx/parallel/unseq/simd_helpers.hpp @@ -13,7 +13,6 @@ #include #include - #include #include #include diff --git a/libs/core/algorithms/include/hpx/parallel/util/loop.hpp b/libs/core/algorithms/include/hpx/parallel/util/loop.hpp index e4af118ab774..a3bf3659cfb1 100644 --- a/libs/core/algorithms/include/hpx/parallel/util/loop.hpp +++ b/libs/core/algorithms/include/hpx/parallel/util/loop.hpp @@ -8,9 +8,9 @@ #include #include +#include #include #include -#include #include #include #include @@ -185,22 +185,12 @@ namespace hpx::parallel::util { } }; - template )> + template && + hpx::is_unsequenced_execution_policy_v)> HPX_HOST_DEVICE HPX_FORCEINLINE Begin tag_invoke( - hpx::parallel::util::loop_pred_t, - Begin HPX_RESTRICT begin, End HPX_RESTRICT end, Pred&& pred) - { - return unseq_first_n( - begin, std::distance(begin, end), HPX_FORWARD(Pred, pred)); - } - - template )> - HPX_HOST_DEVICE HPX_FORCEINLINE Begin tag_invoke( - hpx::parallel::util::loop_pred_t< - hpx::execution::unsequenced_task_policy>, - Begin HPX_RESTRICT begin, End HPX_RESTRICT end, Pred&& pred) + hpx::parallel::util::loop_pred_t, Begin HPX_RESTRICT begin, + End HPX_RESTRICT end, Pred&& pred) { return unseq_first_n( begin, std::distance(begin, end), HPX_FORWARD(Pred, pred)); @@ -1147,7 +1137,7 @@ namespace hpx::parallel::util { // Helper class to repeatedly call a function a given number of times // starting from a given iterator position. - template + template struct loop_idx_n { /////////////////////////////////////////////////////////////////// @@ -1185,8 +1175,8 @@ namespace hpx::parallel::util { } }; - template - struct loop_idx_n + template <> + struct loop_idx_n { /////////////////////////////////////////////////////////////////// // handle sequences of non-futures @@ -1254,7 +1244,7 @@ namespace hpx::parallel::util { std::size_t base_idx, Iter it, std::size_t count, F&& f) { using cat = typename std::iterator_traits::iterator_category; - return detail::loop_idx_n::call( + return detail::loop_idx_n::call( base_idx, it, count, HPX_FORWARD(F, f)); } @@ -1265,7 +1255,7 @@ namespace hpx::parallel::util { F&& f) { using cat = typename std::iterator_traits::iterator_category; - return detail::loop_idx_n::call( + return detail::loop_idx_n::call( base_idx, it, count, tok, HPX_FORWARD(F, f)); } }; diff --git a/libs/core/algorithms/tests/unit/algorithms/util/test_simd_helpers.cpp b/libs/core/algorithms/tests/unit/algorithms/util/test_simd_helpers.cpp index e2927525257c..ab512b4c8316 100644 --- a/libs/core/algorithms/tests/unit/algorithms/util/test_simd_helpers.cpp +++ b/libs/core/algorithms/tests/unit/algorithms/util/test_simd_helpers.cpp @@ -23,7 +23,7 @@ void test_unseq_first_n1_dispatch2(std::size_t length, std::size_t first_index) { first_index = first_index % length; - std::vector v(length, static_cast(0)); + std::vector v(length); std::size_t i = 0; std::for_each(v.begin(), v.end(), [&](T& t) {