1 /* Copyright (c) 2006-2022. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 //#include "src/msg/msg_private.hpp"
9 #include <simgrid/Exception.hpp>
10 #include <simgrid/comm.h>
11 #include <simgrid/s4u/Comm.hpp>
12 #include <simgrid/s4u/Engine.hpp>
13 #include <simgrid/s4u/Mailbox.hpp>
15 #include "src/kernel/activity/CommImpl.hpp"
16 #include "src/kernel/actor/ActorImpl.hpp"
17 #include "src/kernel/actor/SimcallObserver.hpp"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm, s4u_activity, "S4U asynchronous communications");
23 xbt::signal<void(Comm const&)> Comm::on_send;
24 xbt::signal<void(Comm const&)> Comm::on_recv;
25 xbt::signal<void(Comm const&)> Comm::on_completion;
27 CommPtr Comm::set_copy_data_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t))
29 copy_data_function_ = callback;
33 void Comm::copy_buffer_callback(kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
35 XBT_DEBUG("Copy the data over");
36 memcpy(comm->dst_buff_, buff, buff_size);
37 if (comm->is_detached()) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the
38 // original buffer available to the application ASAP
40 comm->src_buff_ = nullptr;
44 void Comm::copy_pointer_callback(kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
46 xbt_assert((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
47 *(void**)(comm->dst_buff_) = buff;
52 if (state_ == State::STARTED && not detached_ &&
53 (pimpl_ == nullptr || pimpl_->get_state() == kernel::activity::State::RUNNING)) {
54 XBT_INFO("Comm %p freed before its completion. Did you forget to detach it? (state: %s)", this, get_state_str());
55 if (pimpl_ != nullptr)
56 XBT_INFO("pimpl_->state: %s", pimpl_->get_state_str());
58 XBT_INFO("pimpl_ is null");
59 xbt_backtrace_display_current();
63 CommPtr Comm::sendto_init()
65 CommPtr res(new Comm());
66 res->pimpl_ = kernel::activity::CommImplPtr(new kernel::activity::CommImpl());
67 boost::static_pointer_cast<kernel::activity::CommImpl>(res->pimpl_)->detach();
68 res->sender_ = kernel::actor::ActorImpl::self();
72 CommPtr Comm::sendto_init(Host* from, Host* to)
74 auto res = Comm::sendto_init()->set_source(from)->set_destination(to);
75 res->set_state(State::STARTING);
79 CommPtr Comm::sendto_async(Host* from, Host* to, uint64_t simulated_size_in_bytes)
81 return Comm::sendto_init()->set_payload_size(simulated_size_in_bytes)->set_source(from)->set_destination(to);
84 void Comm::sendto(Host* from, Host* to, uint64_t simulated_size_in_bytes)
86 sendto_async(from, to, simulated_size_in_bytes)->wait();
89 CommPtr Comm::set_source(Host* from)
91 xbt_assert(state_ == State::INITED || state_ == State::STARTING,
92 "Cannot change the source of a Comm once it's started (state: %s)", to_c_str(state_));
93 boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->set_source(from);
94 // Setting 'source' may allow to start the activity, let's try
95 if (state_ == State::STARTING && remains_ <= 0)
96 XBT_DEBUG("This communication has a payload size of 0 byte. It cannot start yet");
102 Host* Comm::get_source() const
104 return pimpl_ ? boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->get_source() : nullptr;
107 CommPtr Comm::set_destination(Host* to)
109 xbt_assert(state_ == State::INITED || state_ == State::STARTING,
110 "Cannot change the destination of a Comm once it's started (state: %s)", to_c_str(state_));
111 boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->set_destination(to);
112 // Setting 'destination' may allow to start the activity, let's try
113 if (state_ == State::STARTING && remains_ <= 0)
114 XBT_DEBUG("This communication has a payload size of 0 byte. It cannot start yet");
121 Host* Comm::get_destination() const
123 return pimpl_ ? boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->get_destination() : nullptr;
126 CommPtr Comm::set_rate(double rate)
128 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
134 CommPtr Comm::set_mailbox(Mailbox* mailbox)
136 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
142 CommPtr Comm::set_src_data(void* buff)
144 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
146 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
151 CommPtr Comm::set_src_data_size(size_t size)
153 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
155 src_buff_size_ = size;
159 CommPtr Comm::set_src_data(void* buff, size_t size)
161 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
164 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
166 src_buff_size_ = size;
170 CommPtr Comm::set_dst_data(void** buff)
172 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
174 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
179 CommPtr Comm::set_dst_data(void** buff, size_t size)
181 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
184 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
186 dst_buff_size_ = size;
190 CommPtr Comm::set_payload_size(uint64_t bytes)
192 Activity::set_remaining(bytes);
194 boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->set_size(bytes);
199 Actor* Comm::get_sender() const
201 kernel::actor::ActorImplPtr sender = nullptr;
203 sender = boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->src_actor_;
204 return sender ? sender->get_ciface() : nullptr;
207 bool Comm::is_assigned() const
209 return (pimpl_ && boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->is_assigned()) ||
215 xbt_assert(get_state() == State::INITED || get_state() == State::STARTING,
216 "You cannot use %s() once your communication started (not implemented)", __FUNCTION__);
217 if (get_source() != nullptr || get_destination() != nullptr) {
218 xbt_assert(is_assigned(), "When either from_ or to_ is specified, both must be.");
219 xbt_assert(src_buff_ == nullptr && dst_buff_ == nullptr,
220 "Direct host-to-host communications cannot carry any data.");
221 XBT_DEBUG("host-to-host Comm. Pimpl already created and set, just start it.");
222 kernel::actor::simcall_answered([this] {
223 pimpl_->set_state(kernel::activity::State::READY);
224 boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->start();
226 } else if (src_buff_ != nullptr) { // Sender side
228 kernel::actor::CommIsendSimcall observer{sender_,
229 mailbox_->get_impl(),
232 static_cast<unsigned char*>(src_buff_),
239 pimpl_ = kernel::actor::simcall_answered([&observer] { return kernel::activity::CommImpl::isend(&observer); },
241 } else if (dst_buff_ != nullptr) { // Receiver side
242 xbt_assert(not detached_, "Receive cannot be detached");
244 kernel::actor::CommIrecvSimcall observer{receiver_,
245 mailbox_->get_impl(),
246 static_cast<unsigned char*>(dst_buff_),
252 pimpl_ = kernel::actor::simcall_answered([&observer] { return kernel::activity::CommImpl::irecv(&observer); },
255 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
262 pimpl_->set_iface(this);
263 pimpl_->set_actor(sender_);
266 state_ = State::STARTED;
272 xbt_assert(state_ == State::INITED || state_ == State::STARTING,
273 "You cannot use %s() once your communication is %s (not implemented)", __FUNCTION__, get_state_str());
274 xbt_assert(dst_buff_ == nullptr && dst_buff_size_ == 0, "You can only detach sends, not recvs");
280 ssize_t Comm::test_any(const std::vector<CommPtr>& comms)
282 std::vector<ActivityPtr> activities;
283 for (const auto& comm : comms)
284 activities.push_back(boost::dynamic_pointer_cast<Activity>(comm));
285 return Activity::test_any(activities);
288 /** @brief Block the calling actor until the communication is finished, or until timeout
290 * On timeout, an exception is thrown and the communication is invalidated.
292 * @param timeout the amount of seconds to wait for the comm termination.
293 * Negative values denote infinite wait times. 0 as a timeout returns immediately. */
294 Comm* Comm::wait_for(double timeout)
296 XBT_DEBUG("Calling Comm::wait_for with state %s", get_state_str());
297 kernel::actor::ActorImpl* issuer = nullptr;
299 case State::FINISHED:
302 throw NetworkFailureException(XBT_THROW_POINT, "Cannot wait for a failed communication");
304 case State::STARTING: // It's not started yet. Do it in one simcall if it's a regular communication
305 if (get_source() != nullptr || get_destination() != nullptr) {
306 return vetoable_start()->wait_for(timeout); // In the case of host2host comm, do it in two simcalls
307 } else if (src_buff_ != nullptr) {
309 simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
310 copy_data_function_, get_data<void>(), timeout);
314 simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
315 get_data<void>(), timeout, rate_);
320 issuer = kernel::actor::ActorImpl::self();
321 kernel::actor::ActivityWaitSimcall observer{issuer, pimpl_.get(), timeout};
322 if (kernel::actor::simcall_blocking(
323 [&observer] { observer.get_activity()->wait_for(observer.get_issuer(), observer.get_timeout()); },
325 throw TimeoutException(XBT_THROW_POINT, "Timeouted");
327 } catch (const NetworkFailureException& e) {
328 issuer->simcall_.observer_ = nullptr; // Comm failed on network failure, reset the observer to nullptr
329 complete(State::FAILED);
330 e.rethrow_nested(XBT_THROW_POINT, boost::core::demangle(typeid(e).name()) + " raised in kernel mode.");
334 case State::CANCELED:
335 throw CancelException(XBT_THROW_POINT, "Communication canceled");
340 complete(State::FINISHED);
344 ssize_t Comm::wait_any_for(const std::vector<CommPtr>& comms, double timeout)
346 std::vector<ActivityPtr> activities;
347 for (const auto& comm : comms)
348 activities.push_back(boost::dynamic_pointer_cast<Activity>(comm));
351 changed_pos = Activity::wait_any_for(activities, timeout);
352 } catch (const NetworkFailureException& e) {
354 for (auto c : comms) {
355 if (c->pimpl_->get_state() == kernel::activity::State::FAILED) {
356 c->complete(State::FAILED);
359 e.rethrow_nested(XBT_THROW_POINT, boost::core::demangle(typeid(e).name()) + " raised in kernel mode.");
364 void Comm::wait_all(const std::vector<CommPtr>& comms)
366 // TODO: this should be a simcall or something
367 for (auto& comm : comms)
371 size_t Comm::wait_all_for(const std::vector<CommPtr>& comms, double timeout)
378 double deadline = Engine::get_clock() + timeout;
379 std::vector<CommPtr> waited_comm(1, nullptr);
380 for (size_t i = 0; i < comms.size(); i++) {
381 double wait_timeout = std::max(0.0, deadline - Engine::get_clock());
382 waited_comm[0] = comms[i];
383 // Using wait_any_for() here (and not wait_for) because we don't want comms to be invalidated on timeout
384 if (wait_any_for(waited_comm, wait_timeout) == -1) {
385 XBT_DEBUG("Timeout (%g): i = %zu", wait_timeout, i);
392 } // namespace simgrid
393 /* **************************** Public C interface *************************** */
394 void sg_comm_detach(sg_comm_t comm, void (*clean_function)(void*))
396 comm->detach(clean_function);
399 void sg_comm_unref(sg_comm_t comm)
403 int sg_comm_test(sg_comm_t comm)
405 bool finished = comm->test();
411 sg_error_t sg_comm_wait(sg_comm_t comm)
413 return sg_comm_wait_for(comm, -1);
416 sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout)
418 sg_error_t status = SG_OK;
420 simgrid::s4u::CommPtr s4u_comm(comm, false);
422 s4u_comm->wait_for(timeout);
423 } catch (const simgrid::TimeoutException&) {
424 status = SG_ERROR_TIMEOUT;
425 } catch (const simgrid::CancelException&) {
426 status = SG_ERROR_CANCELED;
427 } catch (const simgrid::NetworkFailureException&) {
428 status = SG_ERROR_NETWORK;
433 void sg_comm_wait_all(sg_comm_t* comms, size_t count)
435 sg_comm_wait_all_for(comms, count, -1);
438 size_t sg_comm_wait_all_for(sg_comm_t* comms, size_t count, double timeout)
440 std::vector<simgrid::s4u::CommPtr> s4u_comms;
441 for (size_t i = 0; i < count; i++)
442 s4u_comms.emplace_back(comms[i], false);
444 size_t pos = simgrid::s4u::Comm::wait_all_for(s4u_comms, timeout);
445 for (size_t i = pos; i < count; i++)
446 s4u_comms[i]->add_ref();
450 ssize_t sg_comm_wait_any(sg_comm_t* comms, size_t count)
452 return sg_comm_wait_any_for(comms, count, -1);
455 ssize_t sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout)
457 std::vector<simgrid::s4u::CommPtr> s4u_comms;
458 for (size_t i = 0; i < count; i++)
459 s4u_comms.emplace_back(comms[i], false);
461 ssize_t pos = simgrid::s4u::Comm::wait_any_for(s4u_comms, timeout);
462 for (size_t i = 0; i < count; i++) {
463 if (pos != -1 && static_cast<size_t>(pos) != i)
464 s4u_comms[i]->add_ref();