Logo AND Algorithmique Numérique Distribuée

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