Logo AND Algorithmique Numérique Distribuée

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