Logo AND Algorithmique Numérique Distribuée

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