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;
29 if (state_ == State::STARTED && not detached_ &&
30 (pimpl_ == nullptr || pimpl_->get_state() == kernel::activity::State::RUNNING)) {
31 XBT_INFO("Comm %p freed before its completion. Did you forget to detach it? (state: %s)", this, get_state_str());
32 if (pimpl_ != nullptr)
33 XBT_INFO("pimpl_->state: %s", pimpl_->get_state_str());
35 XBT_INFO("pimpl_ is null");
36 xbt_backtrace_display_current();
40 ssize_t Comm::wait_any_for(const std::vector<CommPtr>& comms, double timeout)
42 std::vector<ActivityPtr> activities;
43 for (const auto& comm : comms)
44 activities.push_back(boost::dynamic_pointer_cast<Activity>(comm));
47 changed_pos = Activity::wait_any_for(activities, timeout);
48 } catch (const NetworkFailureException& e) {
49 for (auto c : comms) {
50 if (c->pimpl_->get_state() == kernel::activity::State::FAILED) {
51 c->complete(State::FAILED);
54 e.rethrow_nested(XBT_THROW_POINT, boost::core::demangle(typeid(e).name()) + " raised in kernel mode.");
59 void Comm::wait_all(const std::vector<CommPtr>& comms)
61 // TODO: this should be a simcall or something
62 for (auto& comm : comms)
66 size_t Comm::wait_all_for(const std::vector<CommPtr>& comms, double timeout)
73 double deadline = Engine::get_clock() + timeout;
74 std::vector<CommPtr> waited_comm(1, nullptr);
75 for (size_t i = 0; i < comms.size(); i++) {
76 double wait_timeout = std::max(0.0, deadline - Engine::get_clock());
77 waited_comm[0] = comms[i];
78 // Using wait_any_for() here (and not wait_for) because we don't want comms to be invalidated on timeout
79 if (wait_any_for(waited_comm, wait_timeout) == -1) {
80 XBT_DEBUG("Timeout (%g): i = %zu", wait_timeout, i);
87 CommPtr Comm::set_source(Host* from)
89 xbt_assert(state_ == State::INITED || state_ == State::STARTING,
90 "Cannot change the source of a Comm once it's started (state: %s)", to_c_str(state_));
92 // Setting 'from_' may allow to start the activity, let's try
98 CommPtr Comm::set_destination(Host* to)
100 xbt_assert(state_ == State::INITED || state_ == State::STARTING,
101 "Cannot change the destination of a Comm once it's started (state: %s)", to_c_str(state_));
103 // Setting 'to_' may allow to start the activity, let's try
109 CommPtr Comm::set_rate(double rate)
111 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
117 CommPtr Comm::set_src_data(void* buff)
119 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
121 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
126 CommPtr Comm::set_src_data_size(size_t size)
128 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
130 src_buff_size_ = size;
134 CommPtr Comm::set_src_data(void* buff, size_t size)
136 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
139 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
141 src_buff_size_ = size;
145 CommPtr Comm::set_dst_data(void** buff)
147 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
149 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
153 void* Comm::get_dst_data()
158 size_t Comm::get_dst_data_size() const
160 return dst_buff_size_;
162 CommPtr Comm::set_dst_data(void** buff, size_t size)
164 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
167 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
169 dst_buff_size_ = size;
172 CommPtr Comm::set_payload_size(uint64_t bytes)
174 Activity::set_remaining(bytes);
178 CommPtr Comm::sendto_init()
180 CommPtr res(new Comm());
181 res->sender_ = kernel::actor::ActorImpl::self();
185 CommPtr Comm::sendto_init(Host* from, Host* to)
187 auto res = Comm::sendto_init();
194 CommPtr Comm::sendto_async(Host* from, Host* to, uint64_t simulated_size_in_bytes)
196 auto res = Comm::sendto_init(from, to)->set_payload_size(simulated_size_in_bytes);
197 res->vetoable_start();
201 void Comm::sendto(Host* from, Host* to, uint64_t simulated_size_in_bytes)
203 sendto_async(from, to, simulated_size_in_bytes)->wait();
208 xbt_assert(get_state() == State::INITED || get_state() == State::STARTING,
209 "You cannot use %s() once your communication started (not implemented)", __FUNCTION__);
210 if (from_ != nullptr || to_ != nullptr) {
211 xbt_assert(from_ != nullptr && to_ != nullptr, "When either from_ or to_ is specified, both must be.");
212 xbt_assert(src_buff_ == nullptr && dst_buff_ == nullptr,
213 "Direct host-to-host communications cannot carry any data.");
214 pimpl_ = kernel::actor::simcall([this] {
215 kernel::activity::CommImplPtr res(new kernel::activity::CommImpl(this->from_, this->to_, this->get_remaining()));
220 } else if (src_buff_ != nullptr) { // Sender side
222 pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
223 clean_fun_, copy_data_function_, get_data<void>(), detached_);
224 } else if (dst_buff_ != nullptr) { // Receiver side
225 xbt_assert(not detached_, "Receive cannot be detached");
227 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
228 copy_data_function_, get_data<void>(), rate_);
230 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
237 pimpl_->set_iface(this);
238 pimpl_->set_actor(sender_);
241 state_ = State::STARTED;
245 /** @brief Block the calling actor until the communication is finished, or until timeout
247 * On timeout, an exception is thrown and the communication is invalidated.
249 * @param timeout the amount of seconds to wait for the comm termination.
250 * Negative values denote infinite wait times. 0 as a timeout returns immediately. */
251 Comm* Comm::wait_for(double timeout)
253 XBT_DEBUG("Calling Comm::wait_for with state %s", get_state_str());
254 kernel::actor::ActorImpl* issuer = nullptr;
256 case State::FINISHED:
259 throw NetworkFailureException(XBT_THROW_POINT, "Cannot wait for a failed communication");
261 case State::STARTING: // It's not started yet. Do it in one simcall if it's a regular communication
262 if (from_ != nullptr || to_ != nullptr) {
263 return vetoable_start()->wait_for(timeout); // In the case of host2host comm, do it in two simcalls
264 } else if (src_buff_ != nullptr) {
266 simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
267 copy_data_function_, get_data<void>(), timeout);
271 simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
272 get_data<void>(), timeout, rate_);
277 issuer = kernel::actor::ActorImpl::self();
278 kernel::actor::ActivityWaitSimcall observer{issuer, pimpl_.get(), timeout};
279 if (kernel::actor::simcall_blocking(
280 [&observer] { observer.get_activity()->wait_for(observer.get_issuer(), observer.get_timeout()); },
282 throw TimeoutException(XBT_THROW_POINT, "Timeouted");
284 } catch (const NetworkFailureException& e) {
285 issuer->simcall_.observer_ = nullptr; // Comm failed on network failure, reset the observer to nullptr
286 complete(State::FAILED);
287 e.rethrow_nested(XBT_THROW_POINT, boost::core::demangle(typeid(e).name()) + " raised in kernel mode.");
291 case State::CANCELED:
292 throw CancelException(XBT_THROW_POINT, "Communication canceled");
297 complete(State::FINISHED);
301 ssize_t Comm::test_any(const std::vector<CommPtr>& comms)
303 std::vector<ActivityPtr> activities;
304 for (const auto& comm : comms)
305 activities.push_back(boost::dynamic_pointer_cast<Activity>(comm));
306 return Activity::test_any(activities);
311 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication is %s (not implemented)",
312 __FUNCTION__, get_state_str());
313 xbt_assert(dst_buff_ == nullptr && dst_buff_size_ == 0, "You can only detach sends, not recvs");
319 Mailbox* Comm::get_mailbox() const
324 Actor* Comm::get_sender() const
326 kernel::actor::ActorImplPtr sender = nullptr;
328 sender = boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->src_actor_;
329 return sender ? sender->get_ciface() : nullptr;
332 CommPtr Comm::set_copy_data_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t))
334 copy_data_function_ = callback;
337 void Comm::copy_buffer_callback(kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
339 XBT_DEBUG("Copy the data over");
340 memcpy(comm->dst_buff_, buff, buff_size);
341 if (comm->detached()) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the
342 // original buffer available to the application ASAP
344 comm->src_buff_ = nullptr;
348 void Comm::copy_pointer_callback(kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
350 xbt_assert((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
351 *(void**)(comm->dst_buff_) = buff;
355 } // namespace simgrid
356 /* **************************** Public C interface *************************** */
357 void sg_comm_detach(sg_comm_t comm, void (*clean_function)(void*))
359 comm->detach(clean_function);
362 void sg_comm_unref(sg_comm_t comm)
366 int sg_comm_test(sg_comm_t comm)
368 bool finished = comm->test();
374 sg_error_t sg_comm_wait(sg_comm_t comm)
376 return sg_comm_wait_for(comm, -1);
379 sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout)
381 sg_error_t status = SG_OK;
383 simgrid::s4u::CommPtr s4u_comm(comm, false);
385 s4u_comm->wait_for(timeout);
386 } catch (const simgrid::TimeoutException&) {
387 status = SG_ERROR_TIMEOUT;
388 } catch (const simgrid::CancelException&) {
389 status = SG_ERROR_CANCELED;
390 } catch (const simgrid::NetworkFailureException&) {
391 status = SG_ERROR_NETWORK;
396 void sg_comm_wait_all(sg_comm_t* comms, size_t count)
398 sg_comm_wait_all_for(comms, count, -1);
401 size_t sg_comm_wait_all_for(sg_comm_t* comms, size_t count, double timeout)
403 std::vector<simgrid::s4u::CommPtr> s4u_comms;
404 for (size_t i = 0; i < count; i++)
405 s4u_comms.emplace_back(comms[i], false);
407 size_t pos = simgrid::s4u::Comm::wait_all_for(s4u_comms, timeout);
408 for (size_t i = pos; i < count; i++)
409 s4u_comms[i]->add_ref();
413 ssize_t sg_comm_wait_any(sg_comm_t* comms, size_t count)
415 return sg_comm_wait_any_for(comms, count, -1);
418 ssize_t sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout)
420 std::vector<simgrid::s4u::CommPtr> s4u_comms;
421 for (size_t i = 0; i < count; i++)
422 s4u_comms.emplace_back(comms[i], false);
424 ssize_t pos = simgrid::s4u::Comm::wait_any_for(s4u_comms, timeout);
425 for (size_t i = 0; i < count; i++) {
426 if (pos != -1 && static_cast<size_t>(pos) != i)
427 s4u_comms[i]->add_ref();