Logo AND Algorithmique Numérique Distribuée

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