Logo AND Algorithmique Numérique Distribuée

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