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/comm.h>
11 #include <simgrid/s4u/Comm.hpp>
12 #include <simgrid/s4u/Engine.hpp>
13 #include <simgrid/s4u/Mailbox.hpp>
15 #include "src/kernel/activity/CommImpl.hpp"
16 #include "src/kernel/actor/ActorImpl.hpp"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm, s4u_activity, "S4U asynchronous communications");
22 xbt::signal<void(Comm const&)> Comm::on_send;
23 xbt::signal<void(Comm const&)> Comm::on_recv;
24 xbt::signal<void(Comm const&)> Comm::on_completion;
28 if (state_ == State::STARTED && not detached_ &&
29 (pimpl_ == nullptr || pimpl_->state_ == kernel::activity::State::RUNNING)) {
30 XBT_INFO("Comm %p freed before its completion. Did you forget to detach it? (state: %s)", 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 ssize_t 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()); });
46 changed_pos = simcall_comm_waitany(rcomms.data(), rcomms.size(), timeout);
47 } catch (const NetworkFailureException& e) {
48 for (auto c : comms) {
49 if (c->pimpl_->state_ == kernel::activity::State::FAILED) {
50 c->complete(State::FAILED);
53 e.rethrow_nested(XBT_THROW_POINT, boost::core::demangle(typeid(e).name()) + " raised in kernel mode.");
55 if (changed_pos != -1)
56 comms.at(changed_pos)->complete(State::FINISHED);
60 void Comm::wait_all(const std::vector<CommPtr>& comms)
62 // TODO: this should be a simcall or something
63 for (auto& comm : comms)
67 size_t Comm::wait_all_for(const std::vector<CommPtr>& comms, double timeout)
74 double deadline = Engine::get_clock() + timeout;
75 std::vector<CommPtr> waited_comm(1, nullptr);
76 for (size_t i = 0; i < comms.size(); i++) {
77 double wait_timeout = std::max(0.0, deadline - Engine::get_clock());
78 waited_comm[0] = comms[i];
79 // Using wait_any_for() here (and not wait_for) because we don't want comms to be invalidated on timeout
80 if (wait_any_for(waited_comm, wait_timeout) == -1) {
81 XBT_DEBUG("Timeout (%g): i = %zu", wait_timeout, i);
88 CommPtr Comm::set_source(Host* from)
90 xbt_assert(state_ == State::INITED || state_ == State::STARTING,
91 "Cannot change the source of a Comm once it's started (state: %s)", to_c_str(state_));
93 // Setting 'from_' may allow to start the activity, let's try
99 CommPtr Comm::set_destination(Host* to)
101 xbt_assert(state_ == State::INITED || state_ == State::STARTING,
102 "Cannot change the destination of a Comm once it's started (state: %s)", to_c_str(state_));
104 // Setting 'to_' may allow to start the activity, let's try
110 CommPtr Comm::set_rate(double rate)
112 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
118 CommPtr Comm::set_src_data(void* buff)
120 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
122 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
127 CommPtr Comm::set_src_data_size(size_t size)
129 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
131 src_buff_size_ = size;
135 CommPtr Comm::set_src_data(void* buff, size_t size)
137 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
140 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
142 src_buff_size_ = size;
146 CommPtr Comm::set_dst_data(void** buff)
148 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
150 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
154 void* Comm::get_dst_data()
159 size_t Comm::get_dst_data_size() const
161 return dst_buff_size_;
163 CommPtr Comm::set_dst_data(void** buff, size_t size)
165 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
168 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
170 dst_buff_size_ = size;
173 CommPtr Comm::set_payload_size(uint64_t bytes)
175 Activity::set_remaining(bytes);
179 CommPtr Comm::sendto_init()
181 CommPtr res(new Comm());
182 res->sender_ = kernel::actor::ActorImpl::self();
186 CommPtr Comm::sendto_init(Host* from, Host* to)
188 auto res = Comm::sendto_init();
195 CommPtr Comm::sendto_async(Host* from, Host* to, uint64_t simulated_size_in_bytes)
197 auto res = Comm::sendto_init(from, to)->set_payload_size(simulated_size_in_bytes);
198 res->vetoable_start();
202 void Comm::sendto(Host* from, Host* to, uint64_t simulated_size_in_bytes)
204 sendto_async(from, to, simulated_size_in_bytes)->wait();
209 xbt_assert(get_state() == State::INITED || get_state() == State::STARTING,
210 "You cannot use %s() once your communication started (not implemented)", __FUNCTION__);
211 if (from_ != nullptr || to_ != nullptr) {
212 xbt_assert(from_ != nullptr && to_ != nullptr, "When either from_ or to_ is specified, both must be.");
213 xbt_assert(src_buff_ == nullptr && dst_buff_ == nullptr,
214 "Direct host-to-host communications cannot carry any data.");
215 pimpl_ = kernel::actor::simcall([this] {
216 kernel::activity::CommImplPtr res(new kernel::activity::CommImpl(this->from_, this->to_, this->get_remaining()));
221 } else if (src_buff_ != nullptr) { // Sender side
223 pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
224 clean_fun_, copy_data_function_, get_user_data(), detached_);
225 } else if (dst_buff_ != nullptr) { // Receiver side
226 xbt_assert(not detached_, "Receive cannot be detached");
228 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
229 copy_data_function_, get_user_data(), rate_);
232 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
239 pimpl_->set_iface(this);
240 pimpl_->set_actor(sender_);
243 state_ = State::STARTED;
247 /** @brief Block the calling actor until the communication is finished, or until timeout
249 * On timeout, an exception is thrown and the communication is invalidated.
251 * @param timeout the amount of seconds to wait for the comm termination.
252 * Negative values denote infinite wait times. 0 as a timeout returns immediately. */
253 Comm* Comm::wait_for(double timeout)
256 case State::FINISHED:
259 throw NetworkFailureException(XBT_THROW_POINT, "Cannot wait for a failed communication");
262 case State::STARTING: // It's not started yet. Do it in one simcall if it's a regular communication
263 if (from_ != nullptr || to_ != nullptr) {
264 return vetoable_start()->wait_for(timeout); // In the case of host2host comm, do it in two simcalls
265 } else if (src_buff_ != nullptr) {
267 simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
268 copy_data_function_, get_user_data(), timeout);
272 simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
273 get_user_data(), timeout, rate_);
279 simcall_comm_wait(get_impl(), timeout);
280 } catch (const NetworkFailureException& e) {
281 complete(State::FAILED);
282 e.rethrow_nested(XBT_THROW_POINT, boost::core::demangle(typeid(e).name()) + " raised in kernel mode.");
286 case State::CANCELED:
287 throw CancelException(XBT_THROW_POINT, "Communication canceled");
292 complete(State::FINISHED);
296 ssize_t Comm::test_any(const std::vector<CommPtr>& comms)
298 std::vector<kernel::activity::CommImpl*> rcomms(comms.size());
299 std::transform(begin(comms), end(comms), begin(rcomms),
300 [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
301 ssize_t changed_pos = simcall_comm_testany(rcomms.data(), rcomms.size());
302 if (changed_pos != -1)
303 comms.at(changed_pos)->complete(State::FINISHED);
309 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication is %s (not implemented)",
310 __FUNCTION__, get_state_str());
311 xbt_assert(dst_buff_ == nullptr && dst_buff_size_ == 0, "You can only detach sends, not recvs");
317 bool Comm::test() // TODO: merge with Activity::test, once modernized
319 xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
320 state_ == State::CANCELED || state_ == State::FINISHED);
322 if (state_ == State::CANCELED || state_ == State::FINISHED)
325 if (state_ == State::INITED || state_ == State::STARTING)
326 this->vetoable_start();
328 if (simcall_comm_test(get_impl())) {
329 complete(State::FINISHED);
335 Mailbox* Comm::get_mailbox() const
340 Actor* Comm::get_sender() const
342 kernel::actor::ActorImplPtr sender = nullptr;
344 sender = boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->src_actor_;
345 return sender ? sender->get_ciface() : nullptr;
348 CommPtr Comm::set_copy_data_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t))
350 copy_data_function_ = callback;
353 void Comm::copy_buffer_callback(kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
355 XBT_DEBUG("Copy the data over");
356 memcpy(comm->dst_buff_, buff, buff_size);
357 if (comm->detached()) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the
358 // original buffer available to the application ASAP
360 comm->src_buff_ = nullptr;
364 void Comm::copy_pointer_callback(kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
366 xbt_assert((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
367 *(void**)(comm->dst_buff_) = buff;
371 } // namespace simgrid
372 /* **************************** Public C interface *************************** */
373 void sg_comm_detach(sg_comm_t comm, void (*clean_function)(void*))
375 comm->detach(clean_function);
378 void sg_comm_unref(sg_comm_t comm)
382 int sg_comm_test(sg_comm_t comm)
384 bool finished = comm->test();
390 sg_error_t sg_comm_wait(sg_comm_t comm)
392 return sg_comm_wait_for(comm, -1);
395 sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout)
397 sg_error_t status = SG_OK;
399 simgrid::s4u::CommPtr s4u_comm(comm, false);
401 s4u_comm->wait_for(timeout);
402 } catch (const simgrid::TimeoutException&) {
403 status = SG_ERROR_TIMEOUT;
404 } catch (const simgrid::CancelException&) {
405 status = SG_ERROR_CANCELED;
406 } catch (const simgrid::NetworkFailureException&) {
407 status = SG_ERROR_NETWORK;
412 void sg_comm_wait_all(sg_comm_t* comms, size_t count)
414 sg_comm_wait_all_for(comms, count, -1);
417 size_t sg_comm_wait_all_for(sg_comm_t* comms, size_t count, double timeout)
419 std::vector<simgrid::s4u::CommPtr> s4u_comms;
420 for (size_t i = 0; i < count; i++)
421 s4u_comms.emplace_back(comms[i], false);
423 size_t pos = simgrid::s4u::Comm::wait_all_for(s4u_comms, timeout);
424 for (size_t i = pos; i < count; i++)
425 s4u_comms[i]->add_ref();
429 ssize_t sg_comm_wait_any(sg_comm_t* comms, size_t count)
431 return sg_comm_wait_any_for(comms, count, -1);
434 ssize_t sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout)
436 std::vector<simgrid::s4u::CommPtr> s4u_comms;
437 for (size_t i = 0; i < count; i++)
438 s4u_comms.emplace_back(comms[i], false);
440 ssize_t pos = simgrid::s4u::Comm::wait_any_for(s4u_comms, timeout);
441 for (size_t i = 0; i < count; i++) {
442 if (pos != -1 && static_cast<size_t>(pos) != i)
443 s4u_comms[i]->add_ref();