Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Specialize parameter for simcall execution_wait.
[simgrid.git] / src / s4u / s4u_Comm.cpp
1 /* Copyright (c) 2006-2019. 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/s4u/Comm.hpp"
10 #include "simgrid/s4u/Mailbox.hpp"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm, s4u_activity, "S4U asynchronous communications");
13
14 namespace simgrid {
15 namespace s4u {
16 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Comm::on_sender_start;
17 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Comm::on_receiver_start;
18 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Comm::on_completion;
19
20 Comm::~Comm()
21 {
22   if (state_ == State::STARTED && not detached_ && (pimpl_ == nullptr || pimpl_->state_ == SIMIX_RUNNING)) {
23     XBT_INFO("Comm %p freed before its completion. Detached: %d, State: %d", this, detached_, (int)state_);
24     if (pimpl_ != nullptr)
25       XBT_INFO("pimpl_->state: %d", pimpl_->state_);
26     else
27       XBT_INFO("pimpl_ is null");
28     xbt_backtrace_display_current();
29   }
30 }
31
32 int Comm::wait_any_for(std::vector<CommPtr>* comms, double timeout)
33 {
34   smx_activity_t* array = new smx_activity_t[comms->size()];
35   for (unsigned int i = 0; i < comms->size(); i++) {
36     array[i] = comms->at(i)->pimpl_;
37   }
38   int idx = simcall_comm_waitany(array, comms->size(), timeout);
39   delete[] array;
40   return idx;
41 }
42
43 void Comm::wait_all(std::vector<CommPtr>* comms)
44 {
45   // TODO: this should be a simcall or something
46   // TODO: we are missing a version with timeout
47   for (CommPtr comm : *comms)
48     comm->wait();
49 }
50
51 Comm* Comm::set_rate(double rate)
52 {
53   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
54              __FUNCTION__);
55   rate_ = rate;
56   return this;
57 }
58
59 Comm* Comm::set_src_data(void* buff)
60 {
61   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
62              __FUNCTION__);
63   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
64   src_buff_ = buff;
65   return this;
66 }
67 Comm* 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 Comm* Comm::set_src_data(void* buff, size_t size)
75 {
76   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
77              __FUNCTION__);
78
79   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
80   src_buff_      = buff;
81   src_buff_size_ = size;
82   return this;
83 }
84 Comm* Comm::set_dst_data(void** buff)
85 {
86   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
87              __FUNCTION__);
88   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
89   dst_buff_ = buff;
90   return this;
91 }
92 size_t Comm::get_dst_data_size()
93 {
94   xbt_assert(state_ == State::FINISHED, "You cannot use %s before your communication terminated", __FUNCTION__);
95   return dst_buff_size_;
96 }
97 Comm* Comm::set_dst_data(void** buff, size_t size)
98 {
99   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
100              __FUNCTION__);
101
102   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
103   dst_buff_      = buff;
104   dst_buff_size_ = size;
105   return this;
106 }
107
108 Comm* Comm::start()
109 {
110   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
111              __FUNCTION__);
112
113   if (src_buff_ != nullptr) { // Sender side
114     on_sender_start(Actor::self());
115     pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
116                                 clean_fun_, copy_data_function_, user_data_, detached_);
117   } else if (dst_buff_ != nullptr) { // Receiver side
118     xbt_assert(not detached_, "Receive cannot be detached");
119     on_receiver_start(Actor::self());
120     pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
121                                 copy_data_function_, user_data_, rate_);
122
123   } else {
124     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
125   }
126   state_ = State::STARTED;
127   return this;
128 }
129
130 /** @brief Block the calling actor until the communication is finished */
131 Comm* Comm::wait()
132 {
133   return this->wait_for(-1);
134 }
135
136 /** @brief Block the calling actor until the communication is finished, or until timeout
137  *
138  * On timeout, an exception is thrown.
139  *
140  * @param timeout the amount of seconds to wait for the comm termination.
141  *                Negative values denote infinite wait times. 0 as a timeout returns immediately. */
142 Comm* Comm::wait_for(double timeout)
143 {
144   switch (state_) {
145     case State::FINISHED:
146       return this;
147
148     case State::INITED: // It's not started yet. Do it in one simcall
149       if (src_buff_ != nullptr) {
150         on_sender_start(Actor::self());
151         simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
152                           copy_data_function_, user_data_, timeout);
153
154       } else { // Receiver
155         on_receiver_start(Actor::self());
156         simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
157                           user_data_, timeout, rate_);
158       }
159       state_ = State::FINISHED;
160       return this;
161
162     case State::STARTED:
163       simcall_comm_wait(pimpl_, timeout);
164       on_completion(Actor::self());
165       state_ = State::FINISHED;
166       return this;
167
168     default:
169       THROW_IMPOSSIBLE;
170   }
171   return this;
172 }
173 int Comm::test_any(std::vector<CommPtr>* comms)
174 {
175   smx_activity_t* array = new smx_activity_t[comms->size()];
176   for (unsigned int i = 0; i < comms->size(); i++) {
177     array[i] = comms->at(i)->pimpl_;
178   }
179   int res = simcall_comm_testany(array, comms->size());
180   delete[] array;
181   return res;
182 }
183
184 Comm* Comm::detach()
185 {
186   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
187              __FUNCTION__);
188   xbt_assert(src_buff_ != nullptr && src_buff_size_ != 0, "You can only detach sends, not recvs");
189   detached_ = true;
190   return start();
191 }
192
193 Comm* Comm::cancel()
194 {
195   simgrid::simix::simcall([this] { static_cast<kernel::activity::CommImpl*>(pimpl_.get())->cancel(); });
196   state_ = State::CANCELED;
197   return this;
198 }
199
200 bool Comm::test()
201 {
202   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
203
204   if (state_ == State::FINISHED)
205     return true;
206
207   if (state_ == State::INITED)
208     this->start();
209
210   if (simcall_comm_test(pimpl_)) {
211     state_ = State::FINISHED;
212     return true;
213   }
214   return false;
215 }
216
217 MailboxPtr Comm::get_mailbox()
218 {
219   return mailbox_;
220 }
221
222 void intrusive_ptr_release(simgrid::s4u::Comm* c)
223 {
224   if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
225     std::atomic_thread_fence(std::memory_order_acquire);
226     delete c;
227   }
228 }
229 void intrusive_ptr_add_ref(simgrid::s4u::Comm* c)
230 {
231   c->refcount_.fetch_add(1, std::memory_order_relaxed);
232 }
233 } // namespace s4u
234 } // namespace simgrid