1 /* Copyright (c) 2009-2019. The SimGrid Team. All rights reserved. */
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. */
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"
15 #include <boost/circular_buffer.hpp>
16 #include <boost/range/algorithm.hpp>
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix, "SIMIX network-related synchronization");
20 static void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall);
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
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)
37 void* other_user_data = nullptr;
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));
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_;
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());
54 comm->mbox_cpy = comm->mbox;
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);
63 XBT_DEBUG("No matching communication synchro found");
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,
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,
79 SIMCALL_SET_MC_VALUE(simcall, 0);
80 simcall_HANDLER_comm_wait(simcall, comm, timeout);
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)
89 XBT_DEBUG("send from mailbox %p", mbox);
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));
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.
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);
102 if (not other_comm) {
103 other_comm = std::move(this_comm);
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());
113 mbox->push(other_comm);
116 XBT_DEBUG("Receive already pushed");
118 other_comm->state_ = SIMIX_READY;
119 other_comm->type = SIMIX_COMM_READY;
121 src_proc->comms.push_back(other_comm);
124 other_comm->detached = true;
125 other_comm->clean_fun = clean_fun;
127 other_comm->clean_fun = nullptr;
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;
138 other_comm->match_fun = match_fun;
139 other_comm->copy_data_fun = copy_data_fun;
142 if (MC_is_active() || MC_record_replay_is_active()) {
143 other_comm->state_ = SIMIX_RUNNING;
144 return (detached ? nullptr : other_comm);
149 return (detached ? nullptr : other_comm);
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)
158 smx_activity_t comm = simcall_HANDLER_comm_irecv(simcall, receiver, mbox, dst_buff, dst_buff_size, match_fun,
159 copy_data_fun, data, rate);
160 SIMCALL_SET_MC_VALUE(simcall, 0);
161 simcall_HANDLER_comm_wait(simcall, comm, timeout);
164 XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_irecv(smx_simcall_t /*simcall*/, smx_actor_t receiver,
165 smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
166 simix_match_func_t match_fun,
167 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data,
170 simgrid::kernel::activity::CommImplPtr this_synchro =
171 simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_RECEIVE));
172 XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
174 simgrid::kernel::activity::CommImplPtr other_comm;
175 //communication already done, get it inside the list of completed comms
176 if (mbox->permanent_receiver_ != nullptr && not mbox->done_comm_queue_.empty()) {
178 XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
179 //find a match in the list of already received comms
180 other_comm = _find_matching_comm(&mbox->done_comm_queue_, SIMIX_COMM_SEND, match_fun, data, this_synchro,
181 /*remove_matching*/ true);
182 //if not found, assume the receiver came first, register it to the mailbox in the classical way
183 if (not other_comm) {
184 XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request into list");
185 other_comm = std::move(this_synchro);
186 mbox->push(other_comm);
188 if (other_comm->surf_action_ && other_comm->remains() < 1e-12) {
189 XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
190 other_comm->state_ = SIMIX_DONE;
191 other_comm->type = SIMIX_COMM_DONE;
192 other_comm->mbox = nullptr;
196 /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
198 /* Look for communication activity matching our needs. We also provide a description of
199 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
201 * If it is not found then push our communication into the rendez-vous point */
202 other_comm = _find_matching_comm(&mbox->comm_queue_, SIMIX_COMM_SEND, match_fun, data, this_synchro,
203 /*remove_matching*/ true);
205 if (other_comm == nullptr) {
206 XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->comm_queue_.size());
207 other_comm = std::move(this_synchro);
208 mbox->push(other_comm);
210 XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
212 other_comm->state_ = SIMIX_READY;
213 other_comm->type = SIMIX_COMM_READY;
215 receiver->comms.push_back(other_comm);
218 /* Setup communication synchro */
219 other_comm->dst_actor_ = receiver;
220 other_comm->dst_buff_ = dst_buff;
221 other_comm->dst_buff_size_ = dst_buff_size;
222 other_comm->dst_data_ = data;
224 if (rate > -1.0 && (other_comm->rate_ < 0.0 || rate < other_comm->rate_))
225 other_comm->rate_ = rate;
227 other_comm->match_fun = match_fun;
228 other_comm->copy_data_fun = copy_data_fun;
230 if (MC_is_active() || MC_record_replay_is_active()) {
231 other_comm->state_ = SIMIX_RUNNING;
238 smx_activity_t SIMIX_comm_iprobe(smx_mailbox_t mbox, int type, simix_match_func_t match_fun, void* data)
240 XBT_DEBUG("iprobe from %p %p", mbox, &mbox->comm_queue_);
241 simgrid::kernel::activity::CommImplPtr this_comm;
244 this_comm = simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_SEND));
245 smx_type = SIMIX_COMM_RECEIVE;
247 this_comm = simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_RECEIVE));
248 smx_type = SIMIX_COMM_SEND;
250 smx_activity_t other_synchro=nullptr;
251 if (mbox->permanent_receiver_ != nullptr && not mbox->done_comm_queue_.empty()) {
252 XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
253 other_synchro = _find_matching_comm(&mbox->done_comm_queue_, (e_smx_comm_type_t)smx_type, match_fun, data,
254 this_comm, /*remove_matching*/ false);
256 if (not other_synchro) {
257 XBT_DEBUG("check if we have more luck in the normal mailbox");
258 other_synchro = _find_matching_comm(&mbox->comm_queue_, (e_smx_comm_type_t)smx_type, match_fun, data, this_comm,
259 /*remove_matching*/ false);
262 return other_synchro;
265 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, smx_activity_t synchro, double timeout)
267 /* Associate this simcall to the wait synchro */
268 XBT_DEBUG("simcall_HANDLER_comm_wait, %p", synchro.get());
270 synchro->simcalls_.push_back(simcall);
271 simcall->issuer->waiting_synchro = synchro;
273 if (MC_is_active() || MC_record_replay_is_active()) {
274 int idx = SIMCALL_GET_MC_VALUE(simcall);
276 synchro->state_ = SIMIX_DONE;
278 /* If we reached this point, the wait simcall must have a timeout */
279 /* Otherwise it shouldn't be enabled and executed by the MC */
283 simgrid::kernel::activity::CommImplPtr comm =
284 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
285 if (comm->src_actor_ == simcall->issuer)
286 comm->state_ = SIMIX_SRC_TIMEOUT;
288 comm->state_ = SIMIX_DST_TIMEOUT;
291 SIMIX_comm_finish(synchro);
295 /* If the synchro has already finish perform the error handling, */
296 /* otherwise set up a waiting timeout on the right side */
297 if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
298 SIMIX_comm_finish(synchro);
299 } else { /* we need a sleep action (even when there is no timeout) to be notified of host failures */
300 simgrid::kernel::resource::Action* sleep = simcall->issuer->host_->pimpl_cpu->sleep(timeout);
301 sleep->set_data(synchro.get());
303 simgrid::kernel::activity::CommImplPtr comm =
304 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
305 if (simcall->issuer == comm->src_actor_)
306 comm->src_timeout_ = sleep;
308 comm->dst_timeout_ = sleep;
312 void simcall_HANDLER_comm_test(smx_simcall_t simcall, smx_activity_t synchro)
314 simgrid::kernel::activity::CommImplPtr comm =
315 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
319 if (MC_is_active() || MC_record_replay_is_active()){
320 res = comm->src_actor_ && comm->dst_actor_;
322 synchro->state_ = SIMIX_DONE;
324 res = synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING;
327 simcall_comm_test__set__result(simcall, res);
328 if (simcall_comm_test__get__result(simcall)) {
329 synchro->simcalls_.push_back(simcall);
330 SIMIX_comm_finish(synchro);
332 SIMIX_simcall_answer(simcall);
336 void simcall_HANDLER_comm_testany(smx_simcall_t simcall, simgrid::kernel::activity::ActivityImplPtr comms[],
339 // The default result is -1 -- this means, "nothing is ready".
340 // It can be changed below, but only if something matches.
341 simcall_comm_testany__set__result(simcall, -1);
343 if (MC_is_active() || MC_record_replay_is_active()){
344 int idx = SIMCALL_GET_MC_VALUE(simcall);
346 SIMIX_simcall_answer(simcall);
348 simgrid::kernel::activity::ActivityImplPtr synchro = comms[idx];
349 simcall_comm_testany__set__result(simcall, idx);
350 synchro->simcalls_.push_back(simcall);
351 synchro->state_ = SIMIX_DONE;
352 SIMIX_comm_finish(synchro);
357 for (std::size_t i = 0; i != count; ++i) {
358 simgrid::kernel::activity::ActivityImplPtr synchro = comms[i];
359 if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
360 simcall_comm_testany__set__result(simcall, i);
361 synchro->simcalls_.push_back(simcall);
362 SIMIX_comm_finish(synchro);
366 SIMIX_simcall_answer(simcall);
369 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, xbt_dynar_t synchros, double timeout)
371 if (MC_is_active() || MC_record_replay_is_active()){
373 xbt_die("Timeout not implemented for waitany in the model-checker");
374 int idx = SIMCALL_GET_MC_VALUE(simcall);
375 smx_activity_t synchro = xbt_dynar_get_as(synchros, idx, smx_activity_t);
376 synchro->simcalls_.push_back(simcall);
377 simcall_comm_waitany__set__result(simcall, idx);
378 synchro->state_ = SIMIX_DONE;
379 SIMIX_comm_finish(synchro);
384 simcall->timer = NULL;
386 simcall->timer = SIMIX_timer_set(SIMIX_get_clock() + timeout, [simcall]() {
387 SIMIX_waitany_remove_simcall_from_actions(simcall);
388 simcall_comm_waitany__set__result(simcall, -1);
389 SIMIX_simcall_answer(simcall);
394 simgrid::kernel::activity::ActivityImpl* ptr;
395 xbt_dynar_foreach(synchros, cursor, ptr){
396 smx_activity_t synchro = simgrid::kernel::activity::ActivityImplPtr(ptr);
397 /* associate this simcall to the the synchro */
398 synchro->simcalls_.push_back(simcall);
400 /* see if the synchro is already finished */
401 if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
402 SIMIX_comm_finish(synchro);
408 void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall)
410 unsigned int cursor = 0;
411 xbt_dynar_t synchros = simcall_comm_waitany__get__comms(simcall);
413 simgrid::kernel::activity::ActivityImpl* ptr;
414 xbt_dynar_foreach(synchros, cursor, ptr){
415 smx_activity_t synchro = simgrid::kernel::activity::ActivityImplPtr(ptr);
417 // Remove the first occurence of simcall:
418 auto i = boost::range::find(synchro->simcalls_, simcall);
419 if (i != synchro->simcalls_.end())
420 synchro->simcalls_.erase(i);
425 * @brief Answers the SIMIX simcalls associated to a communication synchro.
426 * @param synchro a finished communication synchro
428 void SIMIX_comm_finish(smx_activity_t synchro)
430 simgrid::kernel::activity::CommImplPtr comm =
431 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
433 while (not synchro->simcalls_.empty()) {
434 smx_simcall_t simcall = synchro->simcalls_.front();
435 synchro->simcalls_.pop_front();
437 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
438 * list. Afterwards, get the position of the actual synchro in the waitany dynar and return it as the result of the
441 if (simcall->call == SIMCALL_NONE) //FIXME: maybe a better way to handle this case
442 continue; // if process handling comm is killed
443 if (simcall->call == SIMCALL_COMM_WAITANY) {
444 SIMIX_waitany_remove_simcall_from_actions(simcall);
445 if (simcall->timer) {
446 SIMIX_timer_remove(simcall->timer);
447 simcall->timer = nullptr;
449 if (not MC_is_active() && not MC_record_replay_is_active())
450 simcall_comm_waitany__set__result(simcall,
451 xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro));
454 /* If the synchro is still in a rendez-vous point then remove from it */
456 comm->mbox->remove(comm);
458 XBT_DEBUG("SIMIX_comm_finish: synchro state = %d", (int)synchro->state_);
460 /* Check out for errors */
462 if (not simcall->issuer->host_->is_on()) {
463 simcall->issuer->context_->iwannadie = true;
464 simcall->issuer->exception =
465 std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
467 switch (comm->state_) {
470 XBT_DEBUG("Communication %p complete!", synchro.get());
474 case SIMIX_SRC_TIMEOUT:
475 simcall->issuer->exception = std::make_exception_ptr(
476 simgrid::TimeoutError(XBT_THROW_POINT, "Communication timeouted because of the sender"));
479 case SIMIX_DST_TIMEOUT:
480 simcall->issuer->exception = std::make_exception_ptr(
481 simgrid::TimeoutError(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
484 case SIMIX_SRC_HOST_FAILURE:
485 if (simcall->issuer == comm->src_actor_)
486 simcall->issuer->context_->iwannadie = true;
488 simcall->issuer->exception =
489 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
492 case SIMIX_DST_HOST_FAILURE:
493 if (simcall->issuer == comm->dst_actor_)
494 simcall->issuer->context_->iwannadie = true;
496 simcall->issuer->exception =
497 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
500 case SIMIX_LINK_FAILURE:
501 XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
503 synchro.get(), comm->src_actor_ ? comm->src_actor_->host_->get_cname() : nullptr,
504 comm->dst_actor_ ? comm->dst_actor_->host_->get_cname() : nullptr, simcall->issuer->get_cname(),
505 simcall->issuer, comm->detached);
506 if (comm->src_actor_ == simcall->issuer) {
507 XBT_DEBUG("I'm source");
508 } else if (comm->dst_actor_ == simcall->issuer) {
509 XBT_DEBUG("I'm dest");
511 XBT_DEBUG("I'm neither source nor dest");
513 simcall->issuer->throw_exception(
514 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Link failure")));
518 if (simcall->issuer == comm->dst_actor_)
519 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Communication canceled by the sender");
521 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Communication canceled by the receiver");
525 xbt_die("Unexpected synchro state in SIMIX_comm_finish: %d", (int)synchro->state_);
529 /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
530 if (simcall->issuer->exception &&
531 (simcall->call == SIMCALL_COMM_WAITANY || simcall->call == SIMCALL_COMM_TESTANY)) {
532 // First retrieve the rank of our failing synchro
534 if (simcall->call == SIMCALL_COMM_WAITANY) {
535 rank = xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro);
536 } else if (simcall->call == SIMCALL_COMM_TESTANY) {
538 auto* comms = simcall_comm_testany__get__comms(simcall);
539 auto count = simcall_comm_testany__get__count(simcall);
540 auto element = std::find(comms, comms + count, synchro);
541 if (element == comms + count)
544 rank = element - comms;
547 // In order to modify the exception we have to rethrow it:
549 std::rethrow_exception(simcall->issuer->exception);
550 } catch (simgrid::TimeoutError& e) {
552 simcall->issuer->exception = std::make_exception_ptr(e);
553 } catch (simgrid::NetworkFailureException& e) {
555 simcall->issuer->exception = std::make_exception_ptr(e);
556 } catch (xbt_ex& e) {
557 if (e.category == cancel_error) {
559 simcall->issuer->exception = std::make_exception_ptr(e);
561 xbt_die("Unexpected xbt_ex(%s). Please enhance this code", xbt_ex_catname(e.category));
566 simcall->issuer->waiting_synchro = nullptr;
567 simcall->issuer->comms.remove(synchro);
569 if (simcall->issuer == comm->src_actor_) {
570 if (comm->dst_actor_)
571 comm->dst_actor_->comms.remove(synchro);
572 } else if (simcall->issuer == comm->dst_actor_) {
573 if (comm->src_actor_)
574 comm->src_actor_->comms.remove(synchro);
577 comm->dst_actor_->comms.remove(synchro);
578 comm->src_actor_->comms.remove(synchro);
582 if (simcall->issuer->host_->is_on())
583 SIMIX_simcall_answer(simcall);
585 simcall->issuer->context_->iwannadie = true;
590 void SIMIX_comm_copy_buffer_callback(smx_activity_t synchro, void* buff, size_t buff_size)
592 simgrid::kernel::activity::CommImplPtr comm =
593 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
595 XBT_DEBUG("Copy the data over");
596 memcpy(comm->dst_buff_, buff, buff_size);
597 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
599 comm->src_buff_ = nullptr;