Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define and use optimized variants for simcalls comm_testany and comm_waitany.
[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   std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(
35       new simgrid::kernel::activity::CommImpl*[comms->size()]);
36   std::transform(begin(*comms), end(*comms), rcomms.get(), [](const CommPtr& comm) {
37     return static_cast<simgrid::kernel::activity::CommImpl*>(comm->pimpl_.get());
38   });
39   return simcall_comm_waitany(rcomms.get(), comms->size(), timeout);
40 }
41
42 void Comm::wait_all(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 Comm* 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 Comm* 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 Comm* Comm::set_src_data_size(size_t size)
67 {
68   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
69              __FUNCTION__);
70   src_buff_size_ = size;
71   return this;
72 }
73 Comm* Comm::set_src_data(void* buff, size_t size)
74 {
75   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
76              __FUNCTION__);
77
78   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
79   src_buff_      = buff;
80   src_buff_size_ = size;
81   return this;
82 }
83 Comm* Comm::set_dst_data(void** buff)
84 {
85   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
86              __FUNCTION__);
87   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
88   dst_buff_ = buff;
89   return this;
90 }
91 size_t Comm::get_dst_data_size()
92 {
93   xbt_assert(state_ == State::FINISHED, "You cannot use %s before your communication terminated", __FUNCTION__);
94   return dst_buff_size_;
95 }
96 Comm* Comm::set_dst_data(void** buff, size_t size)
97 {
98   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
99              __FUNCTION__);
100
101   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
102   dst_buff_      = buff;
103   dst_buff_size_ = size;
104   return this;
105 }
106
107 Comm* Comm::start()
108 {
109   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
110              __FUNCTION__);
111
112   if (src_buff_ != nullptr) { // Sender side
113     on_sender_start(Actor::self());
114     pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
115                                 clean_fun_, copy_data_function_, user_data_, detached_);
116   } else if (dst_buff_ != nullptr) { // Receiver side
117     xbt_assert(not detached_, "Receive cannot be detached");
118     on_receiver_start(Actor::self());
119     pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
120                                 copy_data_function_, user_data_, rate_);
121
122   } else {
123     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
124   }
125   state_ = State::STARTED;
126   return this;
127 }
128
129 /** @brief Block the calling actor until the communication is finished */
130 Comm* Comm::wait()
131 {
132   return this->wait_for(-1);
133 }
134
135 /** @brief Block the calling actor until the communication is finished, or until timeout
136  *
137  * On timeout, an exception is thrown.
138  *
139  * @param timeout the amount of seconds to wait for the comm termination.
140  *                Negative values denote infinite wait times. 0 as a timeout returns immediately. */
141 Comm* Comm::wait_for(double timeout)
142 {
143   switch (state_) {
144     case State::FINISHED:
145       return this;
146
147     case State::INITED: // It's not started yet. Do it in one simcall
148       if (src_buff_ != nullptr) {
149         on_sender_start(Actor::self());
150         simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
151                           copy_data_function_, user_data_, timeout);
152
153       } else { // Receiver
154         on_receiver_start(Actor::self());
155         simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
156                           user_data_, timeout, rate_);
157       }
158       state_ = State::FINISHED;
159       return this;
160
161     case State::STARTED:
162       simcall_comm_wait(pimpl_, timeout);
163       on_completion(Actor::self());
164       state_ = State::FINISHED;
165       return this;
166
167     default:
168       THROW_IMPOSSIBLE;
169   }
170   return this;
171 }
172 int Comm::test_any(std::vector<CommPtr>* comms)
173 {
174   std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(
175       new simgrid::kernel::activity::CommImpl*[comms->size()]);
176   std::transform(begin(*comms), end(*comms), rcomms.get(), [](const CommPtr& comm) {
177     return static_cast<simgrid::kernel::activity::CommImpl*>(comm->pimpl_.get());
178   });
179   return simcall_comm_testany(rcomms.get(), comms->size());
180 }
181
182 Comm* Comm::detach()
183 {
184   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
185              __FUNCTION__);
186   xbt_assert(src_buff_ != nullptr && src_buff_size_ != 0, "You can only detach sends, not recvs");
187   detached_ = true;
188   return start();
189 }
190
191 Comm* Comm::cancel()
192 {
193   simgrid::simix::simcall([this] { static_cast<kernel::activity::CommImpl*>(pimpl_.get())->cancel(); });
194   state_ = State::CANCELED;
195   return this;
196 }
197
198 bool Comm::test()
199 {
200   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
201
202   if (state_ == State::FINISHED)
203     return true;
204
205   if (state_ == State::INITED)
206     this->start();
207
208   if (simcall_comm_test(pimpl_)) {
209     state_ = State::FINISHED;
210     return true;
211   }
212   return false;
213 }
214
215 MailboxPtr Comm::get_mailbox()
216 {
217   return mailbox_;
218 }
219
220 void intrusive_ptr_release(simgrid::s4u::Comm* c)
221 {
222   if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
223     std::atomic_thread_fence(std::memory_order_acquire);
224     delete c;
225   }
226 }
227 void intrusive_ptr_add_ref(simgrid::s4u::Comm* c)
228 {
229   c->refcount_.fetch_add(1, std::memory_order_relaxed);
230 }
231 } // namespace s4u
232 } // namespace simgrid