Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / s4u / s4u_Comm.cpp
1 /* Copyright (c) 2006-2023. 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 <cmath>
7 #include <simgrid/Exception.hpp>
8 #include <simgrid/comm.h>
9 #include <simgrid/s4u/ActivitySet.hpp>
10 #include <simgrid/s4u/Comm.hpp>
11 #include <simgrid/s4u/Engine.hpp>
12 #include <simgrid/s4u/Mailbox.hpp>
13
14 #include "src/kernel/activity/CommImpl.hpp"
15 #include "src/kernel/actor/ActorImpl.hpp"
16 #include "src/kernel/actor/SimcallObserver.hpp"
17 #include "src/mc/mc.h"
18 #include "src/mc/mc_replay.hpp"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm, s4u_activity, "S4U asynchronous communications");
21
22 namespace simgrid::s4u {
23 xbt::signal<void(Comm const&)> Comm::on_send;
24 xbt::signal<void(Comm const&)> Comm::on_recv;
25
26 CommPtr Comm::set_copy_data_callback(const std::function<void(kernel::activity::CommImpl*, void*, size_t)>& callback)
27 {
28   copy_data_function_ = callback;
29   return this;
30 }
31
32 void Comm::copy_buffer_callback(kernel::activity::CommImpl* comm, void* buff,
33                                 size_t buff_size) // XBT_ATTRIB_DEPRECATED_v338
34 {
35   XBT_DEBUG("Copy the data over");
36   memcpy(comm->dst_buff_, buff, buff_size);
37   if (comm->is_detached()) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the
38                              // original buffer available to the application ASAP
39     xbt_free(buff);
40     comm->src_buff_ = nullptr;
41   }
42 }
43
44 void Comm::copy_pointer_callback(kernel::activity::CommImpl* comm, void* buff,
45                                  size_t buff_size) // XBT_ATTRIB_DEPRECATED_v338
46 {
47   xbt_assert((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
48   *(void**)(comm->dst_buff_) = buff;
49 }
50
51 Comm::~Comm()
52 {
53   if (state_ == State::STARTED && not detached_ &&
54       (pimpl_ == nullptr || pimpl_->get_state() == kernel::activity::State::RUNNING)) {
55     XBT_INFO("Comm %p freed before its completion. Did you forget to detach it? (state: %s)", this, get_state_str());
56     if (pimpl_ != nullptr)
57       XBT_INFO("pimpl_->state: %s", pimpl_->get_state_str());
58     else
59       XBT_INFO("pimpl_ is null");
60     xbt_backtrace_display_current();
61   }
62 }
63
64 void Comm::send(kernel::actor::ActorImpl* sender, const Mailbox* mbox, double task_size, double rate, void* src_buff,
65                 size_t src_buff_size,
66                 const std::function<bool(void*, void*, simgrid::kernel::activity::CommImpl*)>& match_fun,
67                 const std::function<void(simgrid::kernel::activity::CommImpl*, void*, size_t)>& copy_data_fun,
68                 void* data, double timeout)
69 {
70   /* checking for infinite values */
71   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
72   xbt_assert(std::isfinite(rate), "rate is not finite!");
73   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
74
75   xbt_assert(mbox, "No rendez-vous point defined for send");
76
77   if (MC_is_active() || MC_record_replay_is_active()) {
78     /* the model-checker wants two separate simcalls, and wants comm to be nullptr during the simcall */
79     simgrid::kernel::activity::ActivityImplPtr comm = nullptr;
80
81     simgrid::kernel::actor::CommIsendSimcall send_observer{
82         sender,        mbox->get_impl(), task_size, rate,          static_cast<unsigned char*>(src_buff),
83         src_buff_size, match_fun,        nullptr,   copy_data_fun, data,
84         false,         "Isend"};
85     comm = simgrid::kernel::actor::simcall_answered(
86         [&send_observer] { return simgrid::kernel::activity::CommImpl::isend(&send_observer); }, &send_observer);
87
88     if (simgrid::kernel::actor::ActivityWaitSimcall wait_observer{sender, comm.get(), timeout, "Wait"};
89         simgrid::kernel::actor::simcall_blocking(
90             [&wait_observer] {
91               wait_observer.get_activity()->wait_for(wait_observer.get_issuer(), wait_observer.get_timeout());
92             },
93             &wait_observer)) {
94       throw simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted");
95     }
96     comm = nullptr;
97   } else {
98     simgrid::kernel::actor::CommIsendSimcall observer(sender, mbox->get_impl(), task_size, rate,
99                                                       static_cast<unsigned char*>(src_buff), src_buff_size, match_fun,
100                                                       nullptr, copy_data_fun, data, false, "Isend");
101     simgrid::kernel::actor::simcall_blocking([&observer, timeout] {
102       simgrid::kernel::activity::ActivityImplPtr comm = simgrid::kernel::activity::CommImpl::isend(&observer);
103       comm->wait_for(observer.get_issuer(), timeout);
104     });
105   }
106 }
107
108 void Comm::recv(kernel::actor::ActorImpl* receiver, const Mailbox* mbox, void* dst_buff, size_t* dst_buff_size,
109                 const std::function<bool(void*, void*, simgrid::kernel::activity::CommImpl*)>& match_fun,
110                 const std::function<void(simgrid::kernel::activity::CommImpl*, void*, size_t)>& copy_data_fun,
111                 void* data, double timeout, double rate)
112 {
113   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
114   xbt_assert(mbox, "No rendez-vous point defined for recv");
115
116   if (MC_is_active() || MC_record_replay_is_active()) {
117     /* the model-checker wants two separate simcalls, and wants comm to be nullptr during the simcall */
118     simgrid::kernel::activity::ActivityImplPtr comm = nullptr;
119
120     simgrid::kernel::actor::CommIrecvSimcall observer{receiver,
121                                                       mbox->get_impl(),
122                                                       static_cast<unsigned char*>(dst_buff),
123                                                       dst_buff_size,
124                                                       match_fun,
125                                                       copy_data_fun,
126                                                       data,
127                                                       rate,
128                                                       "Irecv"};
129     comm = simgrid::kernel::actor::simcall_answered(
130         [&observer] { return simgrid::kernel::activity::CommImpl::irecv(&observer); }, &observer);
131
132     if (simgrid::kernel::actor::ActivityWaitSimcall wait_observer{receiver, comm.get(), timeout, "wait"};
133         simgrid::kernel::actor::simcall_blocking(
134             [&wait_observer] {
135               wait_observer.get_activity()->wait_for(wait_observer.get_issuer(), wait_observer.get_timeout());
136             },
137             &wait_observer)) {
138       throw simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted");
139     }
140     comm = nullptr;
141   } else {
142     simgrid::kernel::actor::CommIrecvSimcall observer(receiver, mbox->get_impl(), static_cast<unsigned char*>(dst_buff),
143                                                       dst_buff_size, match_fun, copy_data_fun, data, rate, "Irecv");
144     simgrid::kernel::actor::simcall_blocking([&observer, timeout] {
145       simgrid::kernel::activity::ActivityImplPtr comm = simgrid::kernel::activity::CommImpl::irecv(&observer);
146       comm->wait_for(observer.get_issuer(), timeout);
147     });
148   }
149 }
150
151 CommPtr Comm::sendto_init()
152 {
153   CommPtr res(new Comm());
154   res->pimpl_ = kernel::activity::CommImplPtr(new kernel::activity::CommImpl());
155   boost::static_pointer_cast<kernel::activity::CommImpl>(res->pimpl_)->detach();
156   res->sender_ = kernel::actor::ActorImpl::self();
157   return res;
158 }
159
160 CommPtr Comm::sendto_init(Host* from, Host* to)
161 {
162   auto res = Comm::sendto_init()->set_source(from)->set_destination(to);
163   res->set_state(State::STARTING);
164   return res;
165 }
166
167 CommPtr Comm::sendto_async(Host* from, Host* to, uint64_t simulated_size_in_bytes)
168 {
169   return Comm::sendto_init()->set_payload_size(simulated_size_in_bytes)->set_source(from)->set_destination(to);
170 }
171
172 void Comm::sendto(Host* from, Host* to, uint64_t simulated_size_in_bytes)
173 {
174   sendto_async(from, to, simulated_size_in_bytes)->wait();
175 }
176
177 CommPtr Comm::set_source(Host* from)
178 {
179   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
180              "Cannot change the source of a Comm once it's started (state: %s)", to_c_str(state_));
181   boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->set_source(from);
182   // Setting 'source' may allow to start the activity, let's try
183   if (state_ == State::STARTING && remains_ <= 0)
184     XBT_DEBUG("This communication has a payload size of 0 byte. It cannot start yet");
185   else
186     start();
187
188   return this;
189 }
190 Host* Comm::get_source() const
191 {
192   return pimpl_ ? boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->get_source() : nullptr;
193 }
194
195 CommPtr Comm::set_destination(Host* to)
196 {
197   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
198              "Cannot change the destination of a Comm once it's started (state: %s)", to_c_str(state_));
199   boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->set_destination(to);
200   // Setting 'destination' may allow to start the activity, let's try
201   if (state_ == State::STARTING && remains_ <= 0)
202     XBT_DEBUG("This communication has a payload size of 0 byte. It cannot start yet");
203   else
204     start();
205
206   return this;
207 }
208
209 Host* Comm::get_destination() const
210 {
211   return pimpl_ ? boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->get_destination() : nullptr;
212 }
213
214 CommPtr Comm::set_rate(double rate)
215 {
216   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
217              __func__);
218   rate_ = rate;
219   return this;
220 }
221
222 CommPtr Comm::set_mailbox(Mailbox* mailbox)
223 {
224   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
225              __func__);
226   mailbox_ = mailbox;
227   return this;
228 }
229
230 CommPtr Comm::set_src_data(void* buff)
231 {
232   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
233              __func__);
234   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
235   src_buff_ = buff;
236   return this;
237 }
238
239 CommPtr Comm::set_src_data_size(size_t size)
240 {
241   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
242              __func__);
243   src_buff_size_ = size;
244   return this;
245 }
246
247 CommPtr Comm::set_src_data(void* buff, size_t size)
248 {
249   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
250              __func__);
251
252   xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
253   src_buff_      = buff;
254   src_buff_size_ = size;
255   return this;
256 }
257
258 CommPtr Comm::set_dst_data(void** buff)
259 {
260   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
261              __func__);
262   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
263   dst_buff_ = buff;
264   return this;
265 }
266
267 CommPtr Comm::set_dst_data(void** buff, size_t size)
268 {
269   xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
270              __func__);
271
272   xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
273   dst_buff_      = buff;
274   dst_buff_size_ = size;
275   return this;
276 }
277
278 CommPtr Comm::set_payload_size(uint64_t bytes)
279 {
280   set_remaining(bytes);
281   if (pimpl_) {
282     boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->set_size(bytes);
283   }
284   return this;
285 }
286
287 void* Comm::get_payload() const
288 {
289   xbt_assert(get_state() == State::FINISHED,
290              "You can only retrieve the payload of a communication that gracefully terminated, but its state is %s.",
291              get_state_str());
292   return static_cast<kernel::activity::CommImpl*>(pimpl_.get())->payload_;
293 }
294
295 Actor* Comm::get_sender() const
296 {
297   kernel::actor::ActorImplPtr sender = nullptr;
298   if (pimpl_)
299     sender = boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->src_actor_;
300   return sender ? sender->get_ciface() : nullptr;
301 }
302
303 Actor* Comm::get_receiver() const
304 {
305   kernel::actor::ActorImplPtr receiver = nullptr;
306   if (pimpl_)
307     receiver = boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->dst_actor_;
308   return receiver ? receiver->get_ciface() : nullptr;
309 }
310
311 bool Comm::is_assigned() const
312 {
313   return (pimpl_ && boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->is_assigned()) ||
314          mailbox_ != nullptr;
315 }
316
317 Comm* Comm::do_start()
318 {
319   xbt_assert(get_state() == State::INITED || get_state() == State::STARTING,
320              "You cannot use %s() once your communication started (not implemented)", __func__);
321
322   auto myself = kernel::actor::ActorImpl::self();
323
324   if (get_source() != nullptr || get_destination() != nullptr) {
325     xbt_assert(is_assigned(), "When either from_ or to_ is specified, both must be.");
326     xbt_assert(src_buff_ == nullptr && dst_buff_ == nullptr,
327                "Direct host-to-host communications cannot carry any data.");
328     XBT_DEBUG("host-to-host Comm. Pimpl already created and set, just start it.");
329     kernel::actor::simcall_answered([this] {
330       pimpl_->set_state(kernel::activity::State::READY);
331       boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->start();
332     });
333     fire_on_start();
334     fire_on_this_start();
335   } else if (myself == sender_) {
336     on_send(*this);
337     on_this_send(*this);
338     kernel::actor::CommIsendSimcall observer{sender_,
339                                              mailbox_->get_impl(),
340                                              remains_,
341                                              rate_,
342                                              static_cast<unsigned char*>(src_buff_),
343                                              src_buff_size_,
344                                              match_fun_,
345                                              clean_fun_,
346                                              copy_data_function_,
347                                              get_data<void>(),
348                                              detached_,
349                                              "Isend"};
350     pimpl_ = kernel::actor::simcall_answered([&observer] { return kernel::activity::CommImpl::isend(&observer); },
351                                              &observer);
352   } else if (myself == receiver_) {
353     xbt_assert(not detached_, "Receive cannot be detached");
354     on_recv(*this);
355     on_this_recv(*this);
356     kernel::actor::CommIrecvSimcall observer{receiver_,
357                                              mailbox_->get_impl(),
358                                              static_cast<unsigned char*>(dst_buff_),
359                                              &dst_buff_size_,
360                                              match_fun_,
361                                              copy_data_function_,
362                                              get_data<void>(),
363                                              rate_,
364                                              "Irecv"};
365     pimpl_ = kernel::actor::simcall_answered([&observer] { return kernel::activity::CommImpl::irecv(&observer); },
366                                              &observer);
367   } else {
368     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
369   }
370
371   if (suspended_)
372     pimpl_->suspend();
373
374   if (not detached_) {
375     pimpl_->set_iface(this);
376     pimpl_->set_actor(sender_);
377     // Only throw the signal when both sides are here and the status is READY
378     if (pimpl_->get_state() != kernel::activity::State::WAITING) {
379       fire_on_start();
380       fire_on_this_start();
381     }
382   }
383
384   state_ = State::STARTED;
385   return this;
386 }
387
388 Comm* Comm::detach()
389 {
390   xbt_assert(state_ == State::INITED || state_ == State::STARTING,
391              "You cannot use %s() once your communication is %s (not implemented)", __func__, get_state_str());
392   xbt_assert(dst_buff_ == nullptr && dst_buff_size_ == 0, "You can only detach sends, not recvs");
393   detached_ = true;
394   start();
395   return this;
396 }
397
398 ssize_t Comm::test_any(const std::vector<CommPtr>& comms) // XBT_ATTRIB_DEPRECATED_v339
399 {
400   std::vector<kernel::activity::ActivityImpl*> ractivities(comms.size());
401   std::transform(begin(comms), end(comms), begin(ractivities), [](const CommPtr& act) { return act->pimpl_.get(); });
402
403   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
404   kernel::actor::ActivityTestanySimcall observer{issuer, ractivities, "test_any"};
405   ssize_t changed_pos = kernel::actor::simcall_answered(
406       [&observer] {
407         return kernel::activity::ActivityImpl::test_any(observer.get_issuer(), observer.get_activities());
408       },
409       &observer);
410   if (changed_pos != -1)
411     comms.at(changed_pos)->complete(State::FINISHED);
412   return changed_pos;
413 }
414
415 /** @brief Block the calling actor until the communication is finished, or until timeout
416  *
417  * On timeout, an exception is thrown and the communication is invalidated.
418  *
419  * @param timeout the amount of seconds to wait for the comm termination.
420  *                Negative values denote infinite wait times. 0 as a timeout returns immediately. */
421 Comm* Comm::wait_for(double timeout)
422 {
423   XBT_DEBUG("Calling Comm::wait_for with state %s", get_state_str());
424   kernel::actor::ActorImpl* issuer = nullptr;
425   switch (state_) {
426     case State::FINISHED:
427       break;
428     case State::FAILED:
429       throw NetworkFailureException(XBT_THROW_POINT, "Cannot wait for a failed communication");
430     case State::INITED:
431     case State::STARTING: // It's not started yet. Do it in one simcall if it's a regular communication
432       if (get_source() != nullptr || get_destination() != nullptr) {
433         return start()->wait_for(timeout); // In the case of host2host comm, do it in two simcalls
434       } else if (src_buff_ != nullptr) {
435         on_send(*this);
436         on_this_send(*this);
437         send(sender_, mailbox_, remains_, rate_, src_buff_, src_buff_size_, match_fun_, copy_data_function_,
438              get_data<void>(), timeout);
439
440       } else { // Receiver
441         on_recv(*this);
442         on_this_recv(*this);
443         recv(receiver_, mailbox_, dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_, get_data<void>(),
444              timeout, rate_);
445       }
446       break;
447     case State::STARTED:
448       try {
449         issuer = kernel::actor::ActorImpl::self();
450         kernel::actor::ActivityWaitSimcall observer{issuer, pimpl_.get(), timeout, "Wait"};
451         if (kernel::actor::simcall_blocking(
452                 [&observer] { observer.get_activity()->wait_for(observer.get_issuer(), observer.get_timeout()); },
453                 &observer)) {
454           throw TimeoutException(XBT_THROW_POINT, "Timeouted");
455         }
456       } catch (const NetworkFailureException& e) {
457         issuer->simcall_.observer_ = nullptr; // Comm failed on network failure, reset the observer to nullptr
458         complete(State::FAILED);
459         e.rethrow_nested(XBT_THROW_POINT, boost::core::demangle(typeid(e).name()) + " raised in kernel mode.");
460       }
461       break;
462
463     case State::CANCELED:
464       throw CancelException(XBT_THROW_POINT, "Communication canceled");
465
466     default:
467       THROW_IMPOSSIBLE;
468   }
469   complete(State::FINISHED);
470   return this;
471 }
472
473 ssize_t Comm::deprecated_wait_any_for(const std::vector<CommPtr>& comms, double timeout) // XBT_ATTRIB_DEPRECATED_v339
474 {
475   if (comms.empty())
476     return -1;
477   ActivitySet set;
478   for (const auto& comm : comms)
479     set.push(comm);
480   try {
481     auto* ret = set.wait_any_for(timeout).get();
482     for (size_t i = 0; i < comms.size(); i++)
483       if (comms[i].get() == ret)
484         return i;
485
486   } catch (TimeoutException& e) {
487     return -1;
488   } catch (const NetworkFailureException& e) {
489     for (auto c : comms)
490       if (c->pimpl_->get_state() == kernel::activity::State::FAILED)
491         c->complete(State::FAILED);
492
493     e.rethrow_nested(XBT_THROW_POINT, boost::core::demangle(typeid(e).name()) + " raised in kernel mode.");
494   }
495   return -1;
496 }
497
498 void Comm::wait_all(const std::vector<CommPtr>& comms) // XBT_ATTRIB_DEPRECATED_v339
499 {
500   // TODO: this should be a simcall or something
501   for (const auto& comm : comms)
502     comm->wait();
503 }
504
505 size_t Comm::wait_all_for(const std::vector<CommPtr>& comms, double timeout) // XBT_ATTRIB_DEPRECATED_v339
506 {
507   if (timeout < 0.0) {
508     for (const auto& comm : comms)
509       comm->wait();
510     return comms.size();
511   }
512
513   ActivitySet set;
514   for (auto comm : comms)
515     set.push(comm);
516   set.wait_all_for(timeout);
517
518   return set.size();
519 }
520 } // namespace simgrid::s4u
521 /* **************************** Public C interface *************************** */
522 int sg_comm_isinstance(sg_activity_t acti)
523 {
524   return dynamic_cast<simgrid::s4u::Comm*>(acti) != nullptr;
525 }
526
527 void sg_comm_detach(sg_comm_t comm, void (*clean_function)(void*))
528 {
529   comm->detach(clean_function);
530   comm->unref();
531 }
532 void sg_comm_unref(sg_comm_t comm)
533 {
534   comm->unref();
535 }
536 int sg_comm_test(sg_comm_t comm)
537 {
538   bool finished = comm->test();
539   if (finished)
540     comm->unref();
541   return finished;
542 }
543
544 sg_error_t sg_comm_wait(sg_comm_t comm)
545 {
546   return sg_comm_wait_for(comm, -1);
547 }
548
549 sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout)
550 {
551   sg_error_t status = SG_OK;
552
553   simgrid::s4u::CommPtr s4u_comm(comm, false);
554   try {
555     s4u_comm->wait_for(timeout);
556   } catch (const simgrid::TimeoutException&) {
557     status = SG_ERROR_TIMEOUT;
558   } catch (const simgrid::CancelException&) {
559     status = SG_ERROR_CANCELED;
560   } catch (const simgrid::NetworkFailureException&) {
561     status = SG_ERROR_NETWORK;
562   }
563   return status;
564 }
565
566 void sg_comm_wait_all(sg_comm_t* comms, size_t count) // XBT_ATTRIB_DEPRECATED_v339
567 {
568   simgrid::s4u::ActivitySet as;
569   for (size_t i = 0; i < count; i++)
570     as.push(comms[i]);
571
572   as.wait_all();
573 }
574
575 ssize_t sg_comm_wait_any(sg_comm_t* comms, size_t count) // XBT_ATTRIB_DEPRECATED_v339
576 {
577   std::vector<simgrid::s4u::CommPtr> s4u_comms;
578   for (size_t i = 0; i < count; i++)
579     s4u_comms.emplace_back(comms[i], false);
580
581   ssize_t pos = simgrid::s4u::Comm::deprecated_wait_any_for(s4u_comms, -1);
582   for (size_t i = 0; i < count; i++) {
583     if (pos != -1 && static_cast<size_t>(pos) != i)
584       s4u_comms[i]->add_ref();
585   }
586   return pos;
587 }
588
589 ssize_t sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout) // XBT_ATTRIB_DEPRECATED_v339
590 {
591   std::vector<simgrid::s4u::CommPtr> s4u_comms;
592   for (size_t i = 0; i < count; i++)
593     s4u_comms.emplace_back(comms[i], false);
594
595   ssize_t pos = simgrid::s4u::Comm::deprecated_wait_any_for(s4u_comms, timeout);
596   for (size_t i = 0; i < count; i++) {
597     if (pos != -1 && static_cast<size_t>(pos) != i)
598       s4u_comms[i]->add_ref();
599   }
600   return pos;
601 }