Logo AND Algorithmique Numérique Distribuée

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