Logo AND Algorithmique Numérique Distribuée

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