kokkos-execution 0.0.1
Loading...
Searching...
No Matches
parallel_for.hpp
Go to the documentation of this file.
1#ifndef KOKKOS_EXECUTION_PARALLEL_FOR_HPP
2#define KOKKOS_EXECUTION_PARALLEL_FOR_HPP
3
4#include "Kokkos_Macros.hpp"
5#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
6import kokkos.core_impl;
7#endif
8
16
17namespace Kokkos::Execution {
18
19namespace Impl {
20
21template <typename Data>
22concept parallel_for_data = requires {
23 typename Data::functor_t;
24 typename Data::policy_or_iter_space_t;
25 typename Data::properties_t;
26
28 requires stdexec::__is_instance_of<typename Data::properties_t, properties>;
29 requires requires(const Data& data) {
30 { data.functor } -> std::same_as<const typename Data::functor_t&>;
31 { data.policy_or_iter_space } -> std::same_as<const typename Data::policy_or_iter_space_t&>;
32 };
33};
34
35template <typename Functor, execution_policy_or_iteration_space ExecPolicyOrIterSpace, typename... Properties>
36struct ParallelForData;
37
38template <stdexec::sender Sndr, Impl::parallel_for_data Data>
40
41} // namespace Impl
42
45 template <typename Label, typename Functor, typename ExecPolicy>
46 requires(Impl::label<std::remove_cvref_t<Label>> && Kokkos::ExecutionPolicy<std::remove_cvref_t<ExecPolicy>>)
47 constexpr auto operator()(Label&& label, ExecPolicy&& policy, Functor&& functor) const STDEXEC_AUTO_RETURN(
48 stdexec::__closure(
49 *this,
50 std::forward<ExecPolicy>(policy),
51 std::forward<Functor>(functor),
52 Impl::Label{std::forward<Label>(label)}))
53
54 template <typename Functor, typename ExecPolicyOrIterSpace>
55 requires Impl::execution_policy_or_iteration_space<std::remove_cvref_t<ExecPolicyOrIterSpace>>
56 constexpr auto
57 operator()(ExecPolicyOrIterSpace&& policy_or_iter_space, Functor&& functor) const STDEXEC_AUTO_RETURN(
58 stdexec::__closure(
59 *this,
60 std::forward<ExecPolicyOrIterSpace>(policy_or_iter_space),
61 std::forward<Functor>(functor)))
62
63 template <typename Label, typename Functor, std::integral T>
65 constexpr auto operator()(Label&& label, const T work_count, Functor&& functor) const STDEXEC_AUTO_RETURN(
66 stdexec::__closure(
67 *this,
68 Impl::make_iter_space(work_count),
69 std::forward<Functor>(functor),
70 Impl::Label{std::forward<Label>(label)}))
71
72 template <typename Functor, std::integral T>
73 constexpr auto operator()(const T work_count, Functor&& functor) const STDEXEC_AUTO_RETURN(
74 stdexec::__closure(*this, Impl::make_iter_space(work_count), std::forward<Functor>(functor)))
75 template <typename Functor, typename ExecPolicyOrIterSpace, typename... Properties>
77 std::remove_cvref_t<Functor>,
78 std::remove_cvref_t<ExecPolicyOrIterSpace>,
79 std::remove_cvref_t<Properties>...
80 >;
81
82 template <stdexec::sender Sndr, typename Functor, typename ExecPolicyOrIterSpace, typename... Properties>
84 constexpr auto operator()(
85 Sndr&& sndr,
86 ExecPolicyOrIterSpace&& policy_or_iter_space,
87 Functor&& functor,
88 Properties&&... properties) const
91 {
93 parallel_for_data_t<Functor, ExecPolicyOrIterSpace, Properties...>{
94 std::forward<Properties>(properties)...,
95 std::forward<Functor>(functor),
96 std::forward<ExecPolicyOrIterSpace>(policy_or_iter_space)},
97 std::forward<Sndr>(sndr)
98 })
99
100 template <stdexec::sender Sndr, typename Label, typename Functor, typename ExecPolicyOrIterSpace>
101 requires(
102 Impl::label<std::remove_cvref_t<Label>>
103 && Impl::execution_policy_or_iteration_space<std::remove_cvref_t<ExecPolicyOrIterSpace>>)
104 constexpr auto
105 operator()(Sndr&& sndr, Label&& label, ExecPolicyOrIterSpace&& policy_or_iter_space, Functor&& functor) const
106 STDEXEC_AUTO_RETURN(this->operator()(
107 std::forward<Sndr>(sndr),
108 std::forward<ExecPolicyOrIterSpace>(policy_or_iter_space),
109 std::forward<Functor>(functor),
110 Impl::Label{std::forward<Label>(label)}))
111};
112
113namespace Impl {
114
115template <typename Functor, execution_policy_or_iteration_space ExecPolicyOrIterSpace, typename... Properties>
116struct ParallelForData : Properties... {
117 using functor_t = Functor;
118 using policy_or_iter_space_t = ExecPolicyOrIterSpace;
119 using properties_t = properties<Properties...>;
120
123
125 template <typename Self, typename UnaryOp>
126 constexpr STDEXEC_EXPLICIT_THIS_BEGIN(auto transform_policy_or_iter_space)(
127 this Self&& self, // NOLINT(cppcoreguidelines-missing-std-forward)
128 UnaryOp&& unary_op)
129 STDEXEC_AUTO_RETURN(
131 functor_t,
132 std::remove_cvref_t<
133 std::invoke_result_t<UnaryOp, decltype(stdexec::__forward_like<Self>(self.policy_or_iter_space))>
134 >,
135 Properties...
136 >{stdexec::__forward_like<Self>(detail::as_base<Properties>(self))...,
137 stdexec::__forward_like<Self>(self.functor),
138 std::forward<UnaryOp>(unary_op)(stdexec::__forward_like<Self>(self.policy_or_iter_space))})
139 STDEXEC_EXPLICIT_THIS_END(transform_policy_or_iter_space)
140};
141
142template <stdexec::sender Sndr, Impl::parallel_for_data Data>
143struct ParallelForSender : stdexec::__tuple<parallel_for_t, Data, Sndr> {
144 using sender_concept = stdexec::sender_tag;
145
148 using __data_t = Data; // NOLINT(bugprone-reserved-identifier)
149 using __children_t = stdexec::__mlist<Sndr>; // NOLINT(bugprone-reserved-identifier)
151
152 using base_t = stdexec::__tuple<parallel_for_t, __data_t, Sndr>;
153
154 KOKKOS_EXECUTION_COMPL_SIGS_ADD(ParallelForSender, Sndr, stdexec::set_error_t(std::exception_ptr))
155
156 template <stdexec::receiver Rcvr>
157 constexpr auto connect(Rcvr) && = delete;
158
159 template <stdexec::receiver Rcvr>
160 constexpr auto connect(Rcvr) const & = delete;
161
162 static constexpr size_t idx_sndr = 2;
164 Sndr,
165 stdexec::__get<idx_sndr>(static_cast<const base_t&>(*this)))
166};
167
168} // namespace Impl
169
170inline constexpr parallel_for_t parallel_for{};
171
172} // namespace Kokkos::Execution
173
174#endif // KOKKOS_EXECUTION_PARALLEL_FOR_HPP
#define KOKKOS_EXECUTION_COMPL_SIGS_ADD(_decayed_self_type_, _sndr_type_,...)
Constrain the accepted label types.
Definition label.hpp:40
#define KOKKOS_EXECUTION_IMPL_FORWARDING_ATTRIBUTES_GET_ENV(_type_, _obj_)
#define KOKKOS_EXECUTION_RETURNS_BRACED(_type_,...)
Define a function by returning a list-initialized object of an explicit type.
Definition macros.hpp:23
constexpr auto make_iter_space(const T work_count) noexcept -> Kokkos::dims< 1, T >
Make a valid iteration space from a work count.
consteval Impl::Label< std::string_view > label(std::string_view str) noexcept
Definition label.hpp:91
constexpr parallel_for_t parallel_for
constexpr auto transform_policy_or_iter_space(this Self &&self, UnaryOp &&unary_op) noexcept(noexcept(ParallelForData< functor_t, std::remove_cvref_t< std::invoke_result_t< UnaryOp, decltype(stdexec::__forward_like< Self >(self.policy_or_iter_space))> >, Properties... >{stdexec::__forward_like< Self >(detail::as_base< Properties >(self))..., stdexec::__forward_like< Self >(self.functor), std::forward< UnaryOp >(unary_op)(stdexec::__forward_like< Self >(self.policy_or_iter_space))})) -> decltype(ParallelForData< functor_t, std::remove_cvref_t< std::invoke_result_t< UnaryOp, decltype(stdexec::__forward_like< Self >(self.policy_or_iter_space))> >, Properties... >{stdexec::__forward_like< Self >(detail::as_base< Properties >(self))..., stdexec::__forward_like< Self >(self.functor), std::forward< UnaryOp >(unary_op)(stdexec::__forward_like< Self >(self.policy_or_iter_space))})
constexpr auto connect(Rcvr) &&=delete
stdexec::__tuple< parallel_for_t, __data_t, Sndr > base_t
constexpr auto connect(Rcvr) const &=delete
Type list of properties.
Custom algorithm for the Kokkos::parallel_for construct.
constexpr auto operator()(Label &&label, const T work_count, Functor &&functor) const noexcept(noexcept(stdexec::__closure(*this, Impl::make_iter_space(work_count), std::forward< Functor >(functor), Impl::Label{std::forward< Label >(label)}))) -> decltype(stdexec::__closure(*this, Impl::make_iter_space(work_count), std::forward< Functor >(functor), Impl::Label{std::forward< Label >(label)}))
constexpr auto operator()(Sndr &&sndr, ExecPolicyOrIterSpace &&policy_or_iter_space, Functor &&functor, Properties &&... properties) const noexcept(noexcept(Impl::ParallelForSender< Sndr, parallel_for_data_t< Functor, ExecPolicyOrIterSpace, Properties... > >{{ parallel_for_t{}, parallel_for_data_t< Functor, ExecPolicyOrIterSpace, Properties... >{ std::forward< Properties >(properties)..., std::forward< Functor >(functor), std::forward< ExecPolicyOrIterSpace >(policy_or_iter_space)}, std::forward< Sndr >(sndr) }})) -> Impl::ParallelForSender< Sndr, parallel_for_data_t< Functor, ExecPolicyOrIterSpace, Properties... > >
Impl::ParallelForData< std::remove_cvref_t< Functor >, std::remove_cvref_t< ExecPolicyOrIterSpace >, std::remove_cvref_t< Properties >... > parallel_for_data_t