Logo AND Algorithmique Numérique Distribuée

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