Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use CommImpl* for comm_copy callbacks.
[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, comm, 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, comm, 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, smx_activity_t synchro, double timeout)
192 {
193   /* Associate this simcall to the wait synchro */
194   XBT_DEBUG("simcall_HANDLER_comm_wait, %p", synchro.get());
195
196   synchro->simcalls_.push_back(simcall);
197   simcall->issuer->waiting_synchro = synchro;
198
199   if (MC_is_active() || MC_record_replay_is_active()) {
200     int idx = SIMCALL_GET_MC_VALUE(simcall);
201     if (idx == 0) {
202       synchro->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       simgrid::kernel::activity::CommImplPtr comm =
210           boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
211       if (comm->src_actor_ == simcall->issuer)
212         comm->state_ = SIMIX_SRC_TIMEOUT;
213       else
214         comm->state_ = SIMIX_DST_TIMEOUT;
215     }
216
217     boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro)->finish();
218     return;
219   }
220
221   /* If the synchro has already finish perform the error handling, */
222   /* otherwise set up a waiting timeout on the right side          */
223   if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
224     boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro)->finish();
225   } else { /* we need a sleep action (even when there is no timeout) to be notified of host failures */
226     simgrid::kernel::resource::Action* sleep = simcall->issuer->get_host()->pimpl_cpu->sleep(timeout);
227     sleep->set_data(synchro.get());
228
229     simgrid::kernel::activity::CommImplPtr comm =
230         boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
231     if (simcall->issuer == comm->src_actor_)
232       comm->src_timeout_ = sleep;
233     else
234       comm->dst_timeout_ = sleep;
235   }
236 }
237
238 void simcall_HANDLER_comm_test(smx_simcall_t simcall, smx_activity_t synchro)
239 {
240   simgrid::kernel::activity::CommImplPtr comm =
241       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
242
243   int res;
244
245   if (MC_is_active() || MC_record_replay_is_active()) {
246     res = comm->src_actor_ && comm->dst_actor_;
247     if (res)
248       synchro->state_ = SIMIX_DONE;
249   } else {
250     res = synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING;
251   }
252
253   simcall_comm_test__set__result(simcall, res);
254   if (simcall_comm_test__get__result(simcall)) {
255     synchro->simcalls_.push_back(simcall);
256     boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro)->finish();
257   } else {
258     SIMIX_simcall_answer(simcall);
259   }
260 }
261
262 void simcall_HANDLER_comm_testany(smx_simcall_t simcall, simgrid::kernel::activity::ActivityImplPtr comms[],
263                                   size_t count)
264 {
265   // The default result is -1 -- this means, "nothing is ready".
266   // It can be changed below, but only if something matches.
267   simcall_comm_testany__set__result(simcall, -1);
268
269   if (MC_is_active() || MC_record_replay_is_active()) {
270     int idx = SIMCALL_GET_MC_VALUE(simcall);
271     if (idx == -1) {
272       SIMIX_simcall_answer(simcall);
273     } else {
274       simgrid::kernel::activity::ActivityImplPtr synchro = comms[idx];
275       simcall_comm_testany__set__result(simcall, idx);
276       synchro->simcalls_.push_back(simcall);
277       synchro->state_ = SIMIX_DONE;
278       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro)->finish();
279     }
280     return;
281   }
282
283   for (std::size_t i = 0; i != count; ++i) {
284     simgrid::kernel::activity::ActivityImplPtr synchro = comms[i];
285     if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
286       simcall_comm_testany__set__result(simcall, i);
287       synchro->simcalls_.push_back(simcall);
288       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro)->finish();
289       return;
290     }
291   }
292   SIMIX_simcall_answer(simcall);
293 }
294
295 static void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall)
296 {
297   unsigned int cursor  = 0;
298   xbt_dynar_t synchros = simcall_comm_waitany__get__comms(simcall);
299
300   simgrid::kernel::activity::ActivityImpl* ptr;
301   xbt_dynar_foreach (synchros, cursor, ptr) {
302     smx_activity_t synchro = simgrid::kernel::activity::ActivityImplPtr(ptr);
303
304     // Remove the first occurence of simcall:
305     auto i = boost::range::find(synchro->simcalls_, simcall);
306     if (i != synchro->simcalls_.end())
307       synchro->simcalls_.erase(i);
308   }
309 }
310 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, xbt_dynar_t synchros, double timeout)
311 {
312   if (MC_is_active() || MC_record_replay_is_active()) {
313     if (timeout > 0.0)
314       xbt_die("Timeout not implemented for waitany in the model-checker");
315     int idx                = SIMCALL_GET_MC_VALUE(simcall);
316     smx_activity_t synchro = xbt_dynar_get_as(synchros, idx, smx_activity_t);
317     synchro->simcalls_.push_back(simcall);
318     simcall_comm_waitany__set__result(simcall, idx);
319     synchro->state_ = SIMIX_DONE;
320     boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro)->finish();
321     return;
322   }
323
324   if (timeout < 0.0) {
325     simcall->timer = NULL;
326   } else {
327     simcall->timer = simgrid::simix::Timer::set(SIMIX_get_clock() + timeout, [simcall]() {
328       SIMIX_waitany_remove_simcall_from_actions(simcall);
329       simcall_comm_waitany__set__result(simcall, -1);
330       SIMIX_simcall_answer(simcall);
331     });
332   }
333
334   unsigned int cursor;
335   simgrid::kernel::activity::ActivityImpl* ptr;
336   xbt_dynar_foreach (synchros, cursor, ptr) {
337     smx_activity_t synchro = simgrid::kernel::activity::ActivityImplPtr(ptr);
338     /* associate this simcall to the the synchro */
339     synchro->simcalls_.push_back(simcall);
340
341     /* see if the synchro is already finished */
342     if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
343       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro)->finish();
344       break;
345     }
346   }
347 }
348
349 /******************************************************************************/
350 /*                    SIMIX_comm_copy_data callbacks                       */
351 /******************************************************************************/
352 static void (*SIMIX_comm_copy_data_callback)(simgrid::kernel::activity::CommImpl*, void*,
353                                              size_t) = &SIMIX_comm_copy_pointer_callback;
354
355 void SIMIX_comm_copy_buffer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
356 {
357   XBT_DEBUG("Copy the data over");
358   memcpy(comm->dst_buff_, buff, buff_size);
359   if (comm->detached) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the
360                         // original buffer available to the application ASAP
361     xbt_free(buff);
362     comm->src_buff_ = nullptr;
363   }
364 }
365
366 void SIMIX_comm_set_copy_data_callback(void (*callback)(simgrid::kernel::activity::CommImpl*, void*, size_t))
367 {
368   SIMIX_comm_copy_data_callback = callback;
369 }
370
371 void SIMIX_comm_copy_pointer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
372 {
373   xbt_assert((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
374   *(void**)(comm->dst_buff_) = buff;
375 }
376
377 namespace simgrid {
378 namespace kernel {
379 namespace activity {
380
381 CommImpl::CommImpl(CommImpl::Type type) : type(type)
382 {
383   state_   = SIMIX_WAITING;
384   src_data_ = nullptr;
385   dst_data_ = nullptr;
386   XBT_DEBUG("Create comm activity %p", this);
387 }
388
389 CommImpl::~CommImpl()
390 {
391   XBT_DEBUG("Really free communication %p", this);
392
393   cleanupSurf();
394
395   if (detached && state_ != SIMIX_DONE) {
396     /* the communication has failed and was detached:
397      * we have to free the buffer */
398     if (clean_fun)
399       clean_fun(src_buff_);
400     src_buff_ = nullptr;
401   }
402
403   if (mbox)
404     mbox->remove(this);
405 }
406
407 /**  @brief Starts the simulation of a communication synchro. */
408 void CommImpl::start()
409 {
410   /* If both the sender and the receiver are already there, start the communication */
411   if (state_ == SIMIX_READY) {
412
413     s4u::Host* sender   = src_actor_->get_host();
414     s4u::Host* receiver = dst_actor_->get_host();
415
416     surf_action_ = surf_network_model->communicate(sender, receiver, task_size_, rate_);
417     surf_action_->set_data(this);
418     state_ = SIMIX_RUNNING;
419
420     XBT_DEBUG("Starting communication %p from '%s' to '%s' (surf_action: %p)", this, sender->get_cname(),
421               receiver->get_cname(), surf_action_);
422
423     /* If a link is failed, detect it immediately */
424     if (surf_action_->get_state() == resource::Action::State::FAILED) {
425       XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure", sender->get_cname(),
426                 receiver->get_cname());
427       state_ = SIMIX_LINK_FAILURE;
428       cleanupSurf();
429
430     } else if (src_actor_->is_suspended() || dst_actor_->is_suspended()) {
431       /* If any of the process is suspended, create the synchro but stop its execution,
432          it will be restarted when the sender process resume */
433       if (src_actor_->is_suspended())
434         XBT_DEBUG("The communication is suspended on startup because src (%s@%s) was suspended since it initiated the "
435                   "communication",
436                   src_actor_->get_cname(), src_actor_->get_host()->get_cname());
437       else
438         XBT_DEBUG("The communication is suspended on startup because dst (%s@%s) was suspended since it initiated the "
439                   "communication",
440                   dst_actor_->get_cname(), dst_actor_->get_host()->get_cname());
441
442       surf_action_->suspend();
443     }
444   }
445 }
446
447 /** @brief Copy the communication data from the sender's buffer to the receiver's one  */
448 void CommImpl::copy_data()
449 {
450   size_t buff_size = src_buff_size_;
451   /* If there is no data to copy then return */
452   if (not src_buff_ || not dst_buff_ || copied)
453     return;
454
455   XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", this,
456             src_actor_ ? src_actor_->get_host()->get_cname() : "a finished process", src_buff_,
457             dst_actor_ ? dst_actor_->get_host()->get_cname() : "a finished process", dst_buff_, buff_size);
458
459   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
460   if (dst_buff_size_)
461     buff_size = std::min(buff_size, *(dst_buff_size_));
462
463   /* Update the receiver's buffer size to the copied amount */
464   if (dst_buff_size_)
465     *dst_buff_size_ = buff_size;
466
467   if (buff_size > 0) {
468     if (copy_data_fun)
469       copy_data_fun(this, src_buff_, buff_size);
470     else
471       SIMIX_comm_copy_data_callback(this, src_buff_, buff_size);
472   }
473
474   /* Set the copied flag so we copy data only once */
475   /* (this function might be called from both communication ends) */
476   copied = true;
477 }
478
479 void CommImpl::suspend()
480 {
481   /* FIXME: shall we suspend also the timeout synchro? */
482   if (surf_action_)
483     surf_action_->suspend();
484   /* if not created yet, the action will be suspended on creation, in CommImpl::start() */
485 }
486
487 void CommImpl::resume()
488 {
489   /*FIXME: check what happen with the timeouts */
490   if (surf_action_)
491     surf_action_->resume();
492   /* in the other case, the synchro were not really suspended yet, see CommImpl::suspend() and CommImpl::start() */
493 }
494
495 void CommImpl::cancel()
496 {
497   /* if the synchro is a waiting state means that it is still in a mbox so remove from it and delete it */
498   if (state_ == SIMIX_WAITING) {
499     mbox->remove(this);
500     state_ = SIMIX_CANCELED;
501   } else if (not MC_is_active() /* when running the MC there are no surf actions */
502              && not MC_record_replay_is_active() && (state_ == SIMIX_READY || state_ == SIMIX_RUNNING)) {
503     surf_action_->cancel();
504   }
505 }
506
507 /**  @brief get the amount remaining from the communication */
508 double CommImpl::remains()
509 {
510   return surf_action_->get_remains();
511 }
512
513 /** @brief This is part of the cleanup process, probably an internal command */
514 void CommImpl::cleanupSurf()
515 {
516   if (surf_action_) {
517     surf_action_->unref();
518     surf_action_ = nullptr;
519   }
520
521   if (src_timeout_) {
522     src_timeout_->unref();
523     src_timeout_ = nullptr;
524   }
525
526   if (dst_timeout_) {
527     dst_timeout_->unref();
528     dst_timeout_ = nullptr;
529   }
530 }
531
532 void CommImpl::post()
533 {
534   /* Update synchro state */
535   if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FINISHED)
536     state_ = SIMIX_SRC_TIMEOUT;
537   else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FINISHED)
538     state_ = SIMIX_DST_TIMEOUT;
539   else if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FAILED)
540     state_ = SIMIX_SRC_HOST_FAILURE;
541   else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FAILED)
542     state_ = SIMIX_DST_HOST_FAILURE;
543   else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
544     state_ = SIMIX_LINK_FAILURE;
545   } else
546     state_ = SIMIX_DONE;
547
548   XBT_DEBUG("SIMIX_post_comm: comm %p, state %d, src_proc %p, dst_proc %p, detached: %d", this, (int)state_,
549             src_actor_.get(), dst_actor_.get(), detached);
550
551   /* destroy the surf actions associated with the Simix communication */
552   cleanupSurf();
553
554   /* if there are simcalls associated with the synchro, then answer them */
555   if (not simcalls_.empty()) {
556     finish();
557   }
558 }
559
560 void CommImpl::finish()
561 {
562   smx_activity_t synchro = this;
563   while (not simcalls_.empty()) {
564     smx_simcall_t simcall = simcalls_.front();
565     simcalls_.pop_front();
566
567     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
568      * list. Afterwards, get the position of the actual synchro in the waitany dynar and return it as the result of the
569      * simcall */
570
571     if (simcall->call == SIMCALL_NONE) // FIXME: maybe a better way to handle this case
572       continue;                        // if process handling comm is killed
573     if (simcall->call == SIMCALL_COMM_WAITANY) {
574       SIMIX_waitany_remove_simcall_from_actions(simcall);
575       if (simcall->timer) {
576         simcall->timer->remove();
577         simcall->timer = nullptr;
578       }
579       if (not MC_is_active() && not MC_record_replay_is_active())
580         simcall_comm_waitany__set__result(simcall,
581                                           xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro));
582     }
583
584     /* If the synchro is still in a rendez-vous point then remove from it */
585     if (mbox)
586       mbox->remove(this);
587
588     XBT_DEBUG("CommImpl::finish(): synchro state = %d", static_cast<int>(state_));
589
590     /* Check out for errors */
591
592     if (not simcall->issuer->get_host()->is_on()) {
593       simcall->issuer->context_->iwannadie = true;
594       simcall->issuer->exception_ =
595           std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
596     } else {
597       switch (state_) {
598
599         case SIMIX_DONE:
600           XBT_DEBUG("Communication %p complete!", this);
601           copy_data();
602           break;
603
604         case SIMIX_SRC_TIMEOUT:
605           simcall->issuer->exception_ = std::make_exception_ptr(
606               simgrid::TimeoutError(XBT_THROW_POINT, "Communication timeouted because of the sender"));
607           break;
608
609         case SIMIX_DST_TIMEOUT:
610           simcall->issuer->exception_ = std::make_exception_ptr(
611               simgrid::TimeoutError(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
612           break;
613
614         case SIMIX_SRC_HOST_FAILURE:
615           if (simcall->issuer == src_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_DST_HOST_FAILURE:
623           if (simcall->issuer == dst_actor_)
624             simcall->issuer->context_->iwannadie = true;
625           else
626             simcall->issuer->exception_ =
627                 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
628           break;
629
630         case SIMIX_LINK_FAILURE:
631           XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
632                     "detached:%d",
633                     this, src_actor_ ? src_actor_->get_host()->get_cname() : nullptr,
634                     dst_actor_ ? dst_actor_->get_host()->get_cname() : nullptr, simcall->issuer->get_cname(),
635                     simcall->issuer, detached);
636           if (src_actor_ == simcall->issuer) {
637             XBT_DEBUG("I'm source");
638           } else if (dst_actor_ == simcall->issuer) {
639             XBT_DEBUG("I'm dest");
640           } else {
641             XBT_DEBUG("I'm neither source nor dest");
642           }
643           simcall->issuer->throw_exception(
644               std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Link failure")));
645           break;
646
647         case SIMIX_CANCELED:
648           if (simcall->issuer == dst_actor_)
649             simcall->issuer->exception_ = std::make_exception_ptr(
650                 simgrid::CancelException(XBT_THROW_POINT, "Communication canceled by the sender"));
651           else
652             simcall->issuer->exception_ = std::make_exception_ptr(
653                 simgrid::CancelException(XBT_THROW_POINT, "Communication canceled by the receiver"));
654           break;
655
656         default:
657           xbt_die("Unexpected synchro state in CommImpl::finish: %d", static_cast<int>(state_));
658       }
659     }
660
661     /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
662     if (simcall->issuer->exception_ &&
663         (simcall->call == SIMCALL_COMM_WAITANY || simcall->call == SIMCALL_COMM_TESTANY)) {
664       // First retrieve the rank of our failing synchro
665       int rank = -1;
666       if (simcall->call == SIMCALL_COMM_WAITANY) {
667         rank = xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro);
668       } else if (simcall->call == SIMCALL_COMM_TESTANY) {
669         rank         = -1;
670         auto* comms  = simcall_comm_testany__get__comms(simcall);
671         auto count   = simcall_comm_testany__get__count(simcall);
672         auto element = std::find(comms, comms + count, this);
673         if (element == comms + count)
674           rank = -1;
675         else
676           rank = element - comms;
677       }
678
679       // In order to modify the exception we have to rethrow it:
680       try {
681         std::rethrow_exception(simcall->issuer->exception_);
682       } catch (simgrid::TimeoutError& e) {
683         e.value                     = rank;
684         simcall->issuer->exception_ = std::make_exception_ptr(e);
685       } catch (simgrid::NetworkFailureException& e) {
686         e.value                     = rank;
687         simcall->issuer->exception_ = std::make_exception_ptr(e);
688       } catch (simgrid::CancelException& e) {
689         e.value                     = rank;
690         simcall->issuer->exception_ = std::make_exception_ptr(e);
691       }
692     }
693
694     simcall->issuer->waiting_synchro = nullptr;
695     simcall->issuer->comms.remove(this);
696     if (detached) {
697       if (simcall->issuer == src_actor_) {
698         if (dst_actor_)
699           dst_actor_->comms.remove(this);
700       } else if (simcall->issuer == dst_actor_) {
701         if (src_actor_)
702           src_actor_->comms.remove(this);
703       } else {
704         dst_actor_->comms.remove(this);
705         src_actor_->comms.remove(this);
706       }
707     }
708
709     if (simcall->issuer->get_host()->is_on())
710       SIMIX_simcall_answer(simcall);
711     else
712       simcall->issuer->context_->iwannadie = true;
713   }
714 }
715
716 } // namespace activity
717 } // namespace kernel
718 } // namespace simgrid