Logo AND Algorithmique Numérique Distribuée

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