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(Actor const&)> Comm::on_sender_start;
20 xbt::signal<void(Actor const&)> Comm::on_receiver_start;
21 xbt::signal<void(Actor const&)> Comm::on_completion;
25 if (state_ == State::STARTED && not detached_ &&
26 (pimpl_ == nullptr || pimpl_->state_ == kernel::activity::State::RUNNING)) {
27 XBT_INFO("Comm %p freed before its completion. Detached: %d, State: %d", this, detached_, (int)state_);
28 if (pimpl_ != nullptr)
29 XBT_INFO("pimpl_->state: %d", static_cast<int>(pimpl_->state_));
31 XBT_INFO("pimpl_ is null");
32 xbt_backtrace_display_current();
36 int Comm::wait_any_for(const std::vector<CommPtr>* comms, double timeout)
38 auto rcomms = std::make_unique<kernel::activity::CommImpl*[]>(comms->size());
39 std::transform(begin(*comms), end(*comms), rcomms.get(),
40 [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
41 int changed_pos = simcall_comm_waitany(rcomms.get(), comms->size(), timeout);
42 if (changed_pos != -1)
43 comms->at(changed_pos)->release_dependencies();
47 void Comm::wait_all(const std::vector<CommPtr>* comms)
49 // TODO: this should be a simcall or something
50 // TODO: we are missing a version with timeout
51 for (CommPtr comm : *comms)
55 CommPtr Comm::set_rate(double rate)
57 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
63 CommPtr Comm::set_src_data(void* buff)
65 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
67 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
72 CommPtr Comm::set_src_data_size(size_t size)
74 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
76 src_buff_size_ = size;
80 CommPtr Comm::set_src_data(void* buff, size_t size)
82 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
85 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
87 src_buff_size_ = size;
91 CommPtr Comm::set_dst_data(void** buff)
93 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
95 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
100 size_t Comm::get_dst_data_size() const
102 xbt_assert(state_ == State::FINISHED, "You cannot use %s before your communication terminated", __FUNCTION__);
103 return dst_buff_size_;
105 CommPtr Comm::set_dst_data(void** buff, size_t size)
107 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
110 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
112 dst_buff_size_ = size;
116 CommPtr Comm::set_tracing_category(const std::string& category)
118 xbt_assert(state_ == State::INITED, "Cannot change the tracing category of an exec after its start");
119 tracing_category_ = category;
125 xbt_assert(get_state() == State::INITED || get_state() == State::STARTING,
126 "You cannot use %s() once your communication started (not implemented)", __FUNCTION__);
128 if (src_buff_ != nullptr) { // Sender side
129 on_sender_start(*Actor::self());
130 pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
131 clean_fun_, copy_data_function_, get_user_data(), detached_);
132 } else if (dst_buff_ != nullptr) { // Receiver side
133 xbt_assert(not detached_, "Receive cannot be detached");
134 on_receiver_start(*Actor::self());
135 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
136 copy_data_function_, get_user_data(), rate_);
139 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
145 state_ = State::STARTED;
149 /** @brief Block the calling actor until the communication is finished */
152 return this->wait_for(-1);
155 /** @brief Block the calling actor until the communication is finished, or until timeout
157 * On timeout, an exception is thrown and the communication is invalidated.
159 * @param timeout the amount of seconds to wait for the comm termination.
160 * Negative values denote infinite wait times. 0 as a timeout returns immediately. */
161 Comm* Comm::wait_for(double timeout)
164 case State::FINISHED:
168 case State::STARTING: // It's not started yet. Do it in one simcall
169 if (src_buff_ != nullptr) {
170 on_sender_start(*Actor::self());
171 simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
172 copy_data_function_, get_user_data(), timeout);
175 on_receiver_start(*Actor::self());
176 simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
177 get_user_data(), timeout, rate_);
179 state_ = State::FINISHED;
180 this->release_dependencies();
184 simcall_comm_wait(get_impl(), timeout);
185 on_completion(*Actor::self());
186 state_ = State::FINISHED;
187 this->release_dependencies();
190 case State::CANCELED:
191 throw CancelException(XBT_THROW_POINT, "Communication canceled");
199 int Comm::test_any(const std::vector<CommPtr>* comms)
201 auto rcomms = std::make_unique<kernel::activity::CommImpl*[]>(comms->size());
202 std::transform(begin(*comms), end(*comms), rcomms.get(),
203 [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
204 int changed_pos = simcall_comm_testany(rcomms.get(), comms->size());
205 if (changed_pos != -1)
206 comms->at(changed_pos)->release_dependencies();
212 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
214 xbt_assert(src_buff_ != nullptr && src_buff_size_ != 0, "You can only detach sends, not recvs");
222 kernel::actor::simcall([this] {
224 boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->cancel();
226 state_ = State::CANCELED;
232 xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
233 state_ == State::FINISHED);
235 if (state_ == State::FINISHED)
238 if (state_ == State::INITED || state_ == State::STARTING)
239 this->vetoable_start();
241 if (simcall_comm_test(get_impl())) {
242 state_ = State::FINISHED;
243 this->release_dependencies();
249 Mailbox* Comm::get_mailbox() const
254 Actor* Comm::get_sender() const
256 kernel::actor::ActorImplPtr sender = nullptr;
258 sender = boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->src_actor_;
259 return sender ? sender->ciface() : nullptr;
263 } // namespace simgrid
264 /* **************************** Public C interface *************************** */
265 void sg_comm_detach(sg_comm_t comm, void (*clean_function)(void*))
267 comm->detach(clean_function);
270 void sg_comm_unref(sg_comm_t comm)
274 int sg_comm_test(sg_comm_t comm)
276 bool finished = comm->test();
282 sg_error_t sg_comm_wait(sg_comm_t comm)
284 sg_error_t status = SG_OK;
286 simgrid::s4u::CommPtr s4u_comm(comm, false);
288 s4u_comm->wait_for(-1);
289 } catch (const simgrid::TimeoutException&) {
290 status = SG_ERROR_TIMEOUT;
291 } catch (const simgrid::CancelException&) {
292 status = SG_ERROR_CANCELED;
293 } catch (const simgrid::NetworkFailureException&) {
294 status = SG_ERROR_NETWORK;
299 sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout)
301 sg_error_t status = SG_OK;
303 simgrid::s4u::CommPtr s4u_comm(comm, false);
305 s4u_comm->wait_for(timeout);
306 } catch (const simgrid::TimeoutException&) {
307 status = SG_ERROR_TIMEOUT;
308 } catch (const simgrid::CancelException&) {
309 status = SG_ERROR_CANCELED;
310 } catch (const simgrid::NetworkFailureException&) {
311 status = SG_ERROR_NETWORK;
316 void sg_comm_wait_all(sg_comm_t* comms, size_t count)
318 std::vector<simgrid::s4u::CommPtr> s4u_comms;
319 for (unsigned int i = 0; i < count; i++)
320 s4u_comms.emplace_back(comms[i], false);
322 simgrid::s4u::Comm::wait_all(&s4u_comms);
325 int sg_comm_wait_any(sg_comm_t* comms, size_t count)
327 return sg_comm_wait_any_for(comms, count, -1);
330 int sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout)
332 std::vector<simgrid::s4u::CommPtr> s4u_comms;
333 for (unsigned int i = 0; i < count; i++)
334 s4u_comms.emplace_back(comms[i], false);
336 int pos = simgrid::s4u::Comm::wait_any_for(&s4u_comms, timeout);
337 for (unsigned i = 0; i < count; i++) {
338 if (pos != -1 && static_cast<unsigned>(pos) != i)
339 s4u_comms[i]->add_ref();