Logo AND Algorithmique Numérique Distribuée

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