Logo AND Algorithmique Numérique Distribuée

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