Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename method for consistency.
[simgrid.git] / src / s4u / s4u_Comm.cpp
1 /* Copyright (c) 2006-2022. 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/comm.h>
11 #include <simgrid/s4u/Comm.hpp>
12 #include <simgrid/s4u/Engine.hpp>
13 #include <simgrid/s4u/Mailbox.hpp>
14
15 #include "src/kernel/activity/CommImpl.hpp"
16 #include "src/kernel/actor/ActorImpl.hpp"
17 #include "src/kernel/actor/SimcallObserver.hpp"
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm, s4u_activity, "S4U asynchronous communications");
20
21 namespace simgrid {
22 namespace s4u {
23 xbt::signal<void(Comm const&)> Comm::on_send;
24 xbt::signal<void(Comm const&)> Comm::on_recv;
25 xbt::signal<void(Comm const&)> Comm::on_completion;
26
27 CommPtr Comm::set_copy_data_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t))
28 {
29   copy_data_function_ = callback;
30   return this;
31 }
32
33 void Comm::copy_buffer_callback(kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
34 {
35   XBT_DEBUG("Copy the data over");
36   memcpy(comm->dst_buff_, buff, buff_size);
37   if (comm->is_detached()) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the
38                              // original buffer available to the application ASAP
39     xbt_free(buff);
40     comm->src_buff_ = nullptr;
41   }
42 }
43
44 void Comm::copy_pointer_callback(kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
45 {
46   xbt_assert((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
47   *(void**)(comm->dst_buff_) = buff;
48 }
49
50 Comm::~Comm()
51 {
52   if (state_ == State::STARTED && not detached_ &&
53       (pimpl_ == nullptr || pimpl_->get_state() == kernel::activity::State::RUNNING)) {
54     XBT_INFO("Comm %p freed before its completion. Did you forget to detach it? (state: %s)", this, get_state_str());
55     if (pimpl_ != nullptr)
56       XBT_INFO("pimpl_->state: %s", pimpl_->get_state_str());
57     else
58       XBT_INFO("pimpl_ is null");
59     xbt_backtrace_display_current();
60   }
61 }
62
63 CommPtr Comm::sendto_init()
64 {
65   CommPtr res(new Comm());
66   res->pimpl_ = kernel::activity::CommImplPtr(new kernel::activity::CommImpl());
67   boost::static_pointer_cast<kernel::activity::CommImpl>(res->pimpl_)->detach();
68   res->sender_ = kernel::actor::ActorImpl::self();
69   return res;
70 }
71
72 CommPtr Comm::sendto_init(Host* from, Host* to)
73 {
74   auto res = Comm::sendto_init()->set_source(from)->set_destination(to);
75   res->set_state(State::STARTING);
76   return res;
77 }
78
79 CommPtr Comm::sendto_async(Host* from, Host* to, uint64_t simulated_size_in_bytes)
80 {
81   return Comm::sendto_init()->set_payload_size(simulated_size_in_bytes)->set_source(from)->set_destination(to);
82 }
83
84 void Comm::sendto(Host* from, Host* to, uint64_t simulated_size_in_bytes)
85 {
86   sendto_async(from, to, simulated_size_in_bytes)->wait();
87 }
88
89 CommPtr Comm::set_source(Host* from)
90 {
91   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
92              "Cannot change the source of a Comm once it's started (state: %s)", to_c_str(state_));
93   boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->set_source(from);
94   // Setting 'source' may allow to start the activity, let's try
95   if (state_ == State::STARTING && remains_ <= 0)
96     XBT_DEBUG("This communication has a payload size of 0 byte. It cannot start yet");
97   else
98     vetoable_start();
99
100   return this;
101 }
102 Host* Comm::get_source() const
103 {
104   return pimpl_ ? boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->get_source() : nullptr;
105 }
106
107 CommPtr Comm::set_destination(Host* to)
108 {
109   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
110              "Cannot change the destination of a Comm once it's started (state: %s)", to_c_str(state_));
111   boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->set_destination(to);
112   // Setting 'destination' may allow to start the activity, let's try
113   if (state_ == State::STARTING && remains_ <= 0)
114     XBT_DEBUG("This communication has a payload size of 0 byte. It cannot start yet");
115   else
116     vetoable_start();
117
118   return this;
119 }
120
121 Host* Comm::get_destination() const
122 {
123   return pimpl_ ? boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->get_destination() : nullptr;
124 }
125
126 CommPtr Comm::set_rate(double rate)
127 {
128   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
129              __FUNCTION__);
130   rate_ = rate;
131   return this;
132 }
133
134 CommPtr Comm::set_mailbox(Mailbox* mailbox)
135 {
136   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
137              __FUNCTION__);
138   mailbox_ = mailbox;
139   return this;
140 }
141
142 CommPtr Comm::set_src_data(void* buff)
143 {
144   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
145              __FUNCTION__);
146   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
147   src_buff_ = buff;
148   return this;
149 }
150
151 CommPtr Comm::set_src_data_size(size_t size)
152 {
153   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
154              __FUNCTION__);
155   src_buff_size_ = size;
156   return this;
157 }
158
159 CommPtr Comm::set_src_data(void* buff, size_t size)
160 {
161   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
162              __FUNCTION__);
163
164   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
165   src_buff_      = buff;
166   src_buff_size_ = size;
167   return this;
168 }
169
170 CommPtr Comm::set_dst_data(void** buff)
171 {
172   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
173              __FUNCTION__);
174   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
175   dst_buff_ = buff;
176   return this;
177 }
178
179 CommPtr Comm::set_dst_data(void** buff, size_t size)
180 {
181   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
182              __FUNCTION__);
183
184   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
185   dst_buff_      = buff;
186   dst_buff_size_ = size;
187   return this;
188 }
189
190 CommPtr Comm::set_payload_size(uint64_t bytes)
191 {
192   Activity::set_remaining(bytes);
193   if (pimpl_) {
194     boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->set_size(bytes);
195   }
196   return this;
197 }
198
199 Actor* Comm::get_sender() const
200 {
201   kernel::actor::ActorImplPtr sender = nullptr;
202   if (pimpl_)
203     sender = boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->src_actor_;
204   return sender ? sender->get_ciface() : nullptr;
205 }
206
207 bool Comm::is_assigned() const
208 {
209   return (pimpl_ && boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->is_assigned()) ||
210          mailbox_ != nullptr;
211 }
212
213 Comm* Comm::start()
214 {
215   xbt_assert(get_state() == State::INITED || get_state() == State::STARTING,
216              "You cannot use %s() once your communication started (not implemented)", __FUNCTION__);
217   if (get_source() != nullptr || get_destination() != nullptr) {
218     xbt_assert(is_assigned(), "When either from_ or to_ is specified, both must be.");
219     xbt_assert(src_buff_ == nullptr && dst_buff_ == nullptr,
220                "Direct host-to-host communications cannot carry any data.");
221     XBT_DEBUG("host-to-host Comm. Pimpl already created and set, just start it.");
222     kernel::actor::simcall_answered([this] {
223       pimpl_->set_state(kernel::activity::State::READY);
224       boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->start();
225     });
226   } else if (src_buff_ != nullptr) { // Sender side
227     on_send(*this);
228     kernel::actor::CommIsendSimcall observer{sender_,
229                                              mailbox_->get_impl(),
230                                              remains_,
231                                              rate_,
232                                              static_cast<unsigned char*>(src_buff_),
233                                              src_buff_size_,
234                                              match_fun_,
235                                              clean_fun_,
236                                              copy_data_function_,
237                                              get_data<void>(),
238                                              detached_};
239     pimpl_ = kernel::actor::simcall_answered([&observer] { return kernel::activity::CommImpl::isend(&observer); },
240                                              &observer);
241   } else if (dst_buff_ != nullptr) { // Receiver side
242     xbt_assert(not detached_, "Receive cannot be detached");
243     on_recv(*this);
244     kernel::actor::CommIrecvSimcall observer{receiver_,
245                                              mailbox_->get_impl(),
246                                              static_cast<unsigned char*>(dst_buff_),
247                                              &dst_buff_size_,
248                                              match_fun_,
249                                              copy_data_function_,
250                                              get_data<void>(),
251                                              rate_};
252     pimpl_ = kernel::actor::simcall_answered([&observer] { return kernel::activity::CommImpl::irecv(&observer); },
253                                              &observer);
254   } else {
255     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
256   }
257
258   if (suspended_)
259     pimpl_->suspend();
260
261   if (not detached_) {
262     pimpl_->set_iface(this);
263     pimpl_->set_actor(sender_);
264   }
265
266   state_ = State::STARTED;
267   return this;
268 }
269
270 Comm* Comm::detach()
271 {
272   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
273              "You cannot use %s() once your communication is %s (not implemented)", __FUNCTION__, get_state_str());
274   xbt_assert(dst_buff_ == nullptr && dst_buff_size_ == 0, "You can only detach sends, not recvs");
275   detached_ = true;
276   vetoable_start();
277   return this;
278 }
279
280 ssize_t Comm::test_any(const std::vector<CommPtr>& comms)
281 {
282   std::vector<ActivityPtr> activities;
283   for (const auto& comm : comms)
284     activities.push_back(boost::dynamic_pointer_cast<Activity>(comm));
285   return Activity::test_any(activities);
286 }
287
288 /** @brief Block the calling actor until the communication is finished, or until timeout
289  *
290  * On timeout, an exception is thrown and the communication is invalidated.
291  *
292  * @param timeout the amount of seconds to wait for the comm termination.
293  *                Negative values denote infinite wait times. 0 as a timeout returns immediately. */
294 Comm* Comm::wait_for(double timeout)
295 {
296   XBT_DEBUG("Calling Comm::wait_for with state %s", get_state_str());
297   kernel::actor::ActorImpl* issuer = nullptr;
298   switch (state_) {
299     case State::FINISHED:
300       break;
301     case State::FAILED:
302       throw NetworkFailureException(XBT_THROW_POINT, "Cannot wait for a failed communication");
303     case State::INITED:
304     case State::STARTING: // It's not started yet. Do it in one simcall if it's a regular communication
305       if (get_source() != nullptr || get_destination() != nullptr) {
306         return vetoable_start()->wait_for(timeout); // In the case of host2host comm, do it in two simcalls
307       } else if (src_buff_ != nullptr) {
308         on_send(*this);
309         simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
310                           copy_data_function_, get_data<void>(), timeout);
311
312       } else { // Receiver
313         on_recv(*this);
314         simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
315                           get_data<void>(), timeout, rate_);
316       }
317       break;
318     case State::STARTED:
319       try {
320         issuer = kernel::actor::ActorImpl::self();
321         kernel::actor::ActivityWaitSimcall observer{issuer, pimpl_.get(), timeout};
322         if (kernel::actor::simcall_blocking(
323                 [&observer] { observer.get_activity()->wait_for(observer.get_issuer(), observer.get_timeout()); },
324                 &observer)) {
325           throw TimeoutException(XBT_THROW_POINT, "Timeouted");
326         }
327       } catch (const NetworkFailureException& e) {
328         issuer->simcall_.observer_ = nullptr; // Comm failed on network failure, reset the observer to nullptr
329         complete(State::FAILED);
330         e.rethrow_nested(XBT_THROW_POINT, boost::core::demangle(typeid(e).name()) + " raised in kernel mode.");
331       }
332       break;
333
334     case State::CANCELED:
335       throw CancelException(XBT_THROW_POINT, "Communication canceled");
336
337     default:
338       THROW_IMPOSSIBLE;
339   }
340   complete(State::FINISHED);
341   return this;
342 }
343
344 ssize_t Comm::wait_any_for(const std::vector<CommPtr>& comms, double timeout)
345 {
346   std::vector<ActivityPtr> activities;
347   for (const auto& comm : comms)
348     activities.push_back(boost::dynamic_pointer_cast<Activity>(comm));
349   ssize_t changed_pos;
350   try {
351     changed_pos = Activity::wait_any_for(activities, timeout);
352   } catch (const NetworkFailureException& e) {
353     changed_pos = -1;
354     for (auto c : comms) {
355       if (c->pimpl_->get_state() == kernel::activity::State::FAILED) {
356         c->complete(State::FAILED);
357       }
358     }
359     e.rethrow_nested(XBT_THROW_POINT, boost::core::demangle(typeid(e).name()) + " raised in kernel mode.");
360   }
361   return changed_pos;
362 }
363
364 void Comm::wait_all(const std::vector<CommPtr>& comms)
365 {
366   // TODO: this should be a simcall or something
367   for (auto& comm : comms)
368     comm->wait();
369 }
370
371 size_t Comm::wait_all_for(const std::vector<CommPtr>& comms, double timeout)
372 {
373   if (timeout < 0.0) {
374     wait_all(comms);
375     return comms.size();
376   }
377
378   double deadline = Engine::get_clock() + timeout;
379   std::vector<CommPtr> waited_comm(1, nullptr);
380   for (size_t i = 0; i < comms.size(); i++) {
381     double wait_timeout = std::max(0.0, deadline - Engine::get_clock());
382     waited_comm[0]      = comms[i];
383     // Using wait_any_for() here (and not wait_for) because we don't want comms to be invalidated on timeout
384     if (wait_any_for(waited_comm, wait_timeout) == -1) {
385       XBT_DEBUG("Timeout (%g): i = %zu", wait_timeout, i);
386       return i;
387     }
388   }
389   return comms.size();
390 }
391 } // namespace s4u
392 } // namespace simgrid
393 /* **************************** Public C interface *************************** */
394 void sg_comm_detach(sg_comm_t comm, void (*clean_function)(void*))
395 {
396   comm->detach(clean_function);
397   comm->unref();
398 }
399 void sg_comm_unref(sg_comm_t comm)
400 {
401   comm->unref();
402 }
403 int sg_comm_test(sg_comm_t comm)
404 {
405   bool finished = comm->test();
406   if (finished)
407     comm->unref();
408   return finished;
409 }
410
411 sg_error_t sg_comm_wait(sg_comm_t comm)
412 {
413   return sg_comm_wait_for(comm, -1);
414 }
415
416 sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout)
417 {
418   sg_error_t status = SG_OK;
419
420   simgrid::s4u::CommPtr s4u_comm(comm, false);
421   try {
422     s4u_comm->wait_for(timeout);
423   } catch (const simgrid::TimeoutException&) {
424     status = SG_ERROR_TIMEOUT;
425   } catch (const simgrid::CancelException&) {
426     status = SG_ERROR_CANCELED;
427   } catch (const simgrid::NetworkFailureException&) {
428     status = SG_ERROR_NETWORK;
429   }
430   return status;
431 }
432
433 void sg_comm_wait_all(sg_comm_t* comms, size_t count)
434 {
435   sg_comm_wait_all_for(comms, count, -1);
436 }
437
438 size_t sg_comm_wait_all_for(sg_comm_t* comms, size_t count, double timeout)
439 {
440   std::vector<simgrid::s4u::CommPtr> s4u_comms;
441   for (size_t i = 0; i < count; i++)
442     s4u_comms.emplace_back(comms[i], false);
443
444   size_t pos = simgrid::s4u::Comm::wait_all_for(s4u_comms, timeout);
445   for (size_t i = pos; i < count; i++)
446     s4u_comms[i]->add_ref();
447   return pos;
448 }
449
450 ssize_t sg_comm_wait_any(sg_comm_t* comms, size_t count)
451 {
452   return sg_comm_wait_any_for(comms, count, -1);
453 }
454
455 ssize_t sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout)
456 {
457   std::vector<simgrid::s4u::CommPtr> s4u_comms;
458   for (size_t i = 0; i < count; i++)
459     s4u_comms.emplace_back(comms[i], false);
460
461   ssize_t pos = simgrid::s4u::Comm::wait_any_for(s4u_comms, timeout);
462   for (size_t i = 0; i < count; i++) {
463     if (pos != -1 && static_cast<size_t>(pos) != i)
464       s4u_comms[i]->add_ref();
465   }
466   return pos;
467 }