Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
associate a s4u::Comm to a kernel::activity::CommImpl (partial set for now because...
[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/comm.h>
11 #include <simgrid/s4u/Comm.hpp>
12 #include <simgrid/s4u/Engine.hpp>
13 #include <simgrid/s4u/Mailbox.hpp>
14
15 #include "src/kernel/activity/CommImpl.hpp"
16 #include "src/kernel/actor/ActorImpl.hpp"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm, s4u_activity, "S4U asynchronous communications");
19
20 namespace simgrid {
21 namespace s4u {
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;
25
26 void Comm::complete(Activity::State state)
27 {
28   Activity::complete(state);
29   on_completion(*this);
30 }
31
32 Comm::~Comm()
33 {
34   if (state_ == State::STARTED && not detached_ &&
35       (pimpl_ == nullptr || pimpl_->state_ == kernel::activity::State::RUNNING)) {
36     XBT_INFO("Comm %p freed before its completion. Did you forget to detach it? (state: %s)", this, get_state_str());
37     if (pimpl_ != nullptr)
38       XBT_INFO("pimpl_->state: %s", pimpl_->get_state_str());
39     else
40       XBT_INFO("pimpl_ is null");
41     xbt_backtrace_display_current();
42   }
43 }
44
45 ssize_t Comm::wait_any_for(const std::vector<CommPtr>& comms, double timeout)
46 {
47   std::vector<kernel::activity::CommImpl*> rcomms(comms.size());
48   std::transform(begin(comms), end(comms), begin(rcomms),
49                  [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
50   ssize_t changed_pos;
51   try {
52     changed_pos = simcall_comm_waitany(rcomms.data(), rcomms.size(), timeout);
53   } catch (const NetworkFailureException& e) {
54     for (auto c : comms) {
55       if (c->pimpl_->state_ == kernel::activity::State::FAILED) {
56         c->complete(State::FAILED);
57       }
58     }
59     e.rethrow_nested(XBT_THROW_POINT, boost::core::demangle(typeid(e).name()) + " raised in kernel mode.");
60   }
61   if (changed_pos != -1)
62     comms.at(changed_pos)->complete(State::FINISHED);
63   return changed_pos;
64 }
65
66 void Comm::wait_all(const std::vector<CommPtr>& comms)
67 {
68   // TODO: this should be a simcall or something
69   for (auto& comm : comms)
70     comm->wait();
71 }
72
73 size_t Comm::wait_all_for(const std::vector<CommPtr>& comms, double timeout)
74 {
75   if (timeout < 0.0) {
76     wait_all(comms);
77     return comms.size();
78   }
79
80   double deadline = Engine::get_clock() + timeout;
81   std::vector<CommPtr> waited_comm(1, nullptr);
82   for (size_t i = 0; i < comms.size(); i++) {
83     double wait_timeout = std::max(0.0, deadline - Engine::get_clock());
84     waited_comm[0]      = comms[i];
85     // Using wait_any_for() here (and not wait_for) because we don't want comms to be invalidated on timeout
86     if (wait_any_for(waited_comm, wait_timeout) == -1) {
87       XBT_DEBUG("Timeout (%g): i = %zu", wait_timeout, i);
88       return i;
89     }
90   }
91   return comms.size();
92 }
93
94 CommPtr Comm::set_rate(double rate)
95 {
96   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
97              __FUNCTION__);
98   rate_ = rate;
99   return this;
100 }
101
102 CommPtr Comm::set_src_data(void* buff)
103 {
104   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
105              __FUNCTION__);
106   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
107   src_buff_ = buff;
108   return this;
109 }
110
111 CommPtr Comm::set_src_data_size(size_t size)
112 {
113   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
114              __FUNCTION__);
115   src_buff_size_ = size;
116   return this;
117 }
118
119 CommPtr Comm::set_src_data(void* buff, size_t size)
120 {
121   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
122              __FUNCTION__);
123
124   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
125   src_buff_      = buff;
126   src_buff_size_ = size;
127   return this;
128 }
129
130 CommPtr Comm::set_dst_data(void** buff)
131 {
132   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
133              __FUNCTION__);
134   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
135   dst_buff_ = buff;
136   return this;
137 }
138 void* Comm::get_dst_data()
139 {
140   return dst_buff_;
141 }
142
143 size_t Comm::get_dst_data_size() const
144 {
145   return dst_buff_size_;
146 }
147 CommPtr Comm::set_dst_data(void** buff, size_t size)
148 {
149   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
150              __FUNCTION__);
151
152   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
153   dst_buff_      = buff;
154   dst_buff_size_ = size;
155   return this;
156 }
157 CommPtr Comm::set_payload_size(uint64_t bytes)
158 {
159   Activity::set_remaining(bytes);
160   return this;
161 }
162
163 CommPtr Comm::sendto_init(Host* from, Host* to)
164 {
165   CommPtr res(new Comm());
166   res->from_ = from;
167   res->to_   = to;
168
169   return res;
170 }
171
172 CommPtr Comm::sendto_async(Host* from, Host* to, uint64_t simulated_size_in_bytes)
173 {
174   auto res = Comm::sendto_init(from, to)->set_payload_size(simulated_size_in_bytes);
175   res->vetoable_start();
176   return res;
177 }
178
179 void Comm::sendto(Host* from, Host* to, uint64_t simulated_size_in_bytes)
180 {
181   sendto_async(from, to, simulated_size_in_bytes)->wait();
182 }
183
184 Comm* Comm::start()
185 {
186   xbt_assert(get_state() == State::INITED || get_state() == State::STARTING,
187              "You cannot use %s() once your communication started (not implemented)", __FUNCTION__);
188   if (from_ != nullptr || to_ != nullptr) {
189     xbt_assert(from_ != nullptr && to_ != nullptr, "When either from_ or to_ is specified, both must be.");
190     xbt_assert(src_buff_ == nullptr && dst_buff_ == nullptr,
191                "Direct host-to-host communications cannot carry any data.");
192     pimpl_ = kernel::actor::simcall([this] {
193       kernel::activity::CommImplPtr res(new kernel::activity::CommImpl(this->from_, this->to_, this->get_remaining()));
194       res->start();
195       return res;
196     });
197
198   } else if (src_buff_ != nullptr) { // Sender side
199     on_send(*this);
200     pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
201                                 clean_fun_, copy_data_function_, get_user_data(), detached_);
202   } else if (dst_buff_ != nullptr) { // Receiver side
203     xbt_assert(not detached_, "Receive cannot be detached");
204     on_recv(*this);
205     pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
206                                 copy_data_function_, get_user_data(), rate_);
207
208   } else {
209     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
210   }
211
212   if (suspended_)
213     pimpl_->suspend();
214
215   if (not detached_)
216     static_cast<kernel::activity::CommImpl*>(pimpl_.get())->set_iface(this);
217   state_ = State::STARTED;
218   return this;
219 }
220
221 /** @brief Block the calling actor until the communication is finished, or until timeout
222  *
223  * On timeout, an exception is thrown and the communication is invalidated.
224  *
225  * @param timeout the amount of seconds to wait for the comm termination.
226  *                Negative values denote infinite wait times. 0 as a timeout returns immediately. */
227 Comm* Comm::wait_for(double timeout)
228 {
229   switch (state_) {
230     case State::FINISHED:
231       break;
232     case State::FAILED:
233       throw NetworkFailureException(XBT_THROW_POINT, "Cannot wait for a failed communication");
234
235     case State::INITED:
236     case State::STARTING: // It's not started yet. Do it in one simcall if it's a regular communication
237       if (from_ != nullptr || to_ != nullptr) {
238         return vetoable_start()->wait_for(timeout); // In the case of host2host comm, do it in two simcalls
239       } else if (src_buff_ != nullptr) {
240         on_send(*this);
241         simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
242                           copy_data_function_, get_user_data(), timeout);
243
244       } else { // Receiver
245         on_recv(*this);
246         simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
247                           get_user_data(), timeout, rate_);
248       }
249       break;
250
251     case State::STARTED:
252       try {
253         simcall_comm_wait(get_impl(), timeout);
254       } catch (const NetworkFailureException& e) {
255         complete(State::FAILED);
256         e.rethrow_nested(XBT_THROW_POINT, boost::core::demangle(typeid(e).name()) + " raised in kernel mode.");
257       }
258       break;
259
260     case State::CANCELED:
261       throw CancelException(XBT_THROW_POINT, "Communication canceled");
262
263     default:
264       THROW_IMPOSSIBLE;
265   }
266   complete(State::FINISHED);
267   return this;
268 }
269
270 ssize_t Comm::test_any(const std::vector<CommPtr>& comms)
271 {
272   std::vector<kernel::activity::CommImpl*> rcomms(comms.size());
273   std::transform(begin(comms), end(comms), begin(rcomms),
274                  [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
275   ssize_t changed_pos = simcall_comm_testany(rcomms.data(), rcomms.size());
276   if (changed_pos != -1)
277     comms.at(changed_pos)->complete(State::FINISHED);
278   return changed_pos;
279 }
280
281 Comm* Comm::detach()
282 {
283   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication is %s (not implemented)",
284              __FUNCTION__, get_state_str());
285   xbt_assert(dst_buff_ == nullptr && dst_buff_size_ == 0, "You can only detach sends, not recvs");
286   detached_ = true;
287   vetoable_start();
288   return this;
289 }
290
291 bool Comm::test() // TODO: merge with Activity::test, once modernized
292 {
293   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
294              state_ == State::CANCELED || state_ == State::FINISHED);
295
296   if (state_ == State::CANCELED || state_ == State::FINISHED)
297     return true;
298
299   if (state_ == State::INITED || state_ == State::STARTING)
300     this->vetoable_start();
301
302   if (simcall_comm_test(get_impl())) {
303     complete(State::FINISHED);
304     return true;
305   }
306   return false;
307 }
308
309 Mailbox* Comm::get_mailbox() const
310 {
311   return mailbox_;
312 }
313
314 Actor* Comm::get_sender() const
315 {
316   kernel::actor::ActorImplPtr sender = nullptr;
317   if (pimpl_)
318     sender = boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->src_actor_;
319   return sender ? sender->get_ciface() : nullptr;
320 }
321
322 CommPtr Comm::set_copy_data_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t))
323 {
324   copy_data_function_ = callback;
325   return this;
326 }
327 void Comm::copy_buffer_callback(kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
328 {
329   XBT_DEBUG("Copy the data over");
330   memcpy(comm->dst_buff_, buff, buff_size);
331   if (comm->detached()) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the
332                           // original buffer available to the application ASAP
333     xbt_free(buff);
334     comm->src_buff_ = nullptr;
335   }
336 }
337
338 void Comm::copy_pointer_callback(kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
339 {
340   xbt_assert((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
341   *(void**)(comm->dst_buff_) = buff;
342 }
343
344 } // namespace s4u
345 } // namespace simgrid
346 /* **************************** Public C interface *************************** */
347 void sg_comm_detach(sg_comm_t comm, void (*clean_function)(void*))
348 {
349   comm->detach(clean_function);
350   comm->unref();
351 }
352 void sg_comm_unref(sg_comm_t comm)
353 {
354   comm->unref();
355 }
356 int sg_comm_test(sg_comm_t comm)
357 {
358   bool finished = comm->test();
359   if (finished)
360     comm->unref();
361   return finished;
362 }
363
364 sg_error_t sg_comm_wait(sg_comm_t comm)
365 {
366   return sg_comm_wait_for(comm, -1);
367 }
368
369 sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout)
370 {
371   sg_error_t status = SG_OK;
372
373   simgrid::s4u::CommPtr s4u_comm(comm, false);
374   try {
375     s4u_comm->wait_for(timeout);
376   } catch (const simgrid::TimeoutException&) {
377     status = SG_ERROR_TIMEOUT;
378   } catch (const simgrid::CancelException&) {
379     status = SG_ERROR_CANCELED;
380   } catch (const simgrid::NetworkFailureException&) {
381     status = SG_ERROR_NETWORK;
382   }
383   return status;
384 }
385
386 void sg_comm_wait_all(sg_comm_t* comms, size_t count)
387 {
388   sg_comm_wait_all_for(comms, count, -1);
389 }
390
391 size_t sg_comm_wait_all_for(sg_comm_t* comms, size_t count, double timeout)
392 {
393   std::vector<simgrid::s4u::CommPtr> s4u_comms;
394   for (size_t i = 0; i < count; i++)
395     s4u_comms.emplace_back(comms[i], false);
396
397   size_t pos = simgrid::s4u::Comm::wait_all_for(s4u_comms, timeout);
398   for (size_t i = pos; i < count; i++)
399     s4u_comms[i]->add_ref();
400   return pos;
401 }
402
403 ssize_t sg_comm_wait_any(sg_comm_t* comms, size_t count)
404 {
405   return sg_comm_wait_any_for(comms, count, -1);
406 }
407
408 ssize_t sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout)
409 {
410   std::vector<simgrid::s4u::CommPtr> s4u_comms;
411   for (size_t i = 0; i < count; i++)
412     s4u_comms.emplace_back(comms[i], false);
413
414   ssize_t pos = simgrid::s4u::Comm::wait_any_for(s4u_comms, timeout);
415   for (size_t i = 0; i < count; i++) {
416     if (pos != -1 && static_cast<size_t>(pos) != i)
417       s4u_comms[i]->add_ref();
418   }
419   return pos;
420 }