Skip to content

Commit

Permalink
par_unseq in most cases
Browse files Browse the repository at this point in the history
Signed-off-by: Hari Hara Naveen S <[email protected]>
  • Loading branch information
Johan511 committed Aug 9, 2023
1 parent 215ae4f commit bfb7da1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include <hpx/config.hpp>
#include <hpx/execution/traits/is_execution_policy.hpp>
#include <hpx/functional/detail/tag_fallback_invoke.hpp>
#include <hpx/functional/invoke.hpp>
#include <hpx/parallel/algorithms/detail/advance_to_sentinel.hpp>
Expand All @@ -34,6 +35,8 @@ namespace hpx::parallel::detail {
sequential_find_t<ExPolicy>, Iterator first, Sentinel last,
T const& value, Proj proj = Proj())
{
static_assert(hpx::is_sequenced_execution_policy<ExPolicy>::value ||
hpx::is_unsequenced_execution_policy<ExPolicy>::value);
return util::loop_pred<ExPolicy>(
first, last, [&value, &proj](auto const& curr) {
return HPX_INVOKE(proj, *curr) == value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace hpx::parallel::util {
Compiler and Hardware should also support vector operations for IterDiff,
else we see slower performance when compared to sequential version
*/
// f(Iter) -> bool
template <typename Iter, typename IterDiff, typename F>
Iter unseq_first_n(Iter const first, IterDiff const n, F&& f) noexcept
{
Expand Down
44 changes: 44 additions & 0 deletions libs/core/algorithms/include/hpx/parallel/util/loop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,20 @@ namespace hpx::parallel::util {

return call(base_idx, it, num, HPX_FORWARD(F, f));
}

template <typename Iter, typename F>
HPX_HOST_DEVICE HPX_FORCEINLINE static constexpr Iter unseq_call(
std::size_t base_idx, Iter it, std::size_t num, F&& f)
{
// clang-format off
HPX_VECTORIZE
for (std::size_t i = 0; i < num; i++) // -V112
{
HPX_INVOKE(f, *(it + i), base_idx++);
}
// clang-format on
return it + num;
}
};
} // namespace detail

Expand Down Expand Up @@ -931,6 +945,36 @@ namespace hpx::parallel::util {
}
};

template <typename Iter, typename CancelToken, typename F,
HPX_CONCEPT_REQUIRES_(hpx::traits::is_random_access_iterator_v<Iter>)>
HPX_HOST_DEVICE HPX_FORCEINLINE static constexpr Iter tag_invoke(
hpx::parallel::util::loop_idx_n_t<hpx::execution::unsequenced_policy>,
std::size_t base_idx, Iter it, std::size_t count, CancelToken& tok,
F&& f)
{
if (tok.was_cancelled(base_idx))
return it;

return detail::loop_idx_n<std::random_access_iterator_tag,
hpx::execution::unsequenced_policy>::unseq_call(base_idx, it, count,
HPX_FORWARD(F, f));
}

template <typename Iter, typename CancelToken, typename F,
HPX_CONCEPT_REQUIRES_(hpx::traits::is_random_access_iterator_v<Iter>)>
HPX_HOST_DEVICE HPX_FORCEINLINE static constexpr Iter tag_invoke(
hpx::parallel::util::loop_idx_n_t<hpx::execution::unsequenced_task_policy>,
std::size_t base_idx, Iter it, std::size_t count, CancelToken& tok,
F&& f)
{
if (tok.was_cancelled(base_idx))
return it;

return detail::loop_idx_n<std::random_access_iterator_tag,
hpx::execution::unsequenced_task_policy>::unseq_call(base_idx, it, count,
HPX_FORWARD(F, f));
}

#if !defined(HPX_COMPUTE_DEVICE_CODE)
template <typename ExPolicy>
inline constexpr loop_idx_n_t<ExPolicy> loop_idx_n =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> v(length, static_cast<T>(0));
std::vector<T> v(length);
std::size_t i = 0;

std::for_each(v.begin(), v.end(), [&](T& t) {
Expand Down

0 comments on commit bfb7da1

Please sign in to comment.