1 /* Copyright (c) 2009-2018. 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 "src/kernel/activity/MailboxImpl.hpp"
8 #include "src/mc/mc_replay.hpp"
9 #include "src/simix/smx_private.hpp"
10 #include "src/surf/cpu_interface.hpp"
11 #include "src/surf/network_interface.hpp"
14 #include <boost/circular_buffer.hpp>
15 #include <boost/range/algorithm.hpp>
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix, "SIMIX network-related synchronization");
19 static void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall);
20 static void SIMIX_comm_copy_data(smx_activity_t comm);
21 static void SIMIX_comm_start(simgrid::kernel::activity::CommImplPtr synchro);
24 * @brief Checks if there is a communication activity queued in a deque matching our needs
25 * @param deque where to search into
26 * @param type The type of communication we are looking for (comm_send, comm_recv)
27 * @param match_fun the function to apply
28 * @param this_user_data additional parameter to the match_fun
29 * @param my_synchro what to compare against
30 * @param remove_matching whether or not to clean the found object from the queue
31 * @return The communication activity if found, nullptr otherwise
33 static simgrid::kernel::activity::CommImplPtr
34 _find_matching_comm(boost::circular_buffer_space_optimized<smx_activity_t>* deque, e_smx_comm_type_t type,
35 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* this_user_data,
36 simgrid::kernel::activity::CommImplPtr my_synchro, bool remove_matching)
38 void* other_user_data = nullptr;
40 for(auto it = deque->begin(); it != deque->end(); it++){
41 simgrid::kernel::activity::CommImplPtr comm =
42 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(std::move(*it));
44 if (comm->type == SIMIX_COMM_SEND) {
45 other_user_data = comm->src_data;
46 } else if (comm->type == SIMIX_COMM_RECEIVE) {
47 other_user_data = comm->dst_data;
49 if (comm->type == type && (match_fun == nullptr || match_fun(this_user_data, other_user_data, comm.get())) &&
50 (not comm->match_fun || comm->match_fun(other_user_data, this_user_data, my_synchro.get()))) {
51 XBT_DEBUG("Found a matching communication synchro %p", comm.get());
55 comm->mbox_cpy = comm->mbox;
60 XBT_DEBUG("Sorry, communication synchro %p does not match our needs:"
61 " its type is %d but we are looking for a comm of type %d (or maybe the filtering didn't match)",
62 comm.get(), (int)comm->type, (int)type);
64 XBT_DEBUG("No matching communication synchro found");
68 /******************************************************************************/
69 /* Communication synchros */
70 /******************************************************************************/
71 XBT_PRIVATE void simcall_HANDLER_comm_send(smx_simcall_t simcall, smx_actor_t src, smx_mailbox_t mbox, double task_size,
72 double rate, void* src_buff, size_t src_buff_size,
73 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
74 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data,
77 smx_activity_t comm = simcall_HANDLER_comm_isend(simcall, src, mbox, task_size, rate,
78 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun,
80 SIMCALL_SET_MC_VALUE(simcall, 0);
81 simcall_HANDLER_comm_wait(simcall, comm, timeout);
83 XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_isend(
84 smx_simcall_t /*simcall*/, smx_actor_t src_proc, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
85 size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
86 void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
87 void (*copy_data_fun)(smx_activity_t, void*, size_t), // used to copy data if not default one
88 void* data, int detached)
90 XBT_DEBUG("send from mailbox %p", mbox);
92 /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
93 simgrid::kernel::activity::CommImplPtr this_comm =
94 simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_SEND));
96 /* Look for communication synchro matching our needs. We also provide a description of
97 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
99 * If it is not found then push our communication into the rendez-vous point */
100 simgrid::kernel::activity::CommImplPtr other_comm =
101 _find_matching_comm(&mbox->comm_queue_, SIMIX_COMM_RECEIVE, match_fun, data, this_comm, /*remove_matching*/ true);
103 if (not other_comm) {
104 other_comm = std::move(this_comm);
106 if (mbox->permanent_receiver_ != nullptr) {
107 //this mailbox is for small messages, which have to be sent right now
108 other_comm->state_ = SIMIX_READY;
109 other_comm->dst_proc = mbox->permanent_receiver_.get();
110 mbox->done_comm_queue_.push_back(other_comm);
111 XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, other_comm.get());
114 mbox->push(other_comm);
117 XBT_DEBUG("Receive already pushed");
119 other_comm->state_ = SIMIX_READY;
120 other_comm->type = SIMIX_COMM_READY;
122 src_proc->comms.push_back(other_comm);
125 other_comm->detached = true;
126 other_comm->clean_fun = clean_fun;
128 other_comm->clean_fun = nullptr;
131 /* Setup the communication synchro */
132 other_comm->src_proc = src_proc;
133 other_comm->task_size = task_size;
134 other_comm->rate = rate;
135 other_comm->src_buff = src_buff;
136 other_comm->src_buff_size = src_buff_size;
137 other_comm->src_data = data;
139 other_comm->match_fun = match_fun;
140 other_comm->copy_data_fun = copy_data_fun;
143 if (MC_is_active() || MC_record_replay_is_active()) {
144 other_comm->state_ = SIMIX_RUNNING;
145 return (detached ? nullptr : other_comm);
148 SIMIX_comm_start(other_comm);
149 return (detached ? nullptr : other_comm);
152 XBT_PRIVATE void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
153 void* dst_buff, size_t* dst_buff_size,
154 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
155 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data,
156 double timeout, double rate)
158 smx_activity_t comm = SIMIX_comm_irecv(receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun, data, rate);
159 SIMCALL_SET_MC_VALUE(simcall, 0);
160 simcall_HANDLER_comm_wait(simcall, comm, timeout);
163 XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_irecv(smx_simcall_t /*simcall*/, smx_actor_t receiver,
164 smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
165 simix_match_func_t match_fun,
166 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data,
169 return SIMIX_comm_irecv(receiver, mbox, dst_buff, dst_buff_size, match_fun, copy_data_fun, data, rate);
173 SIMIX_comm_irecv(smx_actor_t dst_proc, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
174 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
175 void (*copy_data_fun)(smx_activity_t, void*, size_t), // used to copy data if not default one
176 void* data, double rate)
178 simgrid::kernel::activity::CommImplPtr this_synchro =
179 simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_RECEIVE));
180 XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
182 simgrid::kernel::activity::CommImplPtr other_comm;
183 //communication already done, get it inside the list of completed comms
184 if (mbox->permanent_receiver_ != nullptr && not mbox->done_comm_queue_.empty()) {
186 XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
187 //find a match in the list of already received comms
188 other_comm = _find_matching_comm(&mbox->done_comm_queue_, SIMIX_COMM_SEND, match_fun, data, this_synchro,
189 /*remove_matching*/ true);
190 //if not found, assume the receiver came first, register it to the mailbox in the classical way
191 if (not other_comm) {
192 XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request into list");
193 other_comm = std::move(this_synchro);
194 mbox->push(other_comm);
196 if (other_comm->surfAction_ && other_comm->remains() < 1e-12) {
197 XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
198 other_comm->state_ = SIMIX_DONE;
199 other_comm->type = SIMIX_COMM_DONE;
200 other_comm->mbox = nullptr;
204 /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
206 /* Look for communication activity matching our needs. We also provide a description of
207 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
209 * If it is not found then push our communication into the rendez-vous point */
210 other_comm = _find_matching_comm(&mbox->comm_queue_, SIMIX_COMM_SEND, match_fun, data, this_synchro,
211 /*remove_matching*/ true);
213 if (other_comm == nullptr) {
214 XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->comm_queue_.size());
215 other_comm = std::move(this_synchro);
216 mbox->push(other_comm);
218 XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
220 other_comm->state_ = SIMIX_READY;
221 other_comm->type = SIMIX_COMM_READY;
223 dst_proc->comms.push_back(other_comm);
226 /* Setup communication synchro */
227 other_comm->dst_proc = dst_proc;
228 other_comm->dst_buff = dst_buff;
229 other_comm->dst_buff_size = dst_buff_size;
230 other_comm->dst_data = data;
232 if (rate > -1.0 && (other_comm->rate < 0.0 || rate < other_comm->rate))
233 other_comm->rate = rate;
235 other_comm->match_fun = match_fun;
236 other_comm->copy_data_fun = copy_data_fun;
238 if (MC_is_active() || MC_record_replay_is_active()) {
239 other_comm->state_ = SIMIX_RUNNING;
243 SIMIX_comm_start(other_comm);
247 smx_activity_t simcall_HANDLER_comm_iprobe(smx_simcall_t simcall, smx_mailbox_t mbox, int type,
248 simix_match_func_t match_fun, void* data)
250 return SIMIX_comm_iprobe(simcall->issuer, mbox, type, match_fun, data);
253 smx_activity_t SIMIX_comm_iprobe(smx_actor_t dst_proc, smx_mailbox_t mbox, int type, simix_match_func_t match_fun,
256 XBT_DEBUG("iprobe from %p %p", mbox, &mbox->comm_queue_);
257 simgrid::kernel::activity::CommImplPtr this_comm;
260 this_comm = simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_SEND));
261 smx_type = SIMIX_COMM_RECEIVE;
263 this_comm = simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_RECEIVE));
264 smx_type = SIMIX_COMM_SEND;
266 smx_activity_t other_synchro=nullptr;
267 if (mbox->permanent_receiver_ != nullptr && not mbox->done_comm_queue_.empty()) {
268 XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
269 other_synchro = _find_matching_comm(&mbox->done_comm_queue_, (e_smx_comm_type_t)smx_type, match_fun, data,
270 this_comm, /*remove_matching*/ false);
272 if (not other_synchro) {
273 XBT_DEBUG("check if we have more luck in the normal mailbox");
274 other_synchro = _find_matching_comm(&mbox->comm_queue_, (e_smx_comm_type_t)smx_type, match_fun, data, this_comm,
275 /*remove_matching*/ false);
278 return other_synchro;
281 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, smx_activity_t synchro, double timeout)
283 /* Associate this simcall to the wait synchro */
284 XBT_DEBUG("simcall_HANDLER_comm_wait, %p", synchro.get());
286 synchro->simcalls_.push_back(simcall);
287 simcall->issuer->waiting_synchro = synchro;
289 if (MC_is_active() || MC_record_replay_is_active()) {
290 int idx = SIMCALL_GET_MC_VALUE(simcall);
292 synchro->state_ = SIMIX_DONE;
294 /* If we reached this point, the wait simcall must have a timeout */
295 /* Otherwise it shouldn't be enabled and executed by the MC */
299 simgrid::kernel::activity::CommImplPtr comm =
300 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
301 if (comm->src_proc == simcall->issuer)
302 comm->state_ = SIMIX_SRC_TIMEOUT;
304 comm->state_ = SIMIX_DST_TIMEOUT;
307 SIMIX_comm_finish(synchro);
311 /* If the synchro has already finish perform the error handling, */
312 /* otherwise set up a waiting timeout on the right side */
313 if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
314 SIMIX_comm_finish(synchro);
315 } else { /* we need a sleep action (even when there is no timeout) to be notified of host failures */
316 simgrid::kernel::resource::Action* sleep = simcall->issuer->host_->pimpl_cpu->sleep(timeout);
317 sleep->set_data(synchro.get());
319 simgrid::kernel::activity::CommImplPtr comm =
320 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
321 if (simcall->issuer == comm->src_proc)
322 comm->src_timeout = sleep;
324 comm->dst_timeout = sleep;
328 void simcall_HANDLER_comm_test(smx_simcall_t simcall, smx_activity_t synchro)
330 simgrid::kernel::activity::CommImplPtr comm =
331 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
335 if (MC_is_active() || MC_record_replay_is_active()){
336 res = comm->src_proc && comm->dst_proc;
338 synchro->state_ = SIMIX_DONE;
340 res = synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING;
343 simcall_comm_test__set__result(simcall, res);
344 if (simcall_comm_test__get__result(simcall)) {
345 synchro->simcalls_.push_back(simcall);
346 SIMIX_comm_finish(synchro);
348 SIMIX_simcall_answer(simcall);
352 void simcall_HANDLER_comm_testany(smx_simcall_t simcall, simgrid::kernel::activity::ActivityImplPtr comms[],
355 // The default result is -1 -- this means, "nothing is ready".
356 // It can be changed below, but only if something matches.
357 simcall_comm_testany__set__result(simcall, -1);
359 if (MC_is_active() || MC_record_replay_is_active()){
360 int idx = SIMCALL_GET_MC_VALUE(simcall);
362 SIMIX_simcall_answer(simcall);
364 simgrid::kernel::activity::ActivityImplPtr synchro = comms[idx];
365 simcall_comm_testany__set__result(simcall, idx);
366 synchro->simcalls_.push_back(simcall);
367 synchro->state_ = SIMIX_DONE;
368 SIMIX_comm_finish(synchro);
373 for (std::size_t i = 0; i != count; ++i) {
374 simgrid::kernel::activity::ActivityImplPtr synchro = comms[i];
375 if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
376 simcall_comm_testany__set__result(simcall, i);
377 synchro->simcalls_.push_back(simcall);
378 SIMIX_comm_finish(synchro);
382 SIMIX_simcall_answer(simcall);
385 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, xbt_dynar_t synchros, double timeout)
387 if (MC_is_active() || MC_record_replay_is_active()){
389 xbt_die("Timeout not implemented for waitany in the model-checker");
390 int idx = SIMCALL_GET_MC_VALUE(simcall);
391 smx_activity_t synchro = xbt_dynar_get_as(synchros, idx, smx_activity_t);
392 synchro->simcalls_.push_back(simcall);
393 simcall_comm_waitany__set__result(simcall, idx);
394 synchro->state_ = SIMIX_DONE;
395 SIMIX_comm_finish(synchro);
400 simcall->timer = NULL;
402 simcall->timer = SIMIX_timer_set(SIMIX_get_clock() + timeout, [simcall]() {
403 SIMIX_waitany_remove_simcall_from_actions(simcall);
404 simcall_comm_waitany__set__result(simcall, -1);
405 SIMIX_simcall_answer(simcall);
410 simgrid::kernel::activity::ActivityImpl* ptr;
411 xbt_dynar_foreach(synchros, cursor, ptr){
412 smx_activity_t synchro = simgrid::kernel::activity::ActivityImplPtr(ptr);
413 /* associate this simcall to the the synchro */
414 synchro->simcalls_.push_back(simcall);
416 /* see if the synchro is already finished */
417 if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
418 SIMIX_comm_finish(synchro);
424 void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall)
426 unsigned int cursor = 0;
427 xbt_dynar_t synchros = simcall_comm_waitany__get__comms(simcall);
429 simgrid::kernel::activity::ActivityImpl* ptr;
430 xbt_dynar_foreach(synchros, cursor, ptr){
431 smx_activity_t synchro = simgrid::kernel::activity::ActivityImplPtr(ptr);
433 // Remove the first occurence of simcall:
434 auto i = boost::range::find(synchro->simcalls_, simcall);
435 if (i != synchro->simcalls_.end())
436 synchro->simcalls_.erase(i);
441 * @brief Starts the simulation of a communication synchro.
442 * @param comm the communication that will be started
444 static inline void SIMIX_comm_start(simgrid::kernel::activity::CommImplPtr comm)
446 /* If both the sender and the receiver are already there, start the communication */
447 if (comm->state_ == SIMIX_READY) {
449 simgrid::s4u::Host* sender = comm->src_proc->host_;
450 simgrid::s4u::Host* receiver = comm->dst_proc->host_;
452 comm->surfAction_ = surf_network_model->communicate(sender, receiver, comm->task_size, comm->rate);
453 comm->surfAction_->set_data(comm.get());
454 comm->state_ = SIMIX_RUNNING;
456 XBT_DEBUG("Starting communication %p from '%s' to '%s' (surf_action: %p)", comm.get(), sender->get_cname(),
457 receiver->get_cname(), comm->surfAction_);
459 /* If a link is failed, detect it immediately */
460 if (comm->surfAction_->get_state() == simgrid::kernel::resource::Action::State::FAILED) {
461 XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure", sender->get_cname(),
462 receiver->get_cname());
463 comm->state_ = SIMIX_LINK_FAILURE;
467 /* If any of the process is suspended, create the synchro but stop its execution,
468 it will be restarted when the sender process resume */
469 if (comm->src_proc->is_suspended() || comm->dst_proc->is_suspended()) {
470 if (comm->src_proc->is_suspended())
471 XBT_DEBUG("The communication is suspended on startup because src (%s@%s) was suspended since it initiated the "
473 comm->src_proc->get_cname(), comm->src_proc->host_->get_cname());
475 XBT_DEBUG("The communication is suspended on startup because dst (%s@%s) was suspended since it initiated the "
477 comm->dst_proc->get_cname(), comm->dst_proc->host_->get_cname());
479 comm->surfAction_->suspend();
485 * @brief Answers the SIMIX simcalls associated to a communication synchro.
486 * @param synchro a finished communication synchro
488 void SIMIX_comm_finish(smx_activity_t synchro)
490 simgrid::kernel::activity::CommImplPtr comm =
491 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
493 while (not synchro->simcalls_.empty()) {
494 smx_simcall_t simcall = synchro->simcalls_.front();
495 synchro->simcalls_.pop_front();
497 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
498 * list. Afterwards, get the position of the actual synchro in the waitany dynar and return it as the result of the
501 if (simcall->call == SIMCALL_NONE) //FIXME: maybe a better way to handle this case
502 continue; // if process handling comm is killed
503 if (simcall->call == SIMCALL_COMM_WAITANY) {
504 SIMIX_waitany_remove_simcall_from_actions(simcall);
505 if (simcall->timer) {
506 SIMIX_timer_remove(simcall->timer);
507 simcall->timer = nullptr;
509 if (not MC_is_active() && not MC_record_replay_is_active())
510 simcall_comm_waitany__set__result(simcall,
511 xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro));
514 /* If the synchro is still in a rendez-vous point then remove from it */
516 comm->mbox->remove(comm);
518 XBT_DEBUG("SIMIX_comm_finish: synchro state = %d", (int)synchro->state_);
520 /* Check out for errors */
522 if (simcall->issuer->host_->is_off()) {
523 simcall->issuer->context_->iwannadie = 1;
524 SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
526 switch (comm->state_) {
529 XBT_DEBUG("Communication %p complete!", synchro.get());
530 SIMIX_comm_copy_data(synchro);
533 case SIMIX_SRC_TIMEOUT:
534 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Communication timeouted because of sender");
537 case SIMIX_DST_TIMEOUT:
538 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Communication timeouted because of receiver");
541 case SIMIX_SRC_HOST_FAILURE:
542 if (simcall->issuer == comm->src_proc)
543 simcall->issuer->context_->iwannadie = 1;
545 SMX_EXCEPTION(simcall->issuer, network_error, 0, "Remote peer failed");
548 case SIMIX_DST_HOST_FAILURE:
549 if (simcall->issuer == comm->dst_proc)
550 simcall->issuer->context_->iwannadie = 1;
552 SMX_EXCEPTION(simcall->issuer, network_error, 0, "Remote peer failed");
555 case SIMIX_LINK_FAILURE:
556 XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
558 synchro.get(), comm->src_proc ? comm->src_proc->host_->get_cname() : nullptr,
559 comm->dst_proc ? comm->dst_proc->host_->get_cname() : nullptr, simcall->issuer->get_cname(),
560 simcall->issuer, comm->detached);
561 if (comm->src_proc == simcall->issuer) {
562 XBT_DEBUG("I'm source");
563 } else if (comm->dst_proc == simcall->issuer) {
564 XBT_DEBUG("I'm dest");
566 XBT_DEBUG("I'm neither source nor dest");
568 SMX_EXCEPTION(simcall->issuer, network_error, 0, "Link failure");
572 if (simcall->issuer == comm->dst_proc)
573 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Communication canceled by the sender");
575 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Communication canceled by the receiver");
579 xbt_die("Unexpected synchro state in SIMIX_comm_finish: %d", (int)synchro->state_);
583 /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
584 if (simcall->issuer->exception) {
585 // In order to modify the exception we have to rethrow it:
587 std::rethrow_exception(simcall->issuer->exception);
590 if (simcall->call == SIMCALL_COMM_WAITANY) {
591 e.value = xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro);
593 else if (simcall->call == SIMCALL_COMM_TESTANY) {
595 auto* comms = simcall_comm_testany__get__comms(simcall);
596 auto count = simcall_comm_testany__get__count(simcall);
597 auto element = std::find(comms, comms + count, synchro);
598 if (element == comms + count)
601 e.value = element - comms;
603 simcall->issuer->exception = std::make_exception_ptr(e);
610 if (simcall->issuer->host_->is_off()) {
611 simcall->issuer->context_->iwannadie = 1;
614 simcall->issuer->waiting_synchro = nullptr;
615 simcall->issuer->comms.remove(synchro);
617 if(simcall->issuer == comm->src_proc){
619 comm->dst_proc->comms.remove(synchro);
621 else if(simcall->issuer == comm->dst_proc){
623 comm->src_proc->comms.remove(synchro);
626 comm->dst_proc->comms.remove(synchro);
627 comm->src_proc->comms.remove(synchro);
631 SIMIX_simcall_answer(simcall);
635 /******************************************************************************/
636 /* SIMIX_comm_copy_data callbacks */
637 /******************************************************************************/
638 static void (*SIMIX_comm_copy_data_callback) (smx_activity_t, void*, size_t) = &SIMIX_comm_copy_pointer_callback;
640 void SIMIX_comm_set_copy_data_callback(void (*callback) (smx_activity_t, void*, size_t))
642 SIMIX_comm_copy_data_callback = callback;
645 void SIMIX_comm_copy_pointer_callback(smx_activity_t synchro, void* buff, size_t buff_size)
647 simgrid::kernel::activity::CommImplPtr comm =
648 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
650 xbt_assert((buff_size == sizeof(void *)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
651 *(void **) (comm->dst_buff) = buff;
654 void SIMIX_comm_copy_buffer_callback(smx_activity_t synchro, void* buff, size_t buff_size)
656 simgrid::kernel::activity::CommImplPtr comm =
657 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
659 XBT_DEBUG("Copy the data over");
660 memcpy(comm->dst_buff, buff, buff_size);
661 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
663 comm->src_buff = nullptr;
668 * @brief Copy the communication data from the sender's buffer to the receiver's one
669 * @param synchro The communication
671 void SIMIX_comm_copy_data(smx_activity_t synchro)
673 simgrid::kernel::activity::CommImplPtr comm =
674 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
676 size_t buff_size = comm->src_buff_size;
677 /* If there is no data to copy then return */
678 if (not comm->src_buff || not comm->dst_buff || comm->copied)
681 XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", comm.get(),
682 comm->src_proc ? comm->src_proc->host_->get_cname() : "a finished process", comm->src_buff,
683 comm->dst_proc ? comm->dst_proc->host_->get_cname() : "a finished process", comm->dst_buff, buff_size);
685 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
686 if (comm->dst_buff_size)
687 buff_size = std::min(buff_size, *(comm->dst_buff_size));
689 /* Update the receiver's buffer size to the copied amount */
690 if (comm->dst_buff_size)
691 *comm->dst_buff_size = buff_size;
694 if(comm->copy_data_fun)
695 comm->copy_data_fun (comm, comm->src_buff, buff_size);
697 SIMIX_comm_copy_data_callback (comm, comm->src_buff, buff_size);
700 /* Set the copied flag so we copy data only once */
701 /* (this function might be called from both communication ends) */