Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
No need to test 'not detached_' twice in the same test
[simgrid.git] / src / s4u / s4u_Comm.cpp
1 /* Copyright (c) 2006-2021. 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. Did you forget to detach it? (state: %s)", this, get_state_str());
27     if (pimpl_ != nullptr)
28       XBT_INFO("pimpl_->state: %s", pimpl_->get_state_str());
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 void* Comm::get_dst_data()
99 {
100   return dst_buff_;
101 }
102
103 size_t Comm::get_dst_data_size() const
104 {
105   return dst_buff_size_;
106 }
107 CommPtr Comm::set_dst_data(void** buff, size_t size)
108 {
109   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
110              __FUNCTION__);
111
112   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
113   dst_buff_      = buff;
114   dst_buff_size_ = size;
115   return this;
116 }
117 CommPtr Comm::set_payload_size(double bytes)
118 {
119   Activity::set_remaining(bytes);
120   return this;
121 }
122
123 CommPtr Comm::sendto_init(Host* from, Host* to)
124 {
125   CommPtr res(new Comm());
126   res->from_ = from;
127   res->to_   = to;
128
129   return res;
130 }
131
132 CommPtr Comm::sendto_async(Host* from, Host* to, double simulated_size_in_bytes)
133 {
134   auto res = Comm::sendto_init(from, to)->set_payload_size(simulated_size_in_bytes);
135   res->vetoable_start();
136   return res;
137 }
138
139 Comm* Comm::start()
140 {
141   xbt_assert(get_state() == State::INITED || get_state() == State::STARTING,
142              "You cannot use %s() once your communication started (not implemented)", __FUNCTION__);
143   if (from_ != nullptr || to_ != nullptr) {
144     xbt_assert(from_ != nullptr && to_ != nullptr, "When either from_ or to_ is specified, both must be.");
145     xbt_assert(src_buff_ == nullptr && dst_buff_ == nullptr,
146                "Direct host-to-host communications cannot carry any data.");
147     pimpl_ = kernel::actor::simcall([this] {
148       auto res = new kernel::activity::CommImpl(this->from_, this->to_, this->get_remaining());
149       res->start();
150       return res;
151     });
152
153   } else if (src_buff_ != nullptr) { // Sender side
154     on_start(*this, true /* is_sender*/);
155     pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
156                                 clean_fun_, copy_data_function_, get_user_data(), detached_);
157   } else if (dst_buff_ != nullptr) { // Receiver side
158     xbt_assert(not detached_, "Receive cannot be detached");
159     on_start(*this, false /*is_sender*/);
160     pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
161                                 copy_data_function_, get_user_data(), rate_);
162
163   } else {
164     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
165   }
166
167   if (suspended_)
168     pimpl_->suspend();
169
170   state_ = State::STARTED;
171   return this;
172 }
173
174 /** @brief Block the calling actor until the communication is finished */
175 Comm* Comm::wait()
176 {
177   return this->wait_for(-1);
178 }
179
180 /** @brief Block the calling actor until the communication is finished, or until timeout
181  *
182  * On timeout, an exception is thrown and the communication is invalidated.
183  *
184  * @param timeout the amount of seconds to wait for the comm termination.
185  *                Negative values denote infinite wait times. 0 as a timeout returns immediately. */
186 Comm* Comm::wait_for(double timeout)
187 {
188   switch (state_) {
189     case State::FINISHED:
190       break;
191
192     case State::INITED:
193     case State::STARTING: // It's not started yet. Do it in one simcall if it's a regular communication
194       if (from_ != nullptr || to_ != nullptr) {
195         return vetoable_start()->wait_for(timeout); // In the case of host2host comm, do it in two simcalls
196       } else if (src_buff_ != nullptr) {
197         on_start(*this, true /*is_sender*/);
198         simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
199                           copy_data_function_, get_user_data(), timeout);
200
201       } else { // Receiver
202         on_start(*this, false /*is_sender*/);
203         simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
204                           get_user_data(), timeout, rate_);
205       }
206       state_ = State::FINISHED;
207       this->release_dependencies();
208       break;
209
210     case State::STARTED:
211       simcall_comm_wait(get_impl(), timeout);
212       state_ = State::FINISHED;
213       this->release_dependencies();
214       break;
215
216     case State::CANCELED:
217       throw CancelException(XBT_THROW_POINT, "Communication canceled");
218
219     default:
220       THROW_IMPOSSIBLE;
221   }
222   on_completion(*this);
223   return this;
224 }
225
226 int Comm::test_any(const std::vector<CommPtr>* comms)
227 {
228   std::vector<kernel::activity::CommImpl*> rcomms(comms->size());
229   std::transform(begin(*comms), end(*comms), begin(rcomms),
230                  [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
231   int changed_pos = simcall_comm_testany(rcomms.data(), rcomms.size());
232   if (changed_pos != -1)
233     comms->at(changed_pos)->release_dependencies();
234   return changed_pos;
235 }
236
237 Comm* Comm::detach()
238 {
239   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication is %s (not implemented)",
240              __FUNCTION__, get_state_str());
241   xbt_assert(dst_buff_ == nullptr && dst_buff_size_ == 0, "You can only detach sends, not recvs");
242   detached_ = true;
243   vetoable_start();
244   return this;
245 }
246
247 Comm* Comm::cancel()
248 {
249   kernel::actor::simcall([this] {
250     if (pimpl_)
251       boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->cancel();
252   });
253   state_ = State::CANCELED;
254   return this;
255 }
256
257 bool Comm::test()
258 {
259   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
260              state_ == State::FINISHED);
261
262   if (state_ == State::FINISHED)
263     return true;
264
265   if (state_ == State::INITED || state_ == State::STARTING)
266     this->vetoable_start();
267
268   if (simcall_comm_test(get_impl())) {
269     state_ = State::FINISHED;
270     this->release_dependencies();
271     return true;
272   }
273   return false;
274 }
275
276 Mailbox* Comm::get_mailbox() const
277 {
278   return mailbox_;
279 }
280
281 Actor* Comm::get_sender() const
282 {
283   kernel::actor::ActorImplPtr sender = nullptr;
284   if (pimpl_)
285     sender = boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->src_actor_;
286   return sender ? sender->get_ciface() : nullptr;
287 }
288
289 } // namespace s4u
290 } // namespace simgrid
291 /* **************************** Public C interface *************************** */
292 void sg_comm_detach(sg_comm_t comm, void (*clean_function)(void*))
293 {
294   comm->detach(clean_function);
295   comm->unref();
296 }
297 void sg_comm_unref(sg_comm_t comm)
298 {
299   comm->unref();
300 }
301 int sg_comm_test(sg_comm_t comm)
302 {
303   bool finished = comm->test();
304   if (finished)
305     comm->unref();
306   return finished;
307 }
308
309 sg_error_t sg_comm_wait(sg_comm_t comm)
310 {
311   sg_error_t status = SG_OK;
312
313   simgrid::s4u::CommPtr s4u_comm(comm, false);
314   try {
315     s4u_comm->wait_for(-1);
316   } catch (const simgrid::TimeoutException&) {
317     status = SG_ERROR_TIMEOUT;
318   } catch (const simgrid::CancelException&) {
319     status = SG_ERROR_CANCELED;
320   } catch (const simgrid::NetworkFailureException&) {
321     status = SG_ERROR_NETWORK;
322   }
323   return status;
324 }
325
326 sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout)
327 {
328   sg_error_t status = SG_OK;
329
330   simgrid::s4u::CommPtr s4u_comm(comm, false);
331   try {
332     s4u_comm->wait_for(timeout);
333   } catch (const simgrid::TimeoutException&) {
334     status = SG_ERROR_TIMEOUT;
335   } catch (const simgrid::CancelException&) {
336     status = SG_ERROR_CANCELED;
337   } catch (const simgrid::NetworkFailureException&) {
338     status = SG_ERROR_NETWORK;
339   }
340   return status;
341 }
342
343 void sg_comm_wait_all(sg_comm_t* comms, size_t count)
344 {
345   std::vector<simgrid::s4u::CommPtr> s4u_comms;
346   for (unsigned int i = 0; i < count; i++)
347     s4u_comms.emplace_back(comms[i], false);
348
349   simgrid::s4u::Comm::wait_all(&s4u_comms);
350 }
351
352 int sg_comm_wait_any(sg_comm_t* comms, size_t count)
353 {
354   return sg_comm_wait_any_for(comms, count, -1);
355 }
356
357 int sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout)
358 {
359   std::vector<simgrid::s4u::CommPtr> s4u_comms;
360   for (unsigned int i = 0; i < count; i++)
361     s4u_comms.emplace_back(comms[i], false);
362
363   int pos = simgrid::s4u::Comm::wait_any_for(&s4u_comms, timeout);
364   for (unsigned i = 0; i < count; i++) {
365     if (pos != -1 && static_cast<unsigned>(pos) != i)
366       s4u_comms[i]->add_ref();
367   }
368   return pos;
369 }