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);
21 static void SIMIX_comm_copy_data(smx_activity_t comm);
22 static void SIMIX_comm_start(simgrid::kernel::activity::CommImplPtr synchro);
25 * @brief Checks if there is a communication activity queued in a deque matching our needs
26 * @param deque where to search into
27 * @param type The type of communication we are looking for (comm_send, comm_recv)
28 * @param match_fun the function to apply
29 * @param this_user_data additional parameter to the match_fun
30 * @param my_synchro what to compare against
31 * @param remove_matching whether or not to clean the found object from the queue
32 * @return The communication activity if found, nullptr otherwise
34 static simgrid::kernel::activity::CommImplPtr
35 _find_matching_comm(boost::circular_buffer_space_optimized<smx_activity_t>* deque, e_smx_comm_type_t type,
36 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* this_user_data,
37 simgrid::kernel::activity::CommImplPtr my_synchro, bool remove_matching)
39 void* other_user_data = nullptr;
41 for(auto it = deque->begin(); it != deque->end(); it++){
42 simgrid::kernel::activity::CommImplPtr comm =
43 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(std::move(*it));
45 if (comm->type == SIMIX_COMM_SEND) {
46 other_user_data = comm->src_data_;
47 } else if (comm->type == SIMIX_COMM_RECEIVE) {
48 other_user_data = comm->dst_data_;
50 if (comm->type == type && (match_fun == nullptr || match_fun(this_user_data, other_user_data, comm.get())) &&
51 (not comm->match_fun || comm->match_fun(other_user_data, this_user_data, my_synchro.get()))) {
52 XBT_DEBUG("Found a matching communication synchro %p", comm.get());
56 comm->mbox_cpy = comm->mbox;
61 XBT_DEBUG("Sorry, communication synchro %p does not match our needs:"
62 " its type is %d but we are looking for a comm of type %d (or maybe the filtering didn't match)",
63 comm.get(), (int)comm->type, (int)type);
65 XBT_DEBUG("No matching communication synchro found");
69 /******************************************************************************/
70 /* Communication synchros */
71 /******************************************************************************/
72 XBT_PRIVATE void simcall_HANDLER_comm_send(smx_simcall_t simcall, smx_actor_t src, smx_mailbox_t mbox, double task_size,
73 double rate, void* src_buff, size_t src_buff_size,
74 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
75 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data,
78 smx_activity_t comm = simcall_HANDLER_comm_isend(simcall, src, mbox, task_size, rate,
79 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun,
81 SIMCALL_SET_MC_VALUE(simcall, 0);
82 simcall_HANDLER_comm_wait(simcall, comm, timeout);
84 XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_isend(
85 smx_simcall_t /*simcall*/, smx_actor_t src_proc, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
86 size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
87 void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
88 void (*copy_data_fun)(smx_activity_t, void*, size_t), // used to copy data if not default one
89 void* data, int detached)
91 XBT_DEBUG("send from mailbox %p", mbox);
93 /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
94 simgrid::kernel::activity::CommImplPtr this_comm =
95 simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_SEND));
97 /* Look for communication synchro matching our needs. We also provide a description of
98 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
100 * If it is not found then push our communication into the rendez-vous point */
101 simgrid::kernel::activity::CommImplPtr other_comm =
102 _find_matching_comm(&mbox->comm_queue_, SIMIX_COMM_RECEIVE, match_fun, data, this_comm, /*remove_matching*/ true);
104 if (not other_comm) {
105 other_comm = std::move(this_comm);
107 if (mbox->permanent_receiver_ != nullptr) {
108 //this mailbox is for small messages, which have to be sent right now
109 other_comm->state_ = SIMIX_READY;
110 other_comm->dst_actor_ = mbox->permanent_receiver_.get();
111 mbox->done_comm_queue_.push_back(other_comm);
112 XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, other_comm.get());
115 mbox->push(other_comm);
118 XBT_DEBUG("Receive already pushed");
120 other_comm->state_ = SIMIX_READY;
121 other_comm->type = SIMIX_COMM_READY;
123 src_proc->comms.push_back(other_comm);
126 other_comm->detached = true;
127 other_comm->clean_fun = clean_fun;
129 other_comm->clean_fun = nullptr;
132 /* Setup the communication synchro */
133 other_comm->src_actor_ = src_proc;
134 other_comm->task_size_ = task_size;
135 other_comm->rate_ = rate;
136 other_comm->src_buff_ = src_buff;
137 other_comm->src_buff_size_ = src_buff_size;
138 other_comm->src_data_ = data;
140 other_comm->match_fun = match_fun;
141 other_comm->copy_data_fun = copy_data_fun;
144 if (MC_is_active() || MC_record_replay_is_active()) {
145 other_comm->state_ = SIMIX_RUNNING;
146 return (detached ? nullptr : other_comm);
149 SIMIX_comm_start(other_comm);
150 return (detached ? nullptr : other_comm);
153 XBT_PRIVATE void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
154 void* dst_buff, size_t* dst_buff_size,
155 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
156 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data,
157 double timeout, double rate)
159 smx_activity_t comm = SIMIX_comm_irecv(receiver, mbox, dst_buff, dst_buff_size, match_fun, 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 return SIMIX_comm_irecv(receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun, data, rate);
174 SIMIX_comm_irecv(smx_actor_t dst_proc, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
175 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
176 void (*copy_data_fun)(smx_activity_t, void*, size_t), // used to copy data if not default one
177 void* data, double rate)
179 simgrid::kernel::activity::CommImplPtr this_synchro =
180 simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_RECEIVE));
181 XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
183 simgrid::kernel::activity::CommImplPtr other_comm;
184 //communication already done, get it inside the list of completed comms
185 if (mbox->permanent_receiver_ != nullptr && not mbox->done_comm_queue_.empty()) {
187 XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
188 //find a match in the list of already received comms
189 other_comm = _find_matching_comm(&mbox->done_comm_queue_, SIMIX_COMM_SEND, match_fun, data, this_synchro,
190 /*remove_matching*/ true);
191 //if not found, assume the receiver came first, register it to the mailbox in the classical way
192 if (not other_comm) {
193 XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request into list");
194 other_comm = std::move(this_synchro);
195 mbox->push(other_comm);
197 if (other_comm->surf_action_ && other_comm->remains() < 1e-12) {
198 XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
199 other_comm->state_ = SIMIX_DONE;
200 other_comm->type = SIMIX_COMM_DONE;
201 other_comm->mbox = nullptr;
205 /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
207 /* Look for communication activity matching our needs. We also provide a description of
208 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
210 * If it is not found then push our communication into the rendez-vous point */
211 other_comm = _find_matching_comm(&mbox->comm_queue_, SIMIX_COMM_SEND, match_fun, data, this_synchro,
212 /*remove_matching*/ true);
214 if (other_comm == nullptr) {
215 XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->comm_queue_.size());
216 other_comm = std::move(this_synchro);
217 mbox->push(other_comm);
219 XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
221 other_comm->state_ = SIMIX_READY;
222 other_comm->type = SIMIX_COMM_READY;
224 dst_proc->comms.push_back(other_comm);
227 /* Setup communication synchro */
228 other_comm->dst_actor_ = dst_proc;
229 other_comm->dst_buff_ = dst_buff;
230 other_comm->dst_buff_size_ = dst_buff_size;
231 other_comm->dst_data_ = data;
233 if (rate > -1.0 && (other_comm->rate_ < 0.0 || rate < other_comm->rate_))
234 other_comm->rate_ = rate;
236 other_comm->match_fun = match_fun;
237 other_comm->copy_data_fun = copy_data_fun;
239 if (MC_is_active() || MC_record_replay_is_active()) {
240 other_comm->state_ = SIMIX_RUNNING;
244 SIMIX_comm_start(other_comm);
248 smx_activity_t simcall_HANDLER_comm_iprobe(smx_simcall_t simcall, smx_mailbox_t mbox, int type,
249 simix_match_func_t match_fun, void* data)
251 return SIMIX_comm_iprobe(simcall->issuer, mbox, type, match_fun, data);
254 smx_activity_t SIMIX_comm_iprobe(smx_actor_t dst_proc, smx_mailbox_t mbox, int type, simix_match_func_t match_fun,
257 XBT_DEBUG("iprobe from %p %p", mbox, &mbox->comm_queue_);
258 simgrid::kernel::activity::CommImplPtr this_comm;
261 this_comm = simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_SEND));
262 smx_type = SIMIX_COMM_RECEIVE;
264 this_comm = simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_RECEIVE));
265 smx_type = SIMIX_COMM_SEND;
267 smx_activity_t other_synchro=nullptr;
268 if (mbox->permanent_receiver_ != nullptr && not mbox->done_comm_queue_.empty()) {
269 XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
270 other_synchro = _find_matching_comm(&mbox->done_comm_queue_, (e_smx_comm_type_t)smx_type, match_fun, data,
271 this_comm, /*remove_matching*/ false);
273 if (not other_synchro) {
274 XBT_DEBUG("check if we have more luck in the normal mailbox");
275 other_synchro = _find_matching_comm(&mbox->comm_queue_, (e_smx_comm_type_t)smx_type, match_fun, data, this_comm,
276 /*remove_matching*/ false);
279 return other_synchro;
282 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, smx_activity_t synchro, double timeout)
284 /* Associate this simcall to the wait synchro */
285 XBT_DEBUG("simcall_HANDLER_comm_wait, %p", synchro.get());
287 synchro->simcalls_.push_back(simcall);
288 simcall->issuer->waiting_synchro = synchro;
290 if (MC_is_active() || MC_record_replay_is_active()) {
291 int idx = SIMCALL_GET_MC_VALUE(simcall);
293 synchro->state_ = SIMIX_DONE;
295 /* If we reached this point, the wait simcall must have a timeout */
296 /* Otherwise it shouldn't be enabled and executed by the MC */
300 simgrid::kernel::activity::CommImplPtr comm =
301 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
302 if (comm->src_actor_ == simcall->issuer)
303 comm->state_ = SIMIX_SRC_TIMEOUT;
305 comm->state_ = SIMIX_DST_TIMEOUT;
308 SIMIX_comm_finish(synchro);
312 /* If the synchro has already finish perform the error handling, */
313 /* otherwise set up a waiting timeout on the right side */
314 if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
315 SIMIX_comm_finish(synchro);
316 } else { /* we need a sleep action (even when there is no timeout) to be notified of host failures */
317 simgrid::kernel::resource::Action* sleep = simcall->issuer->host_->pimpl_cpu->sleep(timeout);
318 sleep->set_data(synchro.get());
320 simgrid::kernel::activity::CommImplPtr comm =
321 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
322 if (simcall->issuer == comm->src_actor_)
323 comm->src_timeout_ = sleep;
325 comm->dst_timeout_ = sleep;
329 void simcall_HANDLER_comm_test(smx_simcall_t simcall, smx_activity_t synchro)
331 simgrid::kernel::activity::CommImplPtr comm =
332 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
336 if (MC_is_active() || MC_record_replay_is_active()){
337 res = comm->src_actor_ && comm->dst_actor_;
339 synchro->state_ = SIMIX_DONE;
341 res = synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING;
344 simcall_comm_test__set__result(simcall, res);
345 if (simcall_comm_test__get__result(simcall)) {
346 synchro->simcalls_.push_back(simcall);
347 SIMIX_comm_finish(synchro);
349 SIMIX_simcall_answer(simcall);
353 void simcall_HANDLER_comm_testany(smx_simcall_t simcall, simgrid::kernel::activity::ActivityImplPtr comms[],
356 // The default result is -1 -- this means, "nothing is ready".
357 // It can be changed below, but only if something matches.
358 simcall_comm_testany__set__result(simcall, -1);
360 if (MC_is_active() || MC_record_replay_is_active()){
361 int idx = SIMCALL_GET_MC_VALUE(simcall);
363 SIMIX_simcall_answer(simcall);
365 simgrid::kernel::activity::ActivityImplPtr synchro = comms[idx];
366 simcall_comm_testany__set__result(simcall, idx);
367 synchro->simcalls_.push_back(simcall);
368 synchro->state_ = SIMIX_DONE;
369 SIMIX_comm_finish(synchro);
374 for (std::size_t i = 0; i != count; ++i) {
375 simgrid::kernel::activity::ActivityImplPtr synchro = comms[i];
376 if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
377 simcall_comm_testany__set__result(simcall, i);
378 synchro->simcalls_.push_back(simcall);
379 SIMIX_comm_finish(synchro);
383 SIMIX_simcall_answer(simcall);
386 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, xbt_dynar_t synchros, double timeout)
388 if (MC_is_active() || MC_record_replay_is_active()){
390 xbt_die("Timeout not implemented for waitany in the model-checker");
391 int idx = SIMCALL_GET_MC_VALUE(simcall);
392 smx_activity_t synchro = xbt_dynar_get_as(synchros, idx, smx_activity_t);
393 synchro->simcalls_.push_back(simcall);
394 simcall_comm_waitany__set__result(simcall, idx);
395 synchro->state_ = SIMIX_DONE;
396 SIMIX_comm_finish(synchro);
401 simcall->timer = NULL;
403 simcall->timer = SIMIX_timer_set(SIMIX_get_clock() + timeout, [simcall]() {
404 SIMIX_waitany_remove_simcall_from_actions(simcall);
405 simcall_comm_waitany__set__result(simcall, -1);
406 SIMIX_simcall_answer(simcall);
411 simgrid::kernel::activity::ActivityImpl* ptr;
412 xbt_dynar_foreach(synchros, cursor, ptr){
413 smx_activity_t synchro = simgrid::kernel::activity::ActivityImplPtr(ptr);
414 /* associate this simcall to the the synchro */
415 synchro->simcalls_.push_back(simcall);
417 /* see if the synchro is already finished */
418 if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
419 SIMIX_comm_finish(synchro);
425 void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall)
427 unsigned int cursor = 0;
428 xbt_dynar_t synchros = simcall_comm_waitany__get__comms(simcall);
430 simgrid::kernel::activity::ActivityImpl* ptr;
431 xbt_dynar_foreach(synchros, cursor, ptr){
432 smx_activity_t synchro = simgrid::kernel::activity::ActivityImplPtr(ptr);
434 // Remove the first occurence of simcall:
435 auto i = boost::range::find(synchro->simcalls_, simcall);
436 if (i != synchro->simcalls_.end())
437 synchro->simcalls_.erase(i);
442 * @brief Starts the simulation of a communication synchro.
443 * @param comm the communication that will be started
445 static inline void SIMIX_comm_start(simgrid::kernel::activity::CommImplPtr comm)
447 /* If both the sender and the receiver are already there, start the communication */
448 if (comm->state_ == SIMIX_READY) {
450 simgrid::s4u::Host* sender = comm->src_actor_->host_;
451 simgrid::s4u::Host* receiver = comm->dst_actor_->host_;
453 comm->surf_action_ = surf_network_model->communicate(sender, receiver, comm->task_size_, comm->rate_);
454 comm->surf_action_->set_data(comm.get());
455 comm->state_ = SIMIX_RUNNING;
457 XBT_DEBUG("Starting communication %p from '%s' to '%s' (surf_action: %p)", comm.get(), sender->get_cname(),
458 receiver->get_cname(), comm->surf_action_);
460 /* If a link is failed, detect it immediately */
461 if (comm->surf_action_->get_state() == simgrid::kernel::resource::Action::State::FAILED) {
462 XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure", sender->get_cname(),
463 receiver->get_cname());
464 comm->state_ = SIMIX_LINK_FAILURE;
468 /* If any of the process is suspended, create the synchro but stop its execution,
469 it will be restarted when the sender process resume */
470 if (comm->src_actor_->is_suspended() || comm->dst_actor_->is_suspended()) {
471 if (comm->src_actor_->is_suspended())
472 XBT_DEBUG("The communication is suspended on startup because src (%s@%s) was suspended since it initiated the "
474 comm->src_actor_->get_cname(), comm->src_actor_->host_->get_cname());
476 XBT_DEBUG("The communication is suspended on startup because dst (%s@%s) was suspended since it initiated the "
478 comm->dst_actor_->get_cname(), comm->dst_actor_->host_->get_cname());
480 comm->surf_action_->suspend();
486 * @brief Answers the SIMIX simcalls associated to a communication synchro.
487 * @param synchro a finished communication synchro
489 void SIMIX_comm_finish(smx_activity_t synchro)
491 simgrid::kernel::activity::CommImplPtr comm =
492 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
494 while (not synchro->simcalls_.empty()) {
495 smx_simcall_t simcall = synchro->simcalls_.front();
496 synchro->simcalls_.pop_front();
498 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
499 * list. Afterwards, get the position of the actual synchro in the waitany dynar and return it as the result of the
502 if (simcall->call == SIMCALL_NONE) //FIXME: maybe a better way to handle this case
503 continue; // if process handling comm is killed
504 if (simcall->call == SIMCALL_COMM_WAITANY) {
505 SIMIX_waitany_remove_simcall_from_actions(simcall);
506 if (simcall->timer) {
507 SIMIX_timer_remove(simcall->timer);
508 simcall->timer = nullptr;
510 if (not MC_is_active() && not MC_record_replay_is_active())
511 simcall_comm_waitany__set__result(simcall,
512 xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro));
515 /* If the synchro is still in a rendez-vous point then remove from it */
517 comm->mbox->remove(comm);
519 XBT_DEBUG("SIMIX_comm_finish: synchro state = %d", (int)synchro->state_);
521 /* Check out for errors */
523 if (simcall->issuer->host_->is_off()) {
524 simcall->issuer->context_->iwannadie = true;
525 simcall->issuer->exception =
526 std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
528 switch (comm->state_) {
531 XBT_DEBUG("Communication %p complete!", synchro.get());
532 SIMIX_comm_copy_data(synchro);
535 case SIMIX_SRC_TIMEOUT:
536 simcall->issuer->exception = std::make_exception_ptr(
537 simgrid::TimeoutError(XBT_THROW_POINT, "Communication timeouted because of the sender"));
540 case SIMIX_DST_TIMEOUT:
541 simcall->issuer->exception = std::make_exception_ptr(
542 simgrid::TimeoutError(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
545 case SIMIX_SRC_HOST_FAILURE:
546 if (simcall->issuer == comm->src_actor_)
547 simcall->issuer->context_->iwannadie = true;
549 simcall->issuer->exception =
550 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
553 case SIMIX_DST_HOST_FAILURE:
554 if (simcall->issuer == comm->dst_actor_)
555 simcall->issuer->context_->iwannadie = true;
557 simcall->issuer->exception =
558 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
561 case SIMIX_LINK_FAILURE:
562 XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
564 synchro.get(), comm->src_actor_ ? comm->src_actor_->host_->get_cname() : nullptr,
565 comm->dst_actor_ ? comm->dst_actor_->host_->get_cname() : nullptr, simcall->issuer->get_cname(),
566 simcall->issuer, comm->detached);
567 if (comm->src_actor_ == simcall->issuer) {
568 XBT_DEBUG("I'm source");
569 } else if (comm->dst_actor_ == simcall->issuer) {
570 XBT_DEBUG("I'm dest");
572 XBT_DEBUG("I'm neither source nor dest");
574 simcall->issuer->throw_exception(
575 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Link failure")));
579 if (simcall->issuer == comm->dst_actor_)
580 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Communication canceled by the sender");
582 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Communication canceled by the receiver");
586 xbt_die("Unexpected synchro state in SIMIX_comm_finish: %d", (int)synchro->state_);
590 /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
591 if (simcall->issuer->exception &&
592 (simcall->call == SIMCALL_COMM_WAITANY || simcall->call == SIMCALL_COMM_TESTANY)) {
593 // First retrieve the rank of our failing synchro
595 if (simcall->call == SIMCALL_COMM_WAITANY) {
596 rank = xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro);
597 } else if (simcall->call == SIMCALL_COMM_TESTANY) {
599 auto* comms = simcall_comm_testany__get__comms(simcall);
600 auto count = simcall_comm_testany__get__count(simcall);
601 auto element = std::find(comms, comms + count, synchro);
602 if (element == comms + count)
605 rank = element - comms;
608 // In order to modify the exception we have to rethrow it:
610 std::rethrow_exception(simcall->issuer->exception);
611 } catch (simgrid::TimeoutError& e) {
613 simcall->issuer->exception = std::make_exception_ptr(e);
614 } catch (simgrid::NetworkFailureException& e) {
616 simcall->issuer->exception = std::make_exception_ptr(e);
617 } catch (xbt_ex& e) {
618 if (e.category == cancel_error) {
620 simcall->issuer->exception = std::make_exception_ptr(e);
622 xbt_die("Unexpected xbt_ex(%s). Please enhance this code", xbt_ex_catname(e.category));
627 if (simcall->issuer->host_->is_off())
628 simcall->issuer->context_->iwannadie = true;
630 simcall->issuer->waiting_synchro = nullptr;
631 simcall->issuer->comms.remove(synchro);
633 if (simcall->issuer == comm->src_actor_) {
634 if (comm->dst_actor_)
635 comm->dst_actor_->comms.remove(synchro);
636 } else if (simcall->issuer == comm->dst_actor_) {
637 if (comm->src_actor_)
638 comm->src_actor_->comms.remove(synchro);
641 comm->dst_actor_->comms.remove(synchro);
642 comm->src_actor_->comms.remove(synchro);
646 SIMIX_simcall_answer(simcall);
650 /******************************************************************************/
651 /* SIMIX_comm_copy_data callbacks */
652 /******************************************************************************/
653 static void (*SIMIX_comm_copy_data_callback) (smx_activity_t, void*, size_t) = &SIMIX_comm_copy_pointer_callback;
655 void SIMIX_comm_set_copy_data_callback(void (*callback) (smx_activity_t, void*, size_t))
657 SIMIX_comm_copy_data_callback = callback;
660 void SIMIX_comm_copy_pointer_callback(smx_activity_t synchro, void* buff, size_t buff_size)
662 simgrid::kernel::activity::CommImplPtr comm =
663 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
665 xbt_assert((buff_size == sizeof(void *)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
666 *(void**)(comm->dst_buff_) = buff;
669 void SIMIX_comm_copy_buffer_callback(smx_activity_t synchro, void* buff, size_t buff_size)
671 simgrid::kernel::activity::CommImplPtr comm =
672 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
674 XBT_DEBUG("Copy the data over");
675 memcpy(comm->dst_buff_, buff, buff_size);
676 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
678 comm->src_buff_ = nullptr;
683 * @brief Copy the communication data from the sender's buffer to the receiver's one
684 * @param synchro The communication
686 void SIMIX_comm_copy_data(smx_activity_t synchro)
688 simgrid::kernel::activity::CommImplPtr comm =
689 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
691 size_t buff_size = comm->src_buff_size_;
692 /* If there is no data to copy then return */
693 if (not comm->src_buff_ || not comm->dst_buff_ || comm->copied)
696 XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", comm.get(),
697 comm->src_actor_ ? comm->src_actor_->host_->get_cname() : "a finished process", comm->src_buff_,
698 comm->dst_actor_ ? comm->dst_actor_->host_->get_cname() : "a finished process", comm->dst_buff_, buff_size);
700 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
701 if (comm->dst_buff_size_)
702 buff_size = std::min(buff_size, *(comm->dst_buff_size_));
704 /* Update the receiver's buffer size to the copied amount */
705 if (comm->dst_buff_size_)
706 *comm->dst_buff_size_ = buff_size;
709 if(comm->copy_data_fun)
710 comm->copy_data_fun(comm, comm->src_buff_, buff_size);
712 SIMIX_comm_copy_data_callback(comm, comm->src_buff_, buff_size);
715 /* Set the copied flag so we copy data only once */
716 /* (this function might be called from both communication ends) */