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/modelchecker.h"
10 #include "simgrid/s4u/Host.hpp"
11 #include "src/kernel/activity/MailboxImpl.hpp"
12 #include "src/kernel/context/Context.hpp"
13 #include "src/mc/mc_replay.hpp"
14 #include "src/surf/cpu_interface.hpp"
15 #include "src/surf/network_interface.hpp"
16 #include "src/surf/surf_interface.hpp"
18 #include <boost/range/algorithm.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_SET_MC_VALUE(*simcall, 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;
72 other_comm->set_type(simgrid::kernel::activity::CommImpl::Type::READY);
77 other_comm->clean_fun = clean_fun;
79 other_comm->clean_fun = nullptr;
80 src_proc->activities_.emplace_back(other_comm);
83 /* Setup the communication synchro */
84 other_comm->src_actor_ = src_proc;
85 other_comm->src_data_ = data;
86 (*other_comm).set_src_buff(src_buff, src_buff_size).set_size(task_size).set_rate(rate);
88 other_comm->match_fun = match_fun;
89 other_comm->copy_data_fun = copy_data_fun;
91 if (MC_is_active() || MC_record_replay_is_active())
92 other_comm->state_ = simgrid::kernel::activity::State::RUNNING;
96 return (detached ? nullptr : other_comm);
99 XBT_PRIVATE void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
100 unsigned char* dst_buff, size_t* dst_buff_size,
101 bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
102 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
103 void* data, double timeout, double rate)
105 simgrid::kernel::activity::ActivityImplPtr comm = simcall_HANDLER_comm_irecv(
106 simcall, receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun, data, rate);
107 SIMCALL_SET_MC_VALUE(*simcall, 0);
108 simcall_HANDLER_comm_wait(simcall, static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
111 XBT_PRIVATE simgrid::kernel::activity::ActivityImplPtr
112 simcall_HANDLER_comm_irecv(smx_simcall_t /*simcall*/, smx_actor_t receiver, smx_mailbox_t mbox, unsigned char* dst_buff,
113 size_t* dst_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
114 void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
117 simgrid::kernel::activity::CommImplPtr this_synchro(
118 new simgrid::kernel::activity::CommImpl(simgrid::kernel::activity::CommImpl::Type::RECEIVE));
119 XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
121 simgrid::kernel::activity::CommImplPtr other_comm;
122 // communication already done, get it inside the list of completed comms
123 if (mbox->permanent_receiver_ != nullptr && not mbox->done_comm_queue_.empty()) {
124 XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
125 // find a match in the list of already received comms
126 other_comm = mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::SEND, match_fun, data,
127 this_synchro, /*done*/ true,
128 /*remove_matching*/ true);
129 // if not found, assume the receiver came first, register it to the mailbox in the classical way
130 if (not other_comm) {
131 XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request "
133 other_comm = std::move(this_synchro);
134 mbox->push(other_comm);
136 if (other_comm->surf_action_ && other_comm->get_remaining() < 1e-12) {
137 XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
138 other_comm->state_ = simgrid::kernel::activity::State::DONE;
139 other_comm->set_type(simgrid::kernel::activity::CommImpl::Type::DONE).set_mailbox(nullptr);
143 /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
145 /* Look for communication activity matching our needs. We also provide a description of
146 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
148 * If it is not found then push our communication into the rendez-vous point */
149 other_comm = mbox->find_matching_comm(simgrid::kernel::activity::CommImpl::Type::SEND, match_fun, data,
150 this_synchro, /*done*/ false,
151 /*remove_matching*/ true);
153 if (other_comm == nullptr) {
154 XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->comm_queue_.size());
155 other_comm = std::move(this_synchro);
156 mbox->push(other_comm);
158 XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
160 other_comm->state_ = simgrid::kernel::activity::State::READY;
161 other_comm->set_type(simgrid::kernel::activity::CommImpl::Type::READY);
163 receiver->activities_.emplace_back(other_comm);
166 /* Setup communication synchro */
167 other_comm->dst_actor_ = receiver;
168 other_comm->dst_data_ = data;
169 other_comm->set_dst_buff(dst_buff, dst_buff_size);
171 if (rate > -1.0 && (other_comm->get_rate() < 0.0 || rate < other_comm->get_rate()))
172 other_comm->set_rate(rate);
174 other_comm->match_fun = match_fun;
175 other_comm->copy_data_fun = copy_data_fun;
177 if (MC_is_active() || MC_record_replay_is_active()) {
178 other_comm->state_ = simgrid::kernel::activity::State::RUNNING;
185 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comm, double timeout)
187 /* Associate this simcall to the wait synchro */
188 XBT_DEBUG("simcall_HANDLER_comm_wait, %p", comm);
190 comm->register_simcall(simcall);
192 if (MC_is_active() || MC_record_replay_is_active()) {
193 int idx = SIMCALL_GET_MC_VALUE(*simcall);
195 comm->state_ = simgrid::kernel::activity::State::DONE;
197 /* If we reached this point, the wait simcall must have a timeout */
198 /* Otherwise it shouldn't be enabled and executed by the MC */
202 if (comm->src_actor_ == simcall->issuer_)
203 comm->state_ = simgrid::kernel::activity::State::SRC_TIMEOUT;
205 comm->state_ = simgrid::kernel::activity::State::DST_TIMEOUT;
212 /* If the synchro has already finish perform the error handling, */
213 /* otherwise set up a waiting timeout on the right side */
214 if (comm->state_ != simgrid::kernel::activity::State::WAITING &&
215 comm->state_ != simgrid::kernel::activity::State::RUNNING) {
217 } else { /* we need a sleep action (even when there is no timeout) to be notified of host failures */
218 simgrid::kernel::resource::Action* sleep = simcall->issuer_->get_host()->pimpl_cpu->sleep(timeout);
219 sleep->set_activity(comm);
221 if (simcall->issuer_ == comm->src_actor_)
222 comm->src_timeout_ = sleep;
224 comm->dst_timeout_ = sleep;
228 void simcall_HANDLER_comm_test(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comm)
232 if (MC_is_active() || MC_record_replay_is_active()) {
233 res = comm->src_actor_ && comm->dst_actor_;
235 comm->state_ = simgrid::kernel::activity::State::DONE;
237 res = comm->state_ != simgrid::kernel::activity::State::WAITING &&
238 comm->state_ != simgrid::kernel::activity::State::RUNNING;
241 simcall_comm_test__set__result(simcall, res);
243 comm->simcalls_.push_back(simcall);
246 simcall->issuer_->simcall_answer();
250 void simcall_HANDLER_comm_testany(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comms[], size_t count)
252 // The default result is -1 -- this means, "nothing is ready".
253 // It can be changed below, but only if something matches.
254 simcall_comm_testany__set__result(simcall, -1);
256 if (MC_is_active() || MC_record_replay_is_active()) {
257 int idx = SIMCALL_GET_MC_VALUE(*simcall);
259 simcall->issuer_->simcall_answer();
261 simgrid::kernel::activity::CommImpl* comm = comms[idx];
262 simcall_comm_testany__set__result(simcall, idx);
263 comm->simcalls_.push_back(simcall);
264 comm->state_ = simgrid::kernel::activity::State::DONE;
270 for (std::size_t i = 0; i != count; ++i) {
271 simgrid::kernel::activity::CommImpl* comm = comms[i];
272 if (comm->state_ != simgrid::kernel::activity::State::WAITING &&
273 comm->state_ != simgrid::kernel::activity::State::RUNNING) {
274 simcall_comm_testany__set__result(simcall, i);
275 comm->simcalls_.push_back(simcall);
280 simcall->issuer_->simcall_answer();
283 static void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall)
285 simgrid::kernel::activity::CommImpl** comms = simcall_comm_waitany__get__comms(simcall);
286 size_t count = simcall_comm_waitany__get__count(simcall);
288 for (size_t i = 0; i < count; i++) {
289 // Remove the first occurrence of simcall:
290 auto* comm = comms[i];
291 auto j = boost::range::find(comm->simcalls_, simcall);
292 if (j != comm->simcalls_.end())
293 comm->simcalls_.erase(j);
296 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, simgrid::kernel::activity::CommImpl* comms[], size_t count,
299 if (MC_is_active() || MC_record_replay_is_active()) {
301 xbt_die("Timeout not implemented for waitany in the model-checker");
302 int idx = SIMCALL_GET_MC_VALUE(*simcall);
303 auto* comm = comms[idx];
304 comm->simcalls_.push_back(simcall);
305 simcall_comm_waitany__set__result(simcall, idx);
306 comm->state_ = simgrid::kernel::activity::State::DONE;
312 simcall->timeout_cb_ = nullptr;
314 simcall->timeout_cb_ = simgrid::simix::Timer::set(SIMIX_get_clock() + timeout, [simcall]() {
315 simcall->timeout_cb_ = nullptr;
316 SIMIX_waitany_remove_simcall_from_actions(simcall);
317 simcall_comm_waitany__set__result(simcall, -1);
318 simcall->issuer_->simcall_answer();
322 for (size_t i = 0; i < count; i++) {
323 /* associate this simcall to the the synchro */
324 auto* comm = comms[i];
325 comm->simcalls_.push_back(simcall);
327 /* see if the synchro is already finished */
328 if (comm->state_ != simgrid::kernel::activity::State::WAITING &&
329 comm->state_ != simgrid::kernel::activity::State::RUNNING) {
336 /******************************************************************************/
337 /* SIMIX_comm_copy_data callbacks */
338 /******************************************************************************/
339 static void (*SIMIX_comm_copy_data_callback)(simgrid::kernel::activity::CommImpl*, void*,
340 size_t) = &SIMIX_comm_copy_pointer_callback;
342 void SIMIX_comm_copy_buffer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
344 XBT_DEBUG("Copy the data over");
345 memcpy(comm->dst_buff_, buff, buff_size);
346 if (comm->detached()) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the
347 // original buffer available to the application ASAP
349 comm->src_buff_ = nullptr;
353 void SIMIX_comm_set_copy_data_callback(void (*callback)(simgrid::kernel::activity::CommImpl*, void*, size_t))
355 SIMIX_comm_copy_data_callback = callback;
358 void SIMIX_comm_copy_pointer_callback(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
360 xbt_assert((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
361 *(void**)(comm->dst_buff_) = buff;
368 CommImpl& CommImpl::set_type(CommImpl::Type type)
374 CommImpl& CommImpl::set_size(double size)
380 CommImpl& CommImpl::set_rate(double rate)
385 CommImpl& CommImpl::set_mailbox(MailboxImpl* mbox)
391 CommImpl& CommImpl::set_src_buff(unsigned char* buff, size_t size)
394 src_buff_size_ = size;
398 CommImpl& CommImpl::set_dst_buff(unsigned char* buff, size_t* size)
401 dst_buff_size_ = size;
405 CommImpl& CommImpl::detach()
411 CommImpl::CommImpl(s4u::Host* from, s4u::Host* to, double bytes)
412 : size_(bytes), detached_(true), type_(Type::SEND), from_(from), to_(to)
414 state_ = State::READY;
417 CommImpl::~CommImpl()
419 XBT_DEBUG("Really free communication %p in state %d (detached = %d)", this, static_cast<int>(state_), detached_);
423 if (detached_ && state_ != State::DONE) {
424 /* the communication has failed and was detached:
425 * we have to free the buffer */
427 clean_fun(src_buff_);
434 /** @brief Starts the simulation of a communication synchro. */
435 CommImpl* CommImpl::start()
437 /* If both the sender and the receiver are already there, start the communication */
438 if (state_ == State::READY) {
439 from_ = from_ != nullptr ? from_ : src_actor_->get_host();
440 to_ = to_ != nullptr ? to_ : dst_actor_->get_host();
442 surf_action_ = surf_network_model->communicate(from_, to_, size_, rate_);
443 surf_action_->set_activity(this);
444 surf_action_->set_category(get_tracing_category());
445 state_ = State::RUNNING;
447 XBT_DEBUG("Starting communication %p from '%s' to '%s' (surf_action: %p; state: %s)", this, from_->get_cname(),
448 to_->get_cname(), surf_action_, get_state_str());
450 /* If a link is failed, detect it immediately */
451 if (surf_action_->get_state() == resource::Action::State::FAILED) {
452 XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure", from_->get_cname(),
454 state_ = State::LINK_FAILURE;
457 } else if ((src_actor_ != nullptr && src_actor_->is_suspended()) ||
458 (dst_actor_ != nullptr && dst_actor_->is_suspended())) {
459 /* If any of the process is suspended, create the synchro but stop its execution,
460 it will be restarted when the sender process resume */
461 if (src_actor_->is_suspended())
462 XBT_DEBUG("The communication is suspended on startup because src (%s@%s) was suspended since it initiated the "
464 src_actor_->get_cname(), src_actor_->get_host()->get_cname());
466 XBT_DEBUG("The communication is suspended on startup because dst (%s@%s) was suspended since it initiated the "
468 dst_actor_->get_cname(), dst_actor_->get_host()->get_cname());
470 surf_action_->suspend();
477 /** @brief Copy the communication data from the sender's buffer to the receiver's one */
478 void CommImpl::copy_data()
480 size_t buff_size = src_buff_size_;
481 /* If there is no data to copy then return */
482 if (not src_buff_ || not dst_buff_ || copied_)
485 XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", this,
486 src_actor_ ? src_actor_->get_host()->get_cname() : "a finished process", src_buff_,
487 dst_actor_ ? dst_actor_->get_host()->get_cname() : "a finished process", dst_buff_, buff_size);
489 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
490 if (dst_buff_size_) {
491 buff_size = std::min(buff_size, *(dst_buff_size_));
493 /* Update the receiver's buffer size to the copied amount */
494 *dst_buff_size_ = buff_size;
499 copy_data_fun(this, src_buff_, buff_size);
501 SIMIX_comm_copy_data_callback(this, src_buff_, buff_size);
504 /* Set the copied flag so we copy data only once */
505 /* (this function might be called from both communication ends) */
509 void CommImpl::suspend()
511 /* FIXME: shall we suspend also the timeout synchro? */
513 surf_action_->suspend();
514 /* if not created yet, the action will be suspended on creation, in CommImpl::start() */
517 void CommImpl::resume()
519 /*FIXME: check what happen with the timeouts */
521 surf_action_->resume();
522 /* in the other case, the synchro were not really suspended yet, see CommImpl::suspend() and CommImpl::start() */
525 void CommImpl::cancel()
527 /* if the synchro is a waiting state means that it is still in a mbox so remove from it and delete it */
528 if (state_ == State::WAITING) {
531 state_ = State::CANCELED;
533 } else if (not MC_is_active() /* when running the MC there are no surf actions */
534 && not MC_record_replay_is_active() && (state_ == State::READY || state_ == State::RUNNING)) {
535 surf_action_->cancel();
539 /** @brief This is part of the cleanup process, probably an internal command */
540 void CommImpl::cleanup_surf()
545 src_timeout_->unref();
546 src_timeout_ = nullptr;
550 dst_timeout_->unref();
551 dst_timeout_ = nullptr;
555 void CommImpl::post()
557 /* Update synchro state */
558 if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FINISHED)
559 state_ = State::SRC_TIMEOUT;
560 else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FINISHED)
561 state_ = State::DST_TIMEOUT;
562 else if (src_timeout_ && src_timeout_->get_state() == resource::Action::State::FAILED)
563 state_ = State::SRC_HOST_FAILURE;
564 else if (dst_timeout_ && dst_timeout_->get_state() == resource::Action::State::FAILED)
565 state_ = State::DST_HOST_FAILURE;
566 else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
567 state_ = State::LINK_FAILURE;
569 state_ = State::DONE;
571 XBT_DEBUG("CommImpl::post(): comm %p, state %s, src_proc %p, dst_proc %p, detached: %d", this, get_state_str(),
572 src_actor_.get(), dst_actor_.get(), detached_);
574 /* destroy the surf actions associated with the Simix communication */
577 /* Answer all simcalls associated with the synchro */
581 void CommImpl::finish()
583 while (not simcalls_.empty()) {
584 smx_simcall_t simcall = simcalls_.front();
585 simcalls_.pop_front();
587 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
588 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
591 if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
592 continue; // if actor handling comm is killed
593 if (simcall->call_ == simix::Simcall::COMM_WAITANY) {
594 SIMIX_waitany_remove_simcall_from_actions(simcall);
595 if (simcall->timeout_cb_) {
596 simcall->timeout_cb_->remove();
597 simcall->timeout_cb_ = nullptr;
599 if (not MC_is_active() && not MC_record_replay_is_active()) {
600 CommImpl** comms = simcall_comm_waitany__get__comms(simcall);
601 size_t count = simcall_comm_waitany__get__count(simcall);
602 CommImpl** element = std::find(comms, comms + count, this);
603 int rank = (element != comms + count) ? element - comms : -1;
604 simcall_comm_waitany__set__result(simcall, rank);
608 /* If the synchro is still in a rendez-vous point then remove from it */
612 XBT_DEBUG("CommImpl::finish(): synchro state = %d", static_cast<int>(state_));
614 /* Check out for errors */
616 if (not simcall->issuer_->get_host()->is_on()) {
617 simcall->issuer_->context_->set_wannadie();
621 XBT_DEBUG("Communication %p complete!", this);
625 case State::SRC_TIMEOUT:
626 simcall->issuer_->exception_ = std::make_exception_ptr(
627 simgrid::TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the sender"));
630 case State::DST_TIMEOUT:
631 simcall->issuer_->exception_ = std::make_exception_ptr(
632 simgrid::TimeoutException(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
635 case State::SRC_HOST_FAILURE:
636 if (simcall->issuer_ == src_actor_)
637 simcall->issuer_->context_->set_wannadie();
639 simcall->issuer_->exception_ =
640 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
643 case State::DST_HOST_FAILURE:
644 if (simcall->issuer_ == dst_actor_)
645 simcall->issuer_->context_->set_wannadie();
647 simcall->issuer_->exception_ =
648 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
651 case State::LINK_FAILURE:
652 XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
654 this, src_actor_ ? src_actor_->get_host()->get_cname() : nullptr,
655 dst_actor_ ? dst_actor_->get_host()->get_cname() : nullptr, simcall->issuer_->get_cname(),
656 simcall->issuer_, detached_);
657 if (src_actor_ == simcall->issuer_) {
658 XBT_DEBUG("I'm source");
659 } else if (dst_actor_ == simcall->issuer_) {
660 XBT_DEBUG("I'm dest");
662 XBT_DEBUG("I'm neither source nor dest");
664 simcall->issuer_->throw_exception(
665 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Link failure")));
668 case State::CANCELED:
669 if (simcall->issuer_ == dst_actor_)
670 simcall->issuer_->exception_ = std::make_exception_ptr(
671 simgrid::CancelException(XBT_THROW_POINT, "Communication canceled by the sender"));
673 simcall->issuer_->exception_ = std::make_exception_ptr(
674 simgrid::CancelException(XBT_THROW_POINT, "Communication canceled by the receiver"));
678 xbt_die("Unexpected synchro state in CommImpl::finish: %d", static_cast<int>(state_));
680 simcall->issuer_->simcall_answer();
682 /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
683 if (simcall->issuer_->exception_ &&
684 (simcall->call_ == simix::Simcall::COMM_WAITANY || simcall->call_ == simix::Simcall::COMM_TESTANY)) {
685 // First retrieve the rank of our failing synchro
688 if (simcall->call_ == simix::Simcall::COMM_WAITANY) {
689 comms = simcall_comm_waitany__get__comms(simcall);
690 count = simcall_comm_waitany__get__count(simcall);
692 /* simcall->call_ == simix::Simcall::COMM_TESTANY */
693 comms = simcall_comm_testany__get__comms(simcall);
694 count = simcall_comm_testany__get__count(simcall);
696 CommImpl** element = std::find(comms, comms + count, this);
697 int rank = (element != comms + count) ? element - comms : -1;
699 // In order to modify the exception we have to rethrow it:
701 std::rethrow_exception(simcall->issuer_->exception_);
702 } catch (simgrid::Exception& e) {
707 simcall->issuer_->waiting_synchro_ = nullptr;
708 simcall->issuer_->activities_.remove(this);
710 if (simcall->issuer_ != dst_actor_ && dst_actor_ != nullptr)
711 dst_actor_->activities_.remove(this);
712 if (simcall->issuer_ != src_actor_ && src_actor_ != nullptr)
713 src_actor_->activities_.remove(this);
718 } // namespace activity
719 } // namespace kernel
720 } // namespace simgrid