Logo AND Algorithmique Numérique Distribuée

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