Logo AND Algorithmique Numérique Distribuée

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