Logo AND Algorithmique Numérique Distribuée

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