1 /* Copyright (c) 2006-2021. 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/s4u/Comm.hpp"
11 #include "simgrid/s4u/Mailbox.hpp"
13 #include <simgrid/comm.h>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm, s4u_activity, "S4U asynchronous communications");
19 xbt::signal<void(Comm const&, bool is_sender)> Comm::on_start;
20 xbt::signal<void(Comm const&)> Comm::on_completion;
24 if (state_ == State::STARTED && not detached_ &&
25 (pimpl_ == nullptr || pimpl_->state_ == kernel::activity::State::RUNNING)) {
27 XBT_INFO("Comm %p freed before its completion. Did you forget to detach it? (state: %s)", this, get_state_str());
29 XBT_INFO("Detached comm %p freed before its completion, please report that bug along with a MWE (state: %s).",
30 this, get_state_str());
31 if (pimpl_ != nullptr)
32 XBT_INFO("pimpl_->state: %s", pimpl_->get_state_str());
34 XBT_INFO("pimpl_ is null");
35 xbt_backtrace_display_current();
39 int Comm::wait_any_for(const std::vector<CommPtr>* comms, double timeout)
41 std::vector<kernel::activity::CommImpl*> rcomms(comms->size());
42 std::transform(begin(*comms), end(*comms), begin(rcomms),
43 [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
44 int changed_pos = simcall_comm_waitany(rcomms.data(), rcomms.size(), timeout);
45 if (changed_pos != -1)
46 comms->at(changed_pos)->release_dependencies();
50 void Comm::wait_all(const std::vector<CommPtr>* comms)
52 // TODO: this should be a simcall or something
53 // TODO: we are missing a version with timeout
54 for (CommPtr comm : *comms)
58 CommPtr Comm::set_rate(double rate)
60 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
66 CommPtr Comm::set_src_data(void* buff)
68 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
70 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
75 CommPtr Comm::set_src_data_size(size_t size)
77 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
79 src_buff_size_ = size;
83 CommPtr Comm::set_src_data(void* buff, size_t size)
85 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
88 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
90 src_buff_size_ = size;
94 CommPtr Comm::set_dst_data(void** buff)
96 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
98 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
102 void* Comm::get_dst_data()
107 size_t Comm::get_dst_data_size() const
109 return dst_buff_size_;
111 CommPtr Comm::set_dst_data(void** buff, size_t size)
113 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
116 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
118 dst_buff_size_ = size;
121 Comm* Comm::set_payload_size(double bytes)
123 set_remaining(bytes);
127 CommPtr Comm::sendto_init(Host* from, Host* to)
129 CommPtr res(new Comm());
135 CommPtr Comm::sendto_async(Host* from, Host* to, double simulated_size_in_bytes)
137 auto res = Comm::sendto_init(from, to);
138 res->set_remaining(simulated_size_in_bytes)->start();
144 xbt_assert(get_state() == State::INITED || get_state() == State::STARTING,
145 "You cannot use %s() once your communication started (not implemented)", __FUNCTION__);
146 if (from_ != nullptr || to_ != nullptr) {
147 xbt_assert(from_ != nullptr && to_ != nullptr, "When either from_ or to_ is specified, both must be.");
148 xbt_assert(src_buff_ == nullptr && dst_buff_ == nullptr,
149 "Direct host-to-host communications cannot carry any data.");
150 pimpl_ = kernel::actor::simcall([this] {
151 auto res = new kernel::activity::CommImpl(this->from_, this->to_, this->get_remaining());
156 } else if (src_buff_ != nullptr) { // Sender side
157 on_start(*this, true /* is_sender*/);
158 pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
159 clean_fun_, copy_data_function_, get_user_data(), detached_);
160 } else if (dst_buff_ != nullptr) { // Receiver side
161 xbt_assert(not detached_, "Receive cannot be detached");
162 on_start(*this, false /*is_sender*/);
163 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
164 copy_data_function_, get_user_data(), rate_);
167 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
173 state_ = State::STARTED;
177 /** @brief Block the calling actor until the communication is finished */
180 return this->wait_for(-1);
183 /** @brief Block the calling actor until the communication is finished, or until timeout
185 * On timeout, an exception is thrown and the communication is invalidated.
187 * @param timeout the amount of seconds to wait for the comm termination.
188 * Negative values denote infinite wait times. 0 as a timeout returns immediately. */
189 Comm* Comm::wait_for(double timeout)
192 case State::FINISHED:
196 case State::STARTING: // It's not started yet. Do it in one simcall if it's a regular communication
197 if (from_ != nullptr || to_ != nullptr) {
198 return start()->wait_for(timeout); // In the case of host2host comm, do it in two simcalls
199 } else if (src_buff_ != nullptr) {
200 on_start(*this, true /*is_sender*/);
201 simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
202 copy_data_function_, get_user_data(), timeout);
205 on_start(*this, false /*is_sender*/);
206 simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
207 get_user_data(), timeout, rate_);
209 state_ = State::FINISHED;
210 this->release_dependencies();
214 simcall_comm_wait(get_impl(), timeout);
215 state_ = State::FINISHED;
216 this->release_dependencies();
219 case State::CANCELED:
220 throw CancelException(XBT_THROW_POINT, "Communication canceled");
225 on_completion(*this);
229 int Comm::test_any(const std::vector<CommPtr>* comms)
231 std::vector<kernel::activity::CommImpl*> rcomms(comms->size());
232 std::transform(begin(*comms), end(*comms), begin(rcomms),
233 [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
234 int changed_pos = simcall_comm_testany(rcomms.data(), rcomms.size());
235 if (changed_pos != -1)
236 comms->at(changed_pos)->release_dependencies();
242 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication is %s (not implemented)",
243 __FUNCTION__, get_state_str());
244 xbt_assert(dst_buff_ == nullptr && dst_buff_size_ == 0, "You can only detach sends, not recvs");
252 kernel::actor::simcall([this] {
254 boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->cancel();
256 state_ = State::CANCELED;
262 xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
263 state_ == State::FINISHED);
265 if (state_ == State::FINISHED)
268 if (state_ == State::INITED || state_ == State::STARTING)
269 this->vetoable_start();
271 if (simcall_comm_test(get_impl())) {
272 state_ = State::FINISHED;
273 this->release_dependencies();
279 Mailbox* Comm::get_mailbox() const
284 Actor* Comm::get_sender() const
286 kernel::actor::ActorImplPtr sender = nullptr;
288 sender = boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->src_actor_;
289 return sender ? sender->get_ciface() : nullptr;
293 } // namespace simgrid
294 /* **************************** Public C interface *************************** */
295 void sg_comm_detach(sg_comm_t comm, void (*clean_function)(void*))
297 comm->detach(clean_function);
300 void sg_comm_unref(sg_comm_t comm)
304 int sg_comm_test(sg_comm_t comm)
306 bool finished = comm->test();
312 sg_error_t sg_comm_wait(sg_comm_t comm)
314 sg_error_t status = SG_OK;
316 simgrid::s4u::CommPtr s4u_comm(comm, false);
318 s4u_comm->wait_for(-1);
319 } catch (const simgrid::TimeoutException&) {
320 status = SG_ERROR_TIMEOUT;
321 } catch (const simgrid::CancelException&) {
322 status = SG_ERROR_CANCELED;
323 } catch (const simgrid::NetworkFailureException&) {
324 status = SG_ERROR_NETWORK;
329 sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout)
331 sg_error_t status = SG_OK;
333 simgrid::s4u::CommPtr s4u_comm(comm, false);
335 s4u_comm->wait_for(timeout);
336 } catch (const simgrid::TimeoutException&) {
337 status = SG_ERROR_TIMEOUT;
338 } catch (const simgrid::CancelException&) {
339 status = SG_ERROR_CANCELED;
340 } catch (const simgrid::NetworkFailureException&) {
341 status = SG_ERROR_NETWORK;
346 void sg_comm_wait_all(sg_comm_t* comms, size_t count)
348 std::vector<simgrid::s4u::CommPtr> s4u_comms;
349 for (unsigned int i = 0; i < count; i++)
350 s4u_comms.emplace_back(comms[i], false);
352 simgrid::s4u::Comm::wait_all(&s4u_comms);
355 int sg_comm_wait_any(sg_comm_t* comms, size_t count)
357 return sg_comm_wait_any_for(comms, count, -1);
360 int sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout)
362 std::vector<simgrid::s4u::CommPtr> s4u_comms;
363 for (unsigned int i = 0; i < count; i++)
364 s4u_comms.emplace_back(comms[i], false);
366 int pos = simgrid::s4u::Comm::wait_any_for(&s4u_comms, timeout);
367 for (unsigned i = 0; i < count; i++) {
368 if (pos != -1 && static_cast<unsigned>(pos) != i)
369 s4u_comms[i]->add_ref();