Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
group.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/net/mock/group.hpp
3  *
4  * Implementation of a mock network which does no real communication. All
5  * classes: Group, Connection, and Dispatcher are in this file since they are
6  * tightly interdependent to provide thread-safety.
7  *
8  * Part of Project Thrill - http://project-thrill.org
9  *
10  * Copyright (C) 2015 Timo Bingmann <[email protected]>
11  *
12  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
13  ******************************************************************************/
14 
15 #pragma once
16 #ifndef THRILL_NET_MOCK_GROUP_HEADER
17 #define THRILL_NET_MOCK_GROUP_HEADER
18 
20 #include <thrill/net/group.hpp>
21 
22 #include <string>
23 #include <vector>
24 
25 namespace thrill {
26 namespace net {
27 namespace mock {
28 
29 //! \addtogroup net_mock Mock Network API
30 //! \ingroup net
31 //! \{
32 
33 class Group;
34 class Dispatcher;
35 
36 /*!
37  * A virtual connection through the mock network: each Group has p Connections
38  * to its peers. The Connection hands over packets to the peers via SyncSend(),
39  * and receives them as InboundMsg().
40  */
41 class Connection final : public net::Connection
42 {
43 public:
44  //! construct from mock::Group
45  void Initialize(Group* group, size_t peer);
46 
47  //! Method which is called by other peers to enqueue a message.
48  void InboundMsg(net::Buffer&& msg);
49 
50  //! \name Base Status Functions
51  //! \{
52 
53  bool IsValid() const final { return true; }
54 
55  std::string ToString() const final;
56 
57  std::ostream& OutputOstream(std::ostream& os) const final;
58 
59  //! \}
60 
61  //! \name Send Functions
62  //! \{
63 
64  void SyncSend(
65  const void* data, size_t size, Flags /* flags */ = NoFlags) final;
66 
67  ssize_t SendOne(
68  const void* data, size_t size, Flags flags = NoFlags) final;
69 
70  //! \}
71 
72  //! \name Receive Functions
73  //! \{
74 
75  void SyncRecv(void* out_data, size_t size) final;
76 
77  ssize_t RecvOne(void* out_data, size_t size) final;
78 
79  //! \}
80 
81  //! \name Paired SendReceive Methods
82  //! \{
83 
84  void SyncSendRecv(const void* send_data, size_t send_size,
85  void* recv_data, size_t recv_size) final;
86  void SyncRecvSend(const void* send_data, size_t send_size,
87  void* recv_data, size_t recv_size) final;
88 
89  //! \}
90 
91 private:
92  //! Reference to our group.
93  Group* group_ = nullptr;
94 
95  //! Outgoing peer id of this Connection.
96  size_t peer_ = size_t(-1);
97 
98  //! for access to watch lists and mutex.
99  friend class Dispatcher;
100 
101  //! pimpl data struct with complex components
102  struct Data;
103 
104  //! pimpl data struct with complex components
105  std::unique_ptr<Data> d_;
106 
107  //! some-what internal function to extract the next packet from the queue.
108  net::Buffer RecvNext();
109 };
110 
111 /*!
112  * The central object of a mock network: the Group containing links to other
113  * mock Group forming the network. Note that there is no central object
114  * containing all Groups.
115  */
116 class Group final : public net::Group
117 {
118  static constexpr bool debug = false;
119  static constexpr bool debug_data = true;
120 
121 public:
122  //! \name Base Functions
123  //! \{
124 
125  //! Initialize a Group for the given size rank
126  Group(size_t my_rank, size_t group_size);
127 
128  ~Group();
129 
130  size_t num_hosts() const final;
131 
132  net::Connection& connection(size_t peer) final;
133 
134  void Close() final;
135 
136  using Dispatcher = mock::Dispatcher;
137 
138  std::unique_ptr<net::Dispatcher> ConstructDispatcher() const final;
139 
140  //! \}
141 
142  /*!
143  * Construct a mock network with num_hosts peers and deliver Group contexts
144  * for each of them.
145  */
146  static std::vector<std::unique_ptr<Group> > ConstructLoopbackMesh(
147  size_t num_hosts);
148 
149  //! return hexdump or just [data] if not debugging
150  static std::string MaybeHexdump(const void* data, size_t size);
151 
152 private:
153  //! vector of peers for delivery of messages.
154  std::vector<Group*> peers_;
155 
156  //! vector of virtual connection objects to remote peers
157  Connection* conns_;
158 
159  //! Send a buffer to peer tgt. Blocking, ... sort of.
160  void Send(size_t tgt, net::Buffer&& msg);
161 
162  //! for access to Send()
163  friend class Connection;
164 };
165 
166 /*!
167  * A virtual Dispatcher which waits for messages to arrive in the mock
168  * network. It is implemented as a ConcurrentBoundedQueue, into which
169  * Connections lay notification events.
170  */
171 class Dispatcher final : public net::Dispatcher
172 {
173  static constexpr bool debug = false;
174 
175 public:
176  //! type for file descriptor readiness callbacks
178 
179  Dispatcher();
180  ~Dispatcher();
181 
182  //! \name Implementation of Virtual Methods
183  //! \{
184 
185  void AddRead(net::Connection& _c, const Callback& read_cb) final;
186 
187  void AddWrite(net::Connection& _c, const Callback& write_cb) final;
188 
189  void Cancel(net::Connection&) final;
190 
191  void Notify(Connection* c);
192 
193  void Interrupt() final;
194 
195  void DispatchOne(const std::chrono::milliseconds& timeout) final;
196 
197  //! \}
198 
199 private:
200  //! pimpl data struct with complex components
201  struct Data;
202 
203  //! pimpl data struct with complex components
204  std::unique_ptr<Data> d_;
205 
206  //! callback vectors per watched connection
207  struct Watch;
208 
209  //! lookup method
210  Watch& GetWatch(Connection* c);
211 };
212 
213 //! \}
214 
215 } // namespace mock
216 } // namespace net
217 } // namespace thrill
218 
219 #endif // !THRILL_NET_MOCK_GROUP_HEADER
220 
221 /******************************************************************************/
std::chrono::milliseconds milliseconds
import into class namespace
Definition: dispatcher.hpp:489
size_t peer_
Outgoing peer id of this Connection.
Definition: group.hpp:96
The central object of a mock network: the Group containing links to other mock Group forming the netw...
Definition: group.hpp:116
ssize_t RecvOne(void *out_data, size_t size) final
Definition: group.cpp:109
std::string ToString() const final
return a string representation of this connection, for user output.
Definition: group.cpp:66
void SyncRecv(void *out_data, size_t size) final
Definition: group.cpp:102
tlx::delegate< bool(), mem::GPoolAllocator< char > > AsyncCallback
Signature of async connection readability/writability callbacks.
Definition: dispatcher.hpp:45
A virtual connection through the mock network: each Group has p Connections to its peers...
Definition: group.hpp:41
ssize_t SendOne(const void *data, size_t size, Flags flags=NoFlags) final
Definition: group.cpp:83
std::enable_if< std::is_pod< T >::value, void >::type Send(const T &value)
Definition: connection.hpp:105
A virtual Dispatcher which waits for messages to arrive in the mock network.
Definition: group.hpp:171
bool IsValid() const final
check whether the connection is (still) valid.
Definition: group.hpp:53
void SyncSendRecv(const void *send_data, size_t send_size, void *recv_data, size_t recv_size) final
Definition: group.cpp:114
std::unique_ptr< Data > d_
pimpl data struct with complex components
Definition: group.hpp:102
A Connection represents a link to another peer in a network group.
Definition: connection.hpp:49
std::ostream & OutputOstream(std::ostream &os) const final
virtual method to output to a std::ostream
Definition: group.cpp:70
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
static constexpr bool debug
std::vector< T, Allocator< T > > vector
vector with Manager tracking
Definition: allocator.hpp:228
void Initialize(Group *group, size_t peer)
construct from mock::Group
Definition: group.cpp:51
void SyncSend(const void *data, size_t size, Flags=NoFlags) final
Definition: group.cpp:76
void InboundMsg(net::Buffer &&msg)
Method which is called by other peers to enqueue a message.
Definition: group.cpp:58
friend class Dispatcher
for access to watch lists and mutex.
Definition: group.hpp:99
Simple buffer of characters without initialization or growing functionality.
Definition: buffer.hpp:40
net::Buffer RecvNext()
some-what internal function to extract the next packet from the queue.
Definition: group.cpp:88
Flags
Additional flags for sending or receiving.
Definition: connection.hpp:61
std::unique_ptr< T, Deleter< T > > unique_ptr
unique_ptr with Manager tracking
Definition: allocator.hpp:208
void SyncRecvSend(const void *send_data, size_t send_size, void *recv_data, size_t recv_size) final
Definition: group.cpp:120
Group * group_
Reference to our group.
Definition: group.hpp:93