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;
22 void Comm::complete(Activity::State state)
24 Activity::complete(state);
30 if (state_ == State::STARTED && not detached_ &&
31 (pimpl_ == nullptr || pimpl_->state_ == kernel::activity::State::RUNNING)) {
32 XBT_INFO("Comm %p freed before its completion. Did you forget to detach it? (state: %s)", this, get_state_str());
33 if (pimpl_ != nullptr)
34 XBT_INFO("pimpl_->state: %s", pimpl_->get_state_str());
36 XBT_INFO("pimpl_ is null");
37 xbt_backtrace_display_current();
41 int Comm::wait_any_for(const std::vector<CommPtr>* comms, double timeout)
43 std::vector<kernel::activity::CommImpl*> rcomms(comms->size());
44 std::transform(begin(*comms), end(*comms), begin(rcomms),
45 [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
46 int changed_pos = simcall_comm_waitany(rcomms.data(), rcomms.size(), timeout);
47 if (changed_pos != -1)
48 comms->at(changed_pos)->complete(State::FINISHED);
52 void Comm::wait_all(const std::vector<CommPtr>* comms)
54 // TODO: this should be a simcall or something
55 // TODO: we are missing a version with timeout
56 for (CommPtr comm : *comms)
60 CommPtr Comm::set_rate(double rate)
62 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
68 CommPtr Comm::set_src_data(void* buff)
70 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
72 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
77 CommPtr Comm::set_src_data_size(size_t size)
79 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
81 src_buff_size_ = size;
85 CommPtr Comm::set_src_data(void* buff, size_t size)
87 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
90 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
92 src_buff_size_ = size;
96 CommPtr Comm::set_dst_data(void** buff)
98 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
100 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
104 void* Comm::get_dst_data()
109 size_t Comm::get_dst_data_size() const
111 return dst_buff_size_;
113 CommPtr Comm::set_dst_data(void** buff, size_t size)
115 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
118 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
120 dst_buff_size_ = size;
123 CommPtr Comm::set_payload_size(double bytes)
125 Activity::set_remaining(bytes);
129 CommPtr Comm::sendto_init(Host* from, Host* to)
131 CommPtr res(new Comm());
138 CommPtr Comm::sendto_async(Host* from, Host* to, double simulated_size_in_bytes)
140 auto res = Comm::sendto_init(from, to)->set_payload_size(simulated_size_in_bytes);
141 res->vetoable_start();
145 void Comm::sendto(Host* from, Host* to, double simulated_size_in_bytes)
147 sendto_async(from, to, simulated_size_in_bytes)->wait();
152 xbt_assert(get_state() == State::INITED || get_state() == State::STARTING,
153 "You cannot use %s() once your communication started (not implemented)", __FUNCTION__);
154 if (from_ != nullptr || to_ != nullptr) {
155 xbt_assert(from_ != nullptr && to_ != nullptr, "When either from_ or to_ is specified, both must be.");
156 xbt_assert(src_buff_ == nullptr && dst_buff_ == nullptr,
157 "Direct host-to-host communications cannot carry any data.");
158 pimpl_ = kernel::actor::simcall([this] {
159 kernel::activity::CommImplPtr res(new kernel::activity::CommImpl(this->from_, this->to_, this->get_remaining()));
164 } else if (src_buff_ != nullptr) { // Sender side
165 on_start(*this, true /* is_sender*/);
166 pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
167 clean_fun_, copy_data_function_, get_user_data(), detached_);
168 } else if (dst_buff_ != nullptr) { // Receiver side
169 xbt_assert(not detached_, "Receive cannot be detached");
170 on_start(*this, false /*is_sender*/);
171 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
172 copy_data_function_, get_user_data(), rate_);
175 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
181 state_ = State::STARTED;
185 /** @brief Block the calling actor until the communication is finished */
188 return this->wait_for(-1);
191 /** @brief Block the calling actor until the communication is finished, or until timeout
193 * On timeout, an exception is thrown and the communication is invalidated.
195 * @param timeout the amount of seconds to wait for the comm termination.
196 * Negative values denote infinite wait times. 0 as a timeout returns immediately. */
197 Comm* Comm::wait_for(double timeout)
200 case State::FINISHED:
204 case State::STARTING: // It's not started yet. Do it in one simcall if it's a regular communication
205 if (from_ != nullptr || to_ != nullptr) {
206 return vetoable_start()->wait_for(timeout); // In the case of host2host comm, do it in two simcalls
207 } else if (src_buff_ != nullptr) {
208 on_start(*this, true /*is_sender*/);
209 simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
210 copy_data_function_, get_user_data(), timeout);
213 on_start(*this, false /*is_sender*/);
214 simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
215 get_user_data(), timeout, rate_);
220 simcall_comm_wait(get_impl(), timeout);
223 case State::CANCELED:
224 throw CancelException(XBT_THROW_POINT, "Communication canceled");
229 complete(State::FINISHED);
233 int Comm::test_any(const std::vector<CommPtr>* comms)
235 std::vector<kernel::activity::CommImpl*> rcomms(comms->size());
236 std::transform(begin(*comms), end(*comms), begin(rcomms),
237 [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
238 int changed_pos = simcall_comm_testany(rcomms.data(), rcomms.size());
239 if (changed_pos != -1)
240 comms->at(changed_pos)->complete(State::FINISHED);
246 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication is %s (not implemented)",
247 __FUNCTION__, get_state_str());
248 xbt_assert(dst_buff_ == nullptr && dst_buff_size_ == 0, "You can only detach sends, not recvs");
254 bool Comm::test() // TODO: merge with Activity::test, once modernized
256 xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
257 state_ == State::CANCELED || state_ == State::FINISHED);
259 if (state_ == State::CANCELED || state_ == State::FINISHED)
262 if (state_ == State::INITED || state_ == State::STARTING)
263 this->vetoable_start();
265 if (simcall_comm_test(get_impl())) {
266 complete(State::FINISHED);
272 Mailbox* Comm::get_mailbox() const
277 Actor* Comm::get_sender() const
279 kernel::actor::ActorImplPtr sender = nullptr;
281 sender = boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->src_actor_;
282 return sender ? sender->get_ciface() : nullptr;
286 } // namespace simgrid
287 /* **************************** Public C interface *************************** */
288 void sg_comm_detach(sg_comm_t comm, void (*clean_function)(void*))
290 comm->detach(clean_function);
293 void sg_comm_unref(sg_comm_t comm)
297 int sg_comm_test(sg_comm_t comm)
299 bool finished = comm->test();
305 sg_error_t sg_comm_wait(sg_comm_t comm)
307 sg_error_t status = SG_OK;
309 simgrid::s4u::CommPtr s4u_comm(comm, false);
311 s4u_comm->wait_for(-1);
312 } catch (const simgrid::TimeoutException&) {
313 status = SG_ERROR_TIMEOUT;
314 } catch (const simgrid::CancelException&) {
315 status = SG_ERROR_CANCELED;
316 } catch (const simgrid::NetworkFailureException&) {
317 status = SG_ERROR_NETWORK;
322 sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout)
324 sg_error_t status = SG_OK;
326 simgrid::s4u::CommPtr s4u_comm(comm, false);
328 s4u_comm->wait_for(timeout);
329 } catch (const simgrid::TimeoutException&) {
330 status = SG_ERROR_TIMEOUT;
331 } catch (const simgrid::CancelException&) {
332 status = SG_ERROR_CANCELED;
333 } catch (const simgrid::NetworkFailureException&) {
334 status = SG_ERROR_NETWORK;
339 void sg_comm_wait_all(sg_comm_t* comms, size_t count)
341 std::vector<simgrid::s4u::CommPtr> s4u_comms;
342 for (unsigned int i = 0; i < count; i++)
343 s4u_comms.emplace_back(comms[i], false);
345 simgrid::s4u::Comm::wait_all(&s4u_comms);
348 int sg_comm_wait_any(sg_comm_t* comms, size_t count)
350 return sg_comm_wait_any_for(comms, count, -1);
353 int sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout)
355 std::vector<simgrid::s4u::CommPtr> s4u_comms;
356 for (unsigned int i = 0; i < count; i++)
357 s4u_comms.emplace_back(comms[i], false);
359 int pos = simgrid::s4u::Comm::wait_any_for(&s4u_comms, timeout);
360 for (unsigned i = 0; i < count; i++) {
361 if (pos != -1 && static_cast<unsigned>(pos) != i)
362 s4u_comms[i]->add_ref();