Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a host and not an host
[simgrid.git] / src / s4u / s4u_Comm.cpp
1 /* Copyright (c) 2006-2018. 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_in, double timeout)
33 {
34   // Map to dynar<Synchro*>:
35   xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), [](void* ptr) {
36     intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
37   });
38   for (auto const& comm : *comms_in) {
39     if (comm->state_ == Activity::State::INITED)
40       comm->start();
41     xbt_assert(comm->state_ == Activity::State::STARTED);
42     simgrid::kernel::activity::ActivityImpl* ptr = comm->pimpl_.get();
43     intrusive_ptr_add_ref(ptr);
44     xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, ptr);
45   }
46   // Call the underlying simcall:
47   int idx = simcall_comm_waitany(comms, timeout);
48   xbt_dynar_free(&comms);
49   return idx;
50 }
51
52 void Comm::wait_all(std::vector<CommPtr>* comms)
53 {
54   // TODO: this should be a simcall or something
55   // TODO: we are missing a version with timeout
56   for (CommPtr comm : *comms)
57     comm->wait();
58 }
59
60 Comm* Comm::set_rate(double rate)
61 {
62   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
63              __FUNCTION__);
64   rate_ = rate;
65   return this;
66 }
67
68 Comm* Comm::set_src_data(void* buff)
69 {
70   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
71              __FUNCTION__);
72   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
73   src_buff_ = buff;
74   return this;
75 }
76 Comm* Comm::set_src_data_size(size_t size)
77 {
78   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
79              __FUNCTION__);
80   src_buff_size_ = size;
81   return this;
82 }
83 Comm* Comm::set_src_data(void* buff, size_t size)
84 {
85   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
86              __FUNCTION__);
87
88   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
89   src_buff_      = buff;
90   src_buff_size_ = size;
91   return this;
92 }
93 Comm* Comm::set_dst_data(void** buff)
94 {
95   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
96              __FUNCTION__);
97   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
98   dst_buff_ = buff;
99   return this;
100 }
101 size_t Comm::get_dst_data_size()
102 {
103   xbt_assert(state_ == State::FINISHED, "You cannot use %s before your communication terminated", __FUNCTION__);
104   return dst_buff_size_;
105 }
106 Comm* Comm::set_dst_data(void** buff, size_t size)
107 {
108   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
109              __FUNCTION__);
110
111   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
112   dst_buff_      = buff;
113   dst_buff_size_ = size;
114   return this;
115 }
116
117 Comm* Comm::start()
118 {
119   xbt_assert(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_, 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_, 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.
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       return this;
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_, 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                           user_data_, timeout, rate_);
167       }
168       state_ = State::FINISHED;
169       return this;
170
171     case State::STARTED:
172       simcall_comm_wait(pimpl_, timeout);
173       on_completion(Actor::self());
174       state_ = State::FINISHED;
175       return this;
176
177     default:
178       THROW_IMPOSSIBLE;
179   }
180   return this;
181 }
182 int Comm::test_any(std::vector<CommPtr>* comms)
183 {
184   smx_activity_t* array = new smx_activity_t[comms->size()];
185   for (unsigned int i = 0; i < comms->size(); i++) {
186     array[i] = comms->at(i)->pimpl_;
187   }
188   int res = simcall_comm_testany(array, comms->size());
189   delete[] array;
190   return res;
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   simgrid::simix::simcall([this] { dynamic_cast<kernel::activity::CommImpl*>(pimpl_.get())->cancel(); });
205   state_ = State::CANCELED;
206   return this;
207 }
208
209 bool Comm::test()
210 {
211   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
212
213   if (state_ == State::FINISHED)
214     return true;
215
216   if (state_ == State::INITED)
217     this->start();
218
219   if (simcall_comm_test(pimpl_)) {
220     state_ = State::FINISHED;
221     return true;
222   }
223   return false;
224 }
225
226 MailboxPtr Comm::get_mailbox()
227 {
228   return mailbox_;
229 }
230
231 void intrusive_ptr_release(simgrid::s4u::Comm* c)
232 {
233   if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
234     std::atomic_thread_fence(std::memory_order_acquire);
235     delete c;
236   }
237 }
238 void intrusive_ptr_add_ref(simgrid::s4u::Comm* c)
239 {
240   c->refcount_.fetch_add(1, std::memory_order_relaxed);
241 }
242 } // namespace s4u
243 } // namespace simgrid