Derecho  0.9
Distributed systems toolkit for RDMA
lf_helper.hpp
Go to the documentation of this file.
1 #ifndef LF_HELPER_HPP
2 #define LF_HELPER_HPP
3 
4 #include <cstdint>
5 #include <optional>
6 #include <functional>
7 #include <map>
8 #include <memory>
9 #include <string>
10 #include <vector>
11 #include <rdma/fabric.h>
12 
14 
15 #ifndef LF_VERSION
16 #define LF_VERSION FI_VERSION(1,5)
17 #endif
18 
19 struct fid_mr;
20 struct fid_ep;
21 struct fid_cq;
22 
28 namespace rdma {
29 class exception {};
30 class invalid_args : public exception {};
31 class configuration_failure : public exception {};
32 class connection_broken : public exception {};
33 class creation_failure : public exception {};
38 class unsupported_feature : public exception {};
39 
44 //template<typename fi_struct_type>
45 //struct close {
46 // void operator() (fi_struct_type* fi_struct) const {fi_close(fi_struct->fid);}
47 //};
48 
55  std::unique_ptr<fid_mr, std::function<void(fid_mr *)>> mr;
57  std::unique_ptr<char[]> allocated_buffer;
58 
59  friend class endpoint;
60  friend class task;
61 
62 public:
71  memory_region(size_t size);
80  memory_region(char* buffer, size_t size);
86  uint64_t get_key() const;
87 
88  char* const buffer;
89  const size_t size;
90 };
91 
93 public:
103  remote_memory_region(uint64_t remote_address, size_t length,
104  uint64_t remote_key)
105  : buffer(remote_address), size(length), rkey(remote_key) {}
106 
107  const uint64_t buffer;
108  const size_t size;
109  const uint64_t rkey;
110 };
111 
117  std::unique_ptr<fid_cq, std::function<void(fid_cq *)>> cq;
118 
119  friend class managed_endpoint;
120  friend class task;
121 
122 public:
127  explicit completion_queue();
128 };
129 
130 typedef std::function<void(uint64_t tag, uint32_t immediate, size_t length)>
132 
134 public:
135  typedef uint8_t tag_type;
136  static constexpr unsigned int shift_bits = 64 - 8 * sizeof(tag_type);
137 
138 private:
139  std::optional<tag_type> tag;
140  message_type(tag_type t) : tag(t) {}
141 
142  friend class endpoint;
143  friend class task;
144 
145 public:
146  message_type(const std::string& name, completion_handler send_handler,
147  completion_handler recv_handler,
148  completion_handler write_handler = nullptr);
150 
151  static message_type ignored();
152 };
153 
157 class endpoint {
158 protected:
160  std::unique_ptr<fid_eq, std::function<void(fid_eq *)>> eq;
161  std::unique_ptr<fid_ep, std::function<void(fid_ep *)>> ep;
162 
163  explicit endpoint() {}
164 
165  friend class task;
166 public:
167  virtual ~endpoint();
168 
175  explicit endpoint(size_t remote_index, bool is_lf_server);
188  endpoint(size_t remote_index, bool is_lf_server,
189  std::function<void(endpoint*)> post_recvs);
194  endpoint(endpoint&&) = default;
202  int init(struct fi_info *fi);
215  void connect(size_t remote_index, bool is_lf_server,
216  std::function<void(endpoint *)> post_recvs);
217 
229  bool post_send(const memory_region& mr, size_t offset,
230  size_t size, uint64_t wr_id, uint32_t immediate,
231  const message_type& type);
242  bool post_recv(const memory_region& mr, size_t offset,
243  size_t size, uint64_t wr_id,
244  const message_type& type);
245 
246  bool post_empty_send(uint64_t wr_id, uint32_t immediate,
247  const message_type& type);
248  bool post_empty_recv(uint64_t wr_id, const message_type& type);
249 
250  bool post_write(const memory_region& mr, size_t offset, size_t size,
251  uint64_t wr_id, remote_memory_region remote_mr,
252  size_t remote_offset, const message_type& type,
253  bool signaled = false, bool send_inline = false);
254 
255  fi_addr_t remote_fi_addr;
256 };
257 
258 class managed_endpoint : public endpoint {
259 public:
262  managed_endpoint(size_t remote_index,
263  std::function<void(managed_endpoint*)> post_recvs) {}
264 };
265 
266 class manager_endpoint : public endpoint {
267 public:
269  explicit manager_endpoint() {}
270 };
271 
272 class task {
273 protected:
274  struct task_impl;
275  std::unique_ptr<task_impl> impl;
276  std::shared_ptr<manager_endpoint> mep;
277 
278 public:
279  task(std::shared_ptr<manager_endpoint> manager_ep);
280  virtual ~task();
281 
282  void append_wait(const completion_queue& cq, int count, bool signaled,
283  bool last, uint64_t wr_id, const message_type& type);
284  void append_enable_send(const managed_endpoint& ep, int count);
285  void append_send(const managed_endpoint& ep, const memory_region& mr,
286  size_t offset, size_t length, uint32_t immediate);
287  void append_recv(const managed_endpoint& ep, const memory_region& mr,
288  size_t offset, size_t length);
289  bool post() __attribute__((warn_unused_result));
290 };
291 
292 namespace impl {
293  bool lf_initialize(const std::map<uint32_t, std::pair<ip_addr_t, uint16_t>>& ip_addrs_and_ports,
294  uint32_t node_rank);
295 bool lf_add_connection(uint32_t new_id, const std::pair<ip_addr_t, uint16_t> &new_ip_addr_and_port);
296 bool lf_remove_connection(uint32_t node_id);
297 bool lf_destroy();
298 
299 std::map<uint32_t, remote_memory_region> lf_exchange_memory_regions(
300  const std::vector<uint32_t>& members, uint32_t node_rank,
301  const memory_region& mr);
302 
303 bool set_interrupt_mode(bool enabled);
304 } /* namespace impl */
305 } /* namespace rdma */
306 
307 #endif /* LF_HELPER_HPP */
std::function< void(uint64_t tag, uint32_t immediate, size_t length)> completion_handler
Definition: lf_helper.hpp:131
remote_memory_region(uint64_t remote_address, size_t length, uint64_t remote_key)
Constructor Takes in parameters representing a remote memory region.
Definition: lf_helper.hpp:103
std::unique_ptr< fid_mr, std::function< void(fid_mr *)> > mr
Smart pointer for managing the registered memory region.
Definition: lf_helper.hpp:55
managed_endpoint(size_t remote_index, std::function< void(managed_endpoint *)> post_recvs)
TODO Implement the constructor.
Definition: lf_helper.hpp:262
std::unique_ptr< fid_cq, std::function< void(fid_cq *)> > cq
Smart pointer for managing the completion queue.
Definition: lf_helper.hpp:117
fi_addr_t remote_fi_addr
Definition: lf_helper.hpp:255
A wrapper for fi_close.
Definition: lf_helper.hpp:53
struct rdma::completion_handler_set __attribute__
A C++ wrapper for the libfabric fid_cq struct and its associated functions.
Definition: lf_helper.hpp:115
uint32_t node_rank
Definition: experiment.cpp:45
completion_queue scq
Definition: lf_helper.hpp:260
std::unique_ptr< fid_eq, std::function< void(fid_eq *)> > eq
Smart pointer for managing the endpoint.
Definition: lf_helper.hpp:160
uint32_t rkey
Remote key.
Definition: verbs.hpp:290
Contains functions and classes for low-level RDMA operations, such as setting up memory regions and q...
Definition: lf_helper.hpp:28
bool lf_remove_connection(uint32_t node_id)
Removes a node&#39;s TCP connection, presumably because it has failed.
Definition: lf_helper.cpp:578
message_type(tag_type t)
Definition: lf_helper.hpp:140
std::map< uint32_t, remote_memory_region > lf_exchange_memory_regions(const std::vector< uint32_t > &members, uint32_t node_rank, const memory_region &mr)
Definition: lf_helper.cpp:727
bool lf_add_connection(uint32_t new_id, const std::pair< ip_addr_t, uint16_t > &new_ip_addr_and_port)
Adds a node to the group via tcp.
Definition: lf_helper.cpp:569
const size_t size
Definition: lf_helper.hpp:89
A C++ wrapper for the libfabric fid_ep struct and its associated functions.
Definition: lf_helper.hpp:157
std::unique_ptr< task_impl > impl
Definition: lf_helper.hpp:274
std::unique_ptr< char[]> allocated_buffer
Smart pointer for managing the buffer the mr uses.
Definition: lf_helper.hpp:57
manager_endpoint()
TODO: Implement the constructor.
Definition: lf_helper.hpp:269
bool set_interrupt_mode(bool enabled)
Definition: lf_helper.cpp:752
char *const buffer
Definition: lf_helper.hpp:88
bool lf_destroy()
Definition: lf_helper.cpp:723
std::optional< tag_type > tag
Definition: lf_helper.hpp:139
bool lf_initialize(const std::map< uint32_t, std::pair< ip_addr_t, uint16_t >> &ip_addrs_and_ports, uint32_t node_rank)
Initialize the global context.
Definition: lf_helper.cpp:660
std::unique_ptr< fid_ep, std::function< void(fid_ep *)> > ep
Definition: lf_helper.hpp:161
std::shared_ptr< manager_endpoint > mep
Definition: lf_helper.hpp:276