1 /* Copyright (c) 2006-2019. 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"
7 #include "src/simix/ActorImpl.hpp"
10 #include "simgrid/Exception.hpp"
11 #include "simgrid/s4u/Comm.hpp"
12 #include "simgrid/s4u/Mailbox.hpp"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm, s4u_activity, "S4U asynchronous communications");
18 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Comm::on_sender_start;
19 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Comm::on_receiver_start;
20 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Comm::on_completion;
24 if (state_ == State::STARTED && not detached_ && (pimpl_ == nullptr || pimpl_->state_ == SIMIX_RUNNING)) {
25 XBT_INFO("Comm %p freed before its completion. Detached: %d, State: %d", this, detached_, (int)state_);
26 if (pimpl_ != nullptr)
27 XBT_INFO("pimpl_->state: %d", pimpl_->state_);
29 XBT_INFO("pimpl_ is null");
30 xbt_backtrace_display_current();
34 int Comm::wait_any_for(std::vector<CommPtr>* comms, double timeout)
36 std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(
37 new simgrid::kernel::activity::CommImpl*[comms->size()]);
38 std::transform(begin(*comms), end(*comms), rcomms.get(), [](const CommPtr& comm) {
39 return static_cast<simgrid::kernel::activity::CommImpl*>(comm->pimpl_.get());
41 return simcall_comm_waitany(rcomms.get(), comms->size(), timeout);
44 void Comm::wait_all(std::vector<CommPtr>* comms)
46 // TODO: this should be a simcall or something
47 // TODO: we are missing a version with timeout
48 for (CommPtr comm : *comms)
52 CommPtr Comm::set_rate(double rate)
54 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
60 CommPtr Comm::set_src_data(void* buff)
62 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
64 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
69 CommPtr Comm::set_src_data_size(size_t size)
71 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
73 src_buff_size_ = size;
77 CommPtr Comm::set_src_data(void* buff, size_t size)
79 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
82 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
84 src_buff_size_ = size;
87 CommPtr Comm::set_dst_data(void** buff)
89 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
91 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
96 size_t Comm::get_dst_data_size()
98 xbt_assert(state_ == State::FINISHED, "You cannot use %s before your communication terminated", __FUNCTION__);
99 return dst_buff_size_;
101 CommPtr Comm::set_dst_data(void** buff, size_t size)
103 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
106 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
108 dst_buff_size_ = size;
114 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
117 if (src_buff_ != nullptr) { // Sender side
118 on_sender_start(Actor::self());
119 pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
120 clean_fun_, copy_data_function_, user_data_, detached_);
121 } else if (dst_buff_ != nullptr) { // Receiver side
122 xbt_assert(not detached_, "Receive cannot be detached");
123 on_receiver_start(Actor::self());
124 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
125 copy_data_function_, user_data_, rate_);
128 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
130 state_ = State::STARTED;
134 /** @brief Block the calling actor until the communication is finished */
137 return this->wait_for(-1);
140 /** @brief Block the calling actor until the communication is finished, or until timeout
142 * On timeout, an exception is thrown.
144 * @param timeout the amount of seconds to wait for the comm termination.
145 * Negative values denote infinite wait times. 0 as a timeout returns immediately. */
146 Comm* Comm::wait_for(double timeout)
149 case State::FINISHED:
152 case State::INITED: // It's not started yet. Do it in one simcall
153 if (src_buff_ != nullptr) {
154 on_sender_start(Actor::self());
155 simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
156 copy_data_function_, user_data_, timeout);
159 on_receiver_start(Actor::self());
160 simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
161 user_data_, timeout, rate_);
163 state_ = State::FINISHED;
167 simcall_comm_wait(pimpl_, timeout);
168 on_completion(Actor::self());
169 state_ = State::FINISHED;
172 case State::CANCELED:
173 throw CancelException(XBT_THROW_POINT, "Communication canceled");
180 int Comm::test_any(std::vector<CommPtr>* comms)
182 std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(
183 new simgrid::kernel::activity::CommImpl*[comms->size()]);
184 std::transform(begin(*comms), end(*comms), rcomms.get(), [](const CommPtr& comm) {
185 return static_cast<simgrid::kernel::activity::CommImpl*>(comm->pimpl_.get());
187 return simcall_comm_testany(rcomms.get(), comms->size());
192 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
194 xbt_assert(src_buff_ != nullptr && src_buff_size_ != 0, "You can only detach sends, not recvs");
201 simgrid::simix::simcall([this] {
203 boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->cancel();
205 state_ = State::CANCELED;
211 xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
213 if (state_ == State::FINISHED)
216 if (state_ == State::INITED)
219 if (simcall_comm_test(pimpl_)) {
220 state_ = State::FINISHED;
226 MailboxPtr Comm::get_mailbox()
231 ActorPtr Comm::get_sender()
233 return sender_ ? sender_->iface() : nullptr;
236 void intrusive_ptr_release(simgrid::s4u::Comm* c)
238 if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
239 std::atomic_thread_fence(std::memory_order_acquire);
243 void intrusive_ptr_add_ref(simgrid::s4u::Comm* c)
245 c->refcount_.fetch_add(1, std::memory_order_relaxed);
248 } // namespace simgrid