omega_h
Reliable mesh adaptation
Omega_h_future.hpp
1 #ifndef OMEGA_H_Future_HPP
2 #define OMEGA_H_Future_HPP
3 
4 #include <functional>
5 #include <vector>
6 
7 #include <Omega_h_mpi.h>
8 #include "Omega_h_array.hpp"
9 #include "Omega_h_fail.hpp"
10 
11 namespace Omega_h {
12 
18 template <typename T>
19 class Future {
20  public:
21 #if OMEGA_H_MPI_NEEDS_HOST_COPY
22  using sendbuf_type = HostRead<T>;
23  using recvbuf_type = HostWrite<T>;
24 #else
25  using sendbuf_type = Read<T>;
26  using recvbuf_type = Write<T>;
27 #endif
28 
30  using callback_type = std::function<Read<T>(recvbuf_type)>;
32  using additional_callback_type = std::function<Read<T>(Read<T>)>;
33 
34 #ifdef OMEGA_H_USE_MPI
35  using requests_type = std::vector<MPI_Request>;
36 #else
37  using requests_type = std::vector<int>;
38 #endif // OMEGA_H_USE_MPI
39 
40 #if OMEGA_H_MPI_NEEDS_HOST_COPY
41  Future(HostRead<T> sendbuf, HostWrite<T> recvbuf,
42  const requests_type&& requests, const callback_type callback);
43 #else
44  Future(Read<T> sendbuf, Write<T> recvbuf, const requests_type&& requests = {},
45  const callback_type callback = [](recvbuf_type buf) -> Read<T> {
46  return buf;
47  });
48 
50  explicit Future(Write<T> recvbuf);
51 
52 #endif
54  explicit Future(Read<T> buf);
55 
57  void add_callback(const additional_callback_type& callback) {
58  auto cur_cb = callback_;
59  callback_ = [=](recvbuf_type recvbuf) -> Read<T> {
60  return callback(cur_cb(recvbuf));
61  };
62  }
63 
65  bool completed();
66 
69  Read<T> get();
70 
71  private:
73  enum class Status {
74  waiting,
75  completed,
77  consumed
78  };
79 
80  sendbuf_type sendbuf_;
81  recvbuf_type recvbuf_;
82  callback_type callback_;
83  requests_type requests_;
84  Status status_;
85 };
86 
87 #if OMEGA_H_MPI_NEEDS_HOST_COPY
88 #define OMEGA_H_EXPL_INST_DECL(T) \
89  extern template Future<T>::Future(HostRead<T> sendbuf, HostWrite<T> recvbuf, \
90  const requests_type&&, const callback_type callback);
91 #else
92 #define OMEGA_H_EXPL_INST_DECL(T) \
93  extern template Future<T>::Future(Read<T> sendbuf, Write<T> recvbuf, \
94  const requests_type&& requests, const callback_type callback); \
95  extern template Future<T>::Future(Write<T> recvbuf);
96 #endif
97 OMEGA_H_EXPL_INST_DECL(I8)
98 OMEGA_H_EXPL_INST_DECL(I32)
99 OMEGA_H_EXPL_INST_DECL(I64)
100 OMEGA_H_EXPL_INST_DECL(Real)
101 #undef OMEGA_H_EXPL_INST_DECL
102 
103 #define OMEGA_H_EXPL_INST_DECL(T) \
104  extern template Future<T>::Future(Read<T> buf); \
105  extern template bool Future<T>::completed(); \
106  extern template Read<T> Future<T>::get();
107 
108 OMEGA_H_EXPL_INST_DECL(I8)
109 OMEGA_H_EXPL_INST_DECL(I32)
110 OMEGA_H_EXPL_INST_DECL(I64)
111 OMEGA_H_EXPL_INST_DECL(Real)
112 #undef OMEGA_H_EXPL_INST_DECL
113 
114 } // namespace Omega_h
115 
116 #endif // OMEGA_H_Future_HPP
Abstraction for asynchronous communication.
Definition: Omega_h_future.hpp:19
bool completed()
Definition: Omega_h_future.cpp:42
std::function< Read< T >(recvbuf_type)> callback_type
post-processing callback for asynchronous communication
Definition: Omega_h_future.hpp:30
void add_callback(const additional_callback_type &callback)
register a additional post-communication callback
Definition: Omega_h_future.hpp:57
std::function< Read< T >(Read< T >)> additional_callback_type
optional additional processing callbacks (see add_callback method)
Definition: Omega_h_future.hpp:32
Read< T > get()
Definition: Omega_h_future.cpp:61
Definition: Omega_h_array.hpp:135
Definition: Omega_h_array.hpp:154
Definition: Omega_h_array.hpp:89
Definition: Omega_h_array.hpp:50
Definition: amr_mpi_test.cpp:6