kokkos-execution 0.0.1
Loading...
Searching...
No Matches
example_parallel_for.cpp
Go to the documentation of this file.
2
4
16
17namespace Examples::Graph {
18
19template <char Value, typename ViewType>
20struct Work {
21 ViewType data;
22
23 template <std::integral IndexType>
24 KOKKOS_FUNCTION void operator()(const IndexType index) const noexcept {
25 data(index) = Value;
26 }
27};
28
29template <Kokkos::ExecutionSpace Exec>
31 public:
33 using policy_t = Kokkos::RangePolicy<Exec>;
34 using view_t = Kokkos::View<char[6], Exec>;
35
36 public:
38 : base_t()
39 , data(Kokkos::view_alloc(Kokkos::WithoutInitializing, this->exec, "data")) {
40 }
41
42 void check() const {
43 const auto mirror = Kokkos::create_mirror_view_and_copy(data);
44 if (std::strcmp(mirror.data(), "ABCBA") != 0) {
45 throw std::runtime_error(std::format("'{}' is not equal to 'ABCBA'.", mirror.data()));
46 }
47 }
48
49 void reset() const {
50 Kokkos::deep_copy(this->exec, data, 0);
51 }
52
54 void with_kokkos_graph() const {
55 reset();
56
57 const auto graph = Kokkos::Experimental::create_graph(
58 Kokkos::Experimental::get_device_handle(this->exec), [&](const auto& root) {
59 root.then_parallel_for(policy_t(0, 5), Work<'A', view_t>{.data = data})
60 .then_parallel_for(policy_t(1, 4), Work<'B', view_t>{.data = data})
61 .then_parallel_for(policy_t(2, 3), Work<'C', view_t>{.data = data});
62 });
63
64 graph.submit(this->exec);
65 this->exec.fence();
66
67 check();
68 }
69
71 void with_graph_context() const {
72 reset();
73
75
76 stdexec::sync_wait(
77 stdexec::schedule(gctx.get_scheduler())
81
82 check();
83 }
84
85 protected:
87};
88
89
90} // namespace Examples::Graph
91
92int main(int argc, char* argv[]) {
93 const Kokkos::ScopeGuard guard{argc, argv};
94 {
96
97 pfor.with_kokkos_graph();
98 pfor.with_graph_context();
99 }
100 return EXIT_SUCCESS;
101}
int main(int argc, char **argv)
constexpr parallel_for_t parallel_for
Kokkos::RangePolicy< Exec > policy_t
Kokkos::View< char[6], Exec > view_t
void with_kokkos_graph() const
Create the sequence with Kokkos::Graph.
Kokkos::utils::tests::scoped::ExecutionSpace< Exec > base_t
void with_graph_context() const
Create the sequence with Kokkos::Execution::GraphContext.
void operator()(const IndexType index) const noexcept
Execution context using Kokkos::Experimental::Graph under the hood.
Definition graph.hpp:123
auto get_scheduler() const noexcept -> GraphImpl::Scheduler< execution_space >
Definition graph.hpp:134