Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9bf9864557ec08907666b3417467b73d68cad447
[simgrid.git] / src / simix / smx_network.cpp
1 /* Copyright (c) 2009-2019. 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 "mc/mc.h"
7 #include "simgrid/Exception.hpp"
8 #include "src/kernel/activity/MailboxImpl.hpp"
9 #include "src/mc/mc_replay.hpp"
10 #include "src/simix/smx_network_private.hpp"
11 #include "src/simix/smx_private.hpp"
12 #include "src/surf/cpu_interface.hpp"
13 #include "src/surf/network_interface.hpp"
14
15 #include <boost/circular_buffer.hpp>
16 #include <boost/range/algorithm.hpp>
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix, "SIMIX network-related synchronization");
19
20 static void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall);
21
22 /**
23  *  @brief Checks if there is a communication activity queued in a deque matching our needs
24  *  @param deque where to search into
25  *  @param type The type of communication we are looking for (comm_send, comm_recv)
26  *  @param match_fun the function to apply
27  *  @param this_user_data additional parameter to the match_fun
28  *  @param my_synchro what to compare against
29  *  @param remove_matching whether or not to clean the found object from the queue
30  *  @return The communication activity if found, nullptr otherwise
31  */
32 static simgrid::kernel::activity::CommImplPtr
33 _find_matching_comm(boost::circular_buffer_space_optimized<smx_activity_t>* deque, e_smx_comm_type_t type,
34                     int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* this_user_data,
35                     simgrid::kernel::activity::CommImplPtr my_synchro, bool remove_matching)
36 {
37   void* other_user_data = nullptr;
38
39   for(auto it = deque->begin(); it != deque->end(); it++){
40     simgrid::kernel::activity::CommImplPtr comm =
41         boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(std::move(*it));
42
43     if (comm->type == SIMIX_COMM_SEND) {
44       other_user_data = comm->src_data_;
45     } else if (comm->type == SIMIX_COMM_RECEIVE) {
46       other_user_data = comm->dst_data_;
47     }
48     if (comm->type == type && (match_fun == nullptr || match_fun(this_user_data, other_user_data, comm.get())) &&
49         (not comm->match_fun || comm->match_fun(other_user_data, this_user_data, my_synchro.get()))) {
50       XBT_DEBUG("Found a matching communication synchro %p", comm.get());
51       if (remove_matching)
52         deque->erase(it);
53 #if SIMGRID_HAVE_MC
54       comm->mbox_cpy = comm->mbox;
55 #endif
56       comm->mbox = nullptr;
57       return comm;
58     }
59     XBT_DEBUG("Sorry, communication synchro %p does not match our needs:"
60               " its type is %d but we are looking for a comm of type %d (or maybe the filtering didn't match)",
61               comm.get(), (int)comm->type, (int)type);
62   }
63   XBT_DEBUG("No matching communication synchro found");
64   return nullptr;
65 }
66
67 /******************************************************************************/
68 /*                          Communication synchros                            */
69 /******************************************************************************/
70 XBT_PRIVATE void simcall_HANDLER_comm_send(smx_simcall_t simcall, smx_actor_t src, smx_mailbox_t mbox, double task_size,
71                                            double rate, void* src_buff, size_t src_buff_size,
72                                            int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
73                                            void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data,
74                                            double timeout)
75 {
76   smx_activity_t comm = simcall_HANDLER_comm_isend(simcall, src, mbox, task_size, rate,
77                            src_buff, src_buff_size, match_fun, nullptr, copy_data_fun,
78                data, 0);
79   SIMCALL_SET_MC_VALUE(simcall, 0);
80   simcall_HANDLER_comm_wait(simcall, comm, timeout);
81 }
82 XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_isend(
83     smx_simcall_t /*simcall*/, smx_actor_t src_proc, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
84     size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
85     void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
86     void (*copy_data_fun)(smx_activity_t, void*, size_t), // used to copy data if not default one
87     void* data, int detached)
88 {
89   XBT_DEBUG("send from mailbox %p", mbox);
90
91   /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
92   simgrid::kernel::activity::CommImplPtr this_comm =
93       simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_SEND));
94
95   /* Look for communication synchro matching our needs. We also provide a description of
96    * ourself so that the other side also gets a chance of choosing if it wants to match with us.
97    *
98    * If it is not found then push our communication into the rendez-vous point */
99   simgrid::kernel::activity::CommImplPtr other_comm =
100       _find_matching_comm(&mbox->comm_queue_, SIMIX_COMM_RECEIVE, match_fun, data, this_comm, /*remove_matching*/ true);
101
102   if (not other_comm) {
103     other_comm = std::move(this_comm);
104
105     if (mbox->permanent_receiver_ != nullptr) {
106       //this mailbox is for small messages, which have to be sent right now
107       other_comm->state_  = SIMIX_READY;
108       other_comm->dst_actor_ = mbox->permanent_receiver_.get();
109       mbox->done_comm_queue_.push_back(other_comm);
110       XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, other_comm.get());
111
112     }else{
113       mbox->push(other_comm);
114     }
115   } else {
116     XBT_DEBUG("Receive already pushed");
117
118     other_comm->state_ = SIMIX_READY;
119     other_comm->type = SIMIX_COMM_READY;
120   }
121   src_proc->comms.push_back(other_comm);
122
123   if (detached) {
124     other_comm->detached = true;
125     other_comm->clean_fun = clean_fun;
126   } else {
127     other_comm->clean_fun = nullptr;
128   }
129
130   /* Setup the communication synchro */
131   other_comm->src_actor_     = src_proc;
132   other_comm->task_size_     = task_size;
133   other_comm->rate_          = rate;
134   other_comm->src_buff_      = src_buff;
135   other_comm->src_buff_size_ = src_buff_size;
136   other_comm->src_data_      = data;
137
138   other_comm->match_fun = match_fun;
139   other_comm->copy_data_fun = copy_data_fun;
140
141
142   if (MC_is_active() || MC_record_replay_is_active()) {
143     other_comm->state_ = SIMIX_RUNNING;
144     return (detached ? nullptr : other_comm);
145   }
146
147   other_comm->start();
148
149   return (detached ? nullptr : other_comm);
150 }
151
152 XBT_PRIVATE void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
153                                            void* dst_buff, size_t* dst_buff_size,
154                                            int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
155                                            void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data,
156                                            double timeout, double rate)
157 {
158   smx_activity_t comm = SIMIX_comm_irecv(receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun, data, rate);
159   SIMCALL_SET_MC_VALUE(simcall, 0);
160   simcall_HANDLER_comm_wait(simcall, comm, timeout);
161 }
162
163 XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_irecv(smx_simcall_t /*simcall*/, smx_actor_t receiver,
164                                                       smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
165                                                       simix_match_func_t match_fun,
166                                                       void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data,
167                                                       double rate)
168 {
169   return SIMIX_comm_irecv(receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun, data, rate);
170 }
171
172 smx_activity_t
173 SIMIX_comm_irecv(smx_actor_t dst_proc, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
174                  int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
175                  void (*copy_data_fun)(smx_activity_t, void*, size_t), // used to copy data if not default one
176                  void* data, double rate)
177 {
178   simgrid::kernel::activity::CommImplPtr this_synchro =
179       simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_RECEIVE));
180   XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
181
182   simgrid::kernel::activity::CommImplPtr other_comm;
183   //communication already done, get it inside the list of completed comms
184   if (mbox->permanent_receiver_ != nullptr && not mbox->done_comm_queue_.empty()) {
185
186     XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
187     //find a match in the list of already received comms
188     other_comm = _find_matching_comm(&mbox->done_comm_queue_, SIMIX_COMM_SEND, match_fun, data, this_synchro,
189                                      /*remove_matching*/ true);
190     //if not found, assume the receiver came first, register it to the mailbox in the classical way
191     if (not other_comm) {
192       XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request into list");
193       other_comm = std::move(this_synchro);
194       mbox->push(other_comm);
195     } else {
196       if (other_comm->surf_action_ && other_comm->remains() < 1e-12) {
197         XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
198         other_comm->state_ = SIMIX_DONE;
199         other_comm->type = SIMIX_COMM_DONE;
200         other_comm->mbox = nullptr;
201       }
202     }
203   } else {
204     /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
205
206     /* Look for communication activity matching our needs. We also provide a description of
207      * ourself so that the other side also gets a chance of choosing if it wants to match with us.
208      *
209      * If it is not found then push our communication into the rendez-vous point */
210     other_comm = _find_matching_comm(&mbox->comm_queue_, SIMIX_COMM_SEND, match_fun, data, this_synchro,
211                                      /*remove_matching*/ true);
212
213     if (other_comm == nullptr) {
214       XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->comm_queue_.size());
215       other_comm = std::move(this_synchro);
216       mbox->push(other_comm);
217     } else {
218       XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
219
220       other_comm->state_ = SIMIX_READY;
221       other_comm->type = SIMIX_COMM_READY;
222     }
223     dst_proc->comms.push_back(other_comm);
224   }
225
226   /* Setup communication synchro */
227   other_comm->dst_actor_     = dst_proc;
228   other_comm->dst_buff_      = dst_buff;
229   other_comm->dst_buff_size_ = dst_buff_size;
230   other_comm->dst_data_      = data;
231
232   if (rate > -1.0 && (other_comm->rate_ < 0.0 || rate < other_comm->rate_))
233     other_comm->rate_ = rate;
234
235   other_comm->match_fun = match_fun;
236   other_comm->copy_data_fun = copy_data_fun;
237
238   if (MC_is_active() || MC_record_replay_is_active()) {
239     other_comm->state_ = SIMIX_RUNNING;
240     return other_comm;
241   }
242   other_comm->start();
243   return other_comm;
244 }
245
246 smx_activity_t simcall_HANDLER_comm_iprobe(smx_simcall_t simcall, smx_mailbox_t mbox, int type,
247                                            simix_match_func_t match_fun, void* data)
248 {
249   return SIMIX_comm_iprobe(simcall->issuer, mbox, type, match_fun, data);
250 }
251
252 smx_activity_t SIMIX_comm_iprobe(smx_actor_t dst_proc, smx_mailbox_t mbox, int type, simix_match_func_t match_fun,
253                                  void* data)
254 {
255   XBT_DEBUG("iprobe from %p %p", mbox, &mbox->comm_queue_);
256   simgrid::kernel::activity::CommImplPtr this_comm;
257   int smx_type;
258   if(type == 1){
259     this_comm = simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_SEND));
260     smx_type = SIMIX_COMM_RECEIVE;
261   } else{
262     this_comm = simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_RECEIVE));
263     smx_type = SIMIX_COMM_SEND;
264   }
265   smx_activity_t other_synchro=nullptr;
266   if (mbox->permanent_receiver_ != nullptr && not mbox->done_comm_queue_.empty()) {
267     XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
268     other_synchro = _find_matching_comm(&mbox->done_comm_queue_, (e_smx_comm_type_t)smx_type, match_fun, data,
269                                         this_comm, /*remove_matching*/ false);
270   }
271   if (not other_synchro) {
272     XBT_DEBUG("check if we have more luck in the normal mailbox");
273     other_synchro = _find_matching_comm(&mbox->comm_queue_, (e_smx_comm_type_t)smx_type, match_fun, data, this_comm,
274                                         /*remove_matching*/ false);
275   }
276
277   return other_synchro;
278 }
279
280 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, smx_activity_t synchro, double timeout)
281 {
282   /* Associate this simcall to the wait synchro */
283   XBT_DEBUG("simcall_HANDLER_comm_wait, %p", synchro.get());
284
285   synchro->simcalls_.push_back(simcall);
286   simcall->issuer->waiting_synchro = synchro;
287
288   if (MC_is_active() || MC_record_replay_is_active()) {
289     int idx = SIMCALL_GET_MC_VALUE(simcall);
290     if (idx == 0) {
291       synchro->state_ = SIMIX_DONE;
292     } else {
293       /* If we reached this point, the wait simcall must have a timeout */
294       /* Otherwise it shouldn't be enabled and executed by the MC */
295       if (timeout < 0.0)
296         THROW_IMPOSSIBLE;
297
298       simgrid::kernel::activity::CommImplPtr comm =
299           boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
300       if (comm->src_actor_ == simcall->issuer)
301         comm->state_ = SIMIX_SRC_TIMEOUT;
302       else
303         comm->state_ = SIMIX_DST_TIMEOUT;
304     }
305
306     SIMIX_comm_finish(synchro);
307     return;
308   }
309
310   /* If the synchro has already finish perform the error handling, */
311   /* otherwise set up a waiting timeout on the right side          */
312   if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
313     SIMIX_comm_finish(synchro);
314   } else { /* we need a sleep action (even when there is no timeout) to be notified of host failures */
315     simgrid::kernel::resource::Action* sleep = simcall->issuer->host_->pimpl_cpu->sleep(timeout);
316     sleep->set_data(synchro.get());
317
318     simgrid::kernel::activity::CommImplPtr comm =
319         boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
320     if (simcall->issuer == comm->src_actor_)
321       comm->src_timeout_ = sleep;
322     else
323       comm->dst_timeout_ = sleep;
324   }
325 }
326
327 void simcall_HANDLER_comm_test(smx_simcall_t simcall, smx_activity_t synchro)
328 {
329   simgrid::kernel::activity::CommImplPtr comm =
330       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
331
332   int res;
333
334   if (MC_is_active() || MC_record_replay_is_active()){
335     res = comm->src_actor_ && comm->dst_actor_;
336     if (res)
337       synchro->state_ = SIMIX_DONE;
338   } else {
339     res = synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING;
340   }
341
342   simcall_comm_test__set__result(simcall, res);
343   if (simcall_comm_test__get__result(simcall)) {
344     synchro->simcalls_.push_back(simcall);
345     SIMIX_comm_finish(synchro);
346   } else {
347     SIMIX_simcall_answer(simcall);
348   }
349 }
350
351 void simcall_HANDLER_comm_testany(smx_simcall_t simcall, simgrid::kernel::activity::ActivityImplPtr comms[],
352                                   size_t count)
353 {
354   // The default result is -1 -- this means, "nothing is ready".
355   // It can be changed below, but only if something matches.
356   simcall_comm_testany__set__result(simcall, -1);
357
358   if (MC_is_active() || MC_record_replay_is_active()){
359     int idx = SIMCALL_GET_MC_VALUE(simcall);
360     if(idx == -1){
361       SIMIX_simcall_answer(simcall);
362     }else{
363       simgrid::kernel::activity::ActivityImplPtr synchro = comms[idx];
364       simcall_comm_testany__set__result(simcall, idx);
365       synchro->simcalls_.push_back(simcall);
366       synchro->state_ = SIMIX_DONE;
367       SIMIX_comm_finish(synchro);
368     }
369     return;
370   }
371
372   for (std::size_t i = 0; i != count; ++i) {
373     simgrid::kernel::activity::ActivityImplPtr synchro = comms[i];
374     if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
375       simcall_comm_testany__set__result(simcall, i);
376       synchro->simcalls_.push_back(simcall);
377       SIMIX_comm_finish(synchro);
378       return;
379     }
380   }
381   SIMIX_simcall_answer(simcall);
382 }
383
384 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, xbt_dynar_t synchros, double timeout)
385 {
386   if (MC_is_active() || MC_record_replay_is_active()){
387     if (timeout > 0.0)
388       xbt_die("Timeout not implemented for waitany in the model-checker");
389     int idx = SIMCALL_GET_MC_VALUE(simcall);
390     smx_activity_t synchro = xbt_dynar_get_as(synchros, idx, smx_activity_t);
391     synchro->simcalls_.push_back(simcall);
392     simcall_comm_waitany__set__result(simcall, idx);
393     synchro->state_ = SIMIX_DONE;
394     SIMIX_comm_finish(synchro);
395     return;
396   }
397
398   if (timeout < 0.0){
399     simcall->timer = NULL;
400   } else {
401     simcall->timer = SIMIX_timer_set(SIMIX_get_clock() + timeout, [simcall]() {
402       SIMIX_waitany_remove_simcall_from_actions(simcall);
403       simcall_comm_waitany__set__result(simcall, -1);
404       SIMIX_simcall_answer(simcall);
405     });
406   }
407
408   unsigned int cursor;
409   simgrid::kernel::activity::ActivityImpl* ptr;
410   xbt_dynar_foreach(synchros, cursor, ptr){
411     smx_activity_t synchro = simgrid::kernel::activity::ActivityImplPtr(ptr);
412     /* associate this simcall to the the synchro */
413     synchro->simcalls_.push_back(simcall);
414
415     /* see if the synchro is already finished */
416     if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
417       SIMIX_comm_finish(synchro);
418       break;
419     }
420   }
421 }
422
423 void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall)
424 {
425   unsigned int cursor = 0;
426   xbt_dynar_t synchros = simcall_comm_waitany__get__comms(simcall);
427
428   simgrid::kernel::activity::ActivityImpl* ptr;
429   xbt_dynar_foreach(synchros, cursor, ptr){
430     smx_activity_t synchro = simgrid::kernel::activity::ActivityImplPtr(ptr);
431
432     // Remove the first occurence of simcall:
433     auto i = boost::range::find(synchro->simcalls_, simcall);
434     if (i != synchro->simcalls_.end())
435       synchro->simcalls_.erase(i);
436   }
437 }
438
439 /**
440  * @brief Answers the SIMIX simcalls associated to a communication synchro.
441  * @param synchro a finished communication synchro
442  */
443 void SIMIX_comm_finish(smx_activity_t synchro)
444 {
445   simgrid::kernel::activity::CommImplPtr comm =
446       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
447
448   while (not synchro->simcalls_.empty()) {
449     smx_simcall_t simcall = synchro->simcalls_.front();
450     synchro->simcalls_.pop_front();
451
452     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
453      * list. Afterwards, get the position of the actual synchro in the waitany dynar and return it as the result of the
454      * simcall */
455
456     if (simcall->call == SIMCALL_NONE) //FIXME: maybe a better way to handle this case
457       continue; // if process handling comm is killed
458     if (simcall->call == SIMCALL_COMM_WAITANY) {
459       SIMIX_waitany_remove_simcall_from_actions(simcall);
460       if (simcall->timer) {
461         SIMIX_timer_remove(simcall->timer);
462         simcall->timer = nullptr;
463       }
464       if (not MC_is_active() && not MC_record_replay_is_active())
465         simcall_comm_waitany__set__result(simcall,
466                                           xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro));
467     }
468
469     /* If the synchro is still in a rendez-vous point then remove from it */
470     if (comm->mbox)
471       comm->mbox->remove(comm);
472
473     XBT_DEBUG("SIMIX_comm_finish: synchro state = %d", (int)synchro->state_);
474
475     /* Check out for errors */
476
477     if (not simcall->issuer->host_->is_on()) {
478       simcall->issuer->context_->iwannadie = true;
479       simcall->issuer->exception =
480           std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
481     } else {
482       switch (comm->state_) {
483
484         case SIMIX_DONE:
485           XBT_DEBUG("Communication %p complete!", synchro.get());
486           comm->copy_data();
487           break;
488
489         case SIMIX_SRC_TIMEOUT:
490           simcall->issuer->exception = std::make_exception_ptr(
491               simgrid::TimeoutError(XBT_THROW_POINT, "Communication timeouted because of the sender"));
492           break;
493
494         case SIMIX_DST_TIMEOUT:
495           simcall->issuer->exception = std::make_exception_ptr(
496               simgrid::TimeoutError(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
497           break;
498
499         case SIMIX_SRC_HOST_FAILURE:
500           if (simcall->issuer == comm->src_actor_)
501             simcall->issuer->context_->iwannadie = true;
502           else
503             simcall->issuer->exception =
504                 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
505           break;
506
507         case SIMIX_DST_HOST_FAILURE:
508           if (simcall->issuer == comm->dst_actor_)
509             simcall->issuer->context_->iwannadie = true;
510           else
511             simcall->issuer->exception =
512                 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
513           break;
514
515         case SIMIX_LINK_FAILURE:
516           XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
517                     "detached:%d",
518                     synchro.get(), comm->src_actor_ ? comm->src_actor_->host_->get_cname() : nullptr,
519                     comm->dst_actor_ ? comm->dst_actor_->host_->get_cname() : nullptr, simcall->issuer->get_cname(),
520                     simcall->issuer, comm->detached);
521           if (comm->src_actor_ == simcall->issuer) {
522             XBT_DEBUG("I'm source");
523           } else if (comm->dst_actor_ == simcall->issuer) {
524             XBT_DEBUG("I'm dest");
525           } else {
526             XBT_DEBUG("I'm neither source nor dest");
527           }
528           simcall->issuer->throw_exception(
529               std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Link failure")));
530           break;
531
532         case SIMIX_CANCELED:
533           if (simcall->issuer == comm->dst_actor_)
534             SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Communication canceled by the sender");
535           else
536             SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Communication canceled by the receiver");
537           break;
538
539         default:
540           xbt_die("Unexpected synchro state in SIMIX_comm_finish: %d", (int)synchro->state_);
541       }
542     }
543
544     /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
545     if (simcall->issuer->exception &&
546         (simcall->call == SIMCALL_COMM_WAITANY || simcall->call == SIMCALL_COMM_TESTANY)) {
547       // First retrieve the rank of our failing synchro
548       int rank = -1;
549       if (simcall->call == SIMCALL_COMM_WAITANY) {
550         rank = xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro);
551       } else if (simcall->call == SIMCALL_COMM_TESTANY) {
552         rank         = -1;
553         auto* comms  = simcall_comm_testany__get__comms(simcall);
554         auto count   = simcall_comm_testany__get__count(simcall);
555         auto element = std::find(comms, comms + count, synchro);
556         if (element == comms + count)
557           rank = -1;
558         else
559           rank = element - comms;
560       }
561
562       // In order to modify the exception we have to rethrow it:
563       try {
564         std::rethrow_exception(simcall->issuer->exception);
565       } catch (simgrid::TimeoutError& e) {
566         e.value                    = rank;
567         simcall->issuer->exception = std::make_exception_ptr(e);
568       } catch (simgrid::NetworkFailureException& e) {
569         e.value                    = rank;
570         simcall->issuer->exception = std::make_exception_ptr(e);
571       } catch (xbt_ex& e) {
572         if (e.category == cancel_error) {
573           e.value                    = rank;
574           simcall->issuer->exception = std::make_exception_ptr(e);
575         } else {
576           xbt_die("Unexpected xbt_ex(%s). Please enhance this code", xbt_ex_catname(e.category));
577         }
578       }
579     }
580
581     simcall->issuer->waiting_synchro = nullptr;
582     simcall->issuer->comms.remove(synchro);
583     if(comm->detached){
584       if (simcall->issuer == comm->src_actor_) {
585         if (comm->dst_actor_)
586           comm->dst_actor_->comms.remove(synchro);
587       } else if (simcall->issuer == comm->dst_actor_) {
588         if (comm->src_actor_)
589           comm->src_actor_->comms.remove(synchro);
590       }
591       else{
592         comm->dst_actor_->comms.remove(synchro);
593         comm->src_actor_->comms.remove(synchro);
594       }
595     }
596
597     if (simcall->issuer->host_->is_on())
598       SIMIX_simcall_answer(simcall);
599     else
600       simcall->issuer->context_->iwannadie = true;
601   }
602 }
603
604
605 void SIMIX_comm_copy_buffer_callback(smx_activity_t synchro, void* buff, size_t buff_size)
606 {
607   simgrid::kernel::activity::CommImplPtr comm =
608       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
609
610   XBT_DEBUG("Copy the data over");
611   memcpy(comm->dst_buff_, buff, buff_size);
612   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
613     xbt_free(buff);
614     comm->src_buff_ = nullptr;
615   }
616 }