Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make SIMIX_comm_copy_data_callback a member of CommImpl.
[simgrid.git] / src / kernel / activity / CommImpl.cpp
1 /* Copyright (c) 2007-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/kernel/activity/CommImpl.hpp"
7 #include "simgrid/Exception.hpp"
8 #include "simgrid/kernel/resource/Action.hpp"
9 #include "simgrid/kernel/routing/NetPoint.hpp"
10 #include "simgrid/modelchecker.h"
11 #include "simgrid/s4u/Host.hpp"
12 #include "src/kernel/activity/MailboxImpl.hpp"
13 #include "src/kernel/context/Context.hpp"
14 #include "src/mc/mc_replay.hpp"
15 #include "src/surf/cpu_interface.hpp"
16 #include "src/surf/network_interface.hpp"
17 #include "src/surf/surf_interface.hpp"
18
19 #include <boost/range/algorithm.hpp>
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix, "SIMIX network-related synchronization");
21
22 XBT_PRIVATE void simcall_HANDLER_comm_send(smx_simcall_t simcall, smx_actor_t src, smx_mailbox_t mbox, double task_size,
23                                            double rate, unsigned char* src_buff, size_t src_buff_size,
24                                            bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
25                                            void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
26                                            void* data, double timeout)
27 {
28   simgrid::kernel::activity::ActivityImplPtr comm = simcall_HANDLER_comm_isend(
29       simcall, src, mbox, task_size, rate, src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, false);
30   simcall->mc_value_ = 0;
31   simcall_HANDLER_comm_wait(simcall, static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
32 }
33
34 XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr simcall_HANDLER_comm_isend(
35     smx_simcall_t /*simcall*/, smx_actor_t src_proc, smx_mailbox_t mbox, double task_size, double rate,
36     unsigned char* src_buff, size_t src_buff_size,
37     bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
38     void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
39     void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), // used to copy data if not default one
40     void* data, bool detached)
41 {
42   XBT_DEBUG("send from mailbox %p", mbox);
43
44   /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
45   simgrid::kernel::activity::CommImplPtr this_comm(
46       new simgrid::kernel::activity::CommImpl(simgrid::kernel::activity::CommImpl::Type::SEND));
47
48   /* Look for communication synchro matching our needs. We also provide a description of
49    * ourself so that the other side also gets a chance of choosing if it wants to match with us.
50    *
51    * If it is not found then push our communication into the rendez-vous point */
52   simgrid::kernel::activity::CommImplPtr other_comm =
53       mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::RECEIVE, match_fun, data, this_comm,
54                                /*done*/ false, /*remove_matching*/ true);
55
56   if (not other_comm) {
57     other_comm = std::move(this_comm);
58
59     if (mbox->permanent_receiver_ != nullptr) {
60       // this mailbox is for small messages, which have to be sent right now
61       other_comm->state_     = simgrid::kernel::activity::State::READY;
62       other_comm->dst_actor_ = mbox->permanent_receiver_.get();
63       mbox->done_comm_queue_.push_back(other_comm);
64       XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, other_comm.get());
65
66     } else {
67       mbox->push(other_comm);
68     }
69   } else {
70     XBT_DEBUG("Receive already pushed");
71
72     other_comm->state_ = simgrid::kernel::activity::State::READY;
73   }
74
75   if (detached) {
76     other_comm->detach();
77     other_comm->clean_fun = clean_fun;
78   } else {
79     other_comm->clean_fun = nullptr;
80     src_proc->activities_.emplace_back(other_comm);
81   }
82
83   /* Setup the communication synchro */
84   other_comm->src_actor_ = src_proc;
85   other_comm->src_data_  = data;
86   (*other_comm).set_src_buff(src_buff, src_buff_size).set_size(task_size).set_rate(rate);
87
88   other_comm->match_fun     = match_fun;
89   other_comm->copy_data_fun = copy_data_fun;
90
91   if (MC_is_active() || MC_record_replay_is_active())
92     other_comm->state_ = simgrid::kernel::activity::State::RUNNING;
93   else
94     other_comm->start();
95
96   return (detached ? nullptr : other_comm);
97 }
98
99 XBT_PRIVATE void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
100                                            unsigned char* dst_buff, size_t* dst_buff_size,
101                                            bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
102                                            void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
103                                            void* data, double timeout, double rate)
104 {
105   simgrid::kernel::activity::ActivityImplPtr comm = simcall_HANDLER_comm_irecv(
106       simcall, receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun, data, rate);
107   simcall->mc_value_ = 0;
108   simcall_HANDLER_comm_wait(simcall, static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
109 }
110
111 XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr
112 simcall_HANDLER_comm_irecv(smx_simcall_t /*simcall*/, smx_actor_t receiver, smx_mailbox_t mbox, unsigned char* dst_buff,
113                            size_t* dst_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
114                            void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
115                            double rate)
116 {
117   simgrid::kernel::activity::CommImplPtr this_synchro(
118       new simgrid::kernel::activity::CommImpl(simgrid::kernel::activity::CommImpl::Type::RECEIVE));
119   XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
120
121   simgrid::kernel::activity::CommImplPtr other_comm;
122   // communication already done, get it inside the list of completed comms
123   if (mbox->permanent_receiver_ != nullptr && not mbox->done_comm_queue_.empty()) {
124     XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
125     // find a match in the list of already received comms
126     other_comm = mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::SEND, match_fun, data,
127                                           this_synchro, /*done*/ true,
128                                           /*remove_matching*/ true);
129     // if not found, assume the receiver came first, register it to the mailbox in the classical way
130     if (not other_comm) {
131       XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request "
132                 "into list");
133       other_comm = std::move(this_synchro);
134       mbox->push(other_comm);
135     } else {
136       if (other_comm->surf_action_ && other_comm->get_remaining() < 1e-12) {
137         XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
138         other_comm->state_ = simgrid::kernel::activity::State::DONE;
139         other_comm->set_mailbox(nullptr);
140       }
141     }
142   } else {
143     /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
144
145     /* Look for communication activity matching our needs. We also provide a description of
146      * ourself so that the other side also gets a chance of choosing if it wants to match with us.
147      *
148      * If it is not found then push our communication into the rendez-vous point */
149     other_comm = mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::SEND, match_fun, data,
150                                           this_synchro, /*done*/ false,
151                                           /*remove_matching*/ true);
152
153     if (other_comm == nullptr) {
154       XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->comm_queue_.size());
155       other_comm = std::move(this_synchro);
156       mbox->push(other_comm);
157     } else {
158       XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
159
160       other_comm->state_ = simgrid::kernel::activity::State::READY;
161     }
162     receiver->activities_.emplace_back(other_comm);
163   }
164
165   /* Setup communication synchro */
166   other_comm->dst_actor_ = receiver;
167   other_comm->dst_data_  = data;
168   other_comm->set_dst_buff(dst_buff, dst_buff_size);
169
170   if (rate > -1.0 && (other_comm->get_rate() < 0.0 || rate < other_comm->get_rate()))
171     other_comm->set_rate(rate);
172
173   other_comm->match_fun     = match_fun;
174   other_comm->copy_data_fun = copy_data_fun;
175
176   if (MC_is_active() || MC_record_replay_is_active()) {
177     other_comm->state_ = simgrid::kernel::activity::State::RUNNING;
178     return other_comm;
179   }
180   other_comm->start();
181   return other_comm;
182 }
183
184 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comm, double timeout)
185 {
186   /* Associate this simcall to the wait synchro */
187   XBT_DEBUG("simcall_HANDLER_comm_wait, %p", comm);
188
189   comm->register_simcall(simcall);
190
191   if (MC_is_active() || MC_record_replay_is_active()) {
192     int idx = simcall->mc_value_;
193     if (idx == 0) {
194       comm->state_ = simgrid::kernel::activity::State::DONE;
195     } else {
196       /* If we reached this point, the wait simcall must have a timeout */
197       /* Otherwise it shouldn't be enabled and executed by the MC */
198       if (timeout < 0.0)
199         THROW_IMPOSSIBLE;
200
201       if (comm->src_actor_ == simcall->issuer_)
202         comm->state_ = simgrid::kernel::activity::State::SRC_TIMEOUT;
203       else
204         comm->state_ = simgrid::kernel::activity::State::DST_TIMEOUT;
205     }
206
207     comm->finish();
208     return;
209   }
210
211   /* If the synchro has already finish perform the error handling, */
212   /* otherwise set up a waiting timeout on the right side          */
213   if (comm->state_ != simgrid::kernel::activity::State::WAITING &&
214       comm->state_ != simgrid::kernel::activity::State::RUNNING) {
215     comm->finish();
216   } else { /* we need a sleep action (even when there is no timeout) to be notified of host failures */
217     simgrid::kernel::resource::Action* sleep = simcall->issuer_->get_host()->pimpl_cpu->sleep(timeout);
218     sleep->set_activity(comm);
219
220     if (simcall->issuer_ == comm->src_actor_)
221       comm->src_timeout_ = sleep;
222     else
223       comm->dst_timeout_ = sleep;
224   }
225 }
226
227 void simcall_HANDLER_comm_test(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comm)
228 {
229   bool res;
230
231   if (MC_is_active() || MC_record_replay_is_active()) {
232     res = comm->src_actor_ && comm->dst_actor_;
233     if (res)
234       comm->state_ = simgrid::kernel::activity::State::DONE;
235   } else {
236     res = comm->state_ != simgrid::kernel::activity::State::WAITING &&
237           comm->state_ != simgrid::kernel::activity::State::RUNNING;
238   }
239
240   simcall_comm_test__set__result(simcall, res);
241   if (res) {
242     comm->simcalls_.push_back(simcall);
243     comm->finish();
244   } else {
245     simcall->issuer_->simcall_answer();
246   }
247 }
248
249 void simcall_HANDLER_comm_testany(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comms[], size_t count)
250 {
251   // The default result is -1 -- this means, "nothing is ready".
252   // It can be changed below, but only if something matches.
253   simcall_comm_testany__set__result(simcall, -1);
254
255   if (MC_is_active() || MC_record_replay_is_active()) {
256     int idx = simcall->mc_value_;
257     if (idx == -1) {
258       simcall->issuer_->simcall_answer();
259     } else {
260       simgrid::kernel::activity::CommImpl* comm = comms[idx];
261       simcall_comm_testany__set__result(simcall, idx);
262       comm->simcalls_.push_back(simcall);
263       comm->state_ = simgrid::kernel::activity::State::DONE;
264       comm->finish();
265     }
266     return;
267   }
268
269   for (std::size_t i = 0; i != count; ++i) {
270     simgrid::kernel::activity::CommImpl* comm = comms[i];
271     if (comm->state_ != simgrid::kernel::activity::State::WAITING &&
272         comm->state_ != simgrid::kernel::activity::State::RUNNING) {
273       simcall_comm_testany__set__result(simcall, i);
274       comm->simcalls_.push_back(simcall);
275       comm->finish();
276       return;
277     }
278   }
279   simcall->issuer_->simcall_answer();
280 }
281
282 static void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall)
283 {
284   simgrid::kernel::activity::CommImpl** comms = simcall_comm_waitany__get__comms(simcall);
285   size_t count                                = simcall_comm_waitany__get__count(simcall);
286
287   for (size_t i = 0; i < count; i++) {
288     // Remove the first occurrence of simcall:
289     auto* comm = comms[i];
290     auto j     = boost::range::find(comm->simcalls_, simcall);
291     if (j != comm->simcalls_.end())
292       comm->simcalls_.erase(j);
293   }
294 }
295 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comms[], size_t count,
296                                   double timeout)
297 {
298   if (MC_is_active() || MC_record_replay_is_active()) {
299     if (timeout > 0.0)
300       xbt_die("Timeout not implemented for waitany in the model-checker");
301     int idx                 = simcall->mc_value_;
302     auto* comm              = comms[idx];
303     comm->simcalls_.push_back(simcall);
304     simcall_comm_waitany__set__result(simcall, idx);
305     comm->state_ = simgrid::kernel::activity::State::DONE;
306     comm->finish();
307     return;
308   }
309
310   if (timeout < 0.0) {
311     simcall->timeout_cb_ = nullptr;
312   } else {
313     simcall->timeout_cb_ = simgrid::simix::Timer::set(SIMIX_get_clock() + timeout, [simcall]() {
314       simcall->timeout_cb_ = nullptr;
315       SIMIX_waitany_remove_simcall_from_actions(simcall);
316       simcall_comm_waitany__set__result(simcall, -1);
317       simcall->issuer_->simcall_answer();
318     });
319   }
320
321   for (size_t i = 0; i < count; i++) {
322     /* associate this simcall to the the synchro */
323     auto* comm = comms[i];
324     comm->simcalls_.push_back(simcall);
325
326     /* see if the synchro is already finished */
327     if (comm->state_ != simgrid::kernel::activity::State::WAITING &&
328         comm->state_ != simgrid::kernel::activity::State::RUNNING) {
329       comm->finish();
330       break;
331     }
332   }
333 }
334
335 /******************************************************************************/
336 /*                    SIMIX_comm_copy_data callbacks                       */
337 /******************************************************************************/
338 void SIMIX_comm_set_copy_data_callback(void (*callback)(simgrid::kernel::activity::CommImpl*, void*, size_t))
339 {
340   simgrid::kernel::activity::CommImpl::set_copy_data_callback(callback);
341 }
342
343 void SIMIX_comm_copy_buffer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
344 {
345   XBT_DEBUG("Copy the data over");
346   memcpy(comm->dst_buff_, buff, buff_size);
347   if (comm->detached()) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the
348                           // original buffer available to the application ASAP
349     xbt_free(buff);
350     comm->src_buff_ = nullptr;
351   }
352 }
353
354 void SIMIX_comm_copy_pointer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
355 {
356   xbt_assert((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
357   *(void**)(comm->dst_buff_) = buff;
358 }
359
360 namespace simgrid {
361 namespace kernel {
362 namespace activity {
363
364 void (*CommImpl::copy_data_callback_)(CommImpl*, void*, size_t) = &SIMIX_comm_copy_pointer_callback;
365
366 void CommImpl::set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t))
367 {
368   copy_data_callback_ = callback;
369 }
370
371 CommImpl& CommImpl::set_size(double size)
372 {
373   size_ = size;
374   return *this;
375 }
376
377 CommImpl& CommImpl::set_rate(double rate)
378 {
379   rate_ = rate;
380   return *this;
381 }
382 CommImpl& CommImpl::set_mailbox(MailboxImpl* mbox)
383 {
384   mbox_ = mbox;
385   return *this;
386 }
387
388 CommImpl& CommImpl::set_src_buff(unsigned char* buff, size_t size)
389 {
390   src_buff_      = buff;
391   src_buff_size_ = size;
392   return *this;
393 }
394
395 CommImpl& CommImpl::set_dst_buff(unsigned char* buff, size_t* size)
396 {
397   dst_buff_      = buff;
398   dst_buff_size_ = size;
399   return *this;
400 }
401
402 CommImpl& CommImpl::detach()
403 {
404   detached_ = true;
405   return *this;
406 }
407
408 CommImpl::CommImpl(s4u::Host* from, s4u::Host* to, double bytes) : size_(bytes), detached_(true), from_(from), to_(to)
409 {
410   state_ = State::READY;
411 }
412
413 CommImpl::~CommImpl()
414 {
415   XBT_DEBUG("Really free communication %p in state %s (detached = %d)", this, get_state_str(), detached_);
416
417   cleanup_surf();
418
419   if (detached_ && state_ != State::DONE) {
420     /* the communication has failed and was detached:
421      * we have to free the buffer */
422     if (clean_fun)
423       clean_fun(src_buff_);
424     src_buff_ = nullptr;
425   } else if (mbox_) {
426     mbox_->remove(this);
427   }
428 }
429
430 /**  @brief Starts the simulation of a communication synchro. */
431 CommImpl* CommImpl::start()
432 {
433   /* If both the sender and the receiver are already there, start the communication */
434   if (state_ == State::READY) {
435     from_ = from_ != nullptr ? from_ : src_actor_->get_host();
436     to_   = to_ != nullptr ? to_ : dst_actor_->get_host();
437
438     /* FIXME[donassolo]: getting the network_model from the origin host
439      * Soon we need to change this function to first get the routes and later
440      * create the respective surf actions */
441     auto net_model = from_->get_netpoint()->get_englobing_zone()->get_network_model();
442
443     surf_action_ = net_model->communicate(from_, to_, size_, rate_);
444     surf_action_->set_activity(this);
445     surf_action_->set_category(get_tracing_category());
446     state_ = State::RUNNING;
447
448     XBT_DEBUG("Starting communication %p from '%s' to '%s' (surf_action: %p; state: %s)", this, from_->get_cname(),
449               to_->get_cname(), surf_action_, get_state_str());
450
451     /* If a link is failed, detect it immediately */
452     if (surf_action_->get_state() == resource::Action::State::FAILED) {
453       XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure", from_->get_cname(),
454                 to_->get_cname());
455       state_ = State::LINK_FAILURE;
456       post();
457
458     } else if ((src_actor_ != nullptr && src_actor_->is_suspended()) ||
459                (dst_actor_ != nullptr && dst_actor_->is_suspended())) {
460       /* If any of the process is suspended, create the synchro but stop its execution,
461          it will be restarted when the sender process resume */
462       if (src_actor_->is_suspended())
463         XBT_DEBUG("The communication is suspended on startup because src (%s@%s) was suspended since it initiated the "
464                   "communication",
465                   src_actor_->get_cname(), src_actor_->get_host()->get_cname());
466       else
467         XBT_DEBUG("The communication is suspended on startup because dst (%s@%s) was suspended since it initiated the "
468                   "communication",
469                   dst_actor_->get_cname(), dst_actor_->get_host()->get_cname());
470
471       surf_action_->suspend();
472     }
473   }
474
475   return this;
476 }
477
478 /** @brief Copy the communication data from the sender's buffer to the receiver's one  */
479 void CommImpl::copy_data()
480 {
481   size_t buff_size = src_buff_size_;
482   /* If there is no data to copy then return */
483   if (not src_buff_ || not dst_buff_ || copied_)
484     return;
485
486   XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", this,
487             src_actor_ ? src_actor_->get_host()->get_cname() : "a finished process", src_buff_,
488             dst_actor_ ? dst_actor_->get_host()->get_cname() : "a finished process", dst_buff_, buff_size);
489
490   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
491   if (dst_buff_size_) {
492     buff_size = std::min(buff_size, *(dst_buff_size_));
493
494     /* Update the receiver's buffer size to the copied amount */
495     *dst_buff_size_ = buff_size;
496   }
497
498   if (buff_size > 0) {
499     if (copy_data_fun)
500       copy_data_fun(this, src_buff_, buff_size);
501     else
502       copy_data_callback_(this, src_buff_, buff_size);
503   }
504
505   /* Set the copied flag so we copy data only once */
506   /* (this function might be called from both communication ends) */
507   copied_ = true;
508 }
509
510 void CommImpl::suspend()
511 {
512   /* FIXME: shall we suspend also the timeout synchro? */
513   if (surf_action_)
514     surf_action_->suspend();
515   /* if not created yet, the action will be suspended on creation, in CommImpl::start() */
516 }
517
518 void CommImpl::resume()
519 {
520   /*FIXME: check what happen with the timeouts */
521   if (surf_action_)
522     surf_action_->resume();
523   /* in the other case, the synchro were not really suspended yet, see CommImpl::suspend() and CommImpl::start() */
524 }
525
526 void CommImpl::cancel()
527 {
528   /* if the synchro is a waiting state means that it is still in a mbox so remove from it and delete it */
529   if (state_ == State::WAITING) {
530     if (not detached_) {
531       mbox_->remove(this);
532       state_ = State::CANCELED;
533     }
534   } else if (not MC_is_active() /* when running the MC there are no surf actions */
535              && not MC_record_replay_is_active() && (state_ == State::READY || state_ == State::RUNNING)) {
536     surf_action_->cancel();
537   }
538 }
539
540 /** @brief This is part of the cleanup process, probably an internal command */
541 void CommImpl::cleanup_surf()
542 {
543   clean_action();
544
545   if (src_timeout_) {
546     src_timeout_->unref();
547     src_timeout_ = nullptr;
548   }
549
550   if (dst_timeout_) {
551     dst_timeout_->unref();
552     dst_timeout_ = nullptr;
553   }
554 }
555
556 void CommImpl::post()
557 {
558   /* Update synchro state */
559   if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FINISHED)
560     state_ = State::SRC_TIMEOUT;
561   else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FINISHED)
562     state_ = State::DST_TIMEOUT;
563   else if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FAILED)
564     state_ = State::SRC_HOST_FAILURE;
565   else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FAILED)
566     state_ = State::DST_HOST_FAILURE;
567   else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
568     state_ = State::LINK_FAILURE;
569   } else
570     state_ = State::DONE;
571
572   XBT_DEBUG("CommImpl::post(): comm %p, state %s, src_proc %p, dst_proc %p, detached: %d", this, get_state_str(),
573             src_actor_.get(), dst_actor_.get(), detached_);
574
575   /* destroy the surf actions associated with the Simix communication */
576   cleanup_surf();
577
578   /* Answer all simcalls associated with the synchro */
579   finish();
580 }
581
582 void CommImpl::finish()
583 {
584   while (not simcalls_.empty()) {
585     smx_simcall_t simcall = simcalls_.front();
586     simcalls_.pop_front();
587
588     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
589      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
590      * simcall */
591
592     if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
593       continue;                                 // if actor handling comm is killed
594     if (simcall->call_ == simix::Simcall::COMM_WAITANY) {
595       SIMIX_waitany_remove_simcall_from_actions(simcall);
596       if (simcall->timeout_cb_) {
597         simcall->timeout_cb_->remove();
598         simcall->timeout_cb_ = nullptr;
599       }
600       if (not MC_is_active() && not MC_record_replay_is_active()) {
601         CommImpl** comms   = simcall_comm_waitany__get__comms(simcall);
602         size_t count       = simcall_comm_waitany__get__count(simcall);
603         CommImpl** element = std::find(comms, comms + count, this);
604         int rank           = (element != comms + count) ? element - comms : -1;
605         simcall_comm_waitany__set__result(simcall, rank);
606       }
607     }
608
609     /* If the synchro is still in a rendez-vous point then remove from it */
610     if (mbox_)
611       mbox_->remove(this);
612
613     XBT_DEBUG("CommImpl::finish(): synchro state = %d", static_cast<int>(state_));
614
615     /* Check out for errors */
616
617     if (not simcall->issuer_->get_host()->is_on()) {
618       simcall->issuer_->context_->set_wannadie();
619     } else {
620       switch (state_) {
621         case State::DONE:
622           XBT_DEBUG("Communication %p complete!", this);
623           copy_data();
624           break;
625
626         case State::SRC_TIMEOUT:
627           simcall->issuer_->exception_ = std::make_exception_ptr(
628               simgrid::TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the sender"));
629           break;
630
631         case State::DST_TIMEOUT:
632           simcall->issuer_->exception_ = std::make_exception_ptr(
633               simgrid::TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
634           break;
635
636         case State::SRC_HOST_FAILURE:
637           if (simcall->issuer_ == src_actor_)
638             simcall->issuer_->context_->set_wannadie();
639           else
640             simcall->issuer_->exception_ =
641                 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
642           break;
643
644         case State::DST_HOST_FAILURE:
645           if (simcall->issuer_ == dst_actor_)
646             simcall->issuer_->context_->set_wannadie();
647           else
648             simcall->issuer_->exception_ =
649                 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
650           break;
651
652         case State::LINK_FAILURE:
653           XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
654                     "detached:%d",
655                     this, src_actor_ ? src_actor_->get_host()->get_cname() : nullptr,
656                     dst_actor_ ? dst_actor_->get_host()->get_cname() : nullptr, simcall->issuer_->get_cname(),
657                     simcall->issuer_, detached_);
658           if (src_actor_ == simcall->issuer_) {
659             XBT_DEBUG("I'm source");
660           } else if (dst_actor_ == simcall->issuer_) {
661             XBT_DEBUG("I'm dest");
662           } else {
663             XBT_DEBUG("I'm neither source nor dest");
664           }
665           simcall->issuer_->throw_exception(
666               std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Link failure")));
667           break;
668
669         case State::CANCELED:
670           if (simcall->issuer_ == dst_actor_)
671             simcall->issuer_->exception_ = std::make_exception_ptr(
672                 simgrid::CancelException(XBT_THROW_POINT, "Communication canceled by the sender"));
673           else
674             simcall->issuer_->exception_ = std::make_exception_ptr(
675                 simgrid::CancelException(XBT_THROW_POINT, "Communication canceled by the receiver"));
676           break;
677
678         default:
679           xbt_die("Unexpected synchro state in CommImpl::finish: %d", static_cast<int>(state_));
680       }
681       simcall->issuer_->simcall_answer();
682     }
683     /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
684     if (simcall->issuer_->exception_ &&
685         (simcall->call_ == simix::Simcall::COMM_WAITANY || simcall->call_ == simix::Simcall::COMM_TESTANY)) {
686       // First retrieve the rank of our failing synchro
687       CommImpl** comms;
688       size_t count;
689       if (simcall->call_ == simix::Simcall::COMM_WAITANY) {
690         comms = simcall_comm_waitany__get__comms(simcall);
691         count = simcall_comm_waitany__get__count(simcall);
692       } else {
693         /* simcall->call_ == simix::Simcall::COMM_TESTANY */
694         comms = simcall_comm_testany__get__comms(simcall);
695         count = simcall_comm_testany__get__count(simcall);
696       }
697       CommImpl** element = std::find(comms, comms + count, this);
698       int rank           = (element != comms + count) ? element - comms : -1;
699       // In order to modify the exception we have to rethrow it:
700       try {
701         std::rethrow_exception(simcall->issuer_->exception_);
702       } catch (simgrid::Exception& e) {
703         e.set_value(rank);
704       }
705     }
706
707     simcall->issuer_->waiting_synchro_ = nullptr;
708     simcall->issuer_->activities_.remove(this);
709     if (detached_) {
710       if (simcall->issuer_ != dst_actor_ && dst_actor_ != nullptr)
711         dst_actor_->activities_.remove(this);
712       if (simcall->issuer_ != src_actor_ && src_actor_ != nullptr)
713         src_actor_->activities_.remove(this);
714     }
715   }
716 }
717
718 } // namespace activity
719 } // namespace kernel
720 } // namespace simgrid