Logo AND Algorithmique Numérique Distribuée

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