1 /* Copyright (c) 2007-2021. 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. */
6 #include "src/kernel/activity/CommImpl.hpp"
7 #include "simgrid/Exception.hpp"
8 #include "simgrid/kernel/resource/Action.hpp"
9 #include "simgrid/kernel/routing/NetPoint.hpp"
10 #include "simgrid/modelchecker.h"
11 #include "simgrid/s4u/Host.hpp"
12 #include "src/kernel/activity/MailboxImpl.hpp"
13 #include "src/kernel/context/Context.hpp"
14 #include "src/mc/mc_replay.hpp"
15 #include "src/surf/cpu_interface.hpp"
16 #include "src/surf/network_interface.hpp"
17 #include "src/surf/surf_interface.hpp"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix, "SIMIX network-related synchronization");
21 XBT_PRIVATE void simcall_HANDLER_comm_send(smx_simcall_t simcall, smx_actor_t src, smx_mailbox_t mbox, double task_size,
22 double rate, unsigned char* src_buff, size_t src_buff_size,
23 bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
24 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
25 void* data, double timeout)
27 simgrid::kernel::activity::ActivityImplPtr comm = simcall_HANDLER_comm_isend(
28 simcall, src, mbox, task_size, rate, src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, false);
29 simcall->mc_value_ = 0;
30 simcall_HANDLER_comm_wait(simcall, static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
33 XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr simcall_HANDLER_comm_isend(
34 smx_simcall_t /*simcall*/, smx_actor_t src_proc, smx_mailbox_t mbox, double task_size, double rate,
35 unsigned char* src_buff, size_t src_buff_size,
36 bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
37 void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
38 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), // used to copy data if not default one
39 void* data, bool detached)
41 XBT_DEBUG("send from mailbox %p", mbox);
43 /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
44 simgrid::kernel::activity::CommImplPtr this_comm(
45 new simgrid::kernel::activity::CommImpl(simgrid::kernel::activity::CommImpl::Type::SEND));
47 /* Look for communication synchro matching our needs. We also provide a description of
48 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
50 * If it is not found then push our communication into the rendez-vous point */
51 simgrid::kernel::activity::CommImplPtr other_comm =
52 mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::RECEIVE, match_fun, data, this_comm,
53 /*done*/ false, /*remove_matching*/ true);
56 other_comm = std::move(this_comm);
58 if (mbox->permanent_receiver_ != nullptr) {
59 // this mailbox is for small messages, which have to be sent right now
60 other_comm->state_ = simgrid::kernel::activity::State::READY;
61 other_comm->dst_actor_ = mbox->permanent_receiver_.get();
62 mbox->done_comm_queue_.push_back(other_comm);
63 XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, other_comm.get());
66 mbox->push(other_comm);
69 XBT_DEBUG("Receive already pushed");
71 other_comm->state_ = simgrid::kernel::activity::State::READY;
76 other_comm->clean_fun = clean_fun;
78 other_comm->clean_fun = nullptr;
79 src_proc->activities_.emplace_back(other_comm);
82 /* Setup the communication synchro */
83 other_comm->src_actor_ = src_proc;
84 other_comm->src_data_ = data;
85 (*other_comm).set_src_buff(src_buff, src_buff_size).set_size(task_size).set_rate(rate);
87 other_comm->match_fun = match_fun;
88 other_comm->copy_data_fun = copy_data_fun;
90 if (MC_is_active() || MC_record_replay_is_active())
91 other_comm->state_ = simgrid::kernel::activity::State::RUNNING;
95 return (detached ? nullptr : other_comm);
98 XBT_PRIVATE void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
99 unsigned char* dst_buff, size_t* dst_buff_size,
100 bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
101 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
102 void* data, double timeout, double rate)
104 simgrid::kernel::activity::ActivityImplPtr comm = simcall_HANDLER_comm_irecv(
105 simcall, receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun, data, rate);
106 simcall->mc_value_ = 0;
107 simcall_HANDLER_comm_wait(simcall, static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
110 XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr
111 simcall_HANDLER_comm_irecv(smx_simcall_t /*simcall*/, smx_actor_t receiver, smx_mailbox_t mbox, unsigned char* dst_buff,
112 size_t* dst_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
113 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
116 simgrid::kernel::activity::CommImplPtr this_synchro(
117 new simgrid::kernel::activity::CommImpl(simgrid::kernel::activity::CommImpl::Type::RECEIVE));
118 XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
120 simgrid::kernel::activity::CommImplPtr other_comm;
121 // communication already done, get it inside the list of completed comms
122 if (mbox->permanent_receiver_ != nullptr && not mbox->done_comm_queue_.empty()) {
123 XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
124 // find a match in the list of already received comms
125 other_comm = mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::SEND, match_fun, data,
126 this_synchro, /*done*/ true,
127 /*remove_matching*/ true);
128 // if not found, assume the receiver came first, register it to the mailbox in the classical way
129 if (not other_comm) {
130 XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request "
132 other_comm = std::move(this_synchro);
133 mbox->push(other_comm);
135 if (other_comm->surf_action_ && other_comm->get_remaining() < 1e-12) {
136 XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
137 other_comm->state_ = simgrid::kernel::activity::State::DONE;
138 other_comm->set_mailbox(nullptr);
142 /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
144 /* Look for communication activity matching our needs. We also provide a description of
145 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
147 * If it is not found then push our communication into the rendez-vous point */
148 other_comm = mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::SEND, match_fun, data,
149 this_synchro, /*done*/ false,
150 /*remove_matching*/ true);
152 if (other_comm == nullptr) {
153 XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->comm_queue_.size());
154 other_comm = std::move(this_synchro);
155 mbox->push(other_comm);
157 XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
159 other_comm->state_ = simgrid::kernel::activity::State::READY;
161 receiver->activities_.emplace_back(other_comm);
164 /* Setup communication synchro */
165 other_comm->dst_actor_ = receiver;
166 other_comm->dst_data_ = data;
167 other_comm->set_dst_buff(dst_buff, dst_buff_size);
169 if (rate > -1.0 && (other_comm->get_rate() < 0.0 || rate < other_comm->get_rate()))
170 other_comm->set_rate(rate);
172 other_comm->match_fun = match_fun;
173 other_comm->copy_data_fun = copy_data_fun;
175 if (MC_is_active() || MC_record_replay_is_active()) {
176 other_comm->state_ = simgrid::kernel::activity::State::RUNNING;
183 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comm, double timeout)
185 /* Associate this simcall to the wait synchro */
186 XBT_DEBUG("simcall_HANDLER_comm_wait, %p", comm);
188 comm->register_simcall(simcall);
190 if (MC_is_active() || MC_record_replay_is_active()) {
191 int idx = simcall->mc_value_;
193 comm->state_ = simgrid::kernel::activity::State::DONE;
195 /* If we reached this point, the wait simcall must have a timeout */
196 /* Otherwise it shouldn't be enabled and executed by the MC */
200 if (comm->src_actor_ == simcall->issuer_)
201 comm->state_ = simgrid::kernel::activity::State::SRC_TIMEOUT;
203 comm->state_ = simgrid::kernel::activity::State::DST_TIMEOUT;
210 /* If the synchro has already finish perform the error handling, */
211 /* otherwise set up a waiting timeout on the right side */
212 if (comm->state_ != simgrid::kernel::activity::State::WAITING &&
213 comm->state_ != simgrid::kernel::activity::State::RUNNING) {
215 } else { /* we need a sleep action (even when there is no timeout) to be notified of host failures */
216 simgrid::kernel::resource::Action* sleep = simcall->issuer_->get_host()->pimpl_cpu->sleep(timeout);
217 sleep->set_activity(comm);
219 if (simcall->issuer_ == comm->src_actor_)
220 comm->src_timeout_ = sleep;
222 comm->dst_timeout_ = sleep;
226 bool simcall_HANDLER_comm_test(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comm)
228 if ((MC_is_active() || MC_record_replay_is_active()) && comm->src_actor_ && comm->dst_actor_)
229 comm->state_ = simgrid::kernel::activity::State::DONE;
231 bool res = comm->state_ != simgrid::kernel::activity::State::WAITING &&
232 comm->state_ != simgrid::kernel::activity::State::RUNNING;
239 void simcall_HANDLER_comm_testany(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comms[], size_t count)
241 // The default result is -1 -- this means, "nothing is ready".
242 // It can be changed below, but only if something matches.
243 simcall_comm_testany__set__result(simcall, -1);
245 if (MC_is_active() || MC_record_replay_is_active()) {
246 int idx = simcall->mc_value_;
248 simcall->issuer_->simcall_answer();
250 simgrid::kernel::activity::CommImpl* comm = comms[idx];
251 simcall_comm_testany__set__result(simcall, idx);
252 comm->simcalls_.push_back(simcall);
253 comm->state_ = simgrid::kernel::activity::State::DONE;
259 for (std::size_t i = 0; i != count; ++i) {
260 simgrid::kernel::activity::CommImpl* comm = comms[i];
261 if (comm->state_ != simgrid::kernel::activity::State::WAITING &&
262 comm->state_ != simgrid::kernel::activity::State::RUNNING) {
263 simcall_comm_testany__set__result(simcall, i);
264 comm->simcalls_.push_back(simcall);
269 simcall->issuer_->simcall_answer();
272 static void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall)
274 simgrid::kernel::activity::CommImpl** comms = simcall_comm_waitany__get__comms(simcall);
275 size_t count = simcall_comm_waitany__get__count(simcall);
277 for (size_t i = 0; i < count; i++)
278 comms[i]->unregister_simcall(simcall);
280 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comms[], size_t count,
283 if (MC_is_active() || MC_record_replay_is_active()) {
284 xbt_assert(timeout <= 0.0, "Timeout not implemented for waitany in the model-checker");
285 int idx = simcall->mc_value_;
286 auto* comm = comms[idx];
287 comm->simcalls_.push_back(simcall);
288 simcall_comm_waitany__set__result(simcall, idx);
289 comm->state_ = simgrid::kernel::activity::State::DONE;
295 simcall->timeout_cb_ = nullptr;
297 simcall->timeout_cb_ = simgrid::simix::Timer::set(SIMIX_get_clock() + timeout, [simcall]() {
298 simcall->timeout_cb_ = nullptr;
299 SIMIX_waitany_remove_simcall_from_actions(simcall);
300 simcall_comm_waitany__set__result(simcall, -1);
301 simcall->issuer_->simcall_answer();
305 for (size_t i = 0; i < count; i++) {
306 /* associate this simcall to the the synchro */
307 auto* comm = comms[i];
308 comm->simcalls_.push_back(simcall);
310 /* see if the synchro is already finished */
311 if (comm->state_ != simgrid::kernel::activity::State::WAITING &&
312 comm->state_ != simgrid::kernel::activity::State::RUNNING) {
319 /******************************************************************************/
320 /* SIMIX_comm_copy_data callbacks */
321 /******************************************************************************/
322 void SIMIX_comm_set_copy_data_callback(void (*callback)(simgrid::kernel::activity::CommImpl*, void*, size_t))
324 simgrid::kernel::activity::CommImpl::set_copy_data_callback(callback);
327 void SIMIX_comm_copy_buffer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
329 XBT_DEBUG("Copy the data over");
330 memcpy(comm->dst_buff_, buff, buff_size);
331 if (comm->detached()) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the
332 // original buffer available to the application ASAP
334 comm->src_buff_ = nullptr;
338 void SIMIX_comm_copy_pointer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
340 xbt_assert((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
341 *(void**)(comm->dst_buff_) = buff;
348 void (*CommImpl::copy_data_callback_)(CommImpl*, void*, size_t) = &SIMIX_comm_copy_pointer_callback;
350 void CommImpl::set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t))
352 copy_data_callback_ = callback;
355 CommImpl& CommImpl::set_size(double size)
361 CommImpl& CommImpl::set_rate(double rate)
366 CommImpl& CommImpl::set_mailbox(MailboxImpl* mbox)
372 CommImpl& CommImpl::set_src_buff(unsigned char* buff, size_t size)
375 src_buff_size_ = size;
379 CommImpl& CommImpl::set_dst_buff(unsigned char* buff, size_t* size)
382 dst_buff_size_ = size;
386 CommImpl& CommImpl::detach()
392 CommImpl::CommImpl(s4u::Host* from, s4u::Host* to, double bytes) : size_(bytes), detached_(true), from_(from), to_(to)
394 state_ = State::READY;
397 CommImpl::~CommImpl()
399 XBT_DEBUG("Really free communication %p in state %s (detached = %d)", this, get_state_str(), detached_);
403 if (detached_ && state_ != State::DONE) {
404 /* the communication has failed and was detached:
405 * we have to free the buffer */
407 clean_fun(src_buff_);
414 /** @brief Starts the simulation of a communication synchro. */
415 CommImpl* CommImpl::start()
417 /* If both the sender and the receiver are already there, start the communication */
418 if (state_ == State::READY) {
419 from_ = from_ != nullptr ? from_ : src_actor_->get_host();
420 to_ = to_ != nullptr ? to_ : dst_actor_->get_host();
422 /* FIXME[donassolo]: getting the network_model from the origin host
423 * Soon we need to change this function to first get the routes and later
424 * create the respective surf actions */
425 auto net_model = from_->get_netpoint()->get_englobing_zone()->get_network_model();
427 surf_action_ = net_model->communicate(from_, to_, size_, rate_);
428 surf_action_->set_activity(this);
429 surf_action_->set_category(get_tracing_category());
430 state_ = State::RUNNING;
432 XBT_DEBUG("Starting communication %p from '%s' to '%s' (surf_action: %p; state: %s)", this, from_->get_cname(),
433 to_->get_cname(), surf_action_, get_state_str());
435 /* If a link is failed, detect it immediately */
436 if (surf_action_->get_state() == resource::Action::State::FAILED) {
437 XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure", from_->get_cname(),
439 state_ = State::LINK_FAILURE;
442 } else if ((src_actor_ != nullptr && src_actor_->is_suspended()) ||
443 (dst_actor_ != nullptr && dst_actor_->is_suspended())) {
444 /* If any of the process is suspended, create the synchro but stop its execution,
445 it will be restarted when the sender process resume */
446 if (src_actor_->is_suspended())
447 XBT_DEBUG("The communication is suspended on startup because src (%s@%s) was suspended since it initiated the "
449 src_actor_->get_cname(), src_actor_->get_host()->get_cname());
451 XBT_DEBUG("The communication is suspended on startup because dst (%s@%s) was suspended since it initiated the "
453 dst_actor_->get_cname(), dst_actor_->get_host()->get_cname());
455 surf_action_->suspend();
462 /** @brief Copy the communication data from the sender's buffer to the receiver's one */
463 void CommImpl::copy_data()
465 size_t buff_size = src_buff_size_;
466 /* If there is no data to copy then return */
467 if (not src_buff_ || not dst_buff_ || copied_)
470 XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", this,
471 src_actor_ ? src_actor_->get_host()->get_cname() : "a finished process", src_buff_,
472 dst_actor_ ? dst_actor_->get_host()->get_cname() : "a finished process", dst_buff_, buff_size);
474 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
475 if (dst_buff_size_) {
476 buff_size = std::min(buff_size, *dst_buff_size_);
478 /* Update the receiver's buffer size to the copied amount */
479 *dst_buff_size_ = buff_size;
484 copy_data_fun(this, src_buff_, buff_size);
486 copy_data_callback_(this, src_buff_, buff_size);
489 /* Set the copied flag so we copy data only once */
490 /* (this function might be called from both communication ends) */
494 void CommImpl::suspend()
496 /* FIXME: shall we suspend also the timeout synchro? */
498 surf_action_->suspend();
499 /* if not created yet, the action will be suspended on creation, in CommImpl::start() */
502 void CommImpl::resume()
504 /*FIXME: check what happen with the timeouts */
506 surf_action_->resume();
507 /* in the other case, the synchro were not really suspended yet, see CommImpl::suspend() and CommImpl::start() */
510 void CommImpl::cancel()
512 /* if the synchro is a waiting state means that it is still in a mbox so remove from it and delete it */
513 if (state_ == State::WAITING) {
516 state_ = State::CANCELED;
518 } else if (not MC_is_active() /* when running the MC there are no surf actions */
519 && not MC_record_replay_is_active() && (state_ == State::READY || state_ == State::RUNNING)) {
520 surf_action_->cancel();
524 /** @brief This is part of the cleanup process, probably an internal command */
525 void CommImpl::cleanup_surf()
530 src_timeout_->unref();
531 src_timeout_ = nullptr;
535 dst_timeout_->unref();
536 dst_timeout_ = nullptr;
540 void CommImpl::post()
542 /* Update synchro state */
543 if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FINISHED)
544 state_ = State::SRC_TIMEOUT;
545 else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FINISHED)
546 state_ = State::DST_TIMEOUT;
547 else if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FAILED)
548 state_ = State::SRC_HOST_FAILURE;
549 else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FAILED)
550 state_ = State::DST_HOST_FAILURE;
551 else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
552 state_ = State::LINK_FAILURE;
554 state_ = State::DONE;
556 XBT_DEBUG("CommImpl::post(): comm %p, state %s, src_proc %p, dst_proc %p, detached: %d", this, get_state_str(),
557 src_actor_.get(), dst_actor_.get(), detached_);
559 /* destroy the surf actions associated with the Simix communication */
562 /* Answer all simcalls associated with the synchro */
566 void CommImpl::finish()
568 XBT_DEBUG("CommImpl::finish() in state %s", to_c_str(state_));
570 /* If the synchro is still in a rendez-vous point then remove from it */
574 if (state_ == State::DONE)
577 while (not simcalls_.empty()) {
578 smx_simcall_t simcall = simcalls_.front();
579 simcalls_.pop_front();
581 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
582 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
585 if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
586 continue; // if actor handling comm is killed
587 if (simcall->call_ == simix::Simcall::COMM_WAITANY) {
588 SIMIX_waitany_remove_simcall_from_actions(simcall);
589 if (simcall->timeout_cb_) {
590 simcall->timeout_cb_->remove();
591 simcall->timeout_cb_ = nullptr;
593 if (not MC_is_active() && not MC_record_replay_is_active()) {
594 CommImpl** comms = simcall_comm_waitany__get__comms(simcall);
595 size_t count = simcall_comm_waitany__get__count(simcall);
596 CommImpl** element = std::find(comms, comms + count, this);
597 int rank = (element != comms + count) ? element - comms : -1;
598 simcall_comm_waitany__set__result(simcall, rank);
602 /* Check out for errors */
604 if (not simcall->issuer_->get_host()->is_on()) {
605 simcall->issuer_->context_->set_wannadie();
608 case State::SRC_TIMEOUT:
609 simcall->issuer_->exception_ = std::make_exception_ptr(
610 simgrid::TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the sender"));
613 case State::DST_TIMEOUT:
614 simcall->issuer_->exception_ = std::make_exception_ptr(
615 simgrid::TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
618 case State::SRC_HOST_FAILURE:
619 if (simcall->issuer_ == src_actor_)
620 simcall->issuer_->context_->set_wannadie();
622 simcall->issuer_->exception_ =
623 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
626 case State::DST_HOST_FAILURE:
627 if (simcall->issuer_ == dst_actor_)
628 simcall->issuer_->context_->set_wannadie();
630 simcall->issuer_->exception_ =
631 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
634 case State::LINK_FAILURE:
635 XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
637 this, src_actor_ ? src_actor_->get_host()->get_cname() : nullptr,
638 dst_actor_ ? dst_actor_->get_host()->get_cname() : nullptr, simcall->issuer_->get_cname(),
639 simcall->issuer_, detached_);
640 if (src_actor_ == simcall->issuer_) {
641 XBT_DEBUG("I'm source");
642 } else if (dst_actor_ == simcall->issuer_) {
643 XBT_DEBUG("I'm dest");
645 XBT_DEBUG("I'm neither source nor dest");
647 simcall->issuer_->throw_exception(
648 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Link failure")));
651 case State::CANCELED:
652 if (simcall->issuer_ == dst_actor_)
653 simcall->issuer_->exception_ = std::make_exception_ptr(
654 simgrid::CancelException(XBT_THROW_POINT, "Communication canceled by the sender"));
656 simcall->issuer_->exception_ = std::make_exception_ptr(
657 simgrid::CancelException(XBT_THROW_POINT, "Communication canceled by the receiver"));
661 xbt_assert(state_ == State::DONE, "Internal error in CommImpl::finish(): unexpected synchro state %s",
664 simcall->issuer_->simcall_answer();
666 /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
667 if (simcall->issuer_->exception_ &&
668 (simcall->call_ == simix::Simcall::COMM_WAITANY || simcall->call_ == simix::Simcall::COMM_TESTANY)) {
669 // First retrieve the rank of our failing synchro
672 if (simcall->call_ == simix::Simcall::COMM_WAITANY) {
673 comms = simcall_comm_waitany__get__comms(simcall);
674 count = simcall_comm_waitany__get__count(simcall);
676 /* simcall->call_ == simix::Simcall::COMM_TESTANY */
677 comms = simcall_comm_testany__get__comms(simcall);
678 count = simcall_comm_testany__get__count(simcall);
680 CommImpl** element = std::find(comms, comms + count, this);
681 int rank = (element != comms + count) ? element - comms : -1;
682 // In order to modify the exception we have to rethrow it:
684 std::rethrow_exception(simcall->issuer_->exception_);
685 } catch (simgrid::Exception& e) {
690 simcall->issuer_->waiting_synchro_ = nullptr;
691 simcall->issuer_->activities_.remove(this);
693 if (simcall->issuer_ != dst_actor_ && dst_actor_ != nullptr)
694 dst_actor_->activities_.remove(this);
695 if (simcall->issuer_ != src_actor_ && src_actor_ != nullptr)
696 src_actor_->activities_.remove(this);
701 } // namespace activity
702 } // namespace kernel
703 } // namespace simgrid