kokkos-execution 0.0.1
Loading...
Searching...
No Matches
example_hydra.cpp
Go to the documentation of this file.
2
4
19
20namespace Examples::Graph {
21
22template <typename ViewType, char Value, std::size_t Index>
23struct Write {
24 ViewType data;
25
26 KOKKOS_FUNCTION void operator()() const noexcept {
27 data(Index) = Value;
28 }
29};
30
31template <Kokkos::ExecutionSpace Exec>
33 public:
35
37 static constexpr std::string_view pattern = "SORRY HERACLES";
38
40 using view_t = Kokkos::View<char[pattern.size() + 1], Exec>;
41
42 public:
44 : base_t()
45 , data(Kokkos::view_alloc(this->exec, "data")) {
46 }
47
48 void check() const {
49 const auto mirror = Kokkos::create_mirror_view_and_copy(data);
50 const std::string_view got{mirror.data(), pattern.size()};
51
52 std::printf( // NOLINT(modernize-use-std-print)
53 "%s\n",
54 std::format(
55 "The Lernaean hydra has {} heads. They all spoke at once, inside the graph, and said: '{}'.",
56 pattern.size(),
57 got)
58 .c_str());
59
60 if (got != pattern)
61 throw std::runtime_error(
62 std::format(
63 "Heracles got to a head: got '{}', expected '{}'. Fetch Iolaus and a torch.", got, pattern));
64
65 std::printf( // NOLINT(modernize-use-std-print)
66 "Every head survived the labours. Want a bigger monster? Add characters to 'pattern'.\n");
67 }
68
69 void run() const {
71
72 auto sndr = [&]<std::size_t... Is>(std::index_sequence<Is...>) {
73 return stdexec::when_all(
74 (stdexec::schedule(gctx.get_scheduler())
75 | stdexec::then(Write<view_t, pattern.at(Is), Is>{.data = data}))...);
76 }(std::make_index_sequence<pattern.size()>{});
77
79 this->exec.fence();
80
81 stdexec::sync_wait(std::move(sndr));
82
83 check();
84 }
85
86 protected:
88};
89
90} // namespace Examples::Graph
91
92int main(int argc, char* argv[]) {
93 const Kokkos::ScopeGuard guard{argc, argv};
94 {
96 }
97 return EXIT_SUCCESS;
98}
int main(int argc, char **argv)
Kokkos::View< char[pattern.size()+1], Exec > view_t
One extra slot, zero-initialized, so the result is always null-terminated.
static constexpr std::string_view pattern
One branch (head) per character. To grow another head, extend the string.
Kokkos::utils::tests::scoped::ExecutionSpace< Exec > base_t
void operator()() 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