Logo AND Algorithmique Numérique Distribuée

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