Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
play with (parallel) execs. still not satisfying
[simgrid.git] / src / s4u / s4u_Comm.cpp
1 /* Copyright (c) 2006-2019. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/msg/msg_private.hpp"
7 #include "xbt/log.h"
8
9 #include "simgrid/Exception.hpp"
10 #include "simgrid/s4u/Comm.hpp"
11 #include "simgrid/s4u/Mailbox.hpp"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm, s4u_activity, "S4U asynchronous communications");
14
15 namespace simgrid {
16 namespace s4u {
17 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Comm::on_sender_start;
18 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Comm::on_receiver_start;
19 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Comm::on_completion;
20
21 Comm::~Comm()
22 {
23   if (state_ == State::STARTED && not detached_ && (pimpl_ == nullptr || pimpl_->state_ == SIMIX_RUNNING)) {
24     XBT_INFO("Comm %p freed before its completion. Detached: %d, State: %d", this, detached_, (int)state_);
25     if (pimpl_ != nullptr)
26       XBT_INFO("pimpl_->state: %d", pimpl_->state_);
27     else
28       XBT_INFO("pimpl_ is null");
29     xbt_backtrace_display_current();
30   }
31 }
32
33 int Comm::wait_any_for(std::vector<CommPtr>* comms, double timeout)
34 {
35   std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(
36       new simgrid::kernel::activity::CommImpl*[comms->size()]);
37   std::transform(begin(*comms), end(*comms), rcomms.get(), [](const CommPtr& comm) {
38     return static_cast<simgrid::kernel::activity::CommImpl*>(comm->pimpl_.get());
39   });
40   return simcall_comm_waitany(rcomms.get(), comms->size(), timeout);
41 }
42
43 void Comm::wait_all(std::vector<CommPtr>* comms)
44 {
45   // TODO: this should be a simcall or something
46   // TODO: we are missing a version with timeout
47   for (CommPtr comm : *comms)
48     comm->wait();
49 }
50
51 CommPtr Comm::set_rate(double rate)
52 {
53   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
54              __FUNCTION__);
55   rate_ = rate;
56   return this;
57 }
58
59 CommPtr Comm::set_src_data(void* buff)
60 {
61   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
62              __FUNCTION__);
63   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
64   src_buff_ = buff;
65   return this;
66 }
67
68 CommPtr Comm::set_src_data_size(size_t size)
69 {
70   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
71              __FUNCTION__);
72   src_buff_size_ = size;
73   return this;
74 }
75
76 CommPtr Comm::set_src_data(void* buff, size_t size)
77 {
78   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
79              __FUNCTION__);
80
81   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
82   src_buff_      = buff;
83   src_buff_size_ = size;
84   return this;
85 }
86 CommPtr Comm::set_dst_data(void** buff)
87 {
88   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
89              __FUNCTION__);
90   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
91   dst_buff_ = buff;
92   return this;
93 }
94
95 size_t Comm::get_dst_data_size()
96 {
97   xbt_assert(state_ == State::FINISHED, "You cannot use %s before your communication terminated", __FUNCTION__);
98   return dst_buff_size_;
99 }
100 CommPtr Comm::set_dst_data(void** buff, size_t size)
101 {
102   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
103              __FUNCTION__);
104
105   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
106   dst_buff_      = buff;
107   dst_buff_size_ = size;
108   return this;
109 }
110
111 Comm* Comm::start()
112 {
113   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
114              __FUNCTION__);
115
116   if (src_buff_ != nullptr) { // Sender side
117     on_sender_start(Actor::self());
118     pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
119                                 clean_fun_, copy_data_function_, user_data_, detached_);
120   } else if (dst_buff_ != nullptr) { // Receiver side
121     xbt_assert(not detached_, "Receive cannot be detached");
122     on_receiver_start(Actor::self());
123     pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
124                                 copy_data_function_, user_data_, rate_);
125
126   } else {
127     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
128   }
129   state_ = State::STARTED;
130   return this;
131 }
132
133 /** @brief Block the calling actor until the communication is finished */
134 Comm* Comm::wait()
135 {
136   return this->wait_for(-1);
137 }
138
139 /** @brief Block the calling actor until the communication is finished, or until timeout
140  *
141  * On timeout, an exception is thrown.
142  *
143  * @param timeout the amount of seconds to wait for the comm termination.
144  *                Negative values denote infinite wait times. 0 as a timeout returns immediately. */
145 Comm* Comm::wait_for(double timeout)
146 {
147   switch (state_) {
148     case State::FINISHED:
149       return this;
150
151     case State::INITED: // It's not started yet. Do it in one simcall
152       if (src_buff_ != nullptr) {
153         on_sender_start(Actor::self());
154         simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
155                           copy_data_function_, user_data_, timeout);
156
157       } else { // Receiver
158         on_receiver_start(Actor::self());
159         simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
160                           user_data_, timeout, rate_);
161       }
162       state_ = State::FINISHED;
163       return this;
164
165     case State::STARTED:
166       simcall_comm_wait(pimpl_, timeout);
167       on_completion(Actor::self());
168       state_ = State::FINISHED;
169       return this;
170
171     case State::CANCELED:
172       throw CancelException(XBT_THROW_POINT, "Communication canceled");
173
174     default:
175       THROW_IMPOSSIBLE;
176   }
177   return this;
178 }
179 int Comm::test_any(std::vector<CommPtr>* comms)
180 {
181   std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(
182       new simgrid::kernel::activity::CommImpl*[comms->size()]);
183   std::transform(begin(*comms), end(*comms), rcomms.get(), [](const CommPtr& comm) {
184     return static_cast<simgrid::kernel::activity::CommImpl*>(comm->pimpl_.get());
185   });
186   return simcall_comm_testany(rcomms.get(), comms->size());
187 }
188
189 Comm* Comm::detach()
190 {
191   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
192              __FUNCTION__);
193   xbt_assert(src_buff_ != nullptr && src_buff_size_ != 0, "You can only detach sends, not recvs");
194   detached_ = true;
195   return start();
196 }
197
198 Comm* Comm::cancel()
199 {
200   simgrid::simix::simcall([this] {
201     if (pimpl_)
202       boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->cancel();
203   });
204   state_ = State::CANCELED;
205   return this;
206 }
207
208 bool Comm::test()
209 {
210   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
211
212   if (state_ == State::FINISHED)
213     return true;
214
215   if (state_ == State::INITED)
216     this->start();
217
218   if (simcall_comm_test(pimpl_)) {
219     state_ = State::FINISHED;
220     return true;
221   }
222   return false;
223 }
224
225 MailboxPtr Comm::get_mailbox()
226 {
227   return mailbox_;
228 }
229
230 void intrusive_ptr_release(simgrid::s4u::Comm* c)
231 {
232   if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
233     std::atomic_thread_fence(std::memory_order_acquire);
234     delete c;
235   }
236 }
237 void intrusive_ptr_add_ref(simgrid::s4u::Comm* c)
238 {
239   c->refcount_.fetch_add(1, std::memory_order_relaxed);
240 }
241 } // namespace s4u
242 } // namespace simgrid