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_private.hpp"
11 #include "src/surf/cpu_interface.hpp"
12 #include "src/surf/network_interface.hpp"
14 #include <boost/range/algorithm.hpp>
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix, "SIMIX network-related synchronization");
18 static void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall);
20 /******************************************************************************/
21 /* Communication synchros */
22 /******************************************************************************/
23 XBT_PRIVATE void simcall_HANDLER_comm_send(smx_simcall_t simcall, smx_actor_t src, smx_mailbox_t mbox, double task_size,
24 double rate, void* src_buff, size_t src_buff_size,
25 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
26 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data,
29 smx_activity_t comm = simcall_HANDLER_comm_isend(simcall, src, mbox, task_size, rate,
30 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun,
32 SIMCALL_SET_MC_VALUE(simcall, 0);
33 simcall_HANDLER_comm_wait(simcall, comm, timeout);
36 XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_isend(
37 smx_simcall_t /*simcall*/, smx_actor_t src_proc, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
38 size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
39 void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send
40 void (*copy_data_fun)(smx_activity_t, void*, size_t), // used to copy data if not default one
41 void* data, int detached)
43 XBT_DEBUG("send from mailbox %p", mbox);
45 /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
46 simgrid::kernel::activity::CommImplPtr this_comm =
47 simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_SEND));
49 /* Look for communication synchro matching our needs. We also provide a description of
50 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
52 * If it is not found then push our communication into the rendez-vous point */
53 simgrid::kernel::activity::CommImplPtr other_comm = mbox->find_matching_comm(
54 SIMIX_COMM_RECEIVE, match_fun, data, this_comm, /*done*/ false, /*remove_matching*/ true);
57 other_comm = std::move(this_comm);
59 if (mbox->permanent_receiver_ != nullptr) {
60 //this mailbox is for small messages, which have to be sent right now
61 other_comm->state_ = SIMIX_READY;
62 other_comm->dst_actor_ = mbox->permanent_receiver_.get();
63 mbox->done_comm_queue_.push_back(other_comm);
64 XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, other_comm.get());
67 mbox->push(other_comm);
70 XBT_DEBUG("Receive already pushed");
72 other_comm->state_ = SIMIX_READY;
73 other_comm->type = SIMIX_COMM_READY;
75 src_proc->comms.push_back(other_comm);
78 other_comm->detached = true;
79 other_comm->clean_fun = clean_fun;
81 other_comm->clean_fun = nullptr;
84 /* Setup the communication synchro */
85 other_comm->src_actor_ = src_proc;
86 other_comm->task_size_ = task_size;
87 other_comm->rate_ = rate;
88 other_comm->src_buff_ = src_buff;
89 other_comm->src_buff_size_ = src_buff_size;
90 other_comm->src_data_ = data;
92 other_comm->match_fun = match_fun;
93 other_comm->copy_data_fun = copy_data_fun;
96 if (MC_is_active() || MC_record_replay_is_active()) {
97 other_comm->state_ = SIMIX_RUNNING;
98 return (detached ? nullptr : other_comm);
103 return (detached ? nullptr : other_comm);
106 XBT_PRIVATE void simcall_HANDLER_comm_recv(smx_simcall_t simcall, smx_actor_t receiver, smx_mailbox_t mbox,
107 void* dst_buff, size_t* dst_buff_size,
108 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
109 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data,
110 double timeout, double rate)
112 smx_activity_t comm = simcall_HANDLER_comm_irecv(simcall, receiver, mbox, dst_buff, dst_buff_size, match_fun,
113 copy_data_fun, data, rate);
114 SIMCALL_SET_MC_VALUE(simcall, 0);
115 simcall_HANDLER_comm_wait(simcall, comm, timeout);
118 XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_irecv(smx_simcall_t /*simcall*/, smx_actor_t receiver,
119 smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
120 simix_match_func_t match_fun,
121 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data,
124 simgrid::kernel::activity::CommImplPtr this_synchro =
125 simgrid::kernel::activity::CommImplPtr(new simgrid::kernel::activity::CommImpl(SIMIX_COMM_RECEIVE));
126 XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
128 simgrid::kernel::activity::CommImplPtr other_comm;
129 //communication already done, get it inside the list of completed comms
130 if (mbox->permanent_receiver_ != nullptr && not mbox->done_comm_queue_.empty()) {
132 XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
133 //find a match in the list of already received comms
134 other_comm = mbox->find_matching_comm(SIMIX_COMM_SEND, match_fun, data, this_synchro, /*done*/ true,
135 /*remove_matching*/ true);
136 //if not found, assume the receiver came first, register it to the mailbox in the classical way
137 if (not other_comm) {
138 XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request into list");
139 other_comm = std::move(this_synchro);
140 mbox->push(other_comm);
142 if (other_comm->surf_action_ && other_comm->remains() < 1e-12) {
143 XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
144 other_comm->state_ = SIMIX_DONE;
145 other_comm->type = SIMIX_COMM_DONE;
146 other_comm->mbox = nullptr;
150 /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
152 /* Look for communication activity matching our needs. We also provide a description of
153 * ourself so that the other side also gets a chance of choosing if it wants to match with us.
155 * If it is not found then push our communication into the rendez-vous point */
156 other_comm = mbox->find_matching_comm(SIMIX_COMM_SEND, match_fun, data, this_synchro, /*done*/ false,
157 /*remove_matching*/ true);
159 if (other_comm == nullptr) {
160 XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->comm_queue_.size());
161 other_comm = std::move(this_synchro);
162 mbox->push(other_comm);
164 XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
166 other_comm->state_ = SIMIX_READY;
167 other_comm->type = SIMIX_COMM_READY;
169 receiver->comms.push_back(other_comm);
172 /* Setup communication synchro */
173 other_comm->dst_actor_ = receiver;
174 other_comm->dst_buff_ = dst_buff;
175 other_comm->dst_buff_size_ = dst_buff_size;
176 other_comm->dst_data_ = data;
178 if (rate > -1.0 && (other_comm->rate_ < 0.0 || rate < other_comm->rate_))
179 other_comm->rate_ = rate;
181 other_comm->match_fun = match_fun;
182 other_comm->copy_data_fun = copy_data_fun;
184 if (MC_is_active() || MC_record_replay_is_active()) {
185 other_comm->state_ = SIMIX_RUNNING;
192 void simcall_HANDLER_comm_wait(smx_simcall_t simcall, smx_activity_t synchro, double timeout)
194 /* Associate this simcall to the wait synchro */
195 XBT_DEBUG("simcall_HANDLER_comm_wait, %p", synchro.get());
197 synchro->simcalls_.push_back(simcall);
198 simcall->issuer->waiting_synchro = synchro;
200 if (MC_is_active() || MC_record_replay_is_active()) {
201 int idx = SIMCALL_GET_MC_VALUE(simcall);
203 synchro->state_ = SIMIX_DONE;
205 /* If we reached this point, the wait simcall must have a timeout */
206 /* Otherwise it shouldn't be enabled and executed by the MC */
210 simgrid::kernel::activity::CommImplPtr comm =
211 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
212 if (comm->src_actor_ == simcall->issuer)
213 comm->state_ = SIMIX_SRC_TIMEOUT;
215 comm->state_ = SIMIX_DST_TIMEOUT;
218 SIMIX_comm_finish(synchro);
222 /* If the synchro has already finish perform the error handling, */
223 /* otherwise set up a waiting timeout on the right side */
224 if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
225 SIMIX_comm_finish(synchro);
226 } else { /* we need a sleep action (even when there is no timeout) to be notified of host failures */
227 simgrid::kernel::resource::Action* sleep = simcall->issuer->host_->pimpl_cpu->sleep(timeout);
228 sleep->set_data(synchro.get());
230 simgrid::kernel::activity::CommImplPtr comm =
231 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
232 if (simcall->issuer == comm->src_actor_)
233 comm->src_timeout_ = sleep;
235 comm->dst_timeout_ = sleep;
239 void simcall_HANDLER_comm_test(smx_simcall_t simcall, smx_activity_t synchro)
241 simgrid::kernel::activity::CommImplPtr comm =
242 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
246 if (MC_is_active() || MC_record_replay_is_active()){
247 res = comm->src_actor_ && comm->dst_actor_;
249 synchro->state_ = SIMIX_DONE;
251 res = synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING;
254 simcall_comm_test__set__result(simcall, res);
255 if (simcall_comm_test__get__result(simcall)) {
256 synchro->simcalls_.push_back(simcall);
257 SIMIX_comm_finish(synchro);
259 SIMIX_simcall_answer(simcall);
263 void simcall_HANDLER_comm_testany(smx_simcall_t simcall, simgrid::kernel::activity::ActivityImplPtr comms[],
266 // The default result is -1 -- this means, "nothing is ready".
267 // It can be changed below, but only if something matches.
268 simcall_comm_testany__set__result(simcall, -1);
270 if (MC_is_active() || MC_record_replay_is_active()){
271 int idx = SIMCALL_GET_MC_VALUE(simcall);
273 SIMIX_simcall_answer(simcall);
275 simgrid::kernel::activity::ActivityImplPtr synchro = comms[idx];
276 simcall_comm_testany__set__result(simcall, idx);
277 synchro->simcalls_.push_back(simcall);
278 synchro->state_ = SIMIX_DONE;
279 SIMIX_comm_finish(synchro);
284 for (std::size_t i = 0; i != count; ++i) {
285 simgrid::kernel::activity::ActivityImplPtr synchro = comms[i];
286 if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
287 simcall_comm_testany__set__result(simcall, i);
288 synchro->simcalls_.push_back(simcall);
289 SIMIX_comm_finish(synchro);
293 SIMIX_simcall_answer(simcall);
296 void simcall_HANDLER_comm_waitany(smx_simcall_t simcall, xbt_dynar_t synchros, double timeout)
298 if (MC_is_active() || MC_record_replay_is_active()){
300 xbt_die("Timeout not implemented for waitany in the model-checker");
301 int idx = SIMCALL_GET_MC_VALUE(simcall);
302 smx_activity_t synchro = xbt_dynar_get_as(synchros, idx, smx_activity_t);
303 synchro->simcalls_.push_back(simcall);
304 simcall_comm_waitany__set__result(simcall, idx);
305 synchro->state_ = SIMIX_DONE;
306 SIMIX_comm_finish(synchro);
311 simcall->timer = NULL;
313 simcall->timer = SIMIX_timer_set(SIMIX_get_clock() + timeout, [simcall]() {
314 SIMIX_waitany_remove_simcall_from_actions(simcall);
315 simcall_comm_waitany__set__result(simcall, -1);
316 SIMIX_simcall_answer(simcall);
321 simgrid::kernel::activity::ActivityImpl* ptr;
322 xbt_dynar_foreach(synchros, cursor, ptr){
323 smx_activity_t synchro = simgrid::kernel::activity::ActivityImplPtr(ptr);
324 /* associate this simcall to the the synchro */
325 synchro->simcalls_.push_back(simcall);
327 /* see if the synchro is already finished */
328 if (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING) {
329 SIMIX_comm_finish(synchro);
335 void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall)
337 unsigned int cursor = 0;
338 xbt_dynar_t synchros = simcall_comm_waitany__get__comms(simcall);
340 simgrid::kernel::activity::ActivityImpl* ptr;
341 xbt_dynar_foreach(synchros, cursor, ptr){
342 smx_activity_t synchro = simgrid::kernel::activity::ActivityImplPtr(ptr);
344 // Remove the first occurence of simcall:
345 auto i = boost::range::find(synchro->simcalls_, simcall);
346 if (i != synchro->simcalls_.end())
347 synchro->simcalls_.erase(i);
352 * @brief Answers the SIMIX simcalls associated to a communication synchro.
353 * @param synchro a finished communication synchro
355 void SIMIX_comm_finish(smx_activity_t synchro)
357 simgrid::kernel::activity::CommImplPtr comm =
358 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
360 while (not synchro->simcalls_.empty()) {
361 smx_simcall_t simcall = synchro->simcalls_.front();
362 synchro->simcalls_.pop_front();
364 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
365 * list. Afterwards, get the position of the actual synchro in the waitany dynar and return it as the result of the
368 if (simcall->call == SIMCALL_NONE) //FIXME: maybe a better way to handle this case
369 continue; // if process handling comm is killed
370 if (simcall->call == SIMCALL_COMM_WAITANY) {
371 SIMIX_waitany_remove_simcall_from_actions(simcall);
372 if (simcall->timer) {
373 SIMIX_timer_remove(simcall->timer);
374 simcall->timer = nullptr;
376 if (not MC_is_active() && not MC_record_replay_is_active())
377 simcall_comm_waitany__set__result(simcall,
378 xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro));
381 /* If the synchro is still in a rendez-vous point then remove from it */
383 comm->mbox->remove(comm);
385 XBT_DEBUG("SIMIX_comm_finish: synchro state = %d", (int)synchro->state_);
387 /* Check out for errors */
389 if (not simcall->issuer->host_->is_on()) {
390 simcall->issuer->context_->iwannadie = true;
391 simcall->issuer->exception =
392 std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
394 switch (comm->state_) {
397 XBT_DEBUG("Communication %p complete!", synchro.get());
401 case SIMIX_SRC_TIMEOUT:
402 simcall->issuer->exception = std::make_exception_ptr(
403 simgrid::TimeoutError(XBT_THROW_POINT, "Communication timeouted because of the sender"));
406 case SIMIX_DST_TIMEOUT:
407 simcall->issuer->exception = std::make_exception_ptr(
408 simgrid::TimeoutError(XBT_THROW_POINT, "Communication timeouted because of the receiver"));
411 case SIMIX_SRC_HOST_FAILURE:
412 if (simcall->issuer == comm->src_actor_)
413 simcall->issuer->context_->iwannadie = true;
415 simcall->issuer->exception =
416 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
419 case SIMIX_DST_HOST_FAILURE:
420 if (simcall->issuer == comm->dst_actor_)
421 simcall->issuer->context_->iwannadie = true;
423 simcall->issuer->exception =
424 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Remote peer failed"));
427 case SIMIX_LINK_FAILURE:
428 XBT_DEBUG("Link failure in synchro %p between '%s' and '%s': posting an exception to the issuer: %s (%p) "
430 synchro.get(), comm->src_actor_ ? comm->src_actor_->host_->get_cname() : nullptr,
431 comm->dst_actor_ ? comm->dst_actor_->host_->get_cname() : nullptr, simcall->issuer->get_cname(),
432 simcall->issuer, comm->detached);
433 if (comm->src_actor_ == simcall->issuer) {
434 XBT_DEBUG("I'm source");
435 } else if (comm->dst_actor_ == simcall->issuer) {
436 XBT_DEBUG("I'm dest");
438 XBT_DEBUG("I'm neither source nor dest");
440 simcall->issuer->throw_exception(
441 std::make_exception_ptr(simgrid::NetworkFailureException(XBT_THROW_POINT, "Link failure")));
445 if (simcall->issuer == comm->dst_actor_)
446 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Communication canceled by the sender");
448 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Communication canceled by the receiver");
452 xbt_die("Unexpected synchro state in SIMIX_comm_finish: %d", (int)synchro->state_);
456 /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
457 if (simcall->issuer->exception &&
458 (simcall->call == SIMCALL_COMM_WAITANY || simcall->call == SIMCALL_COMM_TESTANY)) {
459 // First retrieve the rank of our failing synchro
461 if (simcall->call == SIMCALL_COMM_WAITANY) {
462 rank = xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro);
463 } else if (simcall->call == SIMCALL_COMM_TESTANY) {
465 auto* comms = simcall_comm_testany__get__comms(simcall);
466 auto count = simcall_comm_testany__get__count(simcall);
467 auto element = std::find(comms, comms + count, synchro);
468 if (element == comms + count)
471 rank = element - comms;
474 // In order to modify the exception we have to rethrow it:
476 std::rethrow_exception(simcall->issuer->exception);
477 } catch (simgrid::TimeoutError& e) {
479 simcall->issuer->exception = std::make_exception_ptr(e);
480 } catch (simgrid::NetworkFailureException& e) {
482 simcall->issuer->exception = std::make_exception_ptr(e);
483 } catch (xbt_ex& e) {
484 if (e.category == cancel_error) {
486 simcall->issuer->exception = std::make_exception_ptr(e);
488 xbt_die("Unexpected xbt_ex(%s). Please enhance this code", xbt_ex_catname(e.category));
493 simcall->issuer->waiting_synchro = nullptr;
494 simcall->issuer->comms.remove(synchro);
496 if (simcall->issuer == comm->src_actor_) {
497 if (comm->dst_actor_)
498 comm->dst_actor_->comms.remove(synchro);
499 } else if (simcall->issuer == comm->dst_actor_) {
500 if (comm->src_actor_)
501 comm->src_actor_->comms.remove(synchro);
504 comm->dst_actor_->comms.remove(synchro);
505 comm->src_actor_->comms.remove(synchro);
509 if (simcall->issuer->host_->is_on())
510 SIMIX_simcall_answer(simcall);
512 simcall->issuer->context_->iwannadie = true;
516 void SIMIX_comm_copy_buffer_callback(smx_activity_t synchro, void* buff, size_t buff_size)
518 simgrid::kernel::activity::CommImplPtr comm =
519 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
521 XBT_DEBUG("Copy the data over");
522 memcpy(comm->dst_buff_, buff, buff_size);
523 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
525 comm->src_buff_ = nullptr;