Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce code duplication.
[simgrid.git] / src / s4u / s4u_Comm.cpp
1 /* Copyright (c) 2006-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/msg/msg_private.hpp"
7 #include "xbt/log.h"
8
9 #include "simgrid/Exception.hpp"
10 #include "simgrid/s4u/Comm.hpp"
11 #include "simgrid/s4u/Mailbox.hpp"
12
13 #include <simgrid/comm.h>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm, s4u_activity, "S4U asynchronous communications");
16
17 namespace simgrid {
18 namespace s4u {
19 xbt::signal<void(Comm const&, bool is_sender)> Comm::on_start;
20 xbt::signal<void(Comm const&)> Comm::on_completion;
21
22 void Comm::complete(Activity::State state)
23 {
24   Activity::complete(state);
25   on_completion(*this);
26 }
27
28 Comm::~Comm()
29 {
30   if (state_ == State::STARTED && not detached_ &&
31       (pimpl_ == nullptr || pimpl_->state_ == kernel::activity::State::RUNNING)) {
32     XBT_INFO("Comm %p freed before its completion. Did you forget to detach it? (state: %s)", this, get_state_str());
33     if (pimpl_ != nullptr)
34       XBT_INFO("pimpl_->state: %s", pimpl_->get_state_str());
35     else
36       XBT_INFO("pimpl_ is null");
37     xbt_backtrace_display_current();
38   }
39 }
40
41 int Comm::wait_any_for(const std::vector<CommPtr>* comms, double timeout)
42 {
43   std::vector<kernel::activity::CommImpl*> rcomms(comms->size());
44   std::transform(begin(*comms), end(*comms), begin(rcomms),
45                  [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
46   int changed_pos = simcall_comm_waitany(rcomms.data(), rcomms.size(), timeout);
47   if (changed_pos != -1)
48     comms->at(changed_pos)->complete(State::FINISHED);
49   return changed_pos;
50 }
51
52 void Comm::wait_all(const std::vector<CommPtr>* comms)
53 {
54   // TODO: this should be a simcall or something
55   // TODO: we are missing a version with timeout
56   for (CommPtr comm : *comms)
57     comm->wait();
58 }
59
60 CommPtr Comm::set_rate(double rate)
61 {
62   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
63              __FUNCTION__);
64   rate_ = rate;
65   return this;
66 }
67
68 CommPtr Comm::set_src_data(void* buff)
69 {
70   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
71              __FUNCTION__);
72   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
73   src_buff_ = buff;
74   return this;
75 }
76
77 CommPtr Comm::set_src_data_size(size_t size)
78 {
79   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
80              __FUNCTION__);
81   src_buff_size_ = size;
82   return this;
83 }
84
85 CommPtr Comm::set_src_data(void* buff, size_t size)
86 {
87   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
88              __FUNCTION__);
89
90   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
91   src_buff_      = buff;
92   src_buff_size_ = size;
93   return this;
94 }
95
96 CommPtr Comm::set_dst_data(void** buff)
97 {
98   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
99              __FUNCTION__);
100   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
101   dst_buff_ = buff;
102   return this;
103 }
104 void* Comm::get_dst_data()
105 {
106   return dst_buff_;
107 }
108
109 size_t Comm::get_dst_data_size() const
110 {
111   return dst_buff_size_;
112 }
113 CommPtr Comm::set_dst_data(void** buff, size_t size)
114 {
115   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
116              __FUNCTION__);
117
118   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
119   dst_buff_      = buff;
120   dst_buff_size_ = size;
121   return this;
122 }
123 CommPtr Comm::set_payload_size(double bytes)
124 {
125   Activity::set_remaining(bytes);
126   return this;
127 }
128
129 CommPtr Comm::sendto_init(Host* from, Host* to)
130 {
131   CommPtr res(new Comm());
132   res->from_ = from;
133   res->to_   = to;
134
135   return res;
136 }
137
138 CommPtr Comm::sendto_async(Host* from, Host* to, double simulated_size_in_bytes)
139 {
140   auto res = Comm::sendto_init(from, to)->set_payload_size(simulated_size_in_bytes);
141   res->vetoable_start();
142   return res;
143 }
144
145 void Comm::sendto(Host* from, Host* to, double simulated_size_in_bytes)
146 {
147   sendto_async(from, to, simulated_size_in_bytes)->wait();
148 }
149
150 Comm* Comm::start()
151 {
152   xbt_assert(get_state() == State::INITED || get_state() == State::STARTING,
153              "You cannot use %s() once your communication started (not implemented)", __FUNCTION__);
154   if (from_ != nullptr || to_ != nullptr) {
155     xbt_assert(from_ != nullptr && to_ != nullptr, "When either from_ or to_ is specified, both must be.");
156     xbt_assert(src_buff_ == nullptr && dst_buff_ == nullptr,
157                "Direct host-to-host communications cannot carry any data.");
158     pimpl_ = kernel::actor::simcall([this] {
159       kernel::activity::CommImplPtr res(new kernel::activity::CommImpl(this->from_, this->to_, this->get_remaining()));
160       res->start();
161       return res;
162     });
163
164   } else if (src_buff_ != nullptr) { // Sender side
165     on_start(*this, true /* is_sender*/);
166     pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
167                                 clean_fun_, copy_data_function_, get_user_data(), detached_);
168   } else if (dst_buff_ != nullptr) { // Receiver side
169     xbt_assert(not detached_, "Receive cannot be detached");
170     on_start(*this, false /*is_sender*/);
171     pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
172                                 copy_data_function_, get_user_data(), rate_);
173
174   } else {
175     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
176   }
177
178   if (suspended_)
179     pimpl_->suspend();
180
181   state_ = State::STARTED;
182   return this;
183 }
184
185 /** @brief Block the calling actor until the communication is finished, or until timeout
186  *
187  * On timeout, an exception is thrown and the communication is invalidated.
188  *
189  * @param timeout the amount of seconds to wait for the comm termination.
190  *                Negative values denote infinite wait times. 0 as a timeout returns immediately. */
191 Comm* Comm::wait_for(double timeout)
192 {
193   switch (state_) {
194     case State::FINISHED:
195       break;
196
197     case State::INITED:
198     case State::STARTING: // It's not started yet. Do it in one simcall if it's a regular communication
199       if (from_ != nullptr || to_ != nullptr) {
200         return vetoable_start()->wait_for(timeout); // In the case of host2host comm, do it in two simcalls
201       } else if (src_buff_ != nullptr) {
202         on_start(*this, true /*is_sender*/);
203         simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
204                           copy_data_function_, get_user_data(), timeout);
205
206       } else { // Receiver
207         on_start(*this, false /*is_sender*/);
208         simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
209                           get_user_data(), timeout, rate_);
210       }
211       break;
212
213     case State::STARTED:
214       simcall_comm_wait(get_impl(), timeout);
215       break;
216
217     case State::CANCELED:
218       throw CancelException(XBT_THROW_POINT, "Communication canceled");
219
220     default:
221       THROW_IMPOSSIBLE;
222   }
223   complete(State::FINISHED);
224   return this;
225 }
226
227 int Comm::test_any(const std::vector<CommPtr>* comms)
228 {
229   std::vector<kernel::activity::CommImpl*> rcomms(comms->size());
230   std::transform(begin(*comms), end(*comms), begin(rcomms),
231                  [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
232   int changed_pos = simcall_comm_testany(rcomms.data(), rcomms.size());
233   if (changed_pos != -1)
234     comms->at(changed_pos)->complete(State::FINISHED);
235   return changed_pos;
236 }
237
238 Comm* Comm::detach()
239 {
240   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication is %s (not implemented)",
241              __FUNCTION__, get_state_str());
242   xbt_assert(dst_buff_ == nullptr && dst_buff_size_ == 0, "You can only detach sends, not recvs");
243   detached_ = true;
244   vetoable_start();
245   return this;
246 }
247
248 bool Comm::test() // TODO: merge with Activity::test, once modernized
249 {
250   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
251              state_ == State::CANCELED || state_ == State::FINISHED);
252
253   if (state_ == State::CANCELED || state_ == State::FINISHED)
254     return true;
255
256   if (state_ == State::INITED || state_ == State::STARTING)
257     this->vetoable_start();
258
259   if (simcall_comm_test(get_impl())) {
260     complete(State::FINISHED);
261     return true;
262   }
263   return false;
264 }
265
266 Mailbox* Comm::get_mailbox() const
267 {
268   return mailbox_;
269 }
270
271 Actor* Comm::get_sender() const
272 {
273   kernel::actor::ActorImplPtr sender = nullptr;
274   if (pimpl_)
275     sender = boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->src_actor_;
276   return sender ? sender->get_ciface() : nullptr;
277 }
278
279 } // namespace s4u
280 } // namespace simgrid
281 /* **************************** Public C interface *************************** */
282 void sg_comm_detach(sg_comm_t comm, void (*clean_function)(void*))
283 {
284   comm->detach(clean_function);
285   comm->unref();
286 }
287 void sg_comm_unref(sg_comm_t comm)
288 {
289   comm->unref();
290 }
291 int sg_comm_test(sg_comm_t comm)
292 {
293   bool finished = comm->test();
294   if (finished)
295     comm->unref();
296   return finished;
297 }
298
299 sg_error_t sg_comm_wait(sg_comm_t comm)
300 {
301   return sg_comm_wait_for(comm, -1);
302 }
303
304 sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout)
305 {
306   sg_error_t status = SG_OK;
307
308   simgrid::s4u::CommPtr s4u_comm(comm, false);
309   try {
310     s4u_comm->wait_for(timeout);
311   } catch (const simgrid::TimeoutException&) {
312     status = SG_ERROR_TIMEOUT;
313   } catch (const simgrid::CancelException&) {
314     status = SG_ERROR_CANCELED;
315   } catch (const simgrid::NetworkFailureException&) {
316     status = SG_ERROR_NETWORK;
317   }
318   return status;
319 }
320
321 void sg_comm_wait_all(sg_comm_t* comms, size_t count)
322 {
323   std::vector<simgrid::s4u::CommPtr> s4u_comms;
324   for (unsigned int i = 0; i < count; i++)
325     s4u_comms.emplace_back(comms[i], false);
326
327   simgrid::s4u::Comm::wait_all(&s4u_comms);
328 }
329
330 int sg_comm_wait_any(sg_comm_t* comms, size_t count)
331 {
332   return sg_comm_wait_any_for(comms, count, -1);
333 }
334
335 int sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout)
336 {
337   std::vector<simgrid::s4u::CommPtr> s4u_comms;
338   for (unsigned int i = 0; i < count; i++)
339     s4u_comms.emplace_back(comms[i], false);
340
341   int pos = simgrid::s4u::Comm::wait_any_for(&s4u_comms, timeout);
342   for (unsigned i = 0; i < count; i++) {
343     if (pos != -1 && static_cast<unsigned>(pos) != i)
344       s4u_comms[i]->add_ref();
345   }
346   return pos;
347 }