Logo AND Algorithmique Numérique Distribuée

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