Logo AND Algorithmique Numérique Distribuée

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