Logo AND Algorithmique Numérique Distribuée

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