1 /* Copyright (c) 2009-2017. 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. */
8 #include <boost/range/algorithm.hpp>
10 #include "src/kernel/activity/CommImpl.hpp"
13 #include "simgrid/s4u/Host.hpp"
16 #include "simgrid/s4u/Activity.hpp"
17 #include "simgrid/s4u/Mailbox.hpp"
18 #include "src/mc/mc_replay.h"
19 #include "src/simix/smx_private.h"
20 #include "src/surf/cpu_interface.hpp"
21 #include "src/surf/surf_interface.hpp"
23 #include "src/surf/network_interface.hpp"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix, "SIMIX network-related synchronization");
27 static void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall);
28 static void SIMIX_comm_copy_data(smx_activity_t comm);
29 static void SIMIX_comm_start(smx_activity_t synchro);
30 static simgrid::kernel::activity::CommImpl*
31 _find_matching_comm(boost::circular_buffer_space_optimized<smx_activity_t>* deque, e_smx_comm_type_t type,
32 int (*match_fun)(void*, void*, smx_activity_t), void* user_data, smx_activity_t my_synchro,
33 bool remove_matching);
36 * \brief Checks if there is a communication activity queued in a deque matching our needs
37 * \param type The type of communication we are looking for (comm_send, comm_recv)
38 * \return The communication activity if found, nullptr otherwise
40 static simgrid::kernel::activity::CommImpl*
41 _find_matching_comm(boost::circular_buffer_space_optimized<smx_activity_t>* deque, e_smx_comm_type_t type,
42 int (*match_fun)(void*, void*, smx_activity_t), void* this_user_data, smx_activity_t my_synchro,
45 void* other_user_data = nullptr;
47 for(auto it = deque->begin(); it != deque->end(); it++){
48 smx_activity_t synchro = *it;
49 simgrid::kernel::activity::CommImpl* comm = static_cast<simgrid::kernel::activity::CommImpl*>(synchro);
51 if (comm->type == SIMIX_COMM_SEND) {
52 other_user_data = comm->src_data;
53 } else if (comm->type == SIMIX_COMM_RECEIVE) {
54 other_user_data = comm->dst_data;
56 if (comm->type == type && (match_fun == nullptr || match_fun(this_user_data, other_user_data, synchro)) &&
57 (not comm->match_fun || comm->match_fun(other_user_data, this_user_data, my_synchro))) {
58 XBT_DEBUG("Found a matching communication synchro %p", comm);
63 comm->mbox_cpy = comm->mbox;
68 XBT_DEBUG("Sorry, communication synchro %p does not match our needs:"
69 " its type is %d but we are looking for a comm of type %d (or maybe the filtering didn't match)",
70 comm, (int)comm->type, (int)type);
72 XBT_DEBUG("No matching communication synchro found");
76 /******************************************************************************/
77 /* Communication synchros */
78 /******************************************************************************/
79 XBT_PRIVATE void simcall_HANDLER_comm_send(smx_simcall_t simcall, smx_actor_t src, smx_mailbox_t mbox,
80 double task_size, double rate,
81 void *src_buff, size_t src_buff_size,
82 int (*match_fun)(void *, void *,smx_activity_t),
83 void (*copy_data_fun)(smx_activity_t, void*, size_t),
84 void *data, double timeout){
85 smx_activity_t comm = simcall_HANDLER_comm_isend(simcall, src, mbox, task_size, rate,
86 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun,
88 SIMCALL_SET_MC_VALUE(simcall, 0);
89 simcall_HANDLER_comm_wait(simcall, comm, timeout);
91 XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_isend(smx_simcall_t simcall, smx_actor_t src_proc, smx_mailbox_t mbox,
92 double task_size, double rate,
93 void *src_buff, size_t src_buff_size,
94 int (*match_fun)(void *, void *,smx_activity_t),
95 void (*clean_fun)(void *), // used to free the synchro in case of problem after a detached send
96 void (*copy_data_fun)(smx_activity_t, void*, size_t),// used to copy data if not default one
97 void *data, int detached)
99 XBT_DEBUG("send from %p", mbox);
101 /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
102 simgrid::kernel::activity::CommImpl* this_comm = new simgrid::kernel::activity::CommImpl(SIMIX_COMM_SEND);
104 /* Look for communication synchro matching our needs. We also provide a description of
105 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
107 * If it is not found then push our communication into the rendez-vous point */
108 simgrid::kernel::activity::CommImpl* other_comm =
109 _find_matching_comm(&mbox->comm_queue, SIMIX_COMM_RECEIVE, match_fun, data, this_comm, /*remove_matching*/ true);
111 if (not other_comm) {
112 other_comm = this_comm;
114 if (mbox->permanent_receiver!=nullptr){
115 //this mailbox is for small messages, which have to be sent right now
116 other_comm->state = SIMIX_READY;
117 other_comm->dst_proc=mbox->permanent_receiver.get();
118 other_comm = static_cast<simgrid::kernel::activity::CommImpl*>(SIMIX_comm_ref(other_comm));
119 mbox->done_comm_queue.push_back(other_comm);
120 XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, &(other_comm));
123 mbox->push(this_comm);
126 XBT_DEBUG("Receive already pushed");
127 SIMIX_comm_unref(this_comm);
128 SIMIX_comm_unref(this_comm);
130 other_comm->state = SIMIX_READY;
131 other_comm->type = SIMIX_COMM_READY;
134 src_proc->comms.push_back(other_comm);
137 other_comm->detached = true;
138 other_comm->clean_fun = clean_fun;
140 other_comm->clean_fun = nullptr;
143 /* Setup the communication synchro */
144 other_comm->src_proc = src_proc;
145 other_comm->task_size = task_size;
146 other_comm->rate = rate;
147 other_comm->src_buff = src_buff;
148 other_comm->src_buff_size = src_buff_size;
149 other_comm->src_data = data;
151 other_comm->match_fun = match_fun;
152 other_comm->copy_data_fun = copy_data_fun;
155 if (MC_is_active() || MC_record_replay_is_active()) {
156 other_comm->state = SIMIX_RUNNING;
157 return (detached ? nullptr : other_comm);
160 SIMIX_comm_start(other_comm);
161 return (detached ? nullptr : other_comm);
164 XBT_PRIVATE void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
165 void *dst_buff, size_t *dst_buff_size,
166 int (*match_fun)(void *, void *, smx_activity_t),
167 void (*copy_data_fun)(smx_activity_t, void*, size_t),
168 void *data, double timeout, double rate)
170 smx_activity_t comm = SIMIX_comm_irecv(receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun, data, rate);
171 SIMCALL_SET_MC_VALUE(simcall, 0);
172 simcall_HANDLER_comm_wait(simcall, comm, timeout);
175 XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_irecv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
176 void *dst_buff, size_t *dst_buff_size,
177 int (*match_fun)(void *, void *, smx_activity_t),
178 void (*copy_data_fun)(smx_activity_t, void*, size_t),
179 void *data, double rate)
181 return SIMIX_comm_irecv(receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun, data, rate);
184 smx_activity_t SIMIX_comm_irecv(smx_actor_t dst_proc, smx_mailbox_t mbox, void *dst_buff, size_t *dst_buff_size,
185 int (*match_fun)(void *, void *, smx_activity_t),
186 void (*copy_data_fun)(smx_activity_t, void*, size_t), // used to copy data if not default one
187 void *data, double rate)
189 simgrid::kernel::activity::CommImpl* this_synchro = new simgrid::kernel::activity::CommImpl(SIMIX_COMM_RECEIVE);
190 XBT_DEBUG("recv from %p %p. this_synchro=%p", mbox, &mbox->comm_queue, this_synchro);
192 simgrid::kernel::activity::CommImpl* other_comm;
193 //communication already done, get it inside the list of completed comms
194 if (mbox->permanent_receiver != nullptr && not mbox->done_comm_queue.empty()) {
196 XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
197 //find a match in the list of already received comms
198 other_comm = _find_matching_comm(&mbox->done_comm_queue, SIMIX_COMM_SEND, match_fun, data, this_synchro,
199 /*remove_matching*/ true);
200 //if not found, assume the receiver came first, register it to the mailbox in the classical way
201 if (not other_comm) {
202 XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request into list");
203 other_comm = this_synchro;
204 mbox->push(this_synchro);
206 if (other_comm->surf_comm && other_comm->remains() < 1e-12) {
207 XBT_DEBUG("comm %p has been already sent, and is finished, destroy it",other_comm);
208 other_comm->state = SIMIX_DONE;
209 other_comm->type = SIMIX_COMM_DONE;
210 other_comm->mbox = nullptr;
212 SIMIX_comm_unref(other_comm);
213 SIMIX_comm_unref(this_synchro);
216 /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
218 /* Look for communication activity matching our needs. We also provide a description of
219 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
221 * If it is not found then push our communication into the rendez-vous point */
222 other_comm = _find_matching_comm(&mbox->comm_queue, SIMIX_COMM_SEND, match_fun, data, this_synchro,
223 /*remove_matching*/ true);
225 if (not other_comm) {
226 XBT_DEBUG("Receive pushed first %zu", mbox->comm_queue.size());
227 other_comm = this_synchro;
228 mbox->push(this_synchro);
230 XBT_DEBUG("Match my %p with the existing %p", this_synchro, other_comm);
232 other_comm = static_cast<simgrid::kernel::activity::CommImpl*>(other_comm);
234 other_comm->state = SIMIX_READY;
235 other_comm->type = SIMIX_COMM_READY;
236 SIMIX_comm_unref(this_synchro);
237 SIMIX_comm_unref(this_synchro);
239 dst_proc->comms.push_back(other_comm);
242 /* Setup communication synchro */
243 other_comm->dst_proc = dst_proc;
244 other_comm->dst_buff = dst_buff;
245 other_comm->dst_buff_size = dst_buff_size;
246 other_comm->dst_data = data;
248 if (rate > -1.0 && (other_comm->rate < 0.0 || rate < other_comm->rate))
249 other_comm->rate = rate;
251 other_comm->match_fun = match_fun;
252 other_comm->copy_data_fun = copy_data_fun;
254 if (MC_is_active() || MC_record_replay_is_active()) {
255 other_comm->state = SIMIX_RUNNING;
259 SIMIX_comm_start(other_comm);
263 smx_activity_t simcall_HANDLER_comm_iprobe(smx_simcall_t simcall, smx_mailbox_t mbox,
264 int type, int src, int tag,
265 int (*match_fun)(void *, void *, smx_activity_t),
267 return SIMIX_comm_iprobe(simcall->issuer, mbox, type, src, tag, match_fun, data);
270 smx_activity_t SIMIX_comm_iprobe(smx_actor_t dst_proc, smx_mailbox_t mbox, int type, int src,
271 int tag, int (*match_fun)(void *, void *, smx_activity_t), void *data)
273 XBT_DEBUG("iprobe from %p %p", mbox, &mbox->comm_queue);
274 simgrid::kernel::activity::CommImpl* this_comm;
277 this_comm = new simgrid::kernel::activity::CommImpl(SIMIX_COMM_SEND);
278 smx_type = SIMIX_COMM_RECEIVE;
280 this_comm = new simgrid::kernel::activity::CommImpl(SIMIX_COMM_RECEIVE);
281 smx_type = SIMIX_COMM_SEND;
283 smx_activity_t other_synchro=nullptr;
284 if (mbox->permanent_receiver != nullptr && not mbox->done_comm_queue.empty()) {
285 XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
286 other_synchro = _find_matching_comm(&mbox->done_comm_queue,
287 (e_smx_comm_type_t) smx_type, match_fun, data, this_comm,/*remove_matching*/false);
289 if (not other_synchro) {
290 XBT_DEBUG("check if we have more luck in the normal mailbox");
291 other_synchro = _find_matching_comm(&mbox->comm_queue,
292 (e_smx_comm_type_t) smx_type, match_fun, data, this_comm,/*remove_matching*/false);
296 SIMIX_comm_unref(other_synchro);
298 SIMIX_comm_unref(this_comm);
299 return other_synchro;
302 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, smx_activity_t synchro, double timeout)
304 /* Associate this simcall to the wait synchro */
305 XBT_DEBUG("simcall_HANDLER_comm_wait, %p", synchro);
307 synchro->simcalls.push_back(simcall);
308 simcall->issuer->waiting_synchro = synchro;
310 if (MC_is_active() || MC_record_replay_is_active()) {
311 int idx = SIMCALL_GET_MC_VALUE(simcall);
313 synchro->state = SIMIX_DONE;
315 /* If we reached this point, the wait simcall must have a timeout */
316 /* Otherwise it shouldn't be enabled and executed by the MC */
320 simgrid::kernel::activity::CommImpl* comm = static_cast<simgrid::kernel::activity::CommImpl*>(synchro);
321 if (comm->src_proc == simcall->issuer)
322 comm->state = SIMIX_SRC_TIMEOUT;
324 comm->state = SIMIX_DST_TIMEOUT;
327 SIMIX_comm_finish(synchro);
331 /* If the synchro has already finish perform the error handling, */
332 /* otherwise set up a waiting timeout on the right side */
333 if (synchro->state != SIMIX_WAITING && synchro->state != SIMIX_RUNNING) {
334 SIMIX_comm_finish(synchro);
335 } else { /* if (timeout >= 0) { we need a surf sleep action even when there is no timeout, otherwise surf won't tell us when the host fails */
336 surf_action_t sleep = simcall->issuer->host->pimpl_cpu->sleep(timeout);
337 sleep->setData(synchro);
339 simgrid::kernel::activity::CommImpl* comm = static_cast<simgrid::kernel::activity::CommImpl*>(synchro);
340 if (simcall->issuer == comm->src_proc)
341 comm->src_timeout = sleep;
343 comm->dst_timeout = sleep;
347 void simcall_HANDLER_comm_test(smx_simcall_t simcall, smx_activity_t synchro)
349 simgrid::kernel::activity::CommImpl* comm = static_cast<simgrid::kernel::activity::CommImpl*>(synchro);
351 if (MC_is_active() || MC_record_replay_is_active()){
352 simcall_comm_test__set__result(simcall, comm->src_proc && comm->dst_proc);
353 if (simcall_comm_test__get__result(simcall)){
354 synchro->state = SIMIX_DONE;
355 synchro->simcalls.push_back(simcall);
356 SIMIX_comm_finish(synchro);
358 SIMIX_simcall_answer(simcall);
363 simcall_comm_test__set__result(simcall, (synchro->state != SIMIX_WAITING && synchro->state != SIMIX_RUNNING));
364 if (simcall_comm_test__get__result(simcall)) {
365 synchro->simcalls.push_back(simcall);
366 SIMIX_comm_finish(synchro);
368 SIMIX_simcall_answer(simcall);
372 void simcall_HANDLER_comm_testany(
373 smx_simcall_t simcall, simgrid::kernel::activity::ActivityImpl* comms[], size_t count)
375 // The default result is -1 -- this means, "nothing is ready".
376 // It can be changed below, but only if something matches.
377 simcall_comm_testany__set__result(simcall, -1);
379 if (MC_is_active() || MC_record_replay_is_active()){
380 int idx = SIMCALL_GET_MC_VALUE(simcall);
382 SIMIX_simcall_answer(simcall);
384 simgrid::kernel::activity::ActivityImpl* synchro = comms[idx];
385 simcall_comm_testany__set__result(simcall, idx);
386 synchro->simcalls.push_back(simcall);
387 synchro->state = SIMIX_DONE;
388 SIMIX_comm_finish(synchro);
393 for (std::size_t i = 0; i != count; ++i) {
394 simgrid::kernel::activity::ActivityImpl* synchro = comms[i];
395 if (synchro->state != SIMIX_WAITING && synchro->state != SIMIX_RUNNING) {
396 simcall_comm_testany__set__result(simcall, i);
397 synchro->simcalls.push_back(simcall);
398 SIMIX_comm_finish(synchro);
402 SIMIX_simcall_answer(simcall);
405 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, xbt_dynar_t synchros, double timeout)
407 smx_activity_t synchro;
408 unsigned int cursor = 0;
410 if (MC_is_active() || MC_record_replay_is_active()){
412 xbt_die("Timeout not implemented for waitany in the model-checker");
413 int idx = SIMCALL_GET_MC_VALUE(simcall);
414 synchro = xbt_dynar_get_as(synchros, idx, smx_activity_t);
415 synchro->simcalls.push_back(simcall);
416 simcall_comm_waitany__set__result(simcall, idx);
417 synchro->state = SIMIX_DONE;
418 SIMIX_comm_finish(synchro);
423 simcall->timer = NULL;
425 simcall->timer = SIMIX_timer_set(SIMIX_get_clock() + timeout, [simcall]() {
426 SIMIX_waitany_remove_simcall_from_actions(simcall);
427 simcall_comm_waitany__set__result(simcall, -1);
428 SIMIX_simcall_answer(simcall);
432 xbt_dynar_foreach(synchros, cursor, synchro){
433 /* associate this simcall to the the synchro */
434 synchro->simcalls.push_back(simcall);
436 /* see if the synchro is already finished */
437 if (synchro->state != SIMIX_WAITING && synchro->state != SIMIX_RUNNING){
438 SIMIX_comm_finish(synchro);
444 void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall)
446 smx_activity_t synchro;
447 unsigned int cursor = 0;
448 xbt_dynar_t synchros = simcall_comm_waitany__get__comms(simcall);
450 xbt_dynar_foreach(synchros, cursor, synchro) {
451 // Remove the first occurence of simcall:
452 auto i = boost::range::find(synchro->simcalls, simcall);
453 if (i != synchro->simcalls.end())
454 synchro->simcalls.erase(i);
459 * \brief Starts the simulation of a communication synchro.
460 * \param synchro the communication synchro
462 static inline void SIMIX_comm_start(smx_activity_t synchro)
464 simgrid::kernel::activity::CommImpl* comm = static_cast<simgrid::kernel::activity::CommImpl*>(synchro);
466 /* If both the sender and the receiver are already there, start the communication */
467 if (synchro->state == SIMIX_READY) {
469 simgrid::s4u::Host* sender = comm->src_proc->host;
470 simgrid::s4u::Host* receiver = comm->dst_proc->host;
472 comm->surf_comm = surf_network_model->communicate(sender, receiver, comm->task_size, comm->rate);
473 comm->surf_comm->setData(synchro);
474 comm->state = SIMIX_RUNNING;
476 XBT_DEBUG("Starting communication %p from '%s' to '%s' (surf_action: %p)", synchro, sender->cname(),
477 receiver->cname(), comm->surf_comm);
479 /* If a link is failed, detect it immediately */
480 if (comm->surf_comm->getState() == simgrid::surf::Action::State::failed) {
481 XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure", sender->cname(),
483 comm->state = SIMIX_LINK_FAILURE;
487 /* If any of the process is suspend, create the synchro but stop its execution,
488 it will be restarted when the sender process resume */
489 if (SIMIX_process_is_suspended(comm->src_proc) || SIMIX_process_is_suspended(comm->dst_proc)) {
490 if (SIMIX_process_is_suspended(comm->src_proc))
491 XBT_DEBUG("The communication is suspended on startup because src (%s@%s) was suspended since it initiated the "
493 comm->src_proc->cname(), comm->src_proc->host->cname());
495 XBT_DEBUG("The communication is suspended on startup because dst (%s@%s) was suspended since it initiated the "
497 comm->dst_proc->cname(), comm->dst_proc->host->cname());
499 comm->surf_comm->suspend();
505 * \brief Answers the SIMIX simcalls associated to a communication synchro.
506 * \param synchro a finished communication synchro
508 void SIMIX_comm_finish(smx_activity_t synchro)
510 simgrid::kernel::activity::CommImpl* comm = static_cast<simgrid::kernel::activity::CommImpl*>(synchro);
512 while (not synchro->simcalls.empty()) {
513 smx_simcall_t simcall = synchro->simcalls.front();
514 synchro->simcalls.pop_front();
516 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
517 * list. Afterwards, get the position of the actual synchro in the waitany dynar and return it as the result of the
520 if (simcall->call == SIMCALL_NONE) //FIXME: maybe a better way to handle this case
521 continue; // if process handling comm is killed
522 if (simcall->call == SIMCALL_COMM_WAITANY) {
523 SIMIX_waitany_remove_simcall_from_actions(simcall);
524 if (simcall->timer) {
525 SIMIX_timer_remove(simcall->timer);
526 simcall->timer = nullptr;
528 if (not MC_is_active() && not MC_record_replay_is_active())
529 simcall_comm_waitany__set__result(simcall,
530 xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro));
533 /* If the synchro is still in a rendez-vous point then remove from it */
535 comm->mbox->remove(comm);
537 XBT_DEBUG("SIMIX_comm_finish: synchro state = %d", (int)synchro->state);
539 /* Check out for errors */
541 if (simcall->issuer->host->isOff()) {
542 simcall->issuer->context->iwannadie = 1;
543 SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
545 switch (comm->state) {
548 XBT_DEBUG("Communication %p complete!", synchro);
549 SIMIX_comm_copy_data(synchro);
552 case SIMIX_SRC_TIMEOUT:
553 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Communication timeouted because of sender");
556 case SIMIX_DST_TIMEOUT:
557 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Communication timeouted because of receiver");
560 case SIMIX_SRC_HOST_FAILURE:
561 if (simcall->issuer == comm->src_proc)
562 simcall->issuer->context->iwannadie = 1;
563 // SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
565 SMX_EXCEPTION(simcall->issuer, network_error, 0, "Remote peer failed");
568 case SIMIX_DST_HOST_FAILURE:
569 if (simcall->issuer == comm->dst_proc)
570 simcall->issuer->context->iwannadie = 1;
571 // SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
573 SMX_EXCEPTION(simcall->issuer, network_error, 0, "Remote peer failed");
576 case SIMIX_LINK_FAILURE:
577 XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
579 synchro, comm->src_proc ? comm->src_proc->host->cname() : nullptr,
580 comm->dst_proc ? comm->dst_proc->host->cname() : nullptr, simcall->issuer->cname(), simcall->issuer,
582 if (comm->src_proc == simcall->issuer) {
583 XBT_DEBUG("I'm source");
584 } else if (comm->dst_proc == simcall->issuer) {
585 XBT_DEBUG("I'm dest");
587 XBT_DEBUG("I'm neither source nor dest");
589 SMX_EXCEPTION(simcall->issuer, network_error, 0, "Link failure");
593 if (simcall->issuer == comm->dst_proc)
594 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Communication canceled by the sender");
596 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Communication canceled by the receiver");
600 xbt_die("Unexpected synchro state in SIMIX_comm_finish: %d", (int)synchro->state);
604 /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
605 if (simcall->issuer->exception) {
606 // In order to modify the exception we have to rethrow it:
608 std::rethrow_exception(simcall->issuer->exception);
611 if (simcall->call == SIMCALL_COMM_WAITANY) {
612 e.value = xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro);
614 else if (simcall->call == SIMCALL_COMM_TESTANY) {
616 auto comms = simcall_comm_testany__get__comms(simcall);
617 auto count = simcall_comm_testany__get__count(simcall);
618 auto element = std::find(comms, comms + count, synchro);
619 if (element == comms + count)
622 e.value = element - comms;
624 simcall->issuer->exception = std::make_exception_ptr(e);
631 if (simcall->issuer->host->isOff()) {
632 simcall->issuer->context->iwannadie = 1;
635 simcall->issuer->waiting_synchro = nullptr;
636 simcall->issuer->comms.remove(synchro);
638 if(simcall->issuer == comm->src_proc){
640 comm->dst_proc->comms.remove(synchro);
642 else if(simcall->issuer == comm->dst_proc){
644 comm->src_proc->comms.remove(synchro);
647 comm->dst_proc->comms.remove(synchro);
648 comm->src_proc->comms.remove(synchro);
652 SIMIX_simcall_answer(simcall);
656 /******************************************************************************/
657 /* SIMIX_comm_copy_data callbacks */
658 /******************************************************************************/
659 static void (*SIMIX_comm_copy_data_callback) (smx_activity_t, void*, size_t) = &SIMIX_comm_copy_pointer_callback;
661 void SIMIX_comm_set_copy_data_callback(void (*callback) (smx_activity_t, void*, size_t))
663 SIMIX_comm_copy_data_callback = callback;
666 void SIMIX_comm_copy_pointer_callback(smx_activity_t synchro, void* buff, size_t buff_size)
668 simgrid::kernel::activity::CommImpl* comm = static_cast<simgrid::kernel::activity::CommImpl*>(synchro);
670 xbt_assert((buff_size == sizeof(void *)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
671 *(void **) (comm->dst_buff) = buff;
674 void SIMIX_comm_copy_buffer_callback(smx_activity_t synchro, void* buff, size_t buff_size)
676 simgrid::kernel::activity::CommImpl* comm = static_cast<simgrid::kernel::activity::CommImpl*>(synchro);
678 XBT_DEBUG("Copy the data over");
679 memcpy(comm->dst_buff, buff, buff_size);
680 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
682 comm->src_buff = nullptr;
687 * @brief Copy the communication data from the sender's buffer to the receiver's one
688 * @param synchro The communication
690 void SIMIX_comm_copy_data(smx_activity_t synchro)
692 simgrid::kernel::activity::CommImpl* comm = static_cast<simgrid::kernel::activity::CommImpl*>(synchro);
694 size_t buff_size = comm->src_buff_size;
695 /* If there is no data to copy then return */
696 if (not comm->src_buff || not comm->dst_buff || comm->copied)
699 XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", comm,
700 comm->src_proc ? comm->src_proc->host->cname() : "a finished process", comm->src_buff,
701 comm->dst_proc ? comm->dst_proc->host->cname() : "a finished process", comm->dst_buff, buff_size);
703 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
704 if (comm->dst_buff_size)
705 buff_size = MIN(buff_size, *(comm->dst_buff_size));
707 /* Update the receiver's buffer size to the copied amount */
708 if (comm->dst_buff_size)
709 *comm->dst_buff_size = buff_size;
712 if(comm->copy_data_fun)
713 comm->copy_data_fun (comm, comm->src_buff, buff_size);
715 SIMIX_comm_copy_data_callback (comm, comm->src_buff, buff_size);
718 /* Set the copied flag so we copy data only once */
719 /* (this function might be called from both communication ends) */
723 /** Increase the refcount for this comm */
724 smx_activity_t SIMIX_comm_ref(smx_activity_t comm)
727 intrusive_ptr_add_ref(comm);
731 /** Decrease the refcount for this comm */
732 void SIMIX_comm_unref(smx_activity_t comm)
735 intrusive_ptr_release(comm);