Logo AND Algorithmique Numérique Distribuée

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