Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Unify on_start/on_completion signals of Activities
[simgrid.git] / src / s4u / s4u_Comm.cpp
1 /* Copyright (c) 2006-2020. 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 #include <simgrid/comm.h>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm, s4u_activity, "S4U asynchronous communications");
16
17 namespace simgrid {
18 namespace s4u {
19 xbt::signal<void(Comm const&, Actor const&)> Comm::on_sender_start;
20 xbt::signal<void(Comm const&, Actor const&)> Comm::on_receiver_start;
21 xbt::signal<void(Comm const&, Actor const&)> Comm::on_completion;
22
23 Comm::~Comm()
24 {
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_));
30     else
31       XBT_INFO("pimpl_ is null");
32     xbt_backtrace_display_current();
33   }
34 }
35
36 int Comm::wait_any_for(const std::vector<CommPtr>* comms, double timeout)
37 {
38   std::vector<kernel::activity::CommImpl*> rcomms(comms->size());
39   std::transform(begin(*comms), end(*comms), begin(rcomms),
40                  [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
41   int changed_pos = simcall_comm_waitany(rcomms.data(), rcomms.size(), timeout);
42   if (changed_pos != -1)
43     comms->at(changed_pos)->release_dependencies();
44   return changed_pos;
45 }
46
47 void Comm::wait_all(const std::vector<CommPtr>* comms)
48 {
49   // TODO: this should be a simcall or something
50   // TODO: we are missing a version with timeout
51   for (CommPtr comm : *comms)
52     comm->wait();
53 }
54
55 CommPtr Comm::set_rate(double rate)
56 {
57   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
58              __FUNCTION__);
59   rate_ = rate;
60   return this;
61 }
62
63 CommPtr Comm::set_src_data(void* buff)
64 {
65   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
66              __FUNCTION__);
67   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
68   src_buff_ = buff;
69   return this;
70 }
71
72 CommPtr Comm::set_src_data_size(size_t size)
73 {
74   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
75              __FUNCTION__);
76   src_buff_size_ = size;
77   return this;
78 }
79
80 CommPtr Comm::set_src_data(void* buff, size_t size)
81 {
82   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
83              __FUNCTION__);
84
85   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
86   src_buff_      = buff;
87   src_buff_size_ = size;
88   return this;
89 }
90
91 CommPtr Comm::set_dst_data(void** buff)
92 {
93   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
94              __FUNCTION__);
95   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
96   dst_buff_ = buff;
97   return this;
98 }
99
100 size_t Comm::get_dst_data_size() const
101 {
102   xbt_assert(state_ == State::FINISHED, "You cannot use %s before your communication terminated", __FUNCTION__);
103   return dst_buff_size_;
104 }
105 CommPtr Comm::set_dst_data(void** buff, size_t size)
106 {
107   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
108              __FUNCTION__);
109
110   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
111   dst_buff_      = buff;
112   dst_buff_size_ = size;
113   return this;
114 }
115
116 Comm* Comm::start()
117 {
118   xbt_assert(get_state() == State::INITED || get_state() == State::STARTING,
119              "You cannot use %s() once your communication started (not implemented)", __FUNCTION__);
120
121   if (src_buff_ != nullptr) { // Sender side
122     on_sender_start(*this, *Actor::self());
123     pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
124                                 clean_fun_, copy_data_function_, get_user_data(), detached_);
125   } else if (dst_buff_ != nullptr) { // Receiver side
126     xbt_assert(not detached_, "Receive cannot be detached");
127     on_receiver_start(*this, *Actor::self());
128     pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
129                                 copy_data_function_, get_user_data(), rate_);
130
131   } else {
132     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
133   }
134
135   if (suspended_)
136     pimpl_->suspend();
137
138   state_ = State::STARTED;
139   return this;
140 }
141
142 /** @brief Block the calling actor until the communication is finished */
143 Comm* Comm::wait()
144 {
145   return this->wait_for(-1);
146 }
147
148 /** @brief Block the calling actor until the communication is finished, or until timeout
149  *
150  * On timeout, an exception is thrown and the communication is invalidated.
151  *
152  * @param timeout the amount of seconds to wait for the comm termination.
153  *                Negative values denote infinite wait times. 0 as a timeout returns immediately. */
154 Comm* Comm::wait_for(double timeout)
155 {
156   switch (state_) {
157     case State::FINISHED:
158       break;
159
160     case State::INITED:
161     case State::STARTING: // It's not started yet. Do it in one simcall
162       if (src_buff_ != nullptr) {
163         on_sender_start(*this, *Actor::self());
164         simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
165                           copy_data_function_, get_user_data(), timeout);
166
167       } else { // Receiver
168         on_receiver_start(*this, *Actor::self());
169         simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
170                           get_user_data(), timeout, rate_);
171       }
172       state_ = State::FINISHED;
173       this->release_dependencies();
174       break;
175
176     case State::STARTED:
177       simcall_comm_wait(get_impl(), timeout);
178       state_ = State::FINISHED;
179       this->release_dependencies();
180       break;
181
182     case State::CANCELED:
183       throw CancelException(XBT_THROW_POINT, "Communication canceled");
184
185     default:
186       THROW_IMPOSSIBLE;
187   }
188   on_completion(*this, *Actor::self());
189   return this;
190 }
191
192 int Comm::test_any(const std::vector<CommPtr>* comms)
193 {
194   std::vector<kernel::activity::CommImpl*> rcomms(comms->size());
195   std::transform(begin(*comms), end(*comms), begin(rcomms),
196                  [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
197   int changed_pos = simcall_comm_testany(rcomms.data(), rcomms.size());
198   if (changed_pos != -1)
199     comms->at(changed_pos)->release_dependencies();
200   return changed_pos;
201 }
202
203 Comm* Comm::detach()
204 {
205   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
206              __FUNCTION__);
207   xbt_assert(src_buff_ != nullptr && src_buff_size_ != 0, "You can only detach sends, not recvs");
208   detached_ = true;
209   vetoable_start();
210   return this;
211 }
212
213 Comm* Comm::cancel()
214 {
215   kernel::actor::simcall([this] {
216     if (pimpl_)
217       boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->cancel();
218   });
219   state_ = State::CANCELED;
220   return this;
221 }
222
223 bool Comm::test()
224 {
225   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
226              state_ == State::FINISHED);
227
228   if (state_ == State::FINISHED)
229     return true;
230
231   if (state_ == State::INITED || state_ == State::STARTING)
232     this->vetoable_start();
233
234   if (simcall_comm_test(get_impl())) {
235     state_ = State::FINISHED;
236     this->release_dependencies();
237     return true;
238   }
239   return false;
240 }
241
242 Mailbox* Comm::get_mailbox() const
243 {
244   return mailbox_;
245 }
246
247 Actor* Comm::get_sender() const
248 {
249   kernel::actor::ActorImplPtr sender = nullptr;
250   if (pimpl_)
251     sender = boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->src_actor_;
252   return sender ? sender->get_ciface() : nullptr;
253 }
254
255 } // namespace s4u
256 } // namespace simgrid
257 /* **************************** Public C interface *************************** */
258 void sg_comm_detach(sg_comm_t comm, void (*clean_function)(void*))
259 {
260   comm->detach(clean_function);
261   comm->unref();
262 }
263 void sg_comm_unref(sg_comm_t comm)
264 {
265   comm->unref();
266 }
267 int sg_comm_test(sg_comm_t comm)
268 {
269   bool finished = comm->test();
270   if (finished)
271     comm->unref();
272   return finished;
273 }
274
275 sg_error_t sg_comm_wait(sg_comm_t comm)
276 {
277   sg_error_t status = SG_OK;
278
279   simgrid::s4u::CommPtr s4u_comm(comm, false);
280   try {
281     s4u_comm->wait_for(-1);
282   } catch (const simgrid::TimeoutException&) {
283     status = SG_ERROR_TIMEOUT;
284   } catch (const simgrid::CancelException&) {
285     status = SG_ERROR_CANCELED;
286   } catch (const simgrid::NetworkFailureException&) {
287     status = SG_ERROR_NETWORK;
288   }
289   return status;
290 }
291
292 sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout)
293 {
294   sg_error_t status = SG_OK;
295
296   simgrid::s4u::CommPtr s4u_comm(comm, false);
297   try {
298     s4u_comm->wait_for(timeout);
299   } catch (const simgrid::TimeoutException&) {
300     status = SG_ERROR_TIMEOUT;
301   } catch (const simgrid::CancelException&) {
302     status = SG_ERROR_CANCELED;
303   } catch (const simgrid::NetworkFailureException&) {
304     status = SG_ERROR_NETWORK;
305   }
306   return status;
307 }
308
309 void sg_comm_wait_all(sg_comm_t* comms, size_t count)
310 {
311   std::vector<simgrid::s4u::CommPtr> s4u_comms;
312   for (unsigned int i = 0; i < count; i++)
313     s4u_comms.emplace_back(comms[i], false);
314
315   simgrid::s4u::Comm::wait_all(&s4u_comms);
316 }
317
318 int sg_comm_wait_any(sg_comm_t* comms, size_t count)
319 {
320   return sg_comm_wait_any_for(comms, count, -1);
321 }
322
323 int sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout)
324 {
325   std::vector<simgrid::s4u::CommPtr> s4u_comms;
326   for (unsigned int i = 0; i < count; i++)
327     s4u_comms.emplace_back(comms[i], false);
328
329   int pos = simgrid::s4u::Comm::wait_any_for(&s4u_comms, timeout);
330   for (unsigned i = 0; i < count; i++) {
331     if (pos != -1 && static_cast<unsigned>(pos) != i)
332       s4u_comms[i]->add_ref();
333   }
334   return pos;
335 }