Logo AND Algorithmique Numérique Distribuée

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