Logo AND Algorithmique Numérique Distribuée

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