diff --git a/libs/core/algorithms/include/hpx/parallel/util/loop.hpp b/libs/core/algorithms/include/hpx/parallel/util/loop.hpp index 621efb722ad..4db43b6a98c 100644 --- a/libs/core/algorithms/include/hpx/parallel/util/loop.hpp +++ b/libs/core/algorithms/include/hpx/parallel/util/loop.hpp @@ -390,6 +390,32 @@ namespace hpx::parallel::util { return call(it, num, HPX_FORWARD(F, f), tag); } + + template + HPX_HOST_DEVICE HPX_FORCEINLINE static constexpr Iter unseq_call( + 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++); + } + // clang-format on + return it; + } + + template + HPX_HOST_DEVICE HPX_FORCEINLINE static Iter unseq_call( + Iter it, std::size_t num, CancelToken& tok, F&& f) + { + // check at the start of a partition only + if (tok.was_cancelled()) + return it; + + return unseq_call(it, num, HPX_FORWARD(F, f)); + } }; } // namespace detail @@ -426,6 +452,25 @@ namespace hpx::parallel::util { } }; + template )> + HPX_HOST_DEVICE HPX_FORCEINLINE constexpr Iter tag_invoke( + hpx::parallel::util::loop_n_t, + Iter it, std::size_t count, F&& f) + { + return detail::loop_n_helper::unseq_call(it, count, HPX_FORWARD(F, f)); + } + + template )> + HPX_HOST_DEVICE HPX_FORCEINLINE Iter tag_invoke( + hpx::parallel::util::loop_n_t, + Iter it, std::size_t count, CancelToken& tok, F&& f) + { + return detail::loop_n_helper::unseq_call( + it, count, tok, HPX_FORWARD(F, f)); + } + #if !defined(HPX_COMPUTE_DEVICE_CODE) template inline constexpr loop_n_t loop_n = loop_n_t{}; @@ -963,7 +1008,8 @@ namespace hpx::parallel::util { template )> HPX_HOST_DEVICE HPX_FORCEINLINE static constexpr Iter tag_invoke( - hpx::parallel::util::loop_idx_n_t, + 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) { @@ -971,8 +1017,8 @@ namespace hpx::parallel::util { return it; return detail::loop_idx_n::unseq_call(base_idx, it, count, - HPX_FORWARD(F, f)); + hpx::execution::unsequenced_task_policy>::unseq_call(base_idx, it, + count, HPX_FORWARD(F, f)); } #if !defined(HPX_COMPUTE_DEVICE_CODE)