Itoyori  v0.0.1
root_exec.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ityr/common/util.hpp"
4 #include "ityr/ito/ito.hpp"
5 #include "ityr/ori/ori.hpp"
6 
7 namespace ityr {
8 
46 template <typename Fn, typename... Args>
47 inline auto root_exec(Fn&& fn, Args&&... args) {
49 
50  ori::release();
52  ori::acquire();
53 
54  using retval_t = std::invoke_result_t<Fn, Args...>;
55  if constexpr (std::is_void_v<retval_t>) {
57  []() { ori::poll(); },
58  std::forward<Fn>(fn), std::forward<Args>(args)...);
59  // TODO: release() is needed only for the last worker which executed the root thread
60  ori::release();
62  ori::acquire();
63 
64  } else {
65  static_assert(std::is_trivially_copyable_v<retval_t>);
66 
68  []() { ori::poll(); },
69  std::forward<Fn>(fn), std::forward<Args>(args)...);
70  ori::release();
72  ori::acquire();
73  return ret;
74  }
75 }
76 
108 template <typename Fn, typename... Args>
109 inline auto coll_exec(const Fn& fn, const Args&... args) {
111 
112  ori::release();
113 
114  using retval_t = std::invoke_result_t<Fn, Args...>;
115  if constexpr (std::is_void_v<retval_t>) {
116  ito::coll_exec([=]() {
117  ori::acquire();
118  fn(args...);
119  ori::release();
120  });
121 
122  ori::acquire();
123 
124  } else {
125  auto ret = ito::coll_exec([=]() {
126  ori::acquire();
127  auto ret = fn(args...);
128  ori::release();
129  return ret;
130  });
131 
132  ori::acquire();
133 
134  return ret;
135  }
136 }
137 
138 }
#define ITYR_CHECK(cond)
Definition: util.hpp:48
MPI_Comm mpicomm()
Definition: topology.hpp:206
va_list args
Definition: util.hpp:76
void mpi_barrier(MPI_Comm comm)
Definition: mpi_util.hpp:42
auto root_exec(Fn &&fn, Args &&... args)
Definition: ito.hpp:50
auto coll_exec(const Fn &fn, const Args &... args)
Definition: ito.hpp:72
bool is_root()
Definition: ito.hpp:66
bool is_spmd()
Definition: ito.hpp:61
constexpr with_callback_t with_callback
Definition: thread.hpp:11
void poll()
Definition: ori.hpp:224
void release()
Definition: ori.hpp:196
void acquire()
Definition: ori.hpp:206
Definition: allocator.hpp:16
auto coll_exec(const Fn &fn, const Args &... args)
Execute the same function collectively on all processes.
Definition: root_exec.hpp:109
auto root_exec(Fn &&fn, Args &&... args)
Spawn the root thread (collective).
Definition: root_exec.hpp:47