1 /* Copyright (c) 2006-2020. 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)) {
26 XBT_INFO("Comm %p freed before its completion. Detached: %d, State: %d", this, detached_, (int)state_);
27 if (pimpl_ != nullptr)
28 XBT_INFO("pimpl_->state: %d", static_cast<int>(pimpl_->state_));
30 XBT_INFO("pimpl_ is null");
31 xbt_backtrace_display_current();
35 int Comm::wait_any_for(const std::vector<CommPtr>* comms, double timeout)
37 std::vector<kernel::activity::CommImpl*> rcomms(comms->size());
38 std::transform(begin(*comms), end(*comms), begin(rcomms),
39 [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
40 int changed_pos = simcall_comm_waitany(rcomms.data(), rcomms.size(), timeout);
41 if (changed_pos != -1)
42 comms->at(changed_pos)->release_dependencies();
46 void Comm::wait_all(const std::vector<CommPtr>* comms)
48 // TODO: this should be a simcall or something
49 // TODO: we are missing a version with timeout
50 for (CommPtr comm : *comms)
54 CommPtr Comm::set_rate(double rate)
56 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
62 CommPtr Comm::set_src_data(void* buff)
64 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
66 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
71 CommPtr Comm::set_src_data_size(size_t size)
73 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
75 src_buff_size_ = size;
79 CommPtr Comm::set_src_data(void* buff, size_t size)
81 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
84 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
86 src_buff_size_ = size;
90 CommPtr Comm::set_dst_data(void** buff)
92 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
94 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
98 void* Comm::get_dst_data()
103 size_t Comm::get_dst_data_size() const
105 return dst_buff_size_;
107 CommPtr Comm::set_dst_data(void** buff, size_t size)
109 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
112 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
114 dst_buff_size_ = size;
120 xbt_assert(get_state() == State::INITED || get_state() == State::STARTING,
121 "You cannot use %s() once your communication started (not implemented)", __FUNCTION__);
123 if (src_buff_ != nullptr) { // Sender side
124 on_start(*this, true /* is_sender*/);
125 pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
126 clean_fun_, copy_data_function_, get_user_data(), detached_);
127 } else if (dst_buff_ != nullptr) { // Receiver side
128 xbt_assert(not detached_, "Receive cannot be detached");
129 on_start(*this, false /*is_sender*/);
130 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
131 copy_data_function_, get_user_data(), rate_);
134 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
140 state_ = State::STARTED;
144 /** @brief Block the calling actor until the communication is finished */
147 return this->wait_for(-1);
150 /** @brief Block the calling actor until the communication is finished, or until timeout
152 * On timeout, an exception is thrown and the communication is invalidated.
154 * @param timeout the amount of seconds to wait for the comm termination.
155 * Negative values denote infinite wait times. 0 as a timeout returns immediately. */
156 Comm* Comm::wait_for(double timeout)
159 case State::FINISHED:
163 case State::STARTING: // It's not started yet. Do it in one simcall
164 if (src_buff_ != nullptr) {
165 on_start(*this, true /*is_sender*/);
166 simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
167 copy_data_function_, get_user_data(), timeout);
170 on_start(*this, false /*is_sender*/);
171 simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
172 get_user_data(), timeout, rate_);
174 state_ = State::FINISHED;
175 this->release_dependencies();
179 simcall_comm_wait(get_impl(), timeout);
180 state_ = State::FINISHED;
181 this->release_dependencies();
184 case State::CANCELED:
185 throw CancelException(XBT_THROW_POINT, "Communication canceled");
190 on_completion(*this);
194 int Comm::test_any(const std::vector<CommPtr>* comms)
196 std::vector<kernel::activity::CommImpl*> rcomms(comms->size());
197 std::transform(begin(*comms), end(*comms), begin(rcomms),
198 [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
199 int changed_pos = simcall_comm_testany(rcomms.data(), rcomms.size());
200 if (changed_pos != -1)
201 comms->at(changed_pos)->release_dependencies();
207 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
209 xbt_assert(src_buff_ != nullptr && src_buff_size_ != 0, "You can only detach sends, not recvs");
217 kernel::actor::simcall([this] {
219 boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->cancel();
221 state_ = State::CANCELED;
227 xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
228 state_ == State::FINISHED);
230 if (state_ == State::FINISHED)
233 if (state_ == State::INITED || state_ == State::STARTING)
234 this->vetoable_start();
236 if (simcall_comm_test(get_impl())) {
237 state_ = State::FINISHED;
238 this->release_dependencies();
244 Mailbox* Comm::get_mailbox() const
249 Actor* Comm::get_sender() const
251 kernel::actor::ActorImplPtr sender = nullptr;
253 sender = boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->src_actor_;
254 return sender ? sender->get_ciface() : nullptr;
258 } // namespace simgrid
259 /* **************************** Public C interface *************************** */
260 void sg_comm_detach(sg_comm_t comm, void (*clean_function)(void*))
262 comm->detach(clean_function);
265 void sg_comm_unref(sg_comm_t comm)
269 int sg_comm_test(sg_comm_t comm)
271 bool finished = comm->test();
277 sg_error_t sg_comm_wait(sg_comm_t comm)
279 sg_error_t status = SG_OK;
281 simgrid::s4u::CommPtr s4u_comm(comm, false);
283 s4u_comm->wait_for(-1);
284 } catch (const simgrid::TimeoutException&) {
285 status = SG_ERROR_TIMEOUT;
286 } catch (const simgrid::CancelException&) {
287 status = SG_ERROR_CANCELED;
288 } catch (const simgrid::NetworkFailureException&) {
289 status = SG_ERROR_NETWORK;
294 sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout)
296 sg_error_t status = SG_OK;
298 simgrid::s4u::CommPtr s4u_comm(comm, false);
300 s4u_comm->wait_for(timeout);
301 } catch (const simgrid::TimeoutException&) {
302 status = SG_ERROR_TIMEOUT;
303 } catch (const simgrid::CancelException&) {
304 status = SG_ERROR_CANCELED;
305 } catch (const simgrid::NetworkFailureException&) {
306 status = SG_ERROR_NETWORK;
311 void sg_comm_wait_all(sg_comm_t* comms, size_t count)
313 std::vector<simgrid::s4u::CommPtr> s4u_comms;
314 for (unsigned int i = 0; i < count; i++)
315 s4u_comms.emplace_back(comms[i], false);
317 simgrid::s4u::Comm::wait_all(&s4u_comms);
320 int sg_comm_wait_any(sg_comm_t* comms, size_t count)
322 return sg_comm_wait_any_for(comms, count, -1);
325 int sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout)
327 std::vector<simgrid::s4u::CommPtr> s4u_comms;
328 for (unsigned int i = 0; i < count; i++)
329 s4u_comms.emplace_back(comms[i], false);
331 int pos = simgrid::s4u::Comm::wait_any_for(&s4u_comms, timeout);
332 for (unsigned i = 0; i < count; i++) {
333 if (pos != -1 && static_cast<unsigned>(pos) != i)
334 s4u_comms[i]->add_ref();